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);
1004 gtk_stext_compact_buffer (GtkSText *text)
1006 GtkEditable *editable = GTK_EDITABLE (text);
1008 g_return_if_fail (text != NULL);
1009 g_return_if_fail (GTK_IS_STEXT (text));
1010 move_gap (text, gtk_stext_get_length(text));
1014 gtk_stext_insert (GtkSText *text,
1021 GtkEditable *editable = GTK_EDITABLE (text);
1022 gboolean frozen = FALSE;
1024 gint new_line_count = 1;
1025 guint old_height = 0;
1030 g_return_if_fail (text != NULL);
1031 g_return_if_fail (GTK_IS_STEXT (text));
1033 g_return_if_fail (chars != NULL);
1036 if (!nchars || !chars)
1038 nchars = strlen (chars);
1042 if (!text->freeze_count && (length > FREEZE_LENGTH))
1044 gtk_stext_freeze (text);
1048 if (!text->freeze_count && (text->line_start_cache != NULL))
1050 find_line_containing_point (text, text->point.index, TRUE);
1051 old_height = total_line_height (text, text->current_line, 1);
1054 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
1057 widget = GTK_WIDGET (text);
1058 gtk_widget_ensure_style (widget);
1059 if ((widget->style) && (widget->style->font->type == GDK_FONT_FONTSET))
1061 text->use_wchar = TRUE;
1062 g_free (text->text.ch);
1063 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
1064 text->text_len = INITIAL_BUFFER_SIZE;
1065 if (text->scratch_buffer.ch)
1066 g_free (text->scratch_buffer.ch);
1067 text->scratch_buffer.wc = NULL;
1068 text->scratch_buffer_len = 0;
1072 move_gap (text, text->point.index);
1073 make_forward_space (text, length);
1075 if (text->use_wchar)
1077 char *chars_nt = (char *)chars;
1080 chars_nt = g_new (char, length+1);
1081 memcpy (chars_nt, chars, length);
1082 chars_nt[length] = 0;
1084 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
1086 if (chars_nt != chars)
1094 memcpy(text->text.ch + text->gap_position, chars, length);
1097 if (!text->freeze_count && (text->line_start_cache != NULL))
1099 if (text->use_wchar)
1101 for (i=0; i<numwcs; i++)
1102 if (text->text.wc[text->gap_position + i] == '\n')
1107 for (i=0; i<numwcs; i++)
1108 if (text->text.ch[text->gap_position + i] == '\n')
1115 insert_text_property (text, font, fore, back, numwcs);
1117 text->gap_size -= numwcs;
1118 text->gap_position += numwcs;
1120 if (text->point.index < text->first_line_start_index)
1121 text->first_line_start_index += numwcs;
1122 if (text->point.index < editable->selection_start_pos)
1123 editable->selection_start_pos += numwcs;
1124 if (text->point.index < editable->selection_end_pos)
1125 editable->selection_end_pos += numwcs;
1126 /* We'll reset the cursor later anyways if we aren't frozen */
1127 if (text->point.index < text->cursor_mark.index)
1128 text->cursor_mark.index += numwcs;
1130 advance_mark_n (&text->point, numwcs);
1132 if (!text->freeze_count && (text->line_start_cache != NULL))
1133 insert_expose (text, old_height, numwcs, new_line_count);
1137 gtk_stext_thaw (text);
1141 gtk_stext_backward_delete (GtkSText *text,
1144 g_return_val_if_fail (text != NULL, 0);
1145 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
1147 if (nchars > text->point.index || nchars <= 0)
1150 gtk_stext_set_point (text, text->point.index - nchars);
1152 return gtk_stext_forward_delete (text, nchars);
1156 gtk_stext_forward_delete (GtkSText *text,
1159 guint old_lines, old_height;
1160 GtkEditable *editable = GTK_EDITABLE (text);
1161 gboolean frozen = FALSE;
1163 g_return_val_if_fail (text != NULL, 0);
1164 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
1166 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1169 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1171 gtk_stext_freeze (text);
1175 if (!text->freeze_count && text->line_start_cache != NULL)
1177 /* We need to undraw the cursor here, since we may later
1178 * delete the cursor's property
1180 undraw_cursor (text, FALSE);
1181 find_line_containing_point (text, text->point.index, TRUE);
1182 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1185 /* FIXME, or resizing after deleting will be odd */
1186 if (text->point.index < text->first_line_start_index)
1188 if (text->point.index + nchars >= text->first_line_start_index)
1190 text->first_line_start_index = text->point.index;
1191 while ((text->first_line_start_index > 0) &&
1192 (GTK_STEXT_INDEX (text, text->first_line_start_index - 1)
1194 text->first_line_start_index -= 1;
1198 text->first_line_start_index -= nchars;
1201 if (text->point.index < editable->selection_start_pos)
1202 editable->selection_start_pos -=
1203 MIN(nchars, editable->selection_start_pos - text->point.index);
1204 if (text->point.index < editable->selection_end_pos)
1205 editable->selection_end_pos -=
1206 MIN(nchars, editable->selection_end_pos - text->point.index);
1207 /* We'll reset the cursor later anyways if we aren't frozen */
1208 if (text->point.index < text->cursor_mark.index)
1209 move_mark_n (&text->cursor_mark,
1210 -MIN(nchars, text->cursor_mark.index - text->point.index));
1212 move_gap (text, text->point.index);
1214 text->gap_size += nchars;
1216 delete_text_property (text, nchars);
1218 if (!text->freeze_count && (text->line_start_cache != NULL))
1220 delete_expose (text, nchars, old_lines, old_height);
1221 draw_cursor (text, FALSE);
1225 gtk_stext_thaw (text);
1231 gtk_stext_set_position (GtkEditable *editable,
1234 GtkSText *text = (GtkSText *) editable;
1236 undraw_cursor (text, FALSE);
1237 text->cursor_mark = find_mark (text, position);
1238 find_cursor (text, TRUE);
1239 draw_cursor (text, FALSE);
1240 gtk_editable_select_region (editable, 0, 0);
1243 /* SYLPHEED - set_position used. Need to find out whether there's
1244 * a better way to handle this */
1245 static void gtk_stext_set_position_X (GtkEditable *editable,
1248 GtkSText *text = (GtkSText *) editable;
1250 undraw_cursor (text, FALSE);
1251 text->cursor_mark = find_mark (text, position);
1252 find_cursor (text, TRUE);
1253 draw_cursor (text, FALSE);
1257 gtk_stext_get_chars (GtkEditable *editable,
1265 g_return_val_if_fail (editable != NULL, NULL);
1266 g_return_val_if_fail (GTK_IS_STEXT (editable), NULL);
1267 text = GTK_STEXT (editable);
1270 end_pos = TEXT_LENGTH (text);
1272 if ((start_pos < 0) ||
1273 (end_pos > TEXT_LENGTH (text)) ||
1274 (end_pos < start_pos))
1277 move_gap (text, TEXT_LENGTH (text));
1278 make_forward_space (text, 1);
1280 if (text->use_wchar)
1283 ch = text->text.wc[end_pos];
1284 text->text.wc[end_pos] = 0;
1285 retval = gdk_wcstombs (text->text.wc + start_pos);
1286 text->text.wc[end_pos] = ch;
1291 ch = text->text.ch[end_pos];
1292 text->text.ch[end_pos] = 0;
1293 retval = g_strdup (text->text.ch + start_pos);
1294 text->text.ch[end_pos] = ch;
1302 gtk_stext_destroy (GtkObject *object)
1306 g_return_if_fail (object != NULL);
1307 g_return_if_fail (GTK_IS_STEXT (object));
1309 text = (GtkSText*) object;
1311 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1312 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1316 gtk_timeout_remove (text->timer);
1323 gtk_stext_disable_blink(text);
1325 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1329 gtk_stext_finalize (GtkObject *object)
1334 g_return_if_fail (object != NULL);
1335 g_return_if_fail (GTK_IS_STEXT (object));
1337 text = (GtkSText *)object;
1339 gtk_object_unref (GTK_OBJECT (text->hadj));
1340 gtk_object_unref (GTK_OBJECT (text->vadj));
1342 /* Clean up the internal structures */
1343 if (text->use_wchar)
1344 g_free (text->text.wc);
1346 g_free (text->text.ch);
1348 tmp_list = text->text_properties;
1351 destroy_text_property (tmp_list->data);
1352 tmp_list = tmp_list->next;
1355 if (text->current_font)
1356 text_font_unref (text->current_font);
1358 g_list_free (text->text_properties);
1360 if (text->use_wchar)
1362 if (text->scratch_buffer.wc)
1363 g_free (text->scratch_buffer.wc);
1367 if (text->scratch_buffer.ch)
1368 g_free (text->scratch_buffer.ch);
1371 g_list_free (text->tab_stops);
1373 GTK_OBJECT_CLASS(parent_class)->finalize (object);
1377 gtk_stext_realize (GtkWidget *widget)
1380 GtkEditable *editable;
1381 GdkWindowAttr attributes;
1382 gint attributes_mask;
1384 g_return_if_fail (widget != NULL);
1385 g_return_if_fail (GTK_IS_STEXT (widget));
1387 text = GTK_STEXT (widget);
1388 editable = GTK_EDITABLE (widget);
1389 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1391 attributes.window_type = GDK_WINDOW_CHILD;
1392 attributes.x = widget->allocation.x;
1393 attributes.y = widget->allocation.y;
1394 attributes.width = widget->allocation.width;
1395 attributes.height = widget->allocation.height;
1396 attributes.wclass = GDK_INPUT_OUTPUT;
1397 attributes.visual = gtk_widget_get_visual (widget);
1398 attributes.colormap = gtk_widget_get_colormap (widget);
1399 attributes.event_mask = gtk_widget_get_events (widget);
1400 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1401 GDK_BUTTON_PRESS_MASK |
1402 GDK_BUTTON_RELEASE_MASK |
1403 GDK_BUTTON_MOTION_MASK |
1404 GDK_ENTER_NOTIFY_MASK |
1405 GDK_LEAVE_NOTIFY_MASK |
1406 GDK_KEY_PRESS_MASK);
1407 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1409 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1410 gdk_window_set_user_data (widget->window, text);
1412 attributes.x = (widget->style->klass->xthickness + TEXT_BORDER_ROOM);
1413 attributes.y = (widget->style->klass->ythickness + TEXT_BORDER_ROOM);
1414 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1415 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1417 attributes.cursor = gdk_cursor_new (GDK_XTERM);
1418 attributes_mask |= GDK_WA_CURSOR;
1420 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1421 gdk_window_set_user_data (text->text_area, text);
1423 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1425 widget->style = gtk_style_attach (widget->style, widget->window);
1427 /* Can't call gtk_style_set_background here because it's handled specially */
1428 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1429 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1431 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1432 text->bg_gc = create_bg_gc (text);
1434 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1435 (gchar*) line_wrap_bits,
1439 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1440 (gchar*) line_arrow_bits,
1444 text->gc = gdk_gc_new (text->text_area);
1445 gdk_gc_set_exposures (text->gc, TRUE);
1446 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1448 #ifdef USE_GTKGDK_XIM
1449 if (gdk_im_ready () && (editable->ic_attr = gdk_ic_attr_new ()) != NULL)
1452 GdkColormap *colormap;
1454 GdkICAttr *attr = editable->ic_attr;
1455 GdkICAttributesType attrmask = GDK_IC_ALL_REQ;
1457 GdkIMStyle supported_style = GDK_IM_PREEDIT_NONE |
1458 GDK_IM_PREEDIT_NOTHING |
1459 GDK_IM_PREEDIT_POSITION |
1460 GDK_IM_STATUS_NONE |
1461 GDK_IM_STATUS_NOTHING;
1463 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1464 supported_style &= ~GDK_IM_PREEDIT_POSITION;
1466 attr->style = style = gdk_im_decide_style (supported_style);
1467 attr->client_window = text->text_area;
1469 if ((colormap = gtk_widget_get_colormap (widget)) !=
1470 gtk_widget_get_default_colormap ())
1472 attrmask |= GDK_IC_PREEDIT_COLORMAP;
1473 attr->preedit_colormap = colormap;
1476 switch (style & GDK_IM_PREEDIT_MASK)
1478 case GDK_IM_PREEDIT_POSITION:
1479 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1481 g_warning ("over-the-spot style requires fontset");
1485 attrmask |= GDK_IC_PREEDIT_POSITION_REQ;
1486 gdk_window_get_size (text->text_area, &width, &height);
1487 attr->spot_location.x = 0;
1488 attr->spot_location.y = height;
1489 attr->preedit_area.x = 0;
1490 attr->preedit_area.y = 0;
1491 attr->preedit_area.width = width;
1492 attr->preedit_area.height = height;
1493 attr->preedit_fontset = widget->style->font;
1497 editable->ic = gdk_ic_new (attr, attrmask);
1499 if (editable->ic == NULL)
1500 g_warning ("Can't create input context.");
1503 mask = gdk_window_get_events (text->text_area);
1504 mask |= gdk_ic_get_events (editable->ic);
1505 gdk_window_set_events (text->text_area, mask);
1507 if (GTK_WIDGET_HAS_FOCUS (widget))
1508 gdk_im_begin (editable->ic, text->text_area);
1513 realize_properties (text);
1514 gdk_window_show (text->text_area);
1515 init_properties (text);
1517 if (editable->selection_start_pos != editable->selection_end_pos)
1518 gtk_editable_claim_selection (editable, TRUE, GDK_CURRENT_TIME);
1520 recompute_geometry (text);
1524 gtk_stext_style_set (GtkWidget *widget,
1525 GtkStyle *previous_style)
1527 GtkSText *text = GTK_STEXT (widget);
1529 if (GTK_WIDGET_REALIZED (widget))
1531 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1532 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1536 gdk_gc_destroy (text->bg_gc);
1540 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1541 text->bg_gc = create_bg_gc (text);
1543 recompute_geometry (text);
1546 if (text->current_font)
1547 text_font_unref (text->current_font);
1548 text->current_font = get_text_font (widget->style->font);
1552 gtk_stext_state_changed (GtkWidget *widget,
1553 GtkStateType previous_state)
1555 GtkSText *text = GTK_STEXT (widget);
1557 if (GTK_WIDGET_REALIZED (widget))
1559 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1560 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1565 gtk_stext_unrealize (GtkWidget *widget)
1569 g_return_if_fail (widget != NULL);
1570 g_return_if_fail (GTK_IS_STEXT (widget));
1572 text = GTK_STEXT (widget);
1574 #ifdef USE_GTKGDK_XIM
1575 if (GTK_EDITABLE (widget)->ic)
1577 gdk_ic_destroy (GTK_EDITABLE (widget)->ic);
1578 GTK_EDITABLE (widget)->ic = NULL;
1580 if (GTK_EDITABLE (widget)->ic_attr)
1582 gdk_ic_attr_destroy (GTK_EDITABLE (widget)->ic_attr);
1583 GTK_EDITABLE (widget)->ic_attr = NULL;
1587 gdk_window_set_user_data (text->text_area, NULL);
1588 gdk_window_destroy (text->text_area);
1589 text->text_area = NULL;
1591 gdk_gc_destroy (text->gc);
1596 gdk_gc_destroy (text->bg_gc);
1600 gdk_pixmap_unref (text->line_wrap_bitmap);
1601 gdk_pixmap_unref (text->line_arrow_bitmap);
1603 unrealize_properties (text);
1607 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1608 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1612 clear_focus_area (GtkSText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1614 GtkWidget *widget = GTK_WIDGET (text);
1617 gint ythick = TEXT_BORDER_ROOM + widget->style->klass->ythickness;
1618 gint xthick = TEXT_BORDER_ROOM + widget->style->klass->xthickness;
1622 if (area_width == 0 || area_height == 0)
1625 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1627 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1629 gdk_gc_set_ts_origin (text->bg_gc,
1630 (- (gint)text->first_onscreen_hor_pixel + xthick) % width,
1631 (- (gint)text->first_onscreen_ver_pixel + ythick) % height);
1636 gc = widget->style->bg_gc[widget->state];
1639 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1640 area_x, area_y, area_width, area_height);
1644 gtk_stext_draw_focus (GtkWidget *widget)
1650 g_return_if_fail (widget != NULL);
1651 g_return_if_fail (GTK_IS_STEXT (widget));
1653 text = GTK_STEXT (widget);
1655 if (GTK_WIDGET_DRAWABLE (widget))
1657 gint ythick = widget->style->klass->ythickness;
1658 gint xthick = widget->style->klass->xthickness;
1659 gint xextra = TEXT_BORDER_ROOM;
1660 gint yextra = TEXT_BORDER_ROOM;
1662 TDEBUG (("in gtk_stext_draw_focus\n"));
1666 width = widget->allocation.width;
1667 height = widget->allocation.height;
1669 if (GTK_WIDGET_HAS_FOCUS (widget))
1678 gtk_paint_focus (widget->style, widget->window,
1679 NULL, widget, "text",
1681 widget->allocation.width - 1,
1682 widget->allocation.height - 1);
1685 gtk_paint_shadow (widget->style, widget->window,
1686 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1687 NULL, widget, "text",
1688 x, y, width, height);
1692 width -= 2 * xthick;
1693 height -= 2 * ythick;
1696 clear_focus_area (text, x, y, width, yextra);
1698 clear_focus_area (text, x, y + yextra,
1699 xextra, y + height - 2 * yextra);
1701 clear_focus_area (text, x + width - xextra, y + yextra,
1702 xextra, height - 2 * ythick);
1704 clear_focus_area (text, x, x + height - yextra, width, yextra);
1708 TDEBUG (("in gtk_stext_draw_focus (undrawable !!!)\n"));
1713 gtk_stext_size_request (GtkWidget *widget,
1714 GtkRequisition *requisition)
1721 g_return_if_fail (widget != NULL);
1722 g_return_if_fail (GTK_IS_STEXT (widget));
1723 g_return_if_fail (requisition != NULL);
1725 xthickness = widget->style->klass->xthickness + TEXT_BORDER_ROOM;
1726 ythickness = widget->style->klass->ythickness + TEXT_BORDER_ROOM;
1728 char_height = MIN_TEXT_HEIGHT_LINES * (widget->style->font->ascent +
1729 widget->style->font->descent);
1731 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (widget->style->font,
1732 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1736 requisition->width = char_width + xthickness * 2;
1737 requisition->height = char_height + ythickness * 2;
1741 gtk_stext_size_allocate (GtkWidget *widget,
1742 GtkAllocation *allocation)
1745 GtkEditable *editable;
1747 g_return_if_fail (widget != NULL);
1748 g_return_if_fail (GTK_IS_STEXT (widget));
1749 g_return_if_fail (allocation != NULL);
1751 text = GTK_STEXT (widget);
1752 editable = GTK_EDITABLE (widget);
1754 widget->allocation = *allocation;
1755 if (GTK_WIDGET_REALIZED (widget))
1757 gdk_window_move_resize (widget->window,
1758 allocation->x, allocation->y,
1759 allocation->width, allocation->height);
1761 gdk_window_move_resize (text->text_area,
1762 widget->style->klass->xthickness + TEXT_BORDER_ROOM,
1763 widget->style->klass->ythickness + TEXT_BORDER_ROOM,
1764 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->klass->xthickness +
1765 (gint)TEXT_BORDER_ROOM) * 2),
1766 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->klass->ythickness +
1767 (gint)TEXT_BORDER_ROOM) * 2));
1769 #ifdef USE_GTKGDK_XIM
1770 if (editable->ic && (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
1774 gdk_window_get_size (text->text_area, &width, &height);
1775 editable->ic_attr->preedit_area.width = width;
1776 editable->ic_attr->preedit_area.height = height;
1778 gdk_ic_set_attr (editable->ic,
1779 editable->ic_attr, GDK_IC_PREEDIT_AREA);
1783 recompute_geometry (text);
1788 gtk_stext_draw (GtkWidget *widget,
1791 g_return_if_fail (widget != NULL);
1792 g_return_if_fail (GTK_IS_STEXT (widget));
1793 g_return_if_fail (area != NULL);
1795 if (GTK_WIDGET_DRAWABLE (widget))
1797 expose_text (GTK_STEXT (widget), area, TRUE);
1798 gtk_widget_draw_focus (widget);
1803 gtk_stext_expose (GtkWidget *widget,
1804 GdkEventExpose *event)
1806 g_return_val_if_fail (widget != NULL, FALSE);
1807 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1808 g_return_val_if_fail (event != NULL, FALSE);
1810 if (event->window == GTK_STEXT (widget)->text_area)
1812 TDEBUG (("in gtk_stext_expose (expose)\n"));
1813 expose_text (GTK_STEXT (widget), &event->area, TRUE);
1815 else if (event->count == 0)
1817 TDEBUG (("in gtk_stext_expose (focus)\n"));
1818 gtk_widget_draw_focus (widget);
1825 gtk_stext_scroll_timeout (gpointer data)
1828 GdkEventMotion event;
1830 GdkModifierType mask;
1832 GDK_THREADS_ENTER ();
1834 text = GTK_STEXT (data);
1837 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1839 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1846 gtk_stext_motion_notify (GTK_WIDGET (text), &event);
1849 GDK_THREADS_LEAVE ();
1855 gtk_stext_button_press (GtkWidget *widget,
1856 GdkEventButton *event)
1859 GtkEditable *editable;
1860 static GdkAtom ctext_atom = GDK_NONE;
1862 g_return_val_if_fail (widget != NULL, FALSE);
1863 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1864 g_return_val_if_fail (event != NULL, FALSE);
1866 if (ctext_atom == GDK_NONE)
1867 ctext_atom = gdk_atom_intern ("COMPOUND_TEXT", FALSE);
1869 text = GTK_STEXT (widget);
1870 editable = GTK_EDITABLE (widget);
1872 if (text->button && (event->button != text->button))
1875 text->button = event->button;
1877 if (!GTK_WIDGET_HAS_FOCUS (widget))
1878 gtk_widget_grab_focus (widget);
1880 if (event->button == 1)
1882 switch (event->type)
1884 case GDK_BUTTON_PRESS:
1885 gtk_grab_add (widget);
1887 undraw_cursor (text, FALSE);
1888 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1889 draw_cursor (text, FALSE);
1891 /* Set it now, so we display things right. We'll unset it
1892 * later if things don't work out */
1893 editable->has_selection = TRUE;
1894 gtk_stext_set_selection (GTK_EDITABLE(text),
1895 text->cursor_mark.index,
1896 text->cursor_mark.index);
1900 case GDK_2BUTTON_PRESS:
1901 gtk_stext_select_word (text, event->time);
1904 case GDK_3BUTTON_PRESS:
1905 gtk_stext_select_line (text, event->time);
1912 else if (event->type == GDK_BUTTON_PRESS)
1914 if ((event->button == 2) && editable->editable)
1916 if (editable->selection_start_pos == editable->selection_end_pos ||
1917 editable->has_selection)
1919 undraw_cursor (text, FALSE);
1920 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1921 draw_cursor (text, FALSE);
1925 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1926 ctext_atom, event->time);
1930 gtk_grab_add (widget);
1932 undraw_cursor (text, FALSE);
1933 find_mouse_cursor (text, event->x, event->y);
1934 draw_cursor (text, FALSE);
1936 gtk_stext_set_selection (GTK_EDITABLE(text),
1937 text->cursor_mark.index,
1938 text->cursor_mark.index);
1940 editable->has_selection = FALSE;
1941 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1942 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1950 gtk_stext_button_release (GtkWidget *widget,
1951 GdkEventButton *event)
1954 GtkEditable *editable;
1955 g_return_val_if_fail (widget != NULL, FALSE);
1956 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1957 g_return_val_if_fail (event != NULL, FALSE);
1959 text = GTK_STEXT (widget);
1961 gtk_grab_remove (widget);
1963 if (text->button != event->button)
1970 gtk_timeout_remove (text->timer);
1974 if (event->button == 1)
1976 text = GTK_STEXT (widget);
1977 editable = GTK_EDITABLE (widget);
1979 gtk_grab_remove (widget);
1981 editable->has_selection = FALSE;
1982 if (editable->selection_start_pos != editable->selection_end_pos)
1984 if (gtk_selection_owner_set (widget,
1985 GDK_SELECTION_PRIMARY,
1987 editable->has_selection = TRUE;
1989 gtk_stext_update_text (editable, editable->selection_start_pos,
1990 editable->selection_end_pos);
1994 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1995 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1998 else if (event->button == 3)
2000 gtk_grab_remove (widget);
2003 undraw_cursor (text, FALSE);
2004 find_cursor (text, TRUE);
2005 draw_cursor (text, FALSE);
2011 gtk_stext_motion_notify (GtkWidget *widget,
2012 GdkEventMotion *event)
2017 GdkModifierType mask;
2019 g_return_val_if_fail (widget != NULL, FALSE);
2020 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2021 g_return_val_if_fail (event != NULL, FALSE);
2023 text = GTK_STEXT (widget);
2027 mask = event->state;
2028 if (event->is_hint || (text->text_area != event->window))
2030 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
2033 if ((text->button == 0) ||
2034 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
2037 gdk_window_get_size (text->text_area, NULL, &height);
2039 if ((y < 0) || (y > height))
2041 if (text->timer == 0)
2043 text->timer = gtk_timeout_add (SCROLL_TIME,
2044 gtk_stext_scroll_timeout,
2048 scroll_int (text, y/2);
2050 scroll_int (text, (y - height)/2);
2056 undraw_cursor (GTK_STEXT (widget), FALSE);
2057 find_mouse_cursor (GTK_STEXT (widget), x, y);
2058 draw_cursor (GTK_STEXT (widget), FALSE);
2060 gtk_stext_set_selection (GTK_EDITABLE(text),
2061 GTK_EDITABLE(text)->selection_start_pos,
2062 text->cursor_mark.index);
2068 gtk_stext_insert_text (GtkEditable *editable,
2069 const gchar *new_text,
2070 gint new_text_length,
2073 GtkSText *text = GTK_STEXT (editable);
2075 GdkColor *fore, *back;
2077 TextProperty *property;
2079 gtk_stext_set_point (text, *position);
2081 property = MARK_CURRENT_PROPERTY (&text->point);
2082 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
2083 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
2084 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
2086 gtk_stext_insert (text, font, fore, back, new_text, new_text_length);
2088 *position = text->point.index;
2092 gtk_stext_delete_text (GtkEditable *editable,
2098 g_return_if_fail (start_pos >= 0);
2100 text = GTK_STEXT (editable);
2102 gtk_stext_set_point (text, start_pos);
2104 end_pos = TEXT_LENGTH (text);
2106 if (end_pos > start_pos)
2107 gtk_stext_forward_delete (text, end_pos - start_pos);
2111 gtk_stext_key_press (GtkWidget *widget,
2115 GtkEditable *editable;
2120 g_return_val_if_fail (widget != NULL, FALSE);
2121 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2122 g_return_val_if_fail (event != NULL, FALSE);
2126 text = GTK_STEXT (widget);
2127 editable = GTK_EDITABLE (widget);
2129 key = event->keyval;
2132 if ((GTK_EDITABLE(text)->editable == FALSE))
2134 switch (event->keyval)
2137 if (event->state & GDK_CONTROL_MASK)
2138 scroll_int (text, -text->vadj->value);
2143 if (event->state & GDK_CONTROL_MASK)
2144 scroll_int (text, +text->vadj->upper);
2148 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
2149 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
2151 scroll_int (text, -KEY_SCROLL_PIXELS);
2153 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
2155 if (event->state & GDK_CONTROL_MASK)
2156 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2167 gint extend_selection;
2169 guint initial_pos = editable->current_pos;
2171 text->point = find_mark (text, text->cursor_mark.index);
2173 extend_selection = event->state & GDK_SHIFT_MASK;
2174 extend_start = FALSE;
2176 if (extend_selection)
2178 editable->has_selection = TRUE;
2180 if (editable->selection_start_pos == editable->selection_end_pos)
2182 editable->selection_start_pos = text->point.index;
2183 editable->selection_end_pos = text->point.index;
2186 extend_start = (text->point.index == editable->selection_start_pos);
2187 gtk_stext_disable_blink(text);
2193 if (!extend_selection)
2194 gtk_stext_enable_blink(text);
2196 if (event->keyval != GDK_Up && event->keyval != GDK_Down) {
2197 reset_persist_col_pos(text);
2200 switch (event->keyval)
2203 if (event->state & GDK_CONTROL_MASK) {
2204 if (text->wrap_rmargin == 0) {
2205 /* SYLPHEED: old behaviour */
2206 move_cursor_buffer_ver (text, -1);
2209 /* SYLPHEED: contrived, but "trusty" */
2210 move_cursor_buffer_ver(text, -1);
2211 move_cursor_to_display_row_start(text);
2215 if (text->wrap_rmargin > 0) {
2216 /* SYLPHEED: line start */
2217 move_cursor_to_display_row_start(text);
2220 gtk_stext_move_beginning_of_line (text);
2225 if (event->state & GDK_CONTROL_MASK) {
2226 /* SYLPHEED: a little bit contrived... */
2227 if (text->wrap_rmargin == 0) {
2229 move_cursor_buffer_ver (text, +1);
2232 move_cursor_buffer_ver(text, +1);
2233 move_cursor_to_display_row_end(text);
2237 if (text->wrap_rmargin > 0) {
2238 /* SYLPHEED: line end */
2239 move_cursor_to_display_row_end(text);
2242 gtk_stext_move_end_of_line(text);
2247 move_cursor_page_ver (text, -1);
2249 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
2250 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
2252 if (text->wrap_rmargin > 0) {
2255 move_cursor_to_display_row_up(text);
2258 move_cursor_ver (text, -1);
2262 move_cursor_to_display_row_down(text);
2263 /* move_cursor_ver (text, +1); */
2266 if (event->state & GDK_CONTROL_MASK)
2267 gtk_stext_move_backward_word (text);
2269 move_cursor_hor (text, -1);
2272 if (event->state & GDK_CONTROL_MASK)
2273 gtk_stext_move_forward_word (text);
2275 move_cursor_hor (text, +1);
2279 if (event->state & GDK_CONTROL_MASK)
2280 gtk_stext_delete_backward_word (text);
2282 gtk_stext_delete_backward_character (text);
2285 gtk_stext_delete_line (text);
2288 if (event->state & GDK_SHIFT_MASK)
2290 extend_selection = FALSE;
2291 gtk_editable_paste_clipboard (editable);
2293 else if (event->state & GDK_CONTROL_MASK)
2295 gtk_editable_copy_clipboard (editable);
2299 /* gtk_toggle_insert(text) -- IMPLEMENT */
2304 if (event->state & GDK_CONTROL_MASK) {
2305 gtk_stext_delete_forward_word (text);
2307 else if (event->state & GDK_SHIFT_MASK)
2309 extend_selection = FALSE;
2310 gtk_editable_cut_clipboard (editable);
2313 gtk_stext_delete_forward_character (text);
2318 position = text->point.index;
2319 gtk_editable_insert_text (editable, "\t", 1, &position);
2322 if (event->state & GDK_CONTROL_MASK)
2323 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2326 position = text->point.index;
2327 gtk_editable_insert_text (editable, "\n", 1, &position);
2331 /* Don't insert literally */
2338 if (event->state & GDK_CONTROL_MASK)
2340 if ((key >= 'A') && (key <= 'Z'))
2343 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2345 (* control_keys[(int) (key - 'a')]) (editable, event->time);
2351 else if (event->state & GDK_MOD1_MASK)
2353 if ((key >= 'A') && (key <= 'Z'))
2356 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2358 (* alt_keys[(int) (key - 'a')]) (editable, event->time);
2364 else if (event->length > 0)
2366 extend_selection = FALSE;
2368 gtk_editable_delete_selection (editable);
2369 position = text->point.index;
2370 gtk_editable_insert_text (editable, event->string, event->length, &position);
2378 if (return_val && (editable->current_pos != initial_pos))
2380 if (extend_selection)
2382 if (editable->current_pos < editable->selection_start_pos)
2383 gtk_stext_set_selection (editable, editable->current_pos,
2384 editable->selection_end_pos);
2385 else if (editable->current_pos > editable->selection_end_pos)
2386 gtk_stext_set_selection (editable, editable->selection_start_pos,
2387 editable->current_pos);
2391 gtk_stext_set_selection (editable, editable->current_pos,
2392 editable->selection_end_pos);
2394 gtk_stext_set_selection (editable, editable->selection_start_pos,
2395 editable->current_pos);
2399 gtk_stext_set_selection (editable, 0, 0);
2401 gtk_editable_claim_selection (editable,
2402 editable->selection_start_pos != editable->selection_end_pos,
2411 gtk_stext_focus_in (GtkWidget *widget,
2412 GdkEventFocus *event)
2414 g_return_val_if_fail (widget != NULL, FALSE);
2415 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2416 g_return_val_if_fail (event != NULL, FALSE);
2418 TDEBUG (("in gtk_stext_focus_in\n"));
2420 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
2421 gtk_widget_draw_focus (widget);
2423 #ifdef USE_GTKGDK_XIM
2424 if (GTK_EDITABLE(widget)->ic)
2425 gdk_im_begin (GTK_EDITABLE(widget)->ic, GTK_STEXT(widget)->text_area);
2428 draw_cursor (GTK_STEXT(widget), TRUE);
2429 GTK_STEXT(widget)->cursor_visible = TRUE;
2430 gtk_stext_enable_blink(GTK_STEXT(widget));
2436 gtk_stext_focus_out (GtkWidget *widget,
2437 GdkEventFocus *event)
2439 g_return_val_if_fail (widget != NULL, FALSE);
2440 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2441 g_return_val_if_fail (event != NULL, FALSE);
2443 TDEBUG (("in gtk_stext_focus_out\n"));
2445 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
2446 gtk_widget_draw_focus (widget);
2449 * should disable blink before undrawing the cursor - disabling blink
2450 * redraws the cursor.
2452 gtk_stext_disable_blink(GTK_STEXT(widget));
2453 undraw_cursor (GTK_STEXT(widget), TRUE);
2454 GTK_STEXT(widget)->cursor_visible = FALSE;
2456 #ifdef USE_GTKGDK_XIM
2464 gtk_stext_adjustment (GtkAdjustment *adjustment,
2469 g_return_if_fail (adjustment != NULL);
2470 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2471 g_return_if_fail (text != NULL);
2472 g_return_if_fail (GTK_IS_STEXT (text));
2474 /* Clamp the value here, because we'll get really confused
2475 * if someone tries to move the adjusment outside of the
2478 old_val = adjustment->value;
2480 adjustment->value = MIN (adjustment->value, adjustment->upper - adjustment->page_size);
2481 adjustment->value = MAX (adjustment->value, 0.0);
2483 if (adjustment->value != old_val)
2485 gtk_signal_handler_block_by_func (GTK_OBJECT (adjustment),
2486 GTK_SIGNAL_FUNC (gtk_stext_adjustment),
2488 gtk_adjustment_changed (adjustment);
2489 gtk_signal_handler_unblock_by_func (GTK_OBJECT (adjustment),
2490 GTK_SIGNAL_FUNC (gtk_stext_adjustment),
2494 /* Just ignore it if we haven't been size-allocated and realized yet */
2495 if (text->line_start_cache == NULL)
2498 if (adjustment == text->hadj)
2500 g_warning ("horizontal scrolling not implemented");
2504 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2508 undraw_cursor (text, FALSE);
2511 scroll_down (text, diff);
2512 else /* if (diff < 0) */
2513 scroll_up (text, diff);
2515 draw_cursor (text, FALSE);
2517 text->last_ver_value = adjustment->value;
2523 gtk_stext_disconnect (GtkAdjustment *adjustment,
2526 g_return_if_fail (adjustment != NULL);
2527 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2528 g_return_if_fail (text != NULL);
2529 g_return_if_fail (GTK_IS_STEXT (text));
2531 if (adjustment == text->hadj)
2532 gtk_stext_set_adjustments (text, NULL, text->vadj);
2533 if (adjustment == text->vadj)
2534 gtk_stext_set_adjustments (text, text->hadj, NULL);
2538 static GtkSPropertyMark
2539 find_this_line_start_mark (GtkSText* text, guint point_position, const GtkSPropertyMark* near)
2541 GtkSPropertyMark mark;
2543 mark = find_mark_near (text, point_position, near);
2545 while (mark.index > 0 &&
2546 GTK_STEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2547 decrement_mark (&mark);
2553 init_tab_cont (GtkSText* text, PrevTabCont* tab_cont)
2555 tab_cont->pixel_offset = 0;
2556 tab_cont->tab_start.tab_stops = text->tab_stops;
2557 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2559 if (!tab_cont->tab_start.to_next_tab)
2560 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2564 line_params_iterate (GtkSText* text,
2565 const GtkSPropertyMark* mark0,
2566 const PrevTabCont* tab_mark0,
2569 LineIteratorFunction iter)
2570 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2571 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2572 * for each line following MARK, iteration continues unless ITER_CALL
2575 GtkSPropertyMark mark = *mark0;
2576 PrevTabCont tab_conts[2];
2577 LineParams *lp, lpbuf;
2578 gint tab_cont_index = 0;
2581 tab_conts[0] = *tab_mark0;
2583 init_tab_cont (text, tab_conts);
2588 lp = g_chunk_new (LineParams, params_mem_chunk);
2592 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2593 tab_conts + (tab_cont_index + 1) % 2);
2595 if ((*iter) (text, lp, data))
2598 if (LAST_INDEX (text, lp->end))
2602 advance_mark (&mark);
2603 tab_cont_index = (tab_cont_index + 1) % 2;
2608 fetch_lines_iterator (GtkSText* text, LineParams* lp, void* data)
2610 FetchLinesData *fldata = (FetchLinesData*) data;
2612 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2614 switch (fldata->fl_type)
2616 case FetchLinesCount:
2617 if (!text->line_wrap || !lp->wraps)
2620 if (fldata->data >= fldata->data_max)
2624 case FetchLinesPixels:
2626 fldata->data += LINE_HEIGHT(*lp);
2628 if (fldata->data >= fldata->data_max)
2638 fetch_lines (GtkSText* text,
2639 const GtkSPropertyMark* mark0,
2640 const PrevTabCont* tab_cont0,
2644 FetchLinesData fl_data;
2646 fl_data.new_lines = NULL;
2648 fl_data.data_max = data;
2649 fl_data.fl_type = fl_type;
2651 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2653 return g_list_reverse (fl_data.new_lines);
2657 fetch_lines_backward (GtkSText* text)
2659 GList* new_lines = NULL, *new_line_start;
2660 GtkSPropertyMark mark;
2662 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2665 mark = find_this_line_start_mark (text,
2666 CACHE_DATA(text->line_start_cache).start.index - 1,
2667 &CACHE_DATA(text->line_start_cache).start);
2669 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2671 while (new_line_start->next)
2672 new_line_start = new_line_start->next;
2674 new_line_start->next = text->line_start_cache;
2675 text->line_start_cache->prev = new_line_start;
2679 fetch_lines_forward (GtkSText* text, gint line_count)
2681 GtkSPropertyMark mark;
2682 GList* line = text->line_start_cache;
2687 mark = CACHE_DATA(line).end;
2689 if (LAST_INDEX (text, mark))
2692 advance_mark(&mark);
2694 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2697 line->next->prev = line;
2700 /* Compute the number of lines, and vertical pixels for n characters
2701 * starting from the point
2704 compute_lines_pixels (GtkSText* text, guint char_count,
2705 guint *lines, guint *pixels)
2707 GList *line = text->current_line;
2708 gint chars_left = char_count;
2713 /* If chars_left == 0, that means we're joining two lines in a
2714 * deletion, so add in the values for the next line as well
2716 for (; line && chars_left >= 0; line = line->next)
2718 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2720 if (line == text->current_line)
2721 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2723 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2725 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2729 chars_left = 0; /* force another loop */
2732 fetch_lines_forward (text, 1);
2737 total_line_height (GtkSText* text, GList* line, gint line_count)
2741 for (; line && line_count > 0; line = line->next)
2743 height += LINE_HEIGHT(CACHE_DATA(line));
2745 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2749 fetch_lines_forward (text, line_count);
2756 swap_lines (GtkSText* text, GList* old, GList* new, guint old_line_count)
2758 if (old == text->line_start_cache)
2762 for (; old_line_count > 0; old_line_count -= 1)
2764 while (text->line_start_cache &&
2766 CACHE_DATA(text->line_start_cache).wraps)
2767 remove_cache_line(text, text->line_start_cache);
2769 remove_cache_line(text, text->line_start_cache);
2772 last = g_list_last (new);
2774 last->next = text->line_start_cache;
2776 if (text->line_start_cache)
2777 text->line_start_cache->prev = last;
2779 text->line_start_cache = new;
2785 g_assert (old->prev);
2789 for (; old_line_count > 0; old_line_count -= 1)
2791 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2792 old = remove_cache_line (text, old);
2794 old = remove_cache_line (text, old);
2800 last = g_list_last (new);
2810 correct_cache_delete (GtkSText* text, gint nchars, gint lines)
2812 GList* cache = text->current_line;
2815 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2818 for (; cache; cache = cache->next)
2820 GtkSPropertyMark *start = &CACHE_DATA(cache).start;
2821 GtkSPropertyMark *end = &CACHE_DATA(cache).end;
2823 start->index -= nchars;
2824 end->index -= nchars;
2826 if (LAST_INDEX (text, text->point) &&
2827 start->index == text->point.index)
2828 *start = text->point;
2829 else if (start->property == text->point.property)
2830 start->offset = start->index - (text->point.index - text->point.offset);
2832 if (LAST_INDEX (text, text->point) &&
2833 end->index == text->point.index)
2835 if (end->property == text->point.property)
2836 end->offset = end->index - (text->point.index - text->point.offset);
2838 /*TEXT_ASSERT_MARK(text, start, "start");*/
2839 /*TEXT_ASSERT_MARK(text, end, "end");*/
2844 delete_expose (GtkSText* text, guint nchars, guint old_lines, guint old_pixels)
2846 GtkWidget *widget = GTK_WIDGET (text);
2849 guint new_pixels = 0;
2851 GList* new_line = NULL;
2854 text->cursor_virtual_x = 0;
2856 correct_cache_delete (text, nchars, old_lines);
2858 pixel_height = pixel_height_of(text, text->current_line) -
2859 LINE_HEIGHT(CACHE_DATA(text->current_line));
2861 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2862 CACHE_DATA(text->current_line).start = text->point;
2864 new_line = fetch_lines (text,
2865 &CACHE_DATA(text->current_line).start,
2866 &CACHE_DATA(text->current_line).tab_cont,
2870 swap_lines (text, text->current_line, new_line, old_lines);
2872 text->current_line = new_line;
2874 new_pixels = total_line_height (text, new_line, 1);
2876 gdk_window_get_size (text->text_area, &width, &height);
2878 if (old_pixels != new_pixels)
2880 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2882 gdk_draw_pixmap (text->text_area,
2886 pixel_height + old_pixels,
2888 pixel_height + new_pixels,
2892 text->vadj->upper += new_pixels;
2893 text->vadj->upper -= old_pixels;
2894 adjust_adj (text, text->vadj);
2898 rect.y = pixel_height;
2900 rect.height = new_pixels;
2902 expose_text (text, &rect, FALSE);
2903 gtk_stext_draw_focus ( (GtkWidget *) text);
2905 text->cursor_mark = text->point;
2907 find_cursor (text, TRUE);
2909 if (old_pixels != new_pixels)
2911 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2914 rect.y = pixel_height + new_pixels;
2916 rect.height = height - rect.y;
2918 expose_text (text, &rect, FALSE);
2921 process_exposes (text);
2928 /* note, the point has already been moved forward */
2930 correct_cache_insert (GtkSText* text, gint nchars)
2933 GtkSPropertyMark *start;
2934 GtkSPropertyMark *end;
2936 /* If we inserted a property exactly at the beginning of the
2937 * line, we have to correct here, or fetch_lines will
2940 start = &CACHE_DATA(text->current_line).start;
2941 if (start->index == text->point.index - nchars)
2943 *start = text->point;
2944 move_mark_n (start, -nchars);
2947 /* Now correct the offsets, and check for start or end marks that
2948 * are after the point, yet point to a property before the point's
2949 * property. This indicates that they are meant to point to the
2950 * second half of a property we split in insert_text_property(), so
2951 * we fix them up that way.
2953 cache = text->current_line->next;
2955 for (; cache; cache = cache->next)
2957 start = &CACHE_DATA(cache).start;
2958 end = &CACHE_DATA(cache).end;
2960 if (LAST_INDEX (text, text->point) &&
2961 start->index == text->point.index)
2962 *start = text->point;
2965 if (start->property == text->point.property)
2967 start->offset += nchars;
2968 start->index += nchars;
2970 else if (start->property->next &&
2971 (start->property->next->next == text->point.property))
2973 /* We split the property, and this is the second half */
2974 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2975 start->index += nchars;
2976 start->property = text->point.property;
2979 start->index += nchars;
2982 if (LAST_INDEX (text, text->point) &&
2983 end->index == text->point.index)
2987 if (end->property == text->point.property)
2989 end->offset += nchars;
2990 end->index += nchars;
2992 else if (end->property->next &&
2993 (end->property->next->next == text->point.property))
2995 /* We split the property, and this is the second half */
2996 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2997 end->index += nchars;
2998 end->property = text->point.property;
3001 end->index += nchars;
3004 /*TEXT_ASSERT_MARK(text, start, "start");*/
3005 /*TEXT_ASSERT_MARK(text, end, "end");*/
3011 insert_expose (GtkSText* text, guint old_pixels, gint nchars,
3012 guint new_line_count)
3014 GtkWidget *widget = GTK_WIDGET (text);
3017 guint new_pixels = 0;
3019 GList* new_lines = NULL;
3022 text->cursor_virtual_x = 0;
3024 undraw_cursor (text, FALSE);
3026 correct_cache_insert (text, nchars);
3028 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
3030 pixel_height = pixel_height_of(text, text->current_line) -
3031 LINE_HEIGHT(CACHE_DATA(text->current_line));
3033 new_lines = fetch_lines (text,
3034 &CACHE_DATA(text->current_line).start,
3035 &CACHE_DATA(text->current_line).tab_cont,
3039 swap_lines (text, text->current_line, new_lines, 1);
3041 text->current_line = new_lines;
3043 new_pixels = total_line_height (text, new_lines, new_line_count);
3045 gdk_window_get_size (text->text_area, &width, &height);
3047 if (old_pixels != new_pixels)
3049 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
3051 gdk_draw_pixmap (text->text_area,
3055 pixel_height + old_pixels,
3057 pixel_height + new_pixels,
3059 height + (old_pixels - new_pixels) - pixel_height);
3062 text->vadj->upper += new_pixels;
3063 text->vadj->upper -= old_pixels;
3064 adjust_adj (text, text->vadj);
3068 rect.y = pixel_height;
3070 rect.height = new_pixels;
3072 expose_text (text, &rect, FALSE);
3073 gtk_stext_draw_focus ( (GtkWidget *) text);
3075 text->cursor_mark = text->point;
3077 find_cursor (text, TRUE);
3079 draw_cursor (text, FALSE);
3081 if (old_pixels != new_pixels)
3083 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
3086 rect.y = pixel_height + new_pixels;
3088 rect.height = height - rect.y;
3090 expose_text (text, &rect, FALSE);
3093 process_exposes (text);
3096 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
3101 /* Text property functions */
3104 font_hash (gconstpointer font)
3106 return gdk_font_id ((const GdkFont*) font);
3109 static GHashTable *font_cache_table = NULL;
3111 static GtkSTextFont*
3112 get_text_font (GdkFont* gfont)
3117 if (!font_cache_table)
3118 font_cache_table = g_hash_table_new (font_hash, (GCompareFunc) gdk_font_equal);
3120 tf = g_hash_table_lookup (font_cache_table, gfont);
3128 tf = g_new (GtkSTextFont, 1);
3131 tf->gdk_font = gfont;
3132 gdk_font_ref (gfont);
3134 for(i = 0; i < 256; i += 1)
3135 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
3137 g_hash_table_insert (font_cache_table, gfont, tf);
3143 text_font_unref (GtkSTextFont *text_font)
3145 text_font->ref_count--;
3146 if (text_font->ref_count == 0)
3148 g_hash_table_remove (font_cache_table, text_font->gdk_font);
3149 gdk_font_unref (text_font->gdk_font);
3155 text_properties_equal (TextProperty* prop, GdkFont* font, GdkColor *fore, GdkColor *back)
3157 if (prop->flags & PROPERTY_FONT)
3160 GtkSTextFont *text_font;
3165 text_font = get_text_font (font);
3167 retval = (prop->font == text_font);
3168 text_font_unref (text_font);
3177 if (prop->flags & PROPERTY_FOREGROUND)
3179 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
3186 if (prop->flags & PROPERTY_BACKGROUND)
3188 if (!back || !gdk_color_equal (&prop->back_color, back))
3199 realize_property (GtkSText *text, TextProperty *prop)
3201 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3203 if (prop->flags & PROPERTY_FOREGROUND)
3204 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
3206 if (prop->flags & PROPERTY_BACKGROUND)
3207 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
3211 realize_properties (GtkSText *text)
3213 GList *tmp_list = text->text_properties;
3217 realize_property (text, tmp_list->data);
3219 tmp_list = tmp_list->next;
3224 unrealize_property (GtkSText *text, TextProperty *prop)
3226 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3228 if (prop->flags & PROPERTY_FOREGROUND)
3229 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
3231 if (prop->flags & PROPERTY_BACKGROUND)
3232 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
3236 unrealize_properties (GtkSText *text)
3238 GList *tmp_list = text->text_properties;
3242 unrealize_property (text, tmp_list->data);
3244 tmp_list = tmp_list->next;
3248 static TextProperty*
3249 new_text_property (GtkSText *text, GdkFont *font, GdkColor* fore,
3250 GdkColor* back, guint length)
3254 if (text_property_chunk == NULL)
3256 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
3257 sizeof(TextProperty),
3258 1024*sizeof(TextProperty),
3262 prop = g_chunk_new(TextProperty, text_property_chunk);
3267 prop->flags |= PROPERTY_FONT;
3268 prop->font = get_text_font (font);
3275 prop->flags |= PROPERTY_FOREGROUND;
3276 prop->fore_color = *fore;
3281 prop->flags |= PROPERTY_BACKGROUND;
3282 prop->back_color = *back;
3285 prop->length = length;
3287 if (GTK_WIDGET_REALIZED (text))
3288 realize_property (text, prop);
3294 destroy_text_property (TextProperty *prop)
3297 text_font_unref (prop->font);
3299 g_mem_chunk_free (text_property_chunk, prop);
3302 /* Flop the memory between the point and the gap around like a
3305 move_gap (GtkSText* text, guint index)
3307 if (text->gap_position < index)
3309 gint diff = index - text->gap_position;
3311 if (text->use_wchar)
3312 g_memmove (text->text.wc + text->gap_position,
3313 text->text.wc + text->gap_position + text->gap_size,
3314 diff*sizeof (GdkWChar));
3316 g_memmove (text->text.ch + text->gap_position,
3317 text->text.ch + text->gap_position + text->gap_size,
3320 text->gap_position = index;
3322 else if (text->gap_position > index)
3324 gint diff = text->gap_position - index;
3326 if (text->use_wchar)
3327 g_memmove (text->text.wc + index + text->gap_size,
3328 text->text.wc + index,
3329 diff*sizeof (GdkWChar));
3331 g_memmove (text->text.ch + index + text->gap_size,
3332 text->text.ch + index,
3335 text->gap_position = index;
3339 /* Increase the gap size. */
3341 make_forward_space (GtkSText* text, guint len)
3343 if (text->gap_size < len)
3345 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3347 if (sum >= text->text_len)
3351 while (i <= sum) i <<= 1;
3353 if (text->use_wchar)
3354 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3355 i*sizeof(GdkWChar));
3357 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3361 if (text->use_wchar)
3362 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3363 text->text.wc + text->gap_position + text->gap_size,
3364 (text->text_end - (text->gap_position + text->gap_size))
3367 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3368 text->text.ch + text->gap_position + text->gap_size,
3369 text->text_end - (text->gap_position + text->gap_size));
3371 text->text_end += len*2;
3372 text->gap_size += len*2;
3376 /* Inserts into the text property list a list element that guarantees
3377 * that for len characters following the point, text has the correct
3378 * property. does not move point. adjusts text_properties_point and
3379 * text_properties_point_offset relative to the current value of
3382 insert_text_property (GtkSText* text, GdkFont* font,
3383 GdkColor *fore, GdkColor* back, guint len)
3385 GtkSPropertyMark *mark = &text->point;
3386 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3387 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3389 if (MARK_OFFSET(mark) == 0)
3391 /* Point is on the boundary of two properties.
3392 * If it is the same as either, grow, else insert
3395 if (text_properties_equal(forward_prop, font, fore, back))
3397 /* Grow the property in front of us. */
3399 MARK_PROPERTY_LENGTH(mark) += len;
3401 else if (backward_prop &&
3402 text_properties_equal(backward_prop, font, fore, back))
3404 /* Grow property behind us, point property and offset
3407 SET_PROPERTY_MARK (&text->point,
3408 MARK_PREV_LIST_PTR (mark),
3409 backward_prop->length);
3411 backward_prop->length += len;
3413 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3414 (forward_prop->length == 1))
3416 /* Next property just has last position, take it over */
3418 if (GTK_WIDGET_REALIZED (text))
3419 unrealize_property (text, forward_prop);
3421 forward_prop->flags = 0;
3424 forward_prop->flags |= PROPERTY_FONT;
3425 forward_prop->font = get_text_font (font);
3428 forward_prop->font = NULL;
3432 forward_prop->flags |= PROPERTY_FOREGROUND;
3433 forward_prop->fore_color = *fore;
3437 forward_prop->flags |= PROPERTY_BACKGROUND;
3438 forward_prop->back_color = *back;
3440 forward_prop->length += len;
3442 if (GTK_WIDGET_REALIZED (text))
3443 realize_property (text, forward_prop);
3447 /* Splice a new property into the list. */
3449 GList* new_prop = g_list_alloc();
3451 new_prop->next = MARK_LIST_PTR(mark);
3452 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3453 new_prop->next->prev = new_prop;
3456 new_prop->prev->next = new_prop;
3458 new_prop->data = new_text_property (text, font, fore, back, len);
3460 SET_PROPERTY_MARK (mark, new_prop, 0);
3465 /* The following will screw up the line_start cache,
3466 * we'll fix it up in correct_cache_insert
3469 /* In the middle of forward_prop, if properties are equal,
3470 * just add to its length, else split it into two and splice
3472 if (text_properties_equal (forward_prop, font, fore, back))
3474 forward_prop->length += len;
3476 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3477 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3479 /* Inserting before only the last position in the text */
3482 forward_prop->length -= 1;
3484 new_prop = g_list_alloc();
3485 new_prop->data = new_text_property (text, font, fore, back, len+1);
3486 new_prop->prev = MARK_LIST_PTR(mark);
3487 new_prop->next = NULL;
3488 MARK_NEXT_LIST_PTR(mark) = new_prop;
3490 SET_PROPERTY_MARK (mark, new_prop, 0);
3494 GList* new_prop = g_list_alloc();
3495 GList* new_prop_forward = g_list_alloc();
3496 gint old_length = forward_prop->length;
3497 GList* next = MARK_NEXT_LIST_PTR(mark);
3499 /* Set the new lengths according to where they are split. Construct
3500 * two new properties. */
3501 forward_prop->length = MARK_OFFSET(mark);
3503 new_prop_forward->data =
3504 new_text_property(text,
3505 forward_prop->flags & PROPERTY_FONT ?
3506 forward_prop->font->gdk_font : NULL,
3507 forward_prop->flags & PROPERTY_FOREGROUND ?
3508 &forward_prop->fore_color : NULL,
3509 forward_prop->flags & PROPERTY_BACKGROUND ?
3510 &forward_prop->back_color : NULL,
3511 old_length - forward_prop->length);
3513 new_prop->data = new_text_property(text, font, fore, back, len);
3515 /* Now splice things in. */
3516 MARK_NEXT_LIST_PTR(mark) = new_prop;
3517 new_prop->prev = MARK_LIST_PTR(mark);
3519 new_prop->next = new_prop_forward;
3520 new_prop_forward->prev = new_prop;
3522 new_prop_forward->next = next;
3525 next->prev = new_prop_forward;
3527 SET_PROPERTY_MARK (mark, new_prop, 0);
3531 while (text->text_properties_end->next)
3532 text->text_properties_end = text->text_properties_end->next;
3534 while (text->text_properties->prev)
3535 text->text_properties = text->text_properties->prev;
3539 delete_text_property (GtkSText* text, guint nchars)
3541 /* Delete nchars forward from point. */
3543 /* Deleting text properties is problematical, because we
3544 * might be storing around marks pointing to a property.
3546 * The marks in question and how we handle them are:
3548 * point: We know the new value, since it will be at the
3549 * end of the deleted text, and we move it there
3551 * cursor: We just remove the mark and set it equal to the
3552 * point after the operation.
3553 * line-start cache: We replace most affected lines.
3554 * The current line gets used to fetch the new
3555 * lines so, if necessary, (delete at the beginning
3556 * of a line) we fix it up by setting it equal to the
3564 for(; nchars; nchars -= 1)
3566 prop = MARK_CURRENT_PROPERTY(&text->point);
3570 if (prop->length == 0)
3572 tmp = MARK_LIST_PTR (&text->point);
3574 is_first = tmp == text->text_properties;
3576 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3577 text->point.offset = 0;
3579 if (GTK_WIDGET_REALIZED (text))
3580 unrealize_property (text, prop);
3582 destroy_text_property (prop);
3583 g_list_free_1 (tmp);
3585 prop = MARK_CURRENT_PROPERTY (&text->point);
3588 text->text_properties = MARK_LIST_PTR (&text->point);
3590 g_assert (prop->length != 0);
3592 else if (prop->length == text->point.offset)
3594 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3595 text->point.offset = 0;
3599 /* Check to see if we have just the single final position remaining
3600 * along in a property; if so, combine it with the previous property
3602 if (LAST_INDEX (text, text->point) &&
3603 (MARK_OFFSET (&text->point) == 0) &&
3604 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3606 tmp = MARK_LIST_PTR (&text->point);
3607 prop = MARK_CURRENT_PROPERTY(&text->point);
3609 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3610 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3611 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3613 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3615 if (GTK_WIDGET_REALIZED (text))
3616 unrealize_property (text, prop);
3618 destroy_text_property (prop);
3619 g_list_free_1 (tmp);
3624 init_properties (GtkSText *text)
3626 if (!text->text_properties)
3628 text->text_properties = g_list_alloc();
3629 text->text_properties->next = NULL;
3630 text->text_properties->prev = NULL;
3631 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3632 text->text_properties_end = text->text_properties;
3634 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3636 text->point.index = 0;
3641 /**********************************************************************/
3642 /* Property Movement */
3643 /**********************************************************************/
3646 move_mark_n (GtkSPropertyMark* mark, gint n)
3649 advance_mark_n(mark, n);
3651 decrement_mark_n(mark, -n);
3655 advance_mark (GtkSPropertyMark* mark)
3657 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3661 if (prop->length > mark->offset + 1)
3665 mark->property = MARK_NEXT_LIST_PTR (mark);
3671 advance_mark_n (GtkSPropertyMark* mark, gint n)
3678 i = 0; /* otherwise it migth not be init. */
3679 prop = MARK_CURRENT_PROPERTY(mark);
3681 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3682 /* to make it easier */
3683 n += (mark->offset);
3684 mark->index -= mark->offset;
3686 /* first we take seven-mile-leaps to get to the right text
3688 while ((n-i) > prop->length - 1) {
3690 mark->index += prop->length;
3691 mark->property = MARK_NEXT_LIST_PTR (mark);
3692 prop = MARK_CURRENT_PROPERTY (mark);
3696 /* and then the rest */
3697 mark->index += n - i;
3698 mark->offset += n - i;
3702 decrement_mark (GtkSPropertyMark* mark)
3706 if (mark->offset > 0)
3710 mark->property = MARK_PREV_LIST_PTR (mark);
3711 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3716 decrement_mark_n (GtkSPropertyMark* mark, gint n)
3720 while (mark->offset < n) {
3721 /* jump to end of prev */
3722 n -= mark->offset + 1;
3723 mark->index -= mark->offset + 1;
3724 mark->property = MARK_PREV_LIST_PTR (mark);
3725 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3733 static GtkSPropertyMark
3734 find_mark (GtkSText* text, guint mark_position)
3736 return find_mark_near (text, mark_position, &text->point);
3740 * You can also start from the end, what a drag.
3742 static GtkSPropertyMark
3743 find_mark_near (GtkSText* text, guint mark_position, const GtkSPropertyMark* near)
3748 GtkSPropertyMark mark;
3751 diffa = mark_position + 1;
3753 diffa = mark_position - near->index;
3755 diffb = mark_position;
3767 mark.property = text->text_properties;
3771 move_mark_n (&mark, mark_position - mark.index);
3776 /* This routine must be called with scroll == FALSE, only when
3777 * point is at least partially on screen
3781 find_line_containing_point (GtkSText* text, guint point,
3787 text->current_line = NULL;
3791 /* Scroll backwards until the point is on screen
3793 while (CACHE_DATA(text->line_start_cache).start.index > point)
3794 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3796 /* Now additionally try to make sure that the point is fully on screen
3800 while (text->first_cut_pixels != 0 &&
3801 text->line_start_cache->next &&
3802 CACHE_DATA(text->line_start_cache->next).start.index > point)
3803 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3806 gdk_window_get_size (text->text_area, NULL, &height);
3808 for (cache = text->line_start_cache; cache; cache = cache->next)
3812 if (CACHE_DATA(cache).end.index >= point ||
3813 LAST_INDEX(text, CACHE_DATA(cache).end))
3815 text->current_line = cache; /* LOOK HERE, this proc has an
3816 * important side effect. */
3820 TEXT_SHOW_LINE (text, cache, "cache");
3822 if (cache->next == NULL)
3823 fetch_lines_forward (text, 1);
3827 lph = pixel_height_of (text, cache->next);
3829 /* Scroll the bottom of the line is on screen, or until
3830 * the line is the first onscreen line.
3832 while (cache->next != text->line_start_cache && lph > height)
3834 TEXT_SHOW_LINE (text, cache, "cache");
3835 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3836 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3837 lph = pixel_height_of (text, cache->next);
3842 g_assert_not_reached (); /* Must set text->current_line here */
3846 pixel_height_of (GtkSText* text, GList* cache_line)
3848 gint pixels = - text->first_cut_pixels;
3849 GList *cache = text->line_start_cache;
3852 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3854 if (cache->data == cache_line->data)
3857 cache = cache->next;
3863 /**********************************************************************/
3864 /* Search and Placement */
3865 /**********************************************************************/
3868 find_char_width (GtkSText* text, const GtkSPropertyMark *mark, const TabStopMark *tab_mark)
3871 gint16* char_widths;
3873 if (LAST_INDEX (text, *mark))
3876 ch = GTK_STEXT_INDEX (text, mark->index);
3877 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3881 return tab_mark->to_next_tab * char_widths[' '];
3883 else if (!text->use_wchar)
3885 return char_widths[ch];
3889 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3894 advance_tab_mark (GtkSText* text, TabStopMark* tab_mark, GdkWChar ch)
3896 if (tab_mark->to_next_tab == 1 || ch == '\t')
3898 if (tab_mark->tab_stops->next)
3900 tab_mark->tab_stops = tab_mark->tab_stops->next;
3901 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3905 tab_mark->to_next_tab = text->default_tab_width;
3910 tab_mark->to_next_tab -= 1;
3915 advance_tab_mark_n (GtkSText* text, TabStopMark* tab_mark, gint n)
3919 advance_tab_mark (text, tab_mark, 0);
3923 find_cursor_at_line (GtkSText* text, const LineParams* start_line, gint pixel_height)
3926 GtkEditable *editable = (GtkEditable *)text;
3928 GtkSPropertyMark mark = start_line->start;
3929 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3930 gint pixel_width = LINE_START_PIXEL (*start_line);
3932 while (mark.index < text->cursor_mark.index)
3934 pixel_width += find_char_width (text, &mark, &tab_mark);
3936 advance_tab_mark (text, &tab_mark, GTK_STEXT_INDEX(text, mark.index));
3937 advance_mark (&mark);
3940 text->cursor_pos_x = pixel_width;
3941 text->cursor_pos_y = pixel_height;
3942 text->cursor_char_offset = start_line->font_descent;
3943 text->cursor_mark = mark;
3945 ch = LAST_INDEX (text, mark) ?
3946 LINE_DELIM : GTK_STEXT_INDEX (text, mark.index);
3948 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3949 text->cursor_char = 0;
3951 text->cursor_char = ch;
3953 #ifdef USE_GTKGDK_XIM
3954 if (GTK_WIDGET_HAS_FOCUS(text) && gdk_im_ready() && editable->ic &&
3955 (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
3957 GdkICAttributesType mask = GDK_IC_SPOT_LOCATION |
3958 GDK_IC_PREEDIT_FOREGROUND |
3959 GDK_IC_PREEDIT_BACKGROUND;
3961 editable->ic_attr->spot_location.x = text->cursor_pos_x;
3962 editable->ic_attr->spot_location.y
3963 = text->cursor_pos_y - text->cursor_char_offset;
3964 editable->ic_attr->preedit_foreground = *MARK_CURRENT_FORE (text, &mark);
3965 editable->ic_attr->preedit_background = *MARK_CURRENT_BACK (text, &mark);
3967 if (MARK_CURRENT_FONT (text, &mark)->type == GDK_FONT_FONTSET)
3969 mask |= GDK_IC_PREEDIT_FONTSET;
3970 editable->ic_attr->preedit_fontset = MARK_CURRENT_FONT (text, &mark);
3973 gdk_ic_set_attr (editable->ic, editable->ic_attr, mask);
3979 find_cursor (GtkSText* text, gboolean scroll)
3981 if (GTK_WIDGET_REALIZED (text))
3983 find_line_containing_point (text, text->cursor_mark.index, scroll);
3985 if (text->current_line)
3986 find_cursor_at_line (text,
3987 &CACHE_DATA(text->current_line),
3988 pixel_height_of(text, text->current_line));
3991 GTK_EDITABLE (text)->current_pos = text->cursor_mark.index;
3995 find_mouse_cursor_at_line (GtkSText *text, const LineParams* lp,
3996 guint line_pixel_height,
3999 GtkSPropertyMark mark = lp->start;
4000 TabStopMark tab_mark = lp->tab_cont.tab_start;
4002 gint char_width = find_char_width(text, &mark, &tab_mark);
4003 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
4005 text->cursor_pos_y = line_pixel_height;
4009 GdkWChar ch = LAST_INDEX (text, mark) ?
4010 LINE_DELIM : GTK_STEXT_INDEX (text, mark.index);
4012 if (button_x < pixel_width || mark.index == lp->end.index)
4014 text->cursor_pos_x = pixel_width - (char_width+1)/2;
4015 text->cursor_mark = mark;
4016 text->cursor_char_offset = lp->font_descent;
4018 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
4019 text->cursor_char = 0;
4021 text->cursor_char = ch;
4026 advance_tab_mark (text, &tab_mark, ch);
4027 advance_mark (&mark);
4029 pixel_width += char_width/2;
4031 char_width = find_char_width (text, &mark, &tab_mark);
4033 pixel_width += (char_width+1)/2;
4038 find_mouse_cursor (GtkSText* text, gint x, gint y)
4041 GList* cache = text->line_start_cache;
4045 pixel_height = - text->first_cut_pixels;
4047 for (; cache; cache = cache->next)
4049 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
4051 if (y < pixel_height || !cache->next)
4053 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
4055 find_cursor (text, FALSE);
4062 /**********************************************************************/
4064 /**********************************************************************/
4067 free_cache (GtkSText* text)
4069 GList* cache = text->line_start_cache;
4074 cache = cache->prev;
4076 text->line_start_cache = cache;
4079 for (; cache; cache = cache->next)
4080 g_mem_chunk_free (params_mem_chunk, cache->data);
4082 g_list_free (text->line_start_cache);
4084 text->line_start_cache = NULL;
4088 remove_cache_line (GtkSText* text, GList* member)
4095 if (member == text->line_start_cache)
4096 text->line_start_cache = text->line_start_cache->next;
4099 member->prev->next = member->next;
4102 member->next->prev = member->prev;
4104 list = member->next;
4106 g_mem_chunk_free (params_mem_chunk, member->data);
4107 g_list_free_1 (member);
4112 /**********************************************************************/
4114 /**********************************************************************/
4117 move_cursor_buffer_ver (GtkSText *text, int dir)
4119 undraw_cursor (text, FALSE);
4123 scroll_int (text, text->vadj->upper);
4124 text->cursor_mark = find_this_line_start_mark (text,
4126 &text->cursor_mark);
4130 scroll_int (text, - text->vadj->value);
4131 text->cursor_mark = find_this_line_start_mark (text,
4133 &text->cursor_mark);
4136 find_cursor (text, TRUE);
4137 draw_cursor (text, FALSE);
4141 move_cursor_page_ver (GtkSText *text, int dir)
4143 scroll_int (text, dir * text->vadj->page_increment);
4147 move_cursor_ver (GtkSText *text, int count)
4150 GtkSPropertyMark mark;
4153 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
4154 offset = text->cursor_mark.index - mark.index;
4156 if (offset > text->cursor_virtual_x)
4157 text->cursor_virtual_x = offset;
4161 if (mark.index == 0)
4164 decrement_mark (&mark);
4165 mark = find_this_line_start_mark (text, mark.index, &mark);
4169 mark = text->cursor_mark;
4171 while (!LAST_INDEX(text, mark) && GTK_STEXT_INDEX(text, mark.index) != LINE_DELIM)
4172 advance_mark (&mark);
4174 if (LAST_INDEX(text, mark))
4177 advance_mark (&mark);
4180 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
4181 if (LAST_INDEX(text, mark) ||
4182 GTK_STEXT_INDEX(text, mark.index) == LINE_DELIM)
4185 undraw_cursor (text, FALSE);
4187 text->cursor_mark = mark;
4189 find_cursor (text, TRUE);
4191 draw_cursor (text, FALSE);
4195 move_cursor_hor (GtkSText *text, int count)
4197 /* count should be +-1. */
4198 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
4199 (count < 0 && text->cursor_mark.index < (- count)) ||
4203 text->cursor_virtual_x = 0;
4205 undraw_cursor (text, FALSE);
4207 move_mark_n (&text->cursor_mark, count);
4209 find_cursor (text, TRUE);
4211 draw_cursor (text, FALSE);
4214 /* SYLPHEED - the default cursor movement of GtkText is aeons ahead of the
4215 * current technology level of the current generation of programmers and
4216 * end users. so sylpheed has to take a more mundane approach to editing.
4219 * move_cursor_to_display_row_end() (end of display line)
4220 * move_cursor_to_display_row_home() (home of display line)
4221 * move_cursor_to_display_row_up() (line up)
4222 * move_cursor_to_display_row_down() (line down)
4226 * SYLPHEED_TODO: for some reason the line fetcher also returns markers
4227 * of just one character! should investigate this... -- alfons
4230 static void move_cursor_to_display_row_end(GtkSText *text)
4232 LineParams lp = CACHE_DATA(text->current_line);
4233 int to_move = (lp.end.index - text->cursor_mark.index);
4235 /* advance this much */
4237 move_cursor_hor(text, to_move);
4241 static void move_cursor_to_display_row_start(GtkSText *text)
4243 LineParams lp = CACHE_DATA(text->current_line);
4244 int to_move = (text->cursor_mark.index - lp.start.index);
4246 move_cursor_hor(text, -to_move);
4251 static gboolean range_intersect(guint x1, guint x2, guint y1, guint y2)
4254 if (x1 > x2) { tmp = x1; x1 = x2; x2 = tmp; }
4255 if (y1 > y2) { tmp = y1; y1 = y2; y1 = tmp; }
4256 if (y1 < x1) { tmp = x1; x1 = y1; y1 = tmp; tmp = x2; x2 = y2; y2 = tmp; }
4266 static gint back_display_row_fetcher(GtkSText *text,
4270 if (range_intersect(data->start, data->end, params->start.index, params->end.index)) {
4271 XDEBUG( ("%s(%d) - FOUND search (%d, %d), current (%d, %d)", __FILE__, __LINE__,
4272 data->start, data->end,
4273 params->start.index, params->end.index) );
4278 XDEBUG( ("%s(%d) - NEXT search (%d, %d), current (%d, %d)", __FILE__, __LINE__,
4279 data->start, data->end,
4280 params->start.index, params->end.index) );
4286 static void move_cursor_to_display_row_up(GtkSText *text)
4292 GtkSPropertyMark mark;
4294 mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
4297 if (mark.index == 0) {
4298 XDEBUG ( ("%s(%d) top of buffer", __FILE__, __LINE__) );
4301 /* we need previous DISPLAY row not the previous BUFFER line, so we go to start
4302 * of paragraph, and iterate over the lines until we have a LineParams that matches
4304 lp = CACHE_DATA(text->current_line);
4305 col = (text->cursor_mark.index - lp.start.index);
4306 data.start = lp.start.index;
4307 data.end = lp.end.index;
4309 /* get the previous line */
4310 if (mark.index != 0) {
4311 decrement_mark(&mark);
4312 if (mark.index != 0) {
4313 GtkSPropertyMark smark = mark;
4314 XDEBUG( ("%s(%d) finding line start mark", __FILE__, __LINE__) );
4315 mark = find_this_line_start_mark(text, smark.index -1, &smark);
4319 /* let's get the previous display line */
4320 XDEBUG( ("%s(%d) iterating to get display lines", __FILE__, __LINE__) );
4321 line_params_iterate(text, &mark, NULL, FALSE, &data,
4322 (LineIteratorFunction)back_display_row_fetcher);
4323 XDEBUG( ("%s(%d) done iterating. found = %d", __FILE__, __LINE__, data.found) );
4326 if (col < text->persist_column) col = text->persist_column;
4327 else text->persist_column = col;
4328 new_index = data.lp.start.index + col;
4329 XDEBUG( ("%s(%d) - new index = %d", __FILE__, __LINE__, new_index) );
4330 if (new_index > data.lp.end.index) {
4331 new_index = data.lp.end.index;
4333 /* and move the cursor */
4334 XDEBUG( ("%s(%d) - setting index", __FILE__, __LINE__) );
4335 gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);
4346 #if defined(AHX_DEBUG)
4347 static void print_line(GtkSText *text, guint start, guint end)
4349 gchar *buf = alloca(2048), *walk = buf;
4351 memset(buf, 0, 2048);
4352 for (; start <= end; start++) {
4353 *walk++ = GTK_STEXT_INDEX(text, start);
4355 XDEBUG( ("%s", buf) );
4359 static gint forward_display_row_fetcher(GtkSText *text,
4364 XDEBUG( ("%s(%d) - FW RETURNS. search (%d, %d), current (%d, %d)",
4365 __FILE__, __LINE__, data->start, data->end, lp->start.index, lp->end.index) );
4367 data->completed = TRUE;
4368 print_line(text, lp->start.index, lp->end.index);
4371 else if (range_intersect(data->start, data->end, lp->start.index, lp->end.index)) {
4372 XDEBUG( ("%s(%d) - FW FOUND IT. search (%d, %d), current (%d, %d)",
4373 __FILE__, __LINE__, data->start, data->end, lp->start.index, lp->end.index) );
4382 static void move_cursor_to_display_row_down (GtkSText *text)
4387 GtkSPropertyMark mark;
4388 fdrf data = { FALSE, FALSE };
4390 mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
4391 lp = CACHE_DATA(text->current_line);
4392 col = (text->cursor_mark.index - lp.start.index);
4394 data.start = lp.start.index;
4395 data.end = lp.end.index;
4397 /* find the next DISPLAY line */
4398 XDEBUG( ("%s(%d) - FW iterating", __FILE__, __LINE__) ) ;
4399 line_params_iterate(text, &mark, NULL, FALSE, &data,
4400 (LineIteratorFunction)forward_display_row_fetcher);
4401 XDEBUG( ("%s(%d) - FW done iterating", __FILE__, __LINE__) );
4403 if (data.completed) {
4404 if (col < text->persist_column) col = text->persist_column;
4405 else text->persist_column = col;
4406 new_index = data.lp.start.index + col;
4407 if (new_index > data.lp.end.index) {
4408 new_index = data.lp.end.index;
4410 /* and move the cursor */
4411 XDEBUG( ("%s(%d) - FW set pos %d", __FILE__, __LINE__, new_index) );
4412 gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);
4416 static void reset_persist_col_pos(GtkSText *text)
4418 text->persist_column = 0;
4422 gtk_stext_move_cursor (GtkEditable *editable,
4429 move_cursor_hor (GTK_STEXT (editable), 1);
4434 move_cursor_hor (GTK_STEXT (editable), -1);
4440 move_cursor_ver (GTK_STEXT (editable), 1);
4445 move_cursor_ver (GTK_STEXT (editable), -1);
4450 gtk_stext_move_forward_character (GtkSText *text)
4452 move_cursor_hor (text, 1);
4456 gtk_stext_move_backward_character (GtkSText *text)
4458 move_cursor_hor (text, -1);
4462 gtk_stext_move_next_line (GtkSText *text)
4464 move_cursor_ver (text, 1);
4468 gtk_stext_move_previous_line (GtkSText *text)
4470 move_cursor_ver (text, -1);
4474 gtk_stext_move_word (GtkEditable *editable,
4480 gtk_stext_move_forward_word (GTK_STEXT (editable));
4485 gtk_stext_move_backward_word (GTK_STEXT (editable));
4490 gtk_stext_move_forward_word (GtkSText *text)
4492 text->cursor_virtual_x = 0;
4494 undraw_cursor (text, FALSE);
4496 if (text->use_wchar)
4498 while (!LAST_INDEX (text, text->cursor_mark) &&
4499 !gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4500 advance_mark (&text->cursor_mark);
4502 while (!LAST_INDEX (text, text->cursor_mark) &&
4503 gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4504 advance_mark (&text->cursor_mark);
4508 while (!LAST_INDEX (text, text->cursor_mark) &&
4509 !isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4510 advance_mark (&text->cursor_mark);
4512 while (!LAST_INDEX (text, text->cursor_mark) &&
4513 isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4514 advance_mark (&text->cursor_mark);
4517 find_cursor (text, TRUE);
4518 draw_cursor (text, FALSE);
4522 gtk_stext_move_backward_word (GtkSText *text)
4524 text->cursor_virtual_x = 0;
4526 undraw_cursor (text, FALSE);
4528 if (text->use_wchar)
4530 while ((text->cursor_mark.index > 0) &&
4531 !gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4532 decrement_mark (&text->cursor_mark);
4534 while ((text->cursor_mark.index > 0) &&
4535 gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4536 decrement_mark (&text->cursor_mark);
4540 while ((text->cursor_mark.index > 0) &&
4541 !isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4542 decrement_mark (&text->cursor_mark);
4544 while ((text->cursor_mark.index > 0) &&
4545 isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4546 decrement_mark (&text->cursor_mark);
4549 find_cursor (text, TRUE);
4550 draw_cursor (text, FALSE);
4554 gtk_stext_move_page (GtkEditable *editable,
4559 scroll_int (GTK_STEXT (editable),
4560 y * GTK_STEXT(editable)->vadj->page_increment);
4564 gtk_stext_move_to_row (GtkEditable *editable,
4570 gtk_stext_move_to_column (GtkEditable *editable,
4575 text = GTK_STEXT (editable);
4577 text->cursor_virtual_x = 0; /* FIXME */
4579 undraw_cursor (text, FALSE);
4581 /* Move to the beginning of the line */
4582 while ((text->cursor_mark.index > 0) &&
4583 (GTK_STEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4584 decrement_mark (&text->cursor_mark);
4586 while (!LAST_INDEX (text, text->cursor_mark) &&
4587 (GTK_STEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4591 else if (column == 0)
4594 advance_mark (&text->cursor_mark);
4597 find_cursor (text, TRUE);
4598 draw_cursor (text, FALSE);
4602 gtk_stext_move_beginning_of_line (GtkSText *text)
4604 gtk_stext_move_to_column (GTK_EDITABLE (text), 0);
4609 gtk_stext_move_end_of_line (GtkSText *text)
4611 gtk_stext_move_to_column (GTK_EDITABLE (text), -1);
4615 gtk_stext_kill_char (GtkEditable *editable,
4620 text = GTK_STEXT (editable);
4622 if (editable->selection_start_pos != editable->selection_end_pos)
4623 gtk_editable_delete_selection (editable);
4628 if (text->point.index + 1 <= TEXT_LENGTH (text))
4629 gtk_editable_delete_text (editable, text->point.index, text->point.index + 1);
4633 if (text->point.index > 0)
4634 gtk_editable_delete_text (editable, text->point.index - 1, text->point.index);
4640 gtk_stext_delete_forward_character (GtkSText *text)
4642 gtk_stext_kill_char (GTK_EDITABLE (text), 1);
4646 gtk_stext_delete_backward_character (GtkSText *text)
4648 gtk_stext_kill_char (GTK_EDITABLE (text), -1);
4652 gtk_stext_kill_word (GtkEditable *editable,
4655 if (editable->selection_start_pos != editable->selection_end_pos) {
4656 gtk_editable_delete_selection (editable);
4660 gint old_pos = editable->current_pos;
4663 gtk_stext_move_word (editable, 1);
4664 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
4668 gtk_stext_move_word (editable, -1);
4669 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
4675 gtk_stext_delete_forward_word (GtkSText *text)
4677 gtk_stext_kill_word (GTK_EDITABLE (text), 1);
4681 gtk_stext_delete_backward_word (GtkSText *text)
4683 gtk_stext_kill_word (GTK_EDITABLE (text), -1);
4687 gtk_stext_kill_line (GtkEditable *editable,
4690 gint old_pos = editable->current_pos;
4693 gtk_stext_move_to_column (editable, -1);
4694 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
4698 gtk_stext_move_to_column (editable, 0);
4699 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
4704 gtk_stext_delete_line (GtkSText *text)
4706 gtk_stext_move_to_column (GTK_EDITABLE (text), 0);
4707 gtk_stext_kill_line (GTK_EDITABLE (text), 1);
4711 gtk_stext_delete_to_line_end (GtkSText *text)
4713 gtk_stext_kill_line (GTK_EDITABLE (text), 1);
4717 gtk_stext_select_word (GtkSText *text, guint32 time)
4722 GtkEditable *editable;
4723 editable = GTK_EDITABLE (text);
4725 gtk_stext_move_backward_word (text);
4726 start_pos = text->cursor_mark.index;
4728 gtk_stext_move_forward_word (text);
4729 end_pos = text->cursor_mark.index;
4731 editable->has_selection = TRUE;
4732 gtk_stext_set_selection (editable, start_pos, end_pos);
4733 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
4737 gtk_stext_select_line (GtkSText *text, guint32 time)
4742 GtkEditable *editable;
4743 editable = GTK_EDITABLE (text);
4745 gtk_stext_move_beginning_of_line (text);
4746 start_pos = text->cursor_mark.index;
4748 gtk_stext_move_end_of_line (text);
4749 gtk_stext_move_forward_character (text);
4750 end_pos = text->cursor_mark.index;
4752 editable->has_selection = TRUE;
4753 gtk_stext_set_selection (editable, start_pos, end_pos);
4754 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
4757 /**********************************************************************/
4759 /**********************************************************************/
4762 adjust_adj (GtkSText* text, GtkAdjustment* adj)
4766 gdk_window_get_size (text->text_area, NULL, &height);
4768 adj->step_increment = MIN (adj->upper, (float) SCROLL_PIXELS);
4769 adj->page_increment = MIN (adj->upper, height - (float) KEY_SCROLL_PIXELS);
4770 adj->page_size = MIN (adj->upper, height);
4771 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4772 adj->value = MAX (adj->value, 0.0);
4774 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4778 set_vertical_scroll_iterator (GtkSText* text, LineParams* lp, void* data)
4780 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4782 if ((text->first_line_start_index >= lp->start.index) &&
4783 (text->first_line_start_index <= lp->end.index))
4785 svdata->mark = lp->start;
4787 if (text->first_line_start_index == lp->start.index)
4789 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4793 text->first_onscreen_ver_pixel = svdata->pixel_height;
4794 text->first_cut_pixels = 0;
4797 text->vadj->value = (float) text->first_onscreen_ver_pixel;
4800 svdata->pixel_height += LINE_HEIGHT (*lp);
4806 set_vertical_scroll_find_iterator (GtkSText* text, LineParams* lp, void* data)
4808 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4811 if (svdata->pixel_height <= (gint) text->vadj->value &&
4812 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4814 svdata->mark = lp->start;
4816 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4817 text->first_onscreen_ver_pixel = svdata->pixel_height;
4818 text->first_line_start_index = lp->start.index;
4824 svdata->pixel_height += LINE_HEIGHT (*lp);
4832 static GtkSPropertyMark
4833 set_vertical_scroll (GtkSText* text)
4835 GtkSPropertyMark mark = find_mark (text, 0);
4836 SetVerticalScrollData data;
4840 data.pixel_height = 0;
4841 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4843 text->vadj->upper = (float) data.pixel_height;
4844 orig_value = (gint) text->vadj->value;
4846 gdk_window_get_size (text->text_area, NULL, &height);
4848 text->vadj->step_increment = MIN (text->vadj->upper, (float) SCROLL_PIXELS);
4849 text->vadj->page_increment = MIN (text->vadj->upper, height - (float) KEY_SCROLL_PIXELS);
4850 text->vadj->page_size = MIN (text->vadj->upper, height);
4851 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4852 text->vadj->value = MAX (text->vadj->value, 0.0);
4854 text->last_ver_value = (gint)text->vadj->value;
4856 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4858 if (text->vadj->value != orig_value)
4860 /* We got clipped, and don't really know which line to put first. */
4861 data.pixel_height = 0;
4862 data.last_didnt_wrap = TRUE;
4864 line_params_iterate (text, &mark, NULL,
4866 set_vertical_scroll_find_iterator);
4873 scroll_int (GtkSText* text, gint diff)
4877 text->vadj->value += diff;
4879 upper = text->vadj->upper - text->vadj->page_size;
4880 text->vadj->value = MIN (text->vadj->value, upper);
4881 text->vadj->value = MAX (text->vadj->value, 0.0);
4883 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4887 process_exposes (GtkSText *text)
4891 /* Make sure graphics expose events are processed before scrolling
4894 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4896 gtk_widget_event (GTK_WIDGET (text), event);
4897 if (event->expose.count == 0)
4899 gdk_event_free (event);
4902 gdk_event_free (event);
4906 static gint last_visible_line_height (GtkSText* text)
4908 GList *cache = text->line_start_cache;
4911 gdk_window_get_size (text->text_area, NULL, &height);
4913 for (; cache->next; cache = cache->next)
4914 if (pixel_height_of(text, cache->next) > height)
4918 return pixel_height_of(text, cache) - 1;
4923 static gint first_visible_line_height (GtkSText* text)
4925 if (text->first_cut_pixels)
4926 return pixel_height_of(text, text->line_start_cache) + 1;
4932 scroll_down (GtkSText* text, gint diff0)
4938 text->first_onscreen_ver_pixel += diff0;
4942 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4944 text->first_cut_pixels += 1;
4948 text->first_cut_pixels = 0;
4950 text->line_start_cache = text->line_start_cache->next;
4951 g_assert (text->line_start_cache);
4953 text->first_line_start_index =
4954 CACHE_DATA(text->line_start_cache).start.index;
4956 if (!text->line_start_cache->next)
4957 fetch_lines_forward (text, 1);
4963 gdk_window_get_size (text->text_area, &width, &height);
4964 if (height > real_diff)
4965 gdk_draw_pixmap (text->text_area,
4973 height - real_diff);
4976 rect.y = MAX (0, height - real_diff);
4978 rect.height = MIN (height, real_diff);
4980 expose_text (text, &rect, FALSE);
4981 gtk_stext_draw_focus ( (GtkWidget *) text);
4983 if (text->current_line)
4987 text->cursor_pos_y -= real_diff;
4988 cursor_min = drawn_cursor_min(text);
4991 find_mouse_cursor (text, text->cursor_pos_x,
4992 first_visible_line_height (text));
4995 if (height > real_diff)
4996 process_exposes (text);
5000 scroll_up (GtkSText* text, gint diff0)
5006 text->first_onscreen_ver_pixel += diff0;
5010 g_assert (text->line_start_cache);
5012 if (text->first_cut_pixels > 0)
5014 text->first_cut_pixels -= 1;
5018 if (!text->line_start_cache->prev)
5019 fetch_lines_backward (text);
5021 text->line_start_cache = text->line_start_cache->prev;
5023 text->first_line_start_index =
5024 CACHE_DATA(text->line_start_cache).start.index;
5026 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
5032 gdk_window_get_size (text->text_area, &width, &height);
5033 if (height > real_diff)
5034 gdk_draw_pixmap (text->text_area,
5042 height - real_diff);
5047 rect.height = MIN (height, real_diff);
5049 expose_text (text, &rect, FALSE);
5050 gtk_stext_draw_focus ( (GtkWidget *) text);
5052 if (text->current_line)
5057 text->cursor_pos_y += real_diff;
5058 cursor_max = drawn_cursor_max(text);
5059 gdk_window_get_size (text->text_area, NULL, &height);
5061 if (cursor_max >= height)
5062 find_mouse_cursor (text, text->cursor_pos_x,
5063 last_visible_line_height (text));
5066 if (height > real_diff)
5067 process_exposes (text);
5070 /**********************************************************************/
5072 /**********************************************************************/
5074 /* Assumes mark starts a line. Calculates the height, width, and
5075 * displayable character count of a single DISPLAYABLE line. That
5076 * means that in line-wrap mode, this does may not compute the
5077 * properties of an entire line. */
5079 find_line_params (GtkSText* text,
5080 const GtkSPropertyMark* mark,
5081 const PrevTabCont *tab_cont,
5082 PrevTabCont *next_cont)
5085 TabStopMark tab_mark = tab_cont->tab_start;
5086 guint max_display_pixels;
5091 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
5093 if (text->wrap_rmargin) {
5094 /* SYLPHEED - since sylpheed is a mail program, assume we work with
5095 * fixed fonts. only assume we're using one font! */
5096 font = MARK_CURRENT_FONT(text, mark);
5098 /* SYLPHEED - ok for multi byte charsets to check for ASCII char?
5100 if (text->use_wchar)
5101 ch_width = gdk_char_width_wc(font, 'W');
5103 ch_width = gdk_char_width(font, 'W');
5105 /* SYLPHEED - max_display_chars has the rmargin in pixels
5107 max_display_pixels = text->wrap_rmargin * ch_width;
5110 /* SYLPHEED - we don't draw ugly word wrapping thing
5111 * if our wrap margin is set */
5112 if (!text->wrap_rmargin &&
5113 ((GTK_EDITABLE (text)->editable || !text->word_wrap)))
5114 max_display_pixels -= LINE_WRAP_ROOM;
5117 lp.tab_cont = *tab_cont;
5120 lp.pixel_width = tab_cont->pixel_offset;
5121 lp.displayable_chars = 0;
5123 lp.font_descent = 0;
5125 init_tab_cont (text, next_cont);
5127 while (!LAST_INDEX(text, lp.end))
5129 g_assert (lp.end.property);
5131 ch = GTK_STEXT_INDEX (text, lp.end.index);
5132 font = MARK_CURRENT_FONT (text, &lp.end);
5134 if (ch == LINE_DELIM)
5136 /* Newline doesn't count in computation of line height, even
5137 * if its in a bigger font than the rest of the line. Unless,
5138 * of course, there are no other characters. */
5140 if (!lp.font_ascent && !lp.font_descent)
5142 lp.font_ascent = font->ascent;
5143 lp.font_descent = font->descent;
5146 lp.tab_cont_next = *next_cont;
5151 ch_width = find_char_width (text, &lp.end, &tab_mark);
5153 if ((ch_width + lp.pixel_width > max_display_pixels) &&
5154 (lp.end.index > lp.start.index))
5158 if (text->line_wrap)
5160 next_cont->tab_start = tab_mark;
5161 next_cont->pixel_offset = 0;
5165 /* Here's the tough case, a tab is wrapping. */
5166 gint pixels_avail = max_display_pixels - lp.pixel_width;
5167 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
5168 gint spaces_avail = pixels_avail / space_width;
5170 if (spaces_avail == 0)
5172 decrement_mark (&lp.end);
5176 advance_tab_mark (text, &next_cont->tab_start, '\t');
5177 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
5179 lp.displayable_chars += 1;
5184 if (text->word_wrap)
5186 GtkSPropertyMark saved_mark = lp.end;
5187 guint saved_characters = lp.displayable_chars;
5189 lp.displayable_chars += 1;
5191 if (text->use_wchar)
5193 while (!gdk_iswspace (GTK_STEXT_INDEX (text, lp.end.index)) &&
5194 (lp.end.index > lp.start.index))
5196 decrement_mark (&lp.end);
5197 lp.displayable_chars -= 1;
5202 while (!isspace(GTK_STEXT_INDEX (text, lp.end.index)) &&
5203 (lp.end.index > lp.start.index))
5205 decrement_mark (&lp.end);
5206 lp.displayable_chars -= 1;
5210 /* If whole line is one word, revert to char wrapping */
5211 if (lp.end.index == lp.start.index)
5213 lp.end = saved_mark;
5214 lp.displayable_chars = saved_characters;
5215 decrement_mark (&lp.end);
5220 /* Don't include this character, it will wrap. */
5221 decrement_mark (&lp.end);
5225 lp.tab_cont_next = *next_cont;
5232 lp.displayable_chars += 1;
5235 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
5236 lp.font_descent = MAX (font->descent, lp.font_descent);
5237 lp.pixel_width += ch_width;
5239 advance_mark(&lp.end);
5240 advance_tab_mark (text, &tab_mark, ch);
5243 if (LAST_INDEX(text, lp.start))
5245 /* Special case, empty last line. */
5246 font = MARK_CURRENT_FONT (text, &lp.end);
5248 lp.font_ascent = font->ascent;
5249 lp.font_descent = font->descent;
5252 lp.tab_cont_next = *next_cont;
5258 expand_scratch_buffer (GtkSText* text, guint len)
5260 if (len >= text->scratch_buffer_len)
5264 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
5266 if (text->use_wchar)
5268 if (!text->scratch_buffer.wc)
5269 text->scratch_buffer.wc = g_new (GdkWChar, i);
5271 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
5272 i * sizeof (GdkWChar));
5276 if (!text->scratch_buffer.ch)
5277 text->scratch_buffer.ch = g_new (guchar, i);
5279 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
5282 text->scratch_buffer_len = i;
5286 /* Side effect: modifies text->gc
5290 draw_bg_rect (GtkSText* text, GtkSPropertyMark *mark,
5291 gint x, gint y, gint width, gint height,
5292 gboolean already_cleared)
5294 GtkEditable *editable = GTK_EDITABLE(text);
5296 if ((mark->index >= MIN(editable->selection_start_pos, editable->selection_end_pos) &&
5297 mark->index < MAX(editable->selection_start_pos, editable->selection_end_pos)))
5299 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
5300 editable->has_selection ?
5301 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
5303 NULL, GTK_WIDGET(text), "text",
5304 x, y, width, height);
5306 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
5307 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
5309 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
5311 gdk_draw_rectangle (text->text_area,
5313 TRUE, x, y, width, height);
5315 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
5322 rect.height = height;
5324 clear_area (text, &rect);
5326 else if (!already_cleared)
5327 gdk_window_clear_area (text->text_area, x, y, width, height);
5331 draw_line (GtkSText* text,
5332 gint pixel_start_height,
5335 GdkGCValues gc_values;
5338 guint running_offset = lp->tab_cont.pixel_offset;
5339 union { GdkWChar *wc; guchar *ch; } buffer;
5342 GtkEditable *editable = GTK_EDITABLE(text);
5344 guint selection_start_pos = MIN (editable->selection_start_pos, editable->selection_end_pos);
5345 guint selection_end_pos = MAX (editable->selection_start_pos, editable->selection_end_pos);
5347 GtkSPropertyMark mark = lp->start;
5348 TabStopMark tab_mark = lp->tab_cont.tab_start;
5349 gint pixel_height = pixel_start_height + lp->font_ascent;
5350 guint chars = lp->displayable_chars;
5352 /* First provide a contiguous segment of memory. This makes reading
5353 * the code below *much* easier, and only incurs the cost of copying
5354 * when the line being displayed spans the gap. */
5355 if (mark.index <= text->gap_position &&
5356 mark.index + chars > text->gap_position)
5358 expand_scratch_buffer (text, chars);
5360 if (text->use_wchar)
5362 for (i = 0; i < chars; i += 1)
5363 text->scratch_buffer.wc[i] = GTK_STEXT_INDEX(text, mark.index + i);
5364 buffer.wc = text->scratch_buffer.wc;
5368 for (i = 0; i < chars; i += 1)
5369 text->scratch_buffer.ch[i] = GTK_STEXT_INDEX(text, mark.index + i);
5370 buffer.ch = text->scratch_buffer.ch;
5375 if (text->use_wchar)
5377 if (mark.index >= text->gap_position)
5378 buffer.wc = text->text.wc + mark.index + text->gap_size;
5380 buffer.wc = text->text.wc + mark.index;
5384 if (mark.index >= text->gap_position)
5385 buffer.ch = text->text.ch + mark.index + text->gap_size;
5387 buffer.ch = text->text.ch + mark.index;
5392 if (running_offset > 0)
5394 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
5395 LINE_HEIGHT (*lp), TRUE);
5401 if ((text->use_wchar && buffer.wc[0] != '\t') ||
5402 (!text->use_wchar && buffer.ch[0] != '\t'))
5404 union { GdkWChar *wc; guchar *ch; } next_tab;
5409 if (text->use_wchar)
5410 for (i=0; i<chars; i++)
5412 if (buffer.wc[i] == '\t')
5414 next_tab.wc = buffer.wc + i;
5419 next_tab.ch = memchr (buffer.ch, '\t', chars);
5421 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
5423 if (text->use_wchar)
5426 len = MIN (len, next_tab.wc - buffer.wc);
5431 len = MIN (len, next_tab.ch - buffer.ch);
5434 if (mark.index < selection_start_pos)
5435 len = MIN (len, selection_start_pos - mark.index);
5436 else if (mark.index < selection_end_pos)
5437 len = MIN (len, selection_end_pos - mark.index);
5439 font = MARK_CURRENT_FONT (text, &mark);
5440 if (font->type == GDK_FONT_FONT)
5442 gdk_gc_set_font (text->gc, font);
5443 gdk_gc_get_values (text->gc, &gc_values);
5444 if (text->use_wchar)
5445 pixel_width = gdk_text_width_wc (gc_values.font,
5448 pixel_width = gdk_text_width (gc_values.font,
5453 if (text->use_wchar)
5454 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
5456 pixel_width = gdk_text_width (font, buffer.ch, len);
5459 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
5460 pixel_width, LINE_HEIGHT (*lp), TRUE);
5462 if ((mark.index >= selection_start_pos) &&
5463 (mark.index < selection_end_pos))
5465 if (editable->has_selection)
5466 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_SELECTED];
5468 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_ACTIVE];
5472 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
5476 if (text->use_wchar)
5477 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
5484 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
5491 running_offset += pixel_width;
5493 advance_tab_mark_n (text, &tab_mark, len);
5497 gint pixels_remaining;
5503 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
5504 if (GTK_EDITABLE (text)->editable || !text->word_wrap)
5505 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
5507 pixels_remaining -= running_offset;
5509 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
5511 spaces_avail = pixels_remaining / space_width;
5512 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
5514 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
5515 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
5517 running_offset += tab_mark.to_next_tab *
5518 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
5520 advance_tab_mark (text, &tab_mark, '\t');
5523 advance_mark_n (&mark, len);
5524 if (text->use_wchar)
5533 draw_line_wrap (GtkSText* text, guint height /* baseline height */)
5540 if (!GTK_EDITABLE (text)->editable && text->word_wrap)
5543 /* SYLPHEED - don't draw ugly word wrapping thing if
5544 * our wrap margin is set */
5545 if (text->wrap_rmargin > 0) {
5549 if (text->line_wrap)
5551 bitmap = text->line_wrap_bitmap;
5552 bitmap_width = line_wrap_width;
5553 bitmap_height = line_wrap_height;
5557 bitmap = text->line_arrow_bitmap;
5558 bitmap_width = line_arrow_width;
5559 bitmap_height = line_arrow_height;
5562 gdk_window_get_size (text->text_area, &width, NULL);
5563 width -= LINE_WRAP_ROOM;
5565 gdk_gc_set_stipple (text->gc,
5568 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
5570 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5572 gdk_gc_set_ts_origin (text->gc,
5574 height - bitmap_height - 1);
5576 gdk_draw_rectangle (text->text_area,
5580 height - bitmap_height - 1 /* one pixel above the baseline. */,
5584 gdk_gc_set_ts_origin (text->gc, 0, 0);
5586 gdk_gc_set_fill (text->gc, GDK_SOLID);
5590 undraw_cursor (GtkSText* text, gint absolute)
5592 GtkEditable *editable = (GtkEditable *)text;
5594 TDEBUG (("in undraw_cursor\n"));
5597 text->cursor_drawn_level = 0;
5599 if ((text->cursor_drawn_level ++ == 0) &&
5600 (editable->selection_start_pos == editable->selection_end_pos) &&
5601 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5604 gint pixel_width, pixel_height;
5607 g_assert(text->cursor_mark.property);
5609 gc = gdk_gc_new(text->text_area);
5611 gdk_gc_copy(gc, text->gc);
5613 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5616 * changed the cursor to a real block (TM)
5618 if (text->cursor_type == STEXT_CURSOR_BLOCK) {
5619 if (text->use_wchar)
5620 pixel_width = gdk_char_width_wc(font, text->cursor_char);
5622 pixel_width = gdk_char_width(font, text->cursor_char);
5624 pixel_height = LINE_HEIGHT(CACHE_DATA(text->current_line));
5626 draw_bg_rect (text, &text->cursor_mark,
5628 text->cursor_pos_y - (pixel_height + 1),
5634 draw_bg_rect (text, &text->cursor_mark,
5635 text->cursor_pos_x - 1,
5636 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5637 2, font->descent + font->ascent + 1, FALSE);
5641 if (text->cursor_char)
5643 if (font->type == GDK_FONT_FONT)
5644 gdk_gc_set_font (text->gc, font);
5646 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5648 gdk_draw_text_wc (text->text_area, font,
5651 text->cursor_pos_y - text->cursor_char_offset,
5656 gdk_gc_copy(text->gc, gc);
5662 drawn_cursor_min (GtkSText* text)
5666 g_assert(text->cursor_mark.property);
5668 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5670 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5674 drawn_cursor_max (GtkSText* text)
5678 g_assert(text->cursor_mark.property);
5680 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5682 return text->cursor_pos_y - text->cursor_char_offset;
5686 draw_cursor (GtkSText* text, gint absolute)
5688 GtkEditable *editable = (GtkEditable *)text;
5689 gint pixel_width, pixel_height;
5691 TDEBUG (("in draw_cursor\n"));
5694 text->cursor_drawn_level = 1;
5696 if ((--text->cursor_drawn_level == 0) &&
5697 editable->editable &&
5698 (editable->selection_start_pos == editable->selection_end_pos) &&
5699 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5704 g_assert (text->cursor_mark.property);
5706 gc = gdk_gc_new(text->text_area);
5708 gdk_gc_copy(gc, text->gc);
5709 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5710 if (text->cursor_type == STEXT_CURSOR_BLOCK) {
5712 * changed the cursor to a real block (TM)
5714 if (text->use_wchar) {
5715 pixel_width = gdk_char_width_wc(font, text->cursor_char);
5718 pixel_width = gdk_char_width(font, text->cursor_char);
5722 pixel_height = LINE_HEIGHT(CACHE_DATA(text->current_line));
5723 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5724 gdk_gc_set_function(text->gc, GDK_INVERT);
5725 gdk_draw_rectangle(text->text_area, text->gc, TRUE,
5727 text->cursor_pos_y - pixel_height,
5732 gdk_gc_set_line_attributes(text->gc, 2, GDK_LINE_SOLID, GDK_CAP_NOT_LAST, GDK_JOIN_MITER);
5733 gdk_gc_set_foreground(text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5734 gdk_draw_line(text->text_area, text->gc, text->cursor_pos_x,
5735 text->cursor_pos_y + font->descent - text->cursor_char_offset,
5737 text->cursor_pos_y - text->cursor_char_offset - font->ascent );
5740 gdk_gc_copy(text->gc, gc);
5746 create_bg_gc (GtkSText *text)
5750 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5751 values.fill = GDK_TILED;
5753 return gdk_gc_new_with_values (text->text_area, &values,
5754 GDK_GC_FILL | GDK_GC_TILE);
5758 clear_area (GtkSText *text, GdkRectangle *area)
5760 GtkWidget *widget = GTK_WIDGET (text);
5766 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5768 gdk_gc_set_ts_origin (text->bg_gc,
5769 (- (gint)text->first_onscreen_hor_pixel) % width,
5770 (- (gint)text->first_onscreen_ver_pixel) % height);
5772 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5773 area->x, area->y, area->width, area->height);
5776 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5780 expose_text (GtkSText* text, GdkRectangle *area, gboolean cursor)
5782 GList *cache = text->line_start_cache;
5783 gint pixels = - text->first_cut_pixels;
5784 gint min_y = MAX (0, area->y);
5785 gint max_y = MAX (0, area->y + area->height);
5788 gdk_window_get_size (text->text_area, NULL, &height);
5789 max_y = MIN (max_y, height);
5791 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5793 clear_area (text, area);
5795 for (; pixels < height; cache = cache->next)
5797 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5799 draw_line (text, pixels, &CACHE_DATA(cache));
5801 if (CACHE_DATA(cache).wraps)
5802 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5805 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5807 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5808 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5810 /* We undraw and draw the cursor here to get the drawn
5811 * level right ... FIXME - maybe the second parameter
5812 * of draw_cursor should work differently
5814 undraw_cursor (text, FALSE);
5815 draw_cursor (text, FALSE);
5819 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5823 fetch_lines_forward (text, 1);
5832 gtk_stext_update_text (GtkEditable *editable,
5836 GtkSText *text = GTK_STEXT (editable);
5838 GList *cache = text->line_start_cache;
5839 gint pixels = - text->first_cut_pixels;
5845 end_pos = TEXT_LENGTH (text);
5847 if (end_pos < start_pos)
5850 gdk_window_get_size (text->text_area, &width, &height);
5856 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5858 for (; pixels < height; cache = cache->next)
5860 if (CACHE_DATA(cache).start.index < end_pos)
5862 if (CACHE_DATA(cache).end.index >= start_pos)
5865 area.y = MAX(0,pixels);
5866 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5872 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5876 fetch_lines_forward (text, 1);
5884 expose_text (text, &area, TRUE);
5888 recompute_geometry (GtkSText* text)
5890 GtkSPropertyMark mark, start_mark;
5897 mark = start_mark = set_vertical_scroll (text);
5899 /* We need a real start of a line when calling fetch_lines().
5900 * not the start of a wrapped line.
5902 while (mark.index > 0 &&
5903 GTK_STEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5904 decrement_mark (&mark);
5906 gdk_window_get_size (text->text_area, &width, &height);
5908 /* Fetch an entire line, to make sure that we get all the text
5909 * we backed over above, in addition to enough text to fill up
5910 * the space vertically
5913 new_lines = fetch_lines (text,
5919 mark = CACHE_DATA (g_list_last (new_lines)).end;
5920 if (!LAST_INDEX (text, mark))
5922 advance_mark (&mark);
5924 new_lines = g_list_concat (new_lines,
5929 height + text->first_cut_pixels));
5932 /* Now work forward to the actual first onscreen line */
5934 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5935 new_lines = new_lines->next;
5937 text->line_start_cache = new_lines;
5939 find_cursor (text, TRUE);
5942 /**********************************************************************/
5944 /**********************************************************************/
5947 gtk_stext_set_selection (GtkEditable *editable,
5951 GtkSText *text = GTK_STEXT (editable);
5953 guint start1, end1, start2, end2;
5956 end = TEXT_LENGTH (text);
5958 start1 = MIN(start,end);
5959 end1 = MAX(start,end);
5960 start2 = MIN(editable->selection_start_pos, editable->selection_end_pos);
5961 end2 = MAX(editable->selection_start_pos, editable->selection_end_pos);
5963 if (start2 < start1)
5967 tmp = start1; start1 = start2; start2 = tmp;
5968 tmp = end1; end1 = end2; end2 = tmp;
5971 undraw_cursor (text, FALSE);
5972 editable->selection_start_pos = start;
5973 editable->selection_end_pos = end;
5974 draw_cursor (text, FALSE);
5976 /* Expose only what changed */
5978 if (start1 < start2)
5979 gtk_stext_update_text (editable, start1, MIN(end1, start2));
5982 gtk_stext_update_text (editable, MAX(end1, start2), end2);
5983 else if (end2 < end1)
5984 gtk_stext_update_text (editable, end2, end1);
5992 static gint stext_blink_timer_proc(GtkSText *text)
5994 if (text->cursor_state_on) {
5995 text->cursor_state_on = FALSE;
5996 undraw_cursor(text, TRUE);
5997 /* kill this timer... */
5998 gtk_timeout_remove(text->cursor_timer_id);
5999 text->cursor_timer_id = gtk_timeout_add(text->cursor_off_ms, (GtkFunction) stext_blink_timer_proc, (gpointer) text);
6002 text->cursor_state_on = TRUE;
6003 draw_cursor(text, TRUE);
6004 /* kill this timer... */
6005 gtk_timeout_remove(text->cursor_timer_id);
6006 text->cursor_timer_id = gtk_timeout_add(text->cursor_on_ms, (GtkFunction) stext_blink_timer_proc, (gpointer) text);
6011 static gint stext_idle_timer_proc(GtkSText *text)
6013 /* make sure the cursor timer is off */
6014 if (text->cursor_timer_id) {
6015 gtk_timeout_remove(text->cursor_timer_id);
6016 text->cursor_timer_id = 0;
6018 /* assuming it's always on when calling this function ... */
6019 text->cursor_state_on = TRUE;
6020 text->cursor_timer_id = gtk_timeout_add(text->cursor_on_ms, (GtkFunction) stext_blink_timer_proc, (gpointer) text);
6021 /* make sure we kill the timer (could perhaps make this function return FALSE (not documented in
6022 * the current docs). should check the source. */
6023 gtk_idle_remove( text->cursor_idle_time_timer_id );
6024 text->cursor_idle_time_timer_id = 0;
6028 static void gtk_stext_enable_blink (GtkSText *text)
6030 if (text->cursor_timer_on) {
6031 gtk_stext_disable_blink(text);
6032 text->cursor_idle_time_timer_id = gtk_idle_add((GtkFunction) stext_idle_timer_proc, (gpointer) text);
6036 static void gtk_stext_disable_blink (GtkSText *text)
6038 if (text->cursor_timer_on) {
6039 if (text->cursor_idle_time_timer_id) {
6040 gtk_idle_remove( text->cursor_idle_time_timer_id );
6041 text->cursor_idle_time_timer_id = 0;
6043 if (text->cursor_timer_id) {
6044 gtk_timeout_remove( text->cursor_timer_id );
6045 text->cursor_timer_id = 0;
6047 draw_cursor(text, TRUE);
6051 void gtk_stext_set_blink(GtkSText *text, gboolean blinkin_on)
6053 if (text->cursor_timer_on != blinkin_on) {
6054 if (text->cursor_timer_on) {
6055 /* text widget already created? */
6056 if (text->cursor_visible) {
6057 gtk_stext_disable_blink(text);
6059 text->cursor_timer_on = FALSE;
6062 if (text->cursor_visible) {
6063 gtk_stext_enable_blink(text);
6065 text->cursor_timer_on = TRUE;
6071 void gtk_stext_set_wrap_rmargin (GtkSText *text, gint rmargin)
6073 /* not particularly impressive, but it does the job. */
6074 /* TODO: currently only allowed to set this after a
6075 * gtk_stext_new() */
6076 text->wrap_rmargin = rmargin >= 0 ? rmargin : 0;
6079 /**********************************************************************/
6081 /**********************************************************************/
6083 #ifdef DEBUG_GTK_STEXT
6085 gtk_stext_show_cache_line (GtkSText *text, GList *cache,
6086 const char* what, const char* func, gint line)
6088 LineParams *lp = &CACHE_DATA(cache);
6091 if (cache == text->line_start_cache)
6092 g_message ("Line Start Cache: ");
6094 if (cache == text->current_line)
6095 g_message("Current Line: ");
6097 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
6105 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
6106 g_message ("%c", GTK_STEXT_INDEX (text, i));
6112 gtk_stext_show_cache (GtkSText *text, const char* func, gint line)
6114 GList *l = text->line_start_cache;
6120 /* back up to the absolute beginning of the line cache */
6124 g_message ("*** line cache ***\n");
6125 for (; l; l = l->next)
6126 gtk_stext_show_cache_line (text, l, "all", func, line);
6130 gtk_stext_assert_mark (GtkSText *text,
6131 GtkSPropertyMark *mark,
6132 GtkSPropertyMark *before,
6133 GtkSPropertyMark *after,
6138 GtkSPropertyMark correct_mark = find_mark (text, mark->index);
6140 if (mark->offset != correct_mark.offset ||
6141 mark->property != correct_mark.property)
6142 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
6146 gtk_stext_assert (GtkSText *text,
6150 GList* cache = text->line_start_cache;
6151 GtkSPropertyMark* before_mark = NULL;
6152 GtkSPropertyMark* after_mark = NULL;
6154 gtk_stext_show_props (text, msg, line);
6156 for (; cache->prev; cache = cache->prev)
6159 g_message ("*** line markers ***\n");
6161 for (; cache; cache = cache->next)
6163 after_mark = &CACHE_DATA(cache).end;
6164 gtk_stext_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
6165 before_mark = &CACHE_DATA(cache).start;
6168 after_mark = &CACHE_DATA(cache->next).start;
6172 gtk_stext_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
6173 before_mark = &CACHE_DATA(cache).end;
6178 gtk_stext_show_adj (GtkSText *text,
6184 g_message ("*** adjustment ***\n");
6186 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
6193 adj->step_increment,
6194 adj->page_increment,
6199 gtk_stext_show_props (GtkSText *text,
6203 GList* props = text->text_properties;
6206 g_message ("%s:%d: ", msg, line);
6208 for (; props; props = props->next)
6210 TextProperty *p = (TextProperty*)props->data;
6212 proplen += p->length;
6214 g_message ("[%d,%p,", p->length, p);
6215 if (p->flags & PROPERTY_FONT)
6216 g_message ("%p,", p->font);
6219 if (p->flags & PROPERTY_FOREGROUND)
6220 g_message ("%ld, ", p->fore_color.pixel);
6223 if (p->flags & PROPERTY_BACKGROUND)
6224 g_message ("%ld] ", p->back_color.pixel);
6231 if (proplen - 1 != TEXT_LENGTH(text))
6232 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);