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)
480 /* Memory Management. */
481 static GMemChunk *params_mem_chunk = NULL;
482 static GMemChunk *text_property_chunk = NULL;
484 static GtkWidgetClass *parent_class = NULL;
487 static const GtkTextFunction control_keys[26] =
489 (GtkTextFunction)gtk_stext_move_beginning_of_line, /* a */
490 (GtkTextFunction)gtk_stext_move_backward_character, /* b */
491 (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
492 (GtkTextFunction)gtk_stext_delete_forward_character, /* d */
493 (GtkTextFunction)gtk_stext_move_end_of_line, /* e */
494 (GtkTextFunction)gtk_stext_move_forward_character, /* f */
496 (GtkTextFunction)gtk_stext_delete_backward_character, /* h */
499 (GtkTextFunction)gtk_stext_delete_to_line_end, /* k */
502 (GtkTextFunction)gtk_stext_move_next_line, /* n */
504 (GtkTextFunction)gtk_stext_move_previous_line, /* p */
509 (GtkTextFunction)gtk_stext_delete_line, /* u */
510 (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
511 (GtkTextFunction)gtk_stext_delete_backward_word, /* w */
512 (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
517 static const GtkTextFunction alt_keys[26] =
520 (GtkTextFunction)gtk_stext_move_backward_word, /* b */
522 (GtkTextFunction)gtk_stext_delete_forward_word, /* d */
524 (GtkTextFunction)gtk_stext_move_forward_word, /* f */
549 #define line_arrow_width 6
550 #define line_arrow_height 9
551 static unsigned char line_arrow_bits[] = {
552 0x00, 0x00, 0x04, 0x0c, 0x18, 0x3f, 0x18, 0x0c, 0x04};
555 #define line_wrap_width 6
556 #define line_wrap_height 9
557 static unsigned char line_wrap_bits[] = {
558 0x1e, 0x3e, 0x30, 0x30, 0x39, 0x1f, 0x0f, 0x0f, 0x1f, };
560 /**********************************************************************/
562 /**********************************************************************/
565 gtk_stext_get_type (void)
567 static GtkType text_type = 0;
571 static const GtkTypeInfo text_info =
575 sizeof (GtkSTextClass),
576 (GtkClassInitFunc) gtk_stext_class_init,
577 (GtkObjectInitFunc) gtk_stext_init,
578 /* reserved_1 */ NULL,
579 /* reserved_2 */ NULL,
580 (GtkClassInitFunc) NULL,
583 text_type = gtk_type_unique (GTK_TYPE_EDITABLE, &text_info);
590 gtk_stext_class_init (GtkSTextClass *class)
592 GtkObjectClass *object_class;
593 GtkWidgetClass *widget_class;
594 GtkEditableClass *editable_class;
596 object_class = (GtkObjectClass*) class;
597 widget_class = (GtkWidgetClass*) class;
598 editable_class = (GtkEditableClass*) class;
599 parent_class = gtk_type_class (GTK_TYPE_EDITABLE);
601 gtk_object_add_arg_type ("GtkSText::hadjustment",
603 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
605 gtk_object_add_arg_type ("GtkSText::vadjustment",
607 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
609 gtk_object_add_arg_type ("GtkSText::line_wrap",
613 gtk_object_add_arg_type ("GtkSText::word_wrap",
618 object_class->set_arg = gtk_stext_set_arg;
619 object_class->get_arg = gtk_stext_get_arg;
620 object_class->destroy = gtk_stext_destroy;
621 object_class->finalize = gtk_stext_finalize;
623 widget_class->realize = gtk_stext_realize;
624 widget_class->unrealize = gtk_stext_unrealize;
625 widget_class->style_set = gtk_stext_style_set;
626 widget_class->state_changed = gtk_stext_state_changed;
627 widget_class->draw_focus = gtk_stext_draw_focus;
628 widget_class->size_request = gtk_stext_size_request;
629 widget_class->size_allocate = gtk_stext_size_allocate;
630 widget_class->draw = gtk_stext_draw;
631 widget_class->expose_event = gtk_stext_expose;
632 widget_class->button_press_event = gtk_stext_button_press;
633 widget_class->button_release_event = gtk_stext_button_release;
634 widget_class->motion_notify_event = gtk_stext_motion_notify;
635 widget_class->key_press_event = gtk_stext_key_press;
636 widget_class->focus_in_event = gtk_stext_focus_in;
637 widget_class->focus_out_event = gtk_stext_focus_out;
639 widget_class->set_scroll_adjustments_signal =
640 gtk_signal_new ("set_scroll_adjustments",
643 GTK_SIGNAL_OFFSET (GtkSTextClass, set_scroll_adjustments),
644 gtk_marshal_NONE__POINTER_POINTER,
645 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
647 editable_class->set_editable = gtk_stext_real_set_editable;
648 editable_class->insert_text = gtk_stext_insert_text;
649 editable_class->delete_text = gtk_stext_delete_text;
651 editable_class->move_cursor = gtk_stext_move_cursor;
652 editable_class->move_word = gtk_stext_move_word;
653 editable_class->move_page = gtk_stext_move_page;
654 editable_class->move_to_row = gtk_stext_move_to_row;
655 editable_class->move_to_column = gtk_stext_move_to_column;
657 editable_class->kill_char = gtk_stext_kill_char;
658 editable_class->kill_word = gtk_stext_kill_word;
659 editable_class->kill_line = gtk_stext_kill_line;
661 editable_class->update_text = gtk_stext_update_text;
662 editable_class->get_chars = gtk_stext_get_chars;
663 editable_class->set_selection = gtk_stext_set_selection;
664 editable_class->set_position = gtk_stext_set_position;
666 class->set_scroll_adjustments = gtk_stext_set_adjustments;
670 gtk_stext_set_arg (GtkObject *object,
676 text = GTK_STEXT (object);
680 case ARG_HADJUSTMENT:
681 gtk_stext_set_adjustments (text,
682 GTK_VALUE_POINTER (*arg),
685 case ARG_VADJUSTMENT:
686 gtk_stext_set_adjustments (text,
688 GTK_VALUE_POINTER (*arg));
691 gtk_stext_set_line_wrap (text, GTK_VALUE_BOOL (*arg));
694 gtk_stext_set_word_wrap (text, GTK_VALUE_BOOL (*arg));
702 gtk_stext_get_arg (GtkObject *object,
708 text = GTK_STEXT (object);
712 case ARG_HADJUSTMENT:
713 GTK_VALUE_POINTER (*arg) = text->hadj;
715 case ARG_VADJUSTMENT:
716 GTK_VALUE_POINTER (*arg) = text->vadj;
719 GTK_VALUE_BOOL (*arg) = text->line_wrap;
722 GTK_VALUE_BOOL (*arg) = text->word_wrap;
725 arg->type = GTK_TYPE_INVALID;
731 gtk_stext_init (GtkSText *text)
733 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
735 text->text_area = NULL;
740 text->line_wrap_bitmap = NULL;
741 text->line_arrow_bitmap = NULL;
743 text->use_wchar = FALSE;
744 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
745 text->text_len = INITIAL_BUFFER_SIZE;
747 text->scratch_buffer.ch = NULL;
748 text->scratch_buffer_len = 0;
750 text->freeze_count = 0;
752 if (!params_mem_chunk)
753 params_mem_chunk = g_mem_chunk_new ("LineParams",
755 256 * sizeof (LineParams),
758 text->default_tab_width = 4;
759 text->tab_stops = NULL;
761 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
762 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
764 text->line_start_cache = NULL;
765 text->first_cut_pixels = 0;
767 text->line_wrap = TRUE;
768 text->word_wrap = FALSE;
773 text->current_font = NULL;
776 * timer for blinking cursor
778 text->cursor_visible = FALSE; /* don't know whether gtktext stores this somewhere */
779 text->cursor_timer_on = TRUE;
780 text->cursor_off_ms = 500;
781 text->cursor_on_ms = 500;
782 text->cursor_timer_id = 0;
783 text->cursor_idle_time_timer_id = 0;
784 text->wrap_rmargin = 0;
785 text->cursor_type = STEXT_CURSOR_LINE;
786 text->persist_column = 0;
788 init_properties (text);
790 GTK_EDITABLE (text)->editable = FALSE;
792 gtk_editable_set_position (GTK_EDITABLE (text), 0);
796 gtk_stext_new (GtkAdjustment *hadj,
802 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
804 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
806 text = gtk_widget_new (GTK_TYPE_STEXT,
815 gtk_stext_set_word_wrap (GtkSText *text,
818 g_return_if_fail (text != NULL);
819 g_return_if_fail (GTK_IS_STEXT (text));
821 text->word_wrap = (word_wrap != FALSE);
823 if (GTK_WIDGET_REALIZED (text))
825 recompute_geometry (text);
826 gtk_widget_queue_draw (GTK_WIDGET (text));
831 gtk_stext_set_line_wrap (GtkSText *text,
834 g_return_if_fail (text != NULL);
835 g_return_if_fail (GTK_IS_STEXT (text));
837 text->line_wrap = (line_wrap != FALSE);
839 if (GTK_WIDGET_REALIZED (text))
841 recompute_geometry (text);
842 gtk_widget_queue_draw (GTK_WIDGET (text));
847 gtk_stext_set_editable (GtkSText *text,
848 gboolean is_editable)
850 g_return_if_fail (text != NULL);
851 g_return_if_fail (GTK_IS_STEXT (text));
853 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
857 gtk_stext_real_set_editable (GtkEditable *editable,
858 gboolean is_editable)
862 g_return_if_fail (editable != NULL);
863 g_return_if_fail (GTK_IS_STEXT (editable));
865 text = GTK_STEXT (editable);
867 editable->editable = (is_editable != FALSE);
869 if (GTK_WIDGET_REALIZED (text))
871 recompute_geometry (text);
872 gtk_widget_queue_draw (GTK_WIDGET (text));
877 gtk_stext_set_adjustments (GtkSText *text,
881 g_return_if_fail (text != NULL);
882 g_return_if_fail (GTK_IS_STEXT (text));
884 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
886 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
888 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
890 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
892 if (text->hadj && (text->hadj != hadj))
894 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
895 gtk_object_unref (GTK_OBJECT (text->hadj));
898 if (text->vadj && (text->vadj != vadj))
900 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
901 gtk_object_unref (GTK_OBJECT (text->vadj));
904 if (text->hadj != hadj)
907 gtk_object_ref (GTK_OBJECT (text->hadj));
908 gtk_object_sink (GTK_OBJECT (text->hadj));
910 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
911 (GtkSignalFunc) gtk_stext_adjustment,
913 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
914 (GtkSignalFunc) gtk_stext_adjustment,
916 gtk_signal_connect (GTK_OBJECT (text->hadj), "disconnect",
917 (GtkSignalFunc) gtk_stext_disconnect,
919 gtk_stext_adjustment (hadj, text);
922 if (text->vadj != vadj)
925 gtk_object_ref (GTK_OBJECT (text->vadj));
926 gtk_object_sink (GTK_OBJECT (text->vadj));
928 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
929 (GtkSignalFunc) gtk_stext_adjustment,
931 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
932 (GtkSignalFunc) gtk_stext_adjustment,
934 gtk_signal_connect (GTK_OBJECT (text->vadj), "disconnect",
935 (GtkSignalFunc) gtk_stext_disconnect,
937 gtk_stext_adjustment (vadj, text);
942 gtk_stext_set_point (GtkSText *text,
945 g_return_if_fail (text != NULL);
946 g_return_if_fail (GTK_IS_STEXT (text));
947 g_return_if_fail (index <= TEXT_LENGTH (text));
949 text->point = find_mark (text, index);
953 gtk_stext_get_point (GtkSText *text)
955 g_return_val_if_fail (text != NULL, 0);
956 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
958 return text->point.index;
962 gtk_stext_get_length (GtkSText *text)
964 g_return_val_if_fail (text != NULL, 0);
965 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
967 return TEXT_LENGTH (text);
971 gtk_stext_freeze (GtkSText *text)
973 g_return_if_fail (text != NULL);
974 g_return_if_fail (GTK_IS_STEXT (text));
976 text->freeze_count++;
977 undraw_cursor (text, FALSE);
981 gtk_stext_thaw (GtkSText *text)
983 g_return_if_fail (text != NULL);
984 g_return_if_fail (GTK_IS_STEXT (text));
986 if (text->freeze_count)
987 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
989 recompute_geometry (text);
990 gtk_widget_queue_draw (GTK_WIDGET (text));
992 draw_cursor (text, FALSE);
996 gtk_stext_insert (GtkSText *text,
1003 GtkEditable *editable = GTK_EDITABLE (text);
1004 gboolean frozen = FALSE;
1006 gint new_line_count = 1;
1007 guint old_height = 0;
1012 g_return_if_fail (text != NULL);
1013 g_return_if_fail (GTK_IS_STEXT (text));
1015 g_return_if_fail (chars != NULL);
1018 if (!nchars || !chars)
1020 nchars = strlen (chars);
1024 if (!text->freeze_count && (length > FREEZE_LENGTH))
1026 gtk_stext_freeze (text);
1030 if (!text->freeze_count && (text->line_start_cache != NULL))
1032 find_line_containing_point (text, text->point.index, TRUE);
1033 old_height = total_line_height (text, text->current_line, 1);
1036 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
1039 widget = GTK_WIDGET (text);
1040 gtk_widget_ensure_style (widget);
1041 if ((widget->style) && (widget->style->font->type == GDK_FONT_FONTSET))
1043 text->use_wchar = TRUE;
1044 g_free (text->text.ch);
1045 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
1046 text->text_len = INITIAL_BUFFER_SIZE;
1047 if (text->scratch_buffer.ch)
1048 g_free (text->scratch_buffer.ch);
1049 text->scratch_buffer.wc = NULL;
1050 text->scratch_buffer_len = 0;
1054 move_gap (text, text->point.index);
1055 make_forward_space (text, length);
1057 if (text->use_wchar)
1059 char *chars_nt = (char *)chars;
1062 chars_nt = g_new (char, length+1);
1063 memcpy (chars_nt, chars, length);
1064 chars_nt[length] = 0;
1066 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
1068 if (chars_nt != chars)
1076 memcpy(text->text.ch + text->gap_position, chars, length);
1079 if (!text->freeze_count && (text->line_start_cache != NULL))
1081 if (text->use_wchar)
1083 for (i=0; i<numwcs; i++)
1084 if (text->text.wc[text->gap_position + i] == '\n')
1089 for (i=0; i<numwcs; i++)
1090 if (text->text.ch[text->gap_position + i] == '\n')
1097 insert_text_property (text, font, fore, back, numwcs);
1099 text->gap_size -= numwcs;
1100 text->gap_position += numwcs;
1102 if (text->point.index < text->first_line_start_index)
1103 text->first_line_start_index += numwcs;
1104 if (text->point.index < editable->selection_start_pos)
1105 editable->selection_start_pos += numwcs;
1106 if (text->point.index < editable->selection_end_pos)
1107 editable->selection_end_pos += numwcs;
1108 /* We'll reset the cursor later anyways if we aren't frozen */
1109 if (text->point.index < text->cursor_mark.index)
1110 text->cursor_mark.index += numwcs;
1112 advance_mark_n (&text->point, numwcs);
1114 if (!text->freeze_count && (text->line_start_cache != NULL))
1115 insert_expose (text, old_height, numwcs, new_line_count);
1119 gtk_stext_thaw (text);
1123 gtk_stext_backward_delete (GtkSText *text,
1126 g_return_val_if_fail (text != NULL, 0);
1127 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
1129 if (nchars > text->point.index || nchars <= 0)
1132 gtk_stext_set_point (text, text->point.index - nchars);
1134 return gtk_stext_forward_delete (text, nchars);
1138 gtk_stext_forward_delete (GtkSText *text,
1141 guint old_lines, old_height;
1142 GtkEditable *editable = GTK_EDITABLE (text);
1143 gboolean frozen = FALSE;
1145 g_return_val_if_fail (text != NULL, 0);
1146 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
1148 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1151 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1153 gtk_stext_freeze (text);
1157 if (!text->freeze_count && text->line_start_cache != NULL)
1159 /* We need to undraw the cursor here, since we may later
1160 * delete the cursor's property
1162 undraw_cursor (text, FALSE);
1163 find_line_containing_point (text, text->point.index, TRUE);
1164 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1167 /* FIXME, or resizing after deleting will be odd */
1168 if (text->point.index < text->first_line_start_index)
1170 if (text->point.index + nchars >= text->first_line_start_index)
1172 text->first_line_start_index = text->point.index;
1173 while ((text->first_line_start_index > 0) &&
1174 (GTK_STEXT_INDEX (text, text->first_line_start_index - 1)
1176 text->first_line_start_index -= 1;
1180 text->first_line_start_index -= nchars;
1183 if (text->point.index < editable->selection_start_pos)
1184 editable->selection_start_pos -=
1185 MIN(nchars, editable->selection_start_pos - text->point.index);
1186 if (text->point.index < editable->selection_end_pos)
1187 editable->selection_end_pos -=
1188 MIN(nchars, editable->selection_end_pos - text->point.index);
1189 /* We'll reset the cursor later anyways if we aren't frozen */
1190 if (text->point.index < text->cursor_mark.index)
1191 move_mark_n (&text->cursor_mark,
1192 -MIN(nchars, text->cursor_mark.index - text->point.index));
1194 move_gap (text, text->point.index);
1196 text->gap_size += nchars;
1198 delete_text_property (text, nchars);
1200 if (!text->freeze_count && (text->line_start_cache != NULL))
1202 delete_expose (text, nchars, old_lines, old_height);
1203 draw_cursor (text, FALSE);
1207 gtk_stext_thaw (text);
1213 gtk_stext_set_position (GtkEditable *editable,
1216 GtkSText *text = (GtkSText *) editable;
1218 undraw_cursor (text, FALSE);
1219 text->cursor_mark = find_mark (text, position);
1220 find_cursor (text, TRUE);
1221 draw_cursor (text, FALSE);
1222 gtk_editable_select_region (editable, 0, 0);
1225 /* SYLPHEED - set_position used. Need to find out whether there's
1226 * a better way to handle this */
1227 static void gtk_stext_set_position_X (GtkEditable *editable,
1230 GtkSText *text = (GtkSText *) editable;
1232 undraw_cursor (text, FALSE);
1233 text->cursor_mark = find_mark (text, position);
1234 find_cursor (text, TRUE);
1235 draw_cursor (text, FALSE);
1239 gtk_stext_get_chars (GtkEditable *editable,
1247 g_return_val_if_fail (editable != NULL, NULL);
1248 g_return_val_if_fail (GTK_IS_STEXT (editable), NULL);
1249 text = GTK_STEXT (editable);
1252 end_pos = TEXT_LENGTH (text);
1254 if ((start_pos < 0) ||
1255 (end_pos > TEXT_LENGTH (text)) ||
1256 (end_pos < start_pos))
1259 move_gap (text, TEXT_LENGTH (text));
1260 make_forward_space (text, 1);
1262 if (text->use_wchar)
1265 ch = text->text.wc[end_pos];
1266 text->text.wc[end_pos] = 0;
1267 retval = gdk_wcstombs (text->text.wc + start_pos);
1268 text->text.wc[end_pos] = ch;
1273 ch = text->text.ch[end_pos];
1274 text->text.ch[end_pos] = 0;
1275 retval = g_strdup (text->text.ch + start_pos);
1276 text->text.ch[end_pos] = ch;
1284 gtk_stext_destroy (GtkObject *object)
1288 g_return_if_fail (object != NULL);
1289 g_return_if_fail (GTK_IS_STEXT (object));
1291 text = (GtkSText*) object;
1293 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1294 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1298 gtk_timeout_remove (text->timer);
1305 gtk_stext_disable_blink(text);
1307 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1311 gtk_stext_finalize (GtkObject *object)
1316 g_return_if_fail (object != NULL);
1317 g_return_if_fail (GTK_IS_STEXT (object));
1319 text = (GtkSText *)object;
1321 gtk_object_unref (GTK_OBJECT (text->hadj));
1322 gtk_object_unref (GTK_OBJECT (text->vadj));
1324 /* Clean up the internal structures */
1325 if (text->use_wchar)
1326 g_free (text->text.wc);
1328 g_free (text->text.ch);
1330 tmp_list = text->text_properties;
1333 destroy_text_property (tmp_list->data);
1334 tmp_list = tmp_list->next;
1337 if (text->current_font)
1338 text_font_unref (text->current_font);
1340 g_list_free (text->text_properties);
1342 if (text->use_wchar)
1344 if (text->scratch_buffer.wc)
1345 g_free (text->scratch_buffer.wc);
1349 if (text->scratch_buffer.ch)
1350 g_free (text->scratch_buffer.ch);
1353 g_list_free (text->tab_stops);
1355 GTK_OBJECT_CLASS(parent_class)->finalize (object);
1359 gtk_stext_realize (GtkWidget *widget)
1362 GtkEditable *editable;
1363 GdkWindowAttr attributes;
1364 gint attributes_mask;
1366 g_return_if_fail (widget != NULL);
1367 g_return_if_fail (GTK_IS_STEXT (widget));
1369 text = GTK_STEXT (widget);
1370 editable = GTK_EDITABLE (widget);
1371 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1373 attributes.window_type = GDK_WINDOW_CHILD;
1374 attributes.x = widget->allocation.x;
1375 attributes.y = widget->allocation.y;
1376 attributes.width = widget->allocation.width;
1377 attributes.height = widget->allocation.height;
1378 attributes.wclass = GDK_INPUT_OUTPUT;
1379 attributes.visual = gtk_widget_get_visual (widget);
1380 attributes.colormap = gtk_widget_get_colormap (widget);
1381 attributes.event_mask = gtk_widget_get_events (widget);
1382 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1383 GDK_BUTTON_PRESS_MASK |
1384 GDK_BUTTON_RELEASE_MASK |
1385 GDK_BUTTON_MOTION_MASK |
1386 GDK_ENTER_NOTIFY_MASK |
1387 GDK_LEAVE_NOTIFY_MASK |
1388 GDK_KEY_PRESS_MASK);
1389 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1391 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1392 gdk_window_set_user_data (widget->window, text);
1394 attributes.x = (widget->style->klass->xthickness + TEXT_BORDER_ROOM);
1395 attributes.y = (widget->style->klass->ythickness + TEXT_BORDER_ROOM);
1396 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1397 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1399 attributes.cursor = gdk_cursor_new (GDK_XTERM);
1400 attributes_mask |= GDK_WA_CURSOR;
1402 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1403 gdk_window_set_user_data (text->text_area, text);
1405 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1407 widget->style = gtk_style_attach (widget->style, widget->window);
1409 /* Can't call gtk_style_set_background here because it's handled specially */
1410 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1411 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1413 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1414 text->bg_gc = create_bg_gc (text);
1416 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1417 (gchar*) line_wrap_bits,
1421 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1422 (gchar*) line_arrow_bits,
1426 text->gc = gdk_gc_new (text->text_area);
1427 gdk_gc_set_exposures (text->gc, TRUE);
1428 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1430 #ifdef USE_GTKGDK_XIM
1431 if (gdk_im_ready () && (editable->ic_attr = gdk_ic_attr_new ()) != NULL)
1434 GdkColormap *colormap;
1436 GdkICAttr *attr = editable->ic_attr;
1437 GdkICAttributesType attrmask = GDK_IC_ALL_REQ;
1439 GdkIMStyle supported_style = GDK_IM_PREEDIT_NONE |
1440 GDK_IM_PREEDIT_NOTHING |
1441 GDK_IM_PREEDIT_POSITION |
1442 GDK_IM_STATUS_NONE |
1443 GDK_IM_STATUS_NOTHING;
1445 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1446 supported_style &= ~GDK_IM_PREEDIT_POSITION;
1448 attr->style = style = gdk_im_decide_style (supported_style);
1449 attr->client_window = text->text_area;
1451 if ((colormap = gtk_widget_get_colormap (widget)) !=
1452 gtk_widget_get_default_colormap ())
1454 attrmask |= GDK_IC_PREEDIT_COLORMAP;
1455 attr->preedit_colormap = colormap;
1458 switch (style & GDK_IM_PREEDIT_MASK)
1460 case GDK_IM_PREEDIT_POSITION:
1461 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1463 g_warning ("over-the-spot style requires fontset");
1467 attrmask |= GDK_IC_PREEDIT_POSITION_REQ;
1468 gdk_window_get_size (text->text_area, &width, &height);
1469 attr->spot_location.x = 0;
1470 attr->spot_location.y = height;
1471 attr->preedit_area.x = 0;
1472 attr->preedit_area.y = 0;
1473 attr->preedit_area.width = width;
1474 attr->preedit_area.height = height;
1475 attr->preedit_fontset = widget->style->font;
1479 editable->ic = gdk_ic_new (attr, attrmask);
1481 if (editable->ic == NULL)
1482 g_warning ("Can't create input context.");
1485 mask = gdk_window_get_events (text->text_area);
1486 mask |= gdk_ic_get_events (editable->ic);
1487 gdk_window_set_events (text->text_area, mask);
1489 if (GTK_WIDGET_HAS_FOCUS (widget))
1490 gdk_im_begin (editable->ic, text->text_area);
1495 realize_properties (text);
1496 gdk_window_show (text->text_area);
1497 init_properties (text);
1499 if (editable->selection_start_pos != editable->selection_end_pos)
1500 gtk_editable_claim_selection (editable, TRUE, GDK_CURRENT_TIME);
1502 recompute_geometry (text);
1506 gtk_stext_style_set (GtkWidget *widget,
1507 GtkStyle *previous_style)
1509 GtkSText *text = GTK_STEXT (widget);
1511 if (GTK_WIDGET_REALIZED (widget))
1513 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1514 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1518 gdk_gc_destroy (text->bg_gc);
1522 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1523 text->bg_gc = create_bg_gc (text);
1525 recompute_geometry (text);
1528 if (text->current_font)
1529 text_font_unref (text->current_font);
1530 text->current_font = get_text_font (widget->style->font);
1534 gtk_stext_state_changed (GtkWidget *widget,
1535 GtkStateType previous_state)
1537 GtkSText *text = GTK_STEXT (widget);
1539 if (GTK_WIDGET_REALIZED (widget))
1541 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1542 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1547 gtk_stext_unrealize (GtkWidget *widget)
1551 g_return_if_fail (widget != NULL);
1552 g_return_if_fail (GTK_IS_STEXT (widget));
1554 text = GTK_STEXT (widget);
1556 #ifdef USE_GTKGDK_XIM
1557 if (GTK_EDITABLE (widget)->ic)
1559 gdk_ic_destroy (GTK_EDITABLE (widget)->ic);
1560 GTK_EDITABLE (widget)->ic = NULL;
1562 if (GTK_EDITABLE (widget)->ic_attr)
1564 gdk_ic_attr_destroy (GTK_EDITABLE (widget)->ic_attr);
1565 GTK_EDITABLE (widget)->ic_attr = NULL;
1569 gdk_window_set_user_data (text->text_area, NULL);
1570 gdk_window_destroy (text->text_area);
1571 text->text_area = NULL;
1573 gdk_gc_destroy (text->gc);
1578 gdk_gc_destroy (text->bg_gc);
1582 gdk_pixmap_unref (text->line_wrap_bitmap);
1583 gdk_pixmap_unref (text->line_arrow_bitmap);
1585 unrealize_properties (text);
1589 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1590 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1594 clear_focus_area (GtkSText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1596 GtkWidget *widget = GTK_WIDGET (text);
1599 gint ythick = TEXT_BORDER_ROOM + widget->style->klass->ythickness;
1600 gint xthick = TEXT_BORDER_ROOM + widget->style->klass->xthickness;
1604 if (area_width == 0 || area_height == 0)
1607 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1609 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1611 gdk_gc_set_ts_origin (text->bg_gc,
1612 (- (gint)text->first_onscreen_hor_pixel + xthick) % width,
1613 (- (gint)text->first_onscreen_ver_pixel + ythick) % height);
1618 gc = widget->style->bg_gc[widget->state];
1621 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1622 area_x, area_y, area_width, area_height);
1626 gtk_stext_draw_focus (GtkWidget *widget)
1632 g_return_if_fail (widget != NULL);
1633 g_return_if_fail (GTK_IS_STEXT (widget));
1635 text = GTK_STEXT (widget);
1637 if (GTK_WIDGET_DRAWABLE (widget))
1639 gint ythick = widget->style->klass->ythickness;
1640 gint xthick = widget->style->klass->xthickness;
1641 gint xextra = TEXT_BORDER_ROOM;
1642 gint yextra = TEXT_BORDER_ROOM;
1644 TDEBUG (("in gtk_stext_draw_focus\n"));
1648 width = widget->allocation.width;
1649 height = widget->allocation.height;
1651 if (GTK_WIDGET_HAS_FOCUS (widget))
1660 gtk_paint_focus (widget->style, widget->window,
1661 NULL, widget, "text",
1663 widget->allocation.width - 1,
1664 widget->allocation.height - 1);
1667 gtk_paint_shadow (widget->style, widget->window,
1668 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1669 NULL, widget, "text",
1670 x, y, width, height);
1674 width -= 2 * xthick;
1675 height -= 2 * ythick;
1678 clear_focus_area (text, x, y, width, yextra);
1680 clear_focus_area (text, x, y + yextra,
1681 xextra, y + height - 2 * yextra);
1683 clear_focus_area (text, x + width - xextra, y + yextra,
1684 xextra, height - 2 * ythick);
1686 clear_focus_area (text, x, x + height - yextra, width, yextra);
1690 TDEBUG (("in gtk_stext_draw_focus (undrawable !!!)\n"));
1695 gtk_stext_size_request (GtkWidget *widget,
1696 GtkRequisition *requisition)
1703 g_return_if_fail (widget != NULL);
1704 g_return_if_fail (GTK_IS_STEXT (widget));
1705 g_return_if_fail (requisition != NULL);
1707 xthickness = widget->style->klass->xthickness + TEXT_BORDER_ROOM;
1708 ythickness = widget->style->klass->ythickness + TEXT_BORDER_ROOM;
1710 char_height = MIN_TEXT_HEIGHT_LINES * (widget->style->font->ascent +
1711 widget->style->font->descent);
1713 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (widget->style->font,
1714 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1718 requisition->width = char_width + xthickness * 2;
1719 requisition->height = char_height + ythickness * 2;
1723 gtk_stext_size_allocate (GtkWidget *widget,
1724 GtkAllocation *allocation)
1727 GtkEditable *editable;
1729 g_return_if_fail (widget != NULL);
1730 g_return_if_fail (GTK_IS_STEXT (widget));
1731 g_return_if_fail (allocation != NULL);
1733 text = GTK_STEXT (widget);
1734 editable = GTK_EDITABLE (widget);
1736 widget->allocation = *allocation;
1737 if (GTK_WIDGET_REALIZED (widget))
1739 gdk_window_move_resize (widget->window,
1740 allocation->x, allocation->y,
1741 allocation->width, allocation->height);
1743 gdk_window_move_resize (text->text_area,
1744 widget->style->klass->xthickness + TEXT_BORDER_ROOM,
1745 widget->style->klass->ythickness + TEXT_BORDER_ROOM,
1746 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->klass->xthickness +
1747 (gint)TEXT_BORDER_ROOM) * 2),
1748 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->klass->ythickness +
1749 (gint)TEXT_BORDER_ROOM) * 2));
1751 #ifdef USE_GTKGDK_XIM
1752 if (editable->ic && (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
1756 gdk_window_get_size (text->text_area, &width, &height);
1757 editable->ic_attr->preedit_area.width = width;
1758 editable->ic_attr->preedit_area.height = height;
1760 gdk_ic_set_attr (editable->ic,
1761 editable->ic_attr, GDK_IC_PREEDIT_AREA);
1765 recompute_geometry (text);
1770 gtk_stext_draw (GtkWidget *widget,
1773 g_return_if_fail (widget != NULL);
1774 g_return_if_fail (GTK_IS_STEXT (widget));
1775 g_return_if_fail (area != NULL);
1777 if (GTK_WIDGET_DRAWABLE (widget))
1779 expose_text (GTK_STEXT (widget), area, TRUE);
1780 gtk_widget_draw_focus (widget);
1785 gtk_stext_expose (GtkWidget *widget,
1786 GdkEventExpose *event)
1788 g_return_val_if_fail (widget != NULL, FALSE);
1789 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1790 g_return_val_if_fail (event != NULL, FALSE);
1792 if (event->window == GTK_STEXT (widget)->text_area)
1794 TDEBUG (("in gtk_stext_expose (expose)\n"));
1795 expose_text (GTK_STEXT (widget), &event->area, TRUE);
1797 else if (event->count == 0)
1799 TDEBUG (("in gtk_stext_expose (focus)\n"));
1800 gtk_widget_draw_focus (widget);
1807 gtk_stext_scroll_timeout (gpointer data)
1810 GdkEventMotion event;
1812 GdkModifierType mask;
1814 GDK_THREADS_ENTER ();
1816 text = GTK_STEXT (data);
1819 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1821 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1828 gtk_stext_motion_notify (GTK_WIDGET (text), &event);
1831 GDK_THREADS_LEAVE ();
1837 gtk_stext_button_press (GtkWidget *widget,
1838 GdkEventButton *event)
1841 GtkEditable *editable;
1842 static GdkAtom ctext_atom = GDK_NONE;
1844 g_return_val_if_fail (widget != NULL, FALSE);
1845 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1846 g_return_val_if_fail (event != NULL, FALSE);
1848 if (ctext_atom == GDK_NONE)
1849 ctext_atom = gdk_atom_intern ("COMPOUND_TEXT", FALSE);
1851 text = GTK_STEXT (widget);
1852 editable = GTK_EDITABLE (widget);
1854 if (text->button && (event->button != text->button))
1857 text->button = event->button;
1859 if (!GTK_WIDGET_HAS_FOCUS (widget))
1860 gtk_widget_grab_focus (widget);
1862 if (event->button == 1)
1864 switch (event->type)
1866 case GDK_BUTTON_PRESS:
1867 gtk_grab_add (widget);
1869 undraw_cursor (text, FALSE);
1870 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1871 draw_cursor (text, FALSE);
1873 /* Set it now, so we display things right. We'll unset it
1874 * later if things don't work out */
1875 editable->has_selection = TRUE;
1876 gtk_stext_set_selection (GTK_EDITABLE(text),
1877 text->cursor_mark.index,
1878 text->cursor_mark.index);
1882 case GDK_2BUTTON_PRESS:
1883 gtk_stext_select_word (text, event->time);
1886 case GDK_3BUTTON_PRESS:
1887 gtk_stext_select_line (text, event->time);
1894 else if (event->type == GDK_BUTTON_PRESS)
1896 if ((event->button == 2) && editable->editable)
1898 if (editable->selection_start_pos == editable->selection_end_pos ||
1899 editable->has_selection)
1901 undraw_cursor (text, FALSE);
1902 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1903 draw_cursor (text, FALSE);
1907 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1908 ctext_atom, event->time);
1912 gtk_grab_add (widget);
1914 undraw_cursor (text, FALSE);
1915 find_mouse_cursor (text, event->x, event->y);
1916 draw_cursor (text, FALSE);
1918 gtk_stext_set_selection (GTK_EDITABLE(text),
1919 text->cursor_mark.index,
1920 text->cursor_mark.index);
1922 editable->has_selection = FALSE;
1923 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1924 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1932 gtk_stext_button_release (GtkWidget *widget,
1933 GdkEventButton *event)
1936 GtkEditable *editable;
1937 g_return_val_if_fail (widget != NULL, FALSE);
1938 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1939 g_return_val_if_fail (event != NULL, FALSE);
1941 text = GTK_STEXT (widget);
1943 gtk_grab_remove (widget);
1945 if (text->button != event->button)
1952 gtk_timeout_remove (text->timer);
1956 if (event->button == 1)
1958 text = GTK_STEXT (widget);
1959 editable = GTK_EDITABLE (widget);
1961 gtk_grab_remove (widget);
1963 editable->has_selection = FALSE;
1964 if (editable->selection_start_pos != editable->selection_end_pos)
1966 if (gtk_selection_owner_set (widget,
1967 GDK_SELECTION_PRIMARY,
1969 editable->has_selection = TRUE;
1971 gtk_stext_update_text (editable, editable->selection_start_pos,
1972 editable->selection_end_pos);
1976 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1977 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1980 else if (event->button == 3)
1982 gtk_grab_remove (widget);
1985 undraw_cursor (text, FALSE);
1986 find_cursor (text, TRUE);
1987 draw_cursor (text, FALSE);
1993 gtk_stext_motion_notify (GtkWidget *widget,
1994 GdkEventMotion *event)
1999 GdkModifierType mask;
2001 g_return_val_if_fail (widget != NULL, FALSE);
2002 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2003 g_return_val_if_fail (event != NULL, FALSE);
2005 text = GTK_STEXT (widget);
2009 mask = event->state;
2010 if (event->is_hint || (text->text_area != event->window))
2012 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
2015 if ((text->button == 0) ||
2016 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
2019 gdk_window_get_size (text->text_area, NULL, &height);
2021 if ((y < 0) || (y > height))
2023 if (text->timer == 0)
2025 text->timer = gtk_timeout_add (SCROLL_TIME,
2026 gtk_stext_scroll_timeout,
2030 scroll_int (text, y/2);
2032 scroll_int (text, (y - height)/2);
2038 undraw_cursor (GTK_STEXT (widget), FALSE);
2039 find_mouse_cursor (GTK_STEXT (widget), x, y);
2040 draw_cursor (GTK_STEXT (widget), FALSE);
2042 gtk_stext_set_selection (GTK_EDITABLE(text),
2043 GTK_EDITABLE(text)->selection_start_pos,
2044 text->cursor_mark.index);
2050 gtk_stext_insert_text (GtkEditable *editable,
2051 const gchar *new_text,
2052 gint new_text_length,
2055 GtkSText *text = GTK_STEXT (editable);
2057 GdkColor *fore, *back;
2059 TextProperty *property;
2061 gtk_stext_set_point (text, *position);
2063 property = MARK_CURRENT_PROPERTY (&text->point);
2064 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
2065 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
2066 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
2068 gtk_stext_insert (text, font, fore, back, new_text, new_text_length);
2070 *position = text->point.index;
2074 gtk_stext_delete_text (GtkEditable *editable,
2080 g_return_if_fail (start_pos >= 0);
2082 text = GTK_STEXT (editable);
2084 gtk_stext_set_point (text, start_pos);
2086 end_pos = TEXT_LENGTH (text);
2088 if (end_pos > start_pos)
2089 gtk_stext_forward_delete (text, end_pos - start_pos);
2093 gtk_stext_key_press (GtkWidget *widget,
2097 GtkEditable *editable;
2102 g_return_val_if_fail (widget != NULL, FALSE);
2103 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2104 g_return_val_if_fail (event != NULL, FALSE);
2108 text = GTK_STEXT (widget);
2109 editable = GTK_EDITABLE (widget);
2111 key = event->keyval;
2114 if ((GTK_EDITABLE(text)->editable == FALSE))
2116 switch (event->keyval)
2119 if (event->state & GDK_CONTROL_MASK)
2120 scroll_int (text, -text->vadj->value);
2125 if (event->state & GDK_CONTROL_MASK)
2126 scroll_int (text, +text->vadj->upper);
2130 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
2131 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
2133 scroll_int (text, -KEY_SCROLL_PIXELS);
2135 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
2137 if (event->state & GDK_CONTROL_MASK)
2138 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2149 gint extend_selection;
2151 guint initial_pos = editable->current_pos;
2153 text->point = find_mark (text, text->cursor_mark.index);
2155 extend_selection = event->state & GDK_SHIFT_MASK;
2156 extend_start = FALSE;
2158 if (extend_selection)
2160 editable->has_selection = TRUE;
2162 if (editable->selection_start_pos == editable->selection_end_pos)
2164 editable->selection_start_pos = text->point.index;
2165 editable->selection_end_pos = text->point.index;
2168 extend_start = (text->point.index == editable->selection_start_pos);
2169 gtk_stext_disable_blink(text);
2175 if (!extend_selection)
2176 gtk_stext_enable_blink(text);
2178 if (event->keyval != GDK_Up && event->keyval != GDK_Down) {
2179 reset_persist_col_pos(text);
2182 switch (event->keyval)
2185 if (event->state & GDK_CONTROL_MASK)
2186 move_cursor_buffer_ver (text, -1);
2188 if (text->wrap_rmargin > 0) {
2189 /* SYLPHEED: line start */
2190 move_cursor_to_display_row_start(text);
2193 gtk_stext_move_beginning_of_line (text);
2198 if (event->state & GDK_CONTROL_MASK)
2199 move_cursor_buffer_ver (text, +1);
2201 if (text->wrap_rmargin > 0) {
2202 /* SYLPHEED: line end */
2203 move_cursor_to_display_row_end(text);
2206 gtk_stext_move_end_of_line(text);
2211 move_cursor_page_ver (text, -1);
2213 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
2214 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
2216 if (text->wrap_rmargin > 0) {
2219 move_cursor_to_display_row_up(text);
2222 move_cursor_ver (text, -1);
2226 move_cursor_to_display_row_down(text);
2227 // move_cursor_ver (text, +1);
2230 if (event->state & GDK_CONTROL_MASK)
2231 gtk_stext_move_backward_word (text);
2233 move_cursor_hor (text, -1);
2236 if (event->state & GDK_CONTROL_MASK)
2237 gtk_stext_move_forward_word (text);
2239 move_cursor_hor (text, +1);
2243 if (event->state & GDK_CONTROL_MASK)
2244 gtk_stext_delete_backward_word (text);
2246 gtk_stext_delete_backward_character (text);
2249 gtk_stext_delete_line (text);
2252 if (event->state & GDK_SHIFT_MASK)
2254 extend_selection = FALSE;
2255 gtk_editable_paste_clipboard (editable);
2257 else if (event->state & GDK_CONTROL_MASK)
2259 gtk_editable_copy_clipboard (editable);
2263 /* gtk_toggle_insert(text) -- IMPLEMENT */
2267 if (event->state & GDK_CONTROL_MASK)
2268 gtk_stext_delete_forward_word (text);
2269 else if (event->state & GDK_SHIFT_MASK)
2271 extend_selection = FALSE;
2272 gtk_editable_cut_clipboard (editable);
2275 gtk_stext_delete_forward_character (text);
2278 position = text->point.index;
2279 gtk_editable_insert_text (editable, "\t", 1, &position);
2282 if (event->state & GDK_CONTROL_MASK)
2283 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2286 position = text->point.index;
2287 gtk_editable_insert_text (editable, "\n", 1, &position);
2291 /* Don't insert literally */
2298 if (event->state & GDK_CONTROL_MASK)
2300 if ((key >= 'A') && (key <= 'Z'))
2303 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2305 (* control_keys[(int) (key - 'a')]) (editable, event->time);
2311 else if (event->state & GDK_MOD1_MASK)
2313 if ((key >= 'A') && (key <= 'Z'))
2316 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2318 (* alt_keys[(int) (key - 'a')]) (editable, event->time);
2324 else if (event->length > 0)
2326 extend_selection = FALSE;
2328 gtk_editable_delete_selection (editable);
2329 position = text->point.index;
2330 gtk_editable_insert_text (editable, event->string, event->length, &position);
2338 if (return_val && (editable->current_pos != initial_pos))
2340 if (extend_selection)
2342 if (editable->current_pos < editable->selection_start_pos)
2343 gtk_stext_set_selection (editable, editable->current_pos,
2344 editable->selection_end_pos);
2345 else if (editable->current_pos > editable->selection_end_pos)
2346 gtk_stext_set_selection (editable, editable->selection_start_pos,
2347 editable->current_pos);
2351 gtk_stext_set_selection (editable, editable->current_pos,
2352 editable->selection_end_pos);
2354 gtk_stext_set_selection (editable, editable->selection_start_pos,
2355 editable->current_pos);
2359 gtk_stext_set_selection (editable, 0, 0);
2361 gtk_editable_claim_selection (editable,
2362 editable->selection_start_pos != editable->selection_end_pos,
2371 gtk_stext_focus_in (GtkWidget *widget,
2372 GdkEventFocus *event)
2374 g_return_val_if_fail (widget != NULL, FALSE);
2375 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2376 g_return_val_if_fail (event != NULL, FALSE);
2378 TDEBUG (("in gtk_stext_focus_in\n"));
2380 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
2381 gtk_widget_draw_focus (widget);
2383 #ifdef USE_GTKGDK_XIM
2384 if (GTK_EDITABLE(widget)->ic)
2385 gdk_im_begin (GTK_EDITABLE(widget)->ic, GTK_STEXT(widget)->text_area);
2388 draw_cursor (GTK_STEXT(widget), TRUE);
2389 GTK_STEXT(widget)->cursor_visible = TRUE;
2390 gtk_stext_enable_blink(GTK_STEXT(widget));
2396 gtk_stext_focus_out (GtkWidget *widget,
2397 GdkEventFocus *event)
2399 g_return_val_if_fail (widget != NULL, FALSE);
2400 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2401 g_return_val_if_fail (event != NULL, FALSE);
2403 TDEBUG (("in gtk_stext_focus_out\n"));
2405 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
2406 gtk_widget_draw_focus (widget);
2409 * should disable blink before undrawing the cursor - disabling blink
2410 * redraws the cursor.
2412 gtk_stext_disable_blink(GTK_STEXT(widget));
2413 undraw_cursor (GTK_STEXT(widget), TRUE);
2414 GTK_STEXT(widget)->cursor_visible = FALSE;
2416 #ifdef USE_GTKGDK_XIM
2424 gtk_stext_adjustment (GtkAdjustment *adjustment,
2429 g_return_if_fail (adjustment != NULL);
2430 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2431 g_return_if_fail (text != NULL);
2432 g_return_if_fail (GTK_IS_STEXT (text));
2434 /* Clamp the value here, because we'll get really confused
2435 * if someone tries to move the adjusment outside of the
2438 old_val = adjustment->value;
2440 adjustment->value = MIN (adjustment->value, adjustment->upper - adjustment->page_size);
2441 adjustment->value = MAX (adjustment->value, 0.0);
2443 if (adjustment->value != old_val)
2445 gtk_signal_handler_block_by_func (GTK_OBJECT (adjustment),
2446 GTK_SIGNAL_FUNC (gtk_stext_adjustment),
2448 gtk_adjustment_changed (adjustment);
2449 gtk_signal_handler_unblock_by_func (GTK_OBJECT (adjustment),
2450 GTK_SIGNAL_FUNC (gtk_stext_adjustment),
2454 /* Just ignore it if we haven't been size-allocated and realized yet */
2455 if (text->line_start_cache == NULL)
2458 if (adjustment == text->hadj)
2460 g_warning ("horizontal scrolling not implemented");
2464 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2468 undraw_cursor (text, FALSE);
2471 scroll_down (text, diff);
2472 else /* if (diff < 0) */
2473 scroll_up (text, diff);
2475 draw_cursor (text, FALSE);
2477 text->last_ver_value = adjustment->value;
2483 gtk_stext_disconnect (GtkAdjustment *adjustment,
2486 g_return_if_fail (adjustment != NULL);
2487 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2488 g_return_if_fail (text != NULL);
2489 g_return_if_fail (GTK_IS_STEXT (text));
2491 if (adjustment == text->hadj)
2492 gtk_stext_set_adjustments (text, NULL, text->vadj);
2493 if (adjustment == text->vadj)
2494 gtk_stext_set_adjustments (text, text->hadj, NULL);
2498 static GtkSPropertyMark
2499 find_this_line_start_mark (GtkSText* text, guint point_position, const GtkSPropertyMark* near)
2501 GtkSPropertyMark mark;
2503 mark = find_mark_near (text, point_position, near);
2505 while (mark.index > 0 &&
2506 GTK_STEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2507 decrement_mark (&mark);
2513 init_tab_cont (GtkSText* text, PrevTabCont* tab_cont)
2515 tab_cont->pixel_offset = 0;
2516 tab_cont->tab_start.tab_stops = text->tab_stops;
2517 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2519 if (!tab_cont->tab_start.to_next_tab)
2520 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2524 line_params_iterate (GtkSText* text,
2525 const GtkSPropertyMark* mark0,
2526 const PrevTabCont* tab_mark0,
2529 LineIteratorFunction iter)
2530 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2531 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2532 * for each line following MARK, iteration continues unless ITER_CALL
2535 GtkSPropertyMark mark = *mark0;
2536 PrevTabCont tab_conts[2];
2537 LineParams *lp, lpbuf;
2538 gint tab_cont_index = 0;
2541 tab_conts[0] = *tab_mark0;
2543 init_tab_cont (text, tab_conts);
2548 lp = g_chunk_new (LineParams, params_mem_chunk);
2552 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2553 tab_conts + (tab_cont_index + 1) % 2);
2555 if ((*iter) (text, lp, data))
2558 if (LAST_INDEX (text, lp->end))
2562 advance_mark (&mark);
2563 tab_cont_index = (tab_cont_index + 1) % 2;
2568 fetch_lines_iterator (GtkSText* text, LineParams* lp, void* data)
2570 FetchLinesData *fldata = (FetchLinesData*) data;
2572 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2574 switch (fldata->fl_type)
2576 case FetchLinesCount:
2577 if (!text->line_wrap || !lp->wraps)
2580 if (fldata->data >= fldata->data_max)
2584 case FetchLinesPixels:
2586 fldata->data += LINE_HEIGHT(*lp);
2588 if (fldata->data >= fldata->data_max)
2598 fetch_lines (GtkSText* text,
2599 const GtkSPropertyMark* mark0,
2600 const PrevTabCont* tab_cont0,
2604 FetchLinesData fl_data;
2606 fl_data.new_lines = NULL;
2608 fl_data.data_max = data;
2609 fl_data.fl_type = fl_type;
2611 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2613 return g_list_reverse (fl_data.new_lines);
2617 fetch_lines_backward (GtkSText* text)
2619 GList* new_lines = NULL, *new_line_start;
2620 GtkSPropertyMark mark;
2622 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2625 mark = find_this_line_start_mark (text,
2626 CACHE_DATA(text->line_start_cache).start.index - 1,
2627 &CACHE_DATA(text->line_start_cache).start);
2629 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2631 while (new_line_start->next)
2632 new_line_start = new_line_start->next;
2634 new_line_start->next = text->line_start_cache;
2635 text->line_start_cache->prev = new_line_start;
2639 fetch_lines_forward (GtkSText* text, gint line_count)
2641 GtkSPropertyMark mark;
2642 GList* line = text->line_start_cache;
2647 mark = CACHE_DATA(line).end;
2649 if (LAST_INDEX (text, mark))
2652 advance_mark(&mark);
2654 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2657 line->next->prev = line;
2660 /* Compute the number of lines, and vertical pixels for n characters
2661 * starting from the point
2664 compute_lines_pixels (GtkSText* text, guint char_count,
2665 guint *lines, guint *pixels)
2667 GList *line = text->current_line;
2668 gint chars_left = char_count;
2673 /* If chars_left == 0, that means we're joining two lines in a
2674 * deletion, so add in the values for the next line as well
2676 for (; line && chars_left >= 0; line = line->next)
2678 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2680 if (line == text->current_line)
2681 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2683 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2685 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2689 chars_left = 0; /* force another loop */
2692 fetch_lines_forward (text, 1);
2697 total_line_height (GtkSText* text, GList* line, gint line_count)
2701 for (; line && line_count > 0; line = line->next)
2703 height += LINE_HEIGHT(CACHE_DATA(line));
2705 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2709 fetch_lines_forward (text, line_count);
2716 swap_lines (GtkSText* text, GList* old, GList* new, guint old_line_count)
2718 if (old == text->line_start_cache)
2722 for (; old_line_count > 0; old_line_count -= 1)
2724 while (text->line_start_cache &&
2726 CACHE_DATA(text->line_start_cache).wraps)
2727 remove_cache_line(text, text->line_start_cache);
2729 remove_cache_line(text, text->line_start_cache);
2732 last = g_list_last (new);
2734 last->next = text->line_start_cache;
2736 if (text->line_start_cache)
2737 text->line_start_cache->prev = last;
2739 text->line_start_cache = new;
2745 g_assert (old->prev);
2749 for (; old_line_count > 0; old_line_count -= 1)
2751 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2752 old = remove_cache_line (text, old);
2754 old = remove_cache_line (text, old);
2760 last = g_list_last (new);
2770 correct_cache_delete (GtkSText* text, gint nchars, gint lines)
2772 GList* cache = text->current_line;
2775 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2778 for (; cache; cache = cache->next)
2780 GtkSPropertyMark *start = &CACHE_DATA(cache).start;
2781 GtkSPropertyMark *end = &CACHE_DATA(cache).end;
2783 start->index -= nchars;
2784 end->index -= nchars;
2786 if (LAST_INDEX (text, text->point) &&
2787 start->index == text->point.index)
2788 *start = text->point;
2789 else if (start->property == text->point.property)
2790 start->offset = start->index - (text->point.index - text->point.offset);
2792 if (LAST_INDEX (text, text->point) &&
2793 end->index == text->point.index)
2795 if (end->property == text->point.property)
2796 end->offset = end->index - (text->point.index - text->point.offset);
2798 /*TEXT_ASSERT_MARK(text, start, "start");*/
2799 /*TEXT_ASSERT_MARK(text, end, "end");*/
2804 delete_expose (GtkSText* text, guint nchars, guint old_lines, guint old_pixels)
2806 GtkWidget *widget = GTK_WIDGET (text);
2809 guint new_pixels = 0;
2811 GList* new_line = NULL;
2814 text->cursor_virtual_x = 0;
2816 correct_cache_delete (text, nchars, old_lines);
2818 pixel_height = pixel_height_of(text, text->current_line) -
2819 LINE_HEIGHT(CACHE_DATA(text->current_line));
2821 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2822 CACHE_DATA(text->current_line).start = text->point;
2824 new_line = fetch_lines (text,
2825 &CACHE_DATA(text->current_line).start,
2826 &CACHE_DATA(text->current_line).tab_cont,
2830 swap_lines (text, text->current_line, new_line, old_lines);
2832 text->current_line = new_line;
2834 new_pixels = total_line_height (text, new_line, 1);
2836 gdk_window_get_size (text->text_area, &width, &height);
2838 if (old_pixels != new_pixels)
2840 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2842 gdk_draw_pixmap (text->text_area,
2846 pixel_height + old_pixels,
2848 pixel_height + new_pixels,
2852 text->vadj->upper += new_pixels;
2853 text->vadj->upper -= old_pixels;
2854 adjust_adj (text, text->vadj);
2858 rect.y = pixel_height;
2860 rect.height = new_pixels;
2862 expose_text (text, &rect, FALSE);
2863 gtk_stext_draw_focus ( (GtkWidget *) text);
2865 text->cursor_mark = text->point;
2867 find_cursor (text, TRUE);
2869 if (old_pixels != new_pixels)
2871 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2874 rect.y = pixel_height + new_pixels;
2876 rect.height = height - rect.y;
2878 expose_text (text, &rect, FALSE);
2881 process_exposes (text);
2888 /* note, the point has already been moved forward */
2890 correct_cache_insert (GtkSText* text, gint nchars)
2893 GtkSPropertyMark *start;
2894 GtkSPropertyMark *end;
2896 /* If we inserted a property exactly at the beginning of the
2897 * line, we have to correct here, or fetch_lines will
2900 start = &CACHE_DATA(text->current_line).start;
2901 if (start->index == text->point.index - nchars)
2903 *start = text->point;
2904 move_mark_n (start, -nchars);
2907 /* Now correct the offsets, and check for start or end marks that
2908 * are after the point, yet point to a property before the point's
2909 * property. This indicates that they are meant to point to the
2910 * second half of a property we split in insert_text_property(), so
2911 * we fix them up that way.
2913 cache = text->current_line->next;
2915 for (; cache; cache = cache->next)
2917 start = &CACHE_DATA(cache).start;
2918 end = &CACHE_DATA(cache).end;
2920 if (LAST_INDEX (text, text->point) &&
2921 start->index == text->point.index)
2922 *start = text->point;
2925 if (start->property == text->point.property)
2927 start->offset += nchars;
2928 start->index += nchars;
2930 else if (start->property->next &&
2931 (start->property->next->next == text->point.property))
2933 /* We split the property, and this is the second half */
2934 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2935 start->index += nchars;
2936 start->property = text->point.property;
2939 start->index += nchars;
2942 if (LAST_INDEX (text, text->point) &&
2943 end->index == text->point.index)
2947 if (end->property == text->point.property)
2949 end->offset += nchars;
2950 end->index += nchars;
2952 else if (end->property->next &&
2953 (end->property->next->next == text->point.property))
2955 /* We split the property, and this is the second half */
2956 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2957 end->index += nchars;
2958 end->property = text->point.property;
2961 end->index += nchars;
2964 /*TEXT_ASSERT_MARK(text, start, "start");*/
2965 /*TEXT_ASSERT_MARK(text, end, "end");*/
2971 insert_expose (GtkSText* text, guint old_pixels, gint nchars,
2972 guint new_line_count)
2974 GtkWidget *widget = GTK_WIDGET (text);
2977 guint new_pixels = 0;
2979 GList* new_lines = NULL;
2982 text->cursor_virtual_x = 0;
2984 undraw_cursor (text, FALSE);
2986 correct_cache_insert (text, nchars);
2988 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2990 pixel_height = pixel_height_of(text, text->current_line) -
2991 LINE_HEIGHT(CACHE_DATA(text->current_line));
2993 new_lines = fetch_lines (text,
2994 &CACHE_DATA(text->current_line).start,
2995 &CACHE_DATA(text->current_line).tab_cont,
2999 swap_lines (text, text->current_line, new_lines, 1);
3001 text->current_line = new_lines;
3003 new_pixels = total_line_height (text, new_lines, new_line_count);
3005 gdk_window_get_size (text->text_area, &width, &height);
3007 if (old_pixels != new_pixels)
3009 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
3011 gdk_draw_pixmap (text->text_area,
3015 pixel_height + old_pixels,
3017 pixel_height + new_pixels,
3019 height + (old_pixels - new_pixels) - pixel_height);
3022 text->vadj->upper += new_pixels;
3023 text->vadj->upper -= old_pixels;
3024 adjust_adj (text, text->vadj);
3028 rect.y = pixel_height;
3030 rect.height = new_pixels;
3032 expose_text (text, &rect, FALSE);
3033 gtk_stext_draw_focus ( (GtkWidget *) text);
3035 text->cursor_mark = text->point;
3037 find_cursor (text, TRUE);
3039 draw_cursor (text, FALSE);
3041 if (old_pixels != new_pixels)
3043 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
3046 rect.y = pixel_height + new_pixels;
3048 rect.height = height - rect.y;
3050 expose_text (text, &rect, FALSE);
3053 process_exposes (text);
3056 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
3061 /* Text property functions */
3064 font_hash (gconstpointer font)
3066 return gdk_font_id ((const GdkFont*) font);
3069 static GHashTable *font_cache_table = NULL;
3071 static GtkSTextFont*
3072 get_text_font (GdkFont* gfont)
3077 if (!font_cache_table)
3078 font_cache_table = g_hash_table_new (font_hash, (GCompareFunc) gdk_font_equal);
3080 tf = g_hash_table_lookup (font_cache_table, gfont);
3088 tf = g_new (GtkSTextFont, 1);
3091 tf->gdk_font = gfont;
3092 gdk_font_ref (gfont);
3094 for(i = 0; i < 256; i += 1)
3095 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
3097 g_hash_table_insert (font_cache_table, gfont, tf);
3103 text_font_unref (GtkSTextFont *text_font)
3105 text_font->ref_count--;
3106 if (text_font->ref_count == 0)
3108 g_hash_table_remove (font_cache_table, text_font->gdk_font);
3109 gdk_font_unref (text_font->gdk_font);
3115 text_properties_equal (TextProperty* prop, GdkFont* font, GdkColor *fore, GdkColor *back)
3117 if (prop->flags & PROPERTY_FONT)
3120 GtkSTextFont *text_font;
3125 text_font = get_text_font (font);
3127 retval = (prop->font == text_font);
3128 text_font_unref (text_font);
3137 if (prop->flags & PROPERTY_FOREGROUND)
3139 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
3146 if (prop->flags & PROPERTY_BACKGROUND)
3148 if (!back || !gdk_color_equal (&prop->back_color, back))
3159 realize_property (GtkSText *text, TextProperty *prop)
3161 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3163 if (prop->flags & PROPERTY_FOREGROUND)
3164 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
3166 if (prop->flags & PROPERTY_BACKGROUND)
3167 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
3171 realize_properties (GtkSText *text)
3173 GList *tmp_list = text->text_properties;
3177 realize_property (text, tmp_list->data);
3179 tmp_list = tmp_list->next;
3184 unrealize_property (GtkSText *text, TextProperty *prop)
3186 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3188 if (prop->flags & PROPERTY_FOREGROUND)
3189 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
3191 if (prop->flags & PROPERTY_BACKGROUND)
3192 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
3196 unrealize_properties (GtkSText *text)
3198 GList *tmp_list = text->text_properties;
3202 unrealize_property (text, tmp_list->data);
3204 tmp_list = tmp_list->next;
3208 static TextProperty*
3209 new_text_property (GtkSText *text, GdkFont *font, GdkColor* fore,
3210 GdkColor* back, guint length)
3214 if (text_property_chunk == NULL)
3216 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
3217 sizeof(TextProperty),
3218 1024*sizeof(TextProperty),
3222 prop = g_chunk_new(TextProperty, text_property_chunk);
3227 prop->flags |= PROPERTY_FONT;
3228 prop->font = get_text_font (font);
3235 prop->flags |= PROPERTY_FOREGROUND;
3236 prop->fore_color = *fore;
3241 prop->flags |= PROPERTY_BACKGROUND;
3242 prop->back_color = *back;
3245 prop->length = length;
3247 if (GTK_WIDGET_REALIZED (text))
3248 realize_property (text, prop);
3254 destroy_text_property (TextProperty *prop)
3257 text_font_unref (prop->font);
3259 g_mem_chunk_free (text_property_chunk, prop);
3262 /* Flop the memory between the point and the gap around like a
3265 move_gap (GtkSText* text, guint index)
3267 if (text->gap_position < index)
3269 gint diff = index - text->gap_position;
3271 if (text->use_wchar)
3272 g_memmove (text->text.wc + text->gap_position,
3273 text->text.wc + text->gap_position + text->gap_size,
3274 diff*sizeof (GdkWChar));
3276 g_memmove (text->text.ch + text->gap_position,
3277 text->text.ch + text->gap_position + text->gap_size,
3280 text->gap_position = index;
3282 else if (text->gap_position > index)
3284 gint diff = text->gap_position - index;
3286 if (text->use_wchar)
3287 g_memmove (text->text.wc + index + text->gap_size,
3288 text->text.wc + index,
3289 diff*sizeof (GdkWChar));
3291 g_memmove (text->text.ch + index + text->gap_size,
3292 text->text.ch + index,
3295 text->gap_position = index;
3299 /* Increase the gap size. */
3301 make_forward_space (GtkSText* text, guint len)
3303 if (text->gap_size < len)
3305 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3307 if (sum >= text->text_len)
3311 while (i <= sum) i <<= 1;
3313 if (text->use_wchar)
3314 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3315 i*sizeof(GdkWChar));
3317 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3321 if (text->use_wchar)
3322 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3323 text->text.wc + text->gap_position + text->gap_size,
3324 (text->text_end - (text->gap_position + text->gap_size))
3327 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3328 text->text.ch + text->gap_position + text->gap_size,
3329 text->text_end - (text->gap_position + text->gap_size));
3331 text->text_end += len*2;
3332 text->gap_size += len*2;
3336 /* Inserts into the text property list a list element that guarantees
3337 * that for len characters following the point, text has the correct
3338 * property. does not move point. adjusts text_properties_point and
3339 * text_properties_point_offset relative to the current value of
3342 insert_text_property (GtkSText* text, GdkFont* font,
3343 GdkColor *fore, GdkColor* back, guint len)
3345 GtkSPropertyMark *mark = &text->point;
3346 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3347 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3349 if (MARK_OFFSET(mark) == 0)
3351 /* Point is on the boundary of two properties.
3352 * If it is the same as either, grow, else insert
3355 if (text_properties_equal(forward_prop, font, fore, back))
3357 /* Grow the property in front of us. */
3359 MARK_PROPERTY_LENGTH(mark) += len;
3361 else if (backward_prop &&
3362 text_properties_equal(backward_prop, font, fore, back))
3364 /* Grow property behind us, point property and offset
3367 SET_PROPERTY_MARK (&text->point,
3368 MARK_PREV_LIST_PTR (mark),
3369 backward_prop->length);
3371 backward_prop->length += len;
3373 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3374 (forward_prop->length == 1))
3376 /* Next property just has last position, take it over */
3378 if (GTK_WIDGET_REALIZED (text))
3379 unrealize_property (text, forward_prop);
3381 forward_prop->flags = 0;
3384 forward_prop->flags |= PROPERTY_FONT;
3385 forward_prop->font = get_text_font (font);
3388 forward_prop->font = NULL;
3392 forward_prop->flags |= PROPERTY_FOREGROUND;
3393 forward_prop->fore_color = *fore;
3397 forward_prop->flags |= PROPERTY_BACKGROUND;
3398 forward_prop->back_color = *back;
3400 forward_prop->length += len;
3402 if (GTK_WIDGET_REALIZED (text))
3403 realize_property (text, forward_prop);
3407 /* Splice a new property into the list. */
3409 GList* new_prop = g_list_alloc();
3411 new_prop->next = MARK_LIST_PTR(mark);
3412 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3413 new_prop->next->prev = new_prop;
3416 new_prop->prev->next = new_prop;
3418 new_prop->data = new_text_property (text, font, fore, back, len);
3420 SET_PROPERTY_MARK (mark, new_prop, 0);
3425 /* The following will screw up the line_start cache,
3426 * we'll fix it up in correct_cache_insert
3429 /* In the middle of forward_prop, if properties are equal,
3430 * just add to its length, else split it into two and splice
3432 if (text_properties_equal (forward_prop, font, fore, back))
3434 forward_prop->length += len;
3436 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3437 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3439 /* Inserting before only the last position in the text */
3442 forward_prop->length -= 1;
3444 new_prop = g_list_alloc();
3445 new_prop->data = new_text_property (text, font, fore, back, len+1);
3446 new_prop->prev = MARK_LIST_PTR(mark);
3447 new_prop->next = NULL;
3448 MARK_NEXT_LIST_PTR(mark) = new_prop;
3450 SET_PROPERTY_MARK (mark, new_prop, 0);
3454 GList* new_prop = g_list_alloc();
3455 GList* new_prop_forward = g_list_alloc();
3456 gint old_length = forward_prop->length;
3457 GList* next = MARK_NEXT_LIST_PTR(mark);
3459 /* Set the new lengths according to where they are split. Construct
3460 * two new properties. */
3461 forward_prop->length = MARK_OFFSET(mark);
3463 new_prop_forward->data =
3464 new_text_property(text,
3465 forward_prop->flags & PROPERTY_FONT ?
3466 forward_prop->font->gdk_font : NULL,
3467 forward_prop->flags & PROPERTY_FOREGROUND ?
3468 &forward_prop->fore_color : NULL,
3469 forward_prop->flags & PROPERTY_BACKGROUND ?
3470 &forward_prop->back_color : NULL,
3471 old_length - forward_prop->length);
3473 new_prop->data = new_text_property(text, font, fore, back, len);
3475 /* Now splice things in. */
3476 MARK_NEXT_LIST_PTR(mark) = new_prop;
3477 new_prop->prev = MARK_LIST_PTR(mark);
3479 new_prop->next = new_prop_forward;
3480 new_prop_forward->prev = new_prop;
3482 new_prop_forward->next = next;
3485 next->prev = new_prop_forward;
3487 SET_PROPERTY_MARK (mark, new_prop, 0);
3491 while (text->text_properties_end->next)
3492 text->text_properties_end = text->text_properties_end->next;
3494 while (text->text_properties->prev)
3495 text->text_properties = text->text_properties->prev;
3499 delete_text_property (GtkSText* text, guint nchars)
3501 /* Delete nchars forward from point. */
3503 /* Deleting text properties is problematical, because we
3504 * might be storing around marks pointing to a property.
3506 * The marks in question and how we handle them are:
3508 * point: We know the new value, since it will be at the
3509 * end of the deleted text, and we move it there
3511 * cursor: We just remove the mark and set it equal to the
3512 * point after the operation.
3513 * line-start cache: We replace most affected lines.
3514 * The current line gets used to fetch the new
3515 * lines so, if necessary, (delete at the beginning
3516 * of a line) we fix it up by setting it equal to the
3524 for(; nchars; nchars -= 1)
3526 prop = MARK_CURRENT_PROPERTY(&text->point);
3530 if (prop->length == 0)
3532 tmp = MARK_LIST_PTR (&text->point);
3534 is_first = tmp == text->text_properties;
3536 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3537 text->point.offset = 0;
3539 if (GTK_WIDGET_REALIZED (text))
3540 unrealize_property (text, prop);
3542 destroy_text_property (prop);
3543 g_list_free_1 (tmp);
3545 prop = MARK_CURRENT_PROPERTY (&text->point);
3548 text->text_properties = MARK_LIST_PTR (&text->point);
3550 g_assert (prop->length != 0);
3552 else if (prop->length == text->point.offset)
3554 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3555 text->point.offset = 0;
3559 /* Check to see if we have just the single final position remaining
3560 * along in a property; if so, combine it with the previous property
3562 if (LAST_INDEX (text, text->point) &&
3563 (MARK_OFFSET (&text->point) == 0) &&
3564 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3566 tmp = MARK_LIST_PTR (&text->point);
3567 prop = MARK_CURRENT_PROPERTY(&text->point);
3569 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3570 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3571 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3573 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3575 if (GTK_WIDGET_REALIZED (text))
3576 unrealize_property (text, prop);
3578 destroy_text_property (prop);
3579 g_list_free_1 (tmp);
3584 init_properties (GtkSText *text)
3586 if (!text->text_properties)
3588 text->text_properties = g_list_alloc();
3589 text->text_properties->next = NULL;
3590 text->text_properties->prev = NULL;
3591 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3592 text->text_properties_end = text->text_properties;
3594 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3596 text->point.index = 0;
3601 /**********************************************************************/
3602 /* Property Movement */
3603 /**********************************************************************/
3606 move_mark_n (GtkSPropertyMark* mark, gint n)
3609 advance_mark_n(mark, n);
3611 decrement_mark_n(mark, -n);
3615 advance_mark (GtkSPropertyMark* mark)
3617 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3621 if (prop->length > mark->offset + 1)
3625 mark->property = MARK_NEXT_LIST_PTR (mark);
3631 advance_mark_n (GtkSPropertyMark* mark, gint n)
3638 i = 0; /* otherwise it migth not be init. */
3639 prop = MARK_CURRENT_PROPERTY(mark);
3641 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3642 /* to make it easier */
3643 n += (mark->offset);
3644 mark->index -= mark->offset;
3646 /* first we take seven-mile-leaps to get to the right text
3648 while ((n-i) > prop->length - 1) {
3650 mark->index += prop->length;
3651 mark->property = MARK_NEXT_LIST_PTR (mark);
3652 prop = MARK_CURRENT_PROPERTY (mark);
3656 /* and then the rest */
3657 mark->index += n - i;
3658 mark->offset += n - i;
3662 decrement_mark (GtkSPropertyMark* mark)
3666 if (mark->offset > 0)
3670 mark->property = MARK_PREV_LIST_PTR (mark);
3671 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3676 decrement_mark_n (GtkSPropertyMark* mark, gint n)
3680 while (mark->offset < n) {
3681 /* jump to end of prev */
3682 n -= mark->offset + 1;
3683 mark->index -= mark->offset + 1;
3684 mark->property = MARK_PREV_LIST_PTR (mark);
3685 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3693 static GtkSPropertyMark
3694 find_mark (GtkSText* text, guint mark_position)
3696 return find_mark_near (text, mark_position, &text->point);
3700 * You can also start from the end, what a drag.
3702 static GtkSPropertyMark
3703 find_mark_near (GtkSText* text, guint mark_position, const GtkSPropertyMark* near)
3708 GtkSPropertyMark mark;
3711 diffa = mark_position + 1;
3713 diffa = mark_position - near->index;
3715 diffb = mark_position;
3727 mark.property = text->text_properties;
3731 move_mark_n (&mark, mark_position - mark.index);
3736 /* This routine must be called with scroll == FALSE, only when
3737 * point is at least partially on screen
3741 find_line_containing_point (GtkSText* text, guint point,
3747 text->current_line = NULL;
3751 /* Scroll backwards until the point is on screen
3753 while (CACHE_DATA(text->line_start_cache).start.index > point)
3754 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3756 /* Now additionally try to make sure that the point is fully on screen
3760 while (text->first_cut_pixels != 0 &&
3761 text->line_start_cache->next &&
3762 CACHE_DATA(text->line_start_cache->next).start.index > point)
3763 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3766 gdk_window_get_size (text->text_area, NULL, &height);
3768 for (cache = text->line_start_cache; cache; cache = cache->next)
3772 if (CACHE_DATA(cache).end.index >= point ||
3773 LAST_INDEX(text, CACHE_DATA(cache).end))
3775 text->current_line = cache; /* LOOK HERE, this proc has an
3776 * important side effect. */
3780 TEXT_SHOW_LINE (text, cache, "cache");
3782 if (cache->next == NULL)
3783 fetch_lines_forward (text, 1);
3787 lph = pixel_height_of (text, cache->next);
3789 /* Scroll the bottom of the line is on screen, or until
3790 * the line is the first onscreen line.
3792 while (cache->next != text->line_start_cache && lph > height)
3794 TEXT_SHOW_LINE (text, cache, "cache");
3795 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3796 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3797 lph = pixel_height_of (text, cache->next);
3802 g_assert_not_reached (); /* Must set text->current_line here */
3806 pixel_height_of (GtkSText* text, GList* cache_line)
3808 gint pixels = - text->first_cut_pixels;
3809 GList *cache = text->line_start_cache;
3812 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3814 if (cache->data == cache_line->data)
3817 cache = cache->next;
3823 /**********************************************************************/
3824 /* Search and Placement */
3825 /**********************************************************************/
3828 find_char_width (GtkSText* text, const GtkSPropertyMark *mark, const TabStopMark *tab_mark)
3831 gint16* char_widths;
3833 if (LAST_INDEX (text, *mark))
3836 ch = GTK_STEXT_INDEX (text, mark->index);
3837 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3841 return tab_mark->to_next_tab * char_widths[' '];
3843 else if (!text->use_wchar)
3845 return char_widths[ch];
3849 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3854 advance_tab_mark (GtkSText* text, TabStopMark* tab_mark, GdkWChar ch)
3856 if (tab_mark->to_next_tab == 1 || ch == '\t')
3858 if (tab_mark->tab_stops->next)
3860 tab_mark->tab_stops = tab_mark->tab_stops->next;
3861 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3865 tab_mark->to_next_tab = text->default_tab_width;
3870 tab_mark->to_next_tab -= 1;
3875 advance_tab_mark_n (GtkSText* text, TabStopMark* tab_mark, gint n)
3879 advance_tab_mark (text, tab_mark, 0);
3883 find_cursor_at_line (GtkSText* text, const LineParams* start_line, gint pixel_height)
3886 GtkEditable *editable = (GtkEditable *)text;
3888 GtkSPropertyMark mark = start_line->start;
3889 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3890 gint pixel_width = LINE_START_PIXEL (*start_line);
3892 while (mark.index < text->cursor_mark.index)
3894 pixel_width += find_char_width (text, &mark, &tab_mark);
3896 advance_tab_mark (text, &tab_mark, GTK_STEXT_INDEX(text, mark.index));
3897 advance_mark (&mark);
3900 text->cursor_pos_x = pixel_width;
3901 text->cursor_pos_y = pixel_height;
3902 text->cursor_char_offset = start_line->font_descent;
3903 text->cursor_mark = mark;
3905 ch = LAST_INDEX (text, mark) ?
3906 LINE_DELIM : GTK_STEXT_INDEX (text, mark.index);
3908 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3909 text->cursor_char = 0;
3911 text->cursor_char = ch;
3913 #ifdef USE_GTKGDK_XIM
3914 if (GTK_WIDGET_HAS_FOCUS(text) && gdk_im_ready() && editable->ic &&
3915 (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
3917 GdkICAttributesType mask = GDK_IC_SPOT_LOCATION |
3918 GDK_IC_PREEDIT_FOREGROUND |
3919 GDK_IC_PREEDIT_BACKGROUND;
3921 editable->ic_attr->spot_location.x = text->cursor_pos_x;
3922 editable->ic_attr->spot_location.y
3923 = text->cursor_pos_y - text->cursor_char_offset;
3924 editable->ic_attr->preedit_foreground = *MARK_CURRENT_FORE (text, &mark);
3925 editable->ic_attr->preedit_background = *MARK_CURRENT_BACK (text, &mark);
3927 if (MARK_CURRENT_FONT (text, &mark)->type == GDK_FONT_FONTSET)
3929 mask |= GDK_IC_PREEDIT_FONTSET;
3930 editable->ic_attr->preedit_fontset = MARK_CURRENT_FONT (text, &mark);
3933 gdk_ic_set_attr (editable->ic, editable->ic_attr, mask);
3939 find_cursor (GtkSText* text, gboolean scroll)
3941 if (GTK_WIDGET_REALIZED (text))
3943 find_line_containing_point (text, text->cursor_mark.index, scroll);
3945 if (text->current_line)
3946 find_cursor_at_line (text,
3947 &CACHE_DATA(text->current_line),
3948 pixel_height_of(text, text->current_line));
3951 GTK_EDITABLE (text)->current_pos = text->cursor_mark.index;
3955 find_mouse_cursor_at_line (GtkSText *text, const LineParams* lp,
3956 guint line_pixel_height,
3959 GtkSPropertyMark mark = lp->start;
3960 TabStopMark tab_mark = lp->tab_cont.tab_start;
3962 gint char_width = find_char_width(text, &mark, &tab_mark);
3963 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3965 text->cursor_pos_y = line_pixel_height;
3969 GdkWChar ch = LAST_INDEX (text, mark) ?
3970 LINE_DELIM : GTK_STEXT_INDEX (text, mark.index);
3972 if (button_x < pixel_width || mark.index == lp->end.index)
3974 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3975 text->cursor_mark = mark;
3976 text->cursor_char_offset = lp->font_descent;
3978 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3979 text->cursor_char = 0;
3981 text->cursor_char = ch;
3986 advance_tab_mark (text, &tab_mark, ch);
3987 advance_mark (&mark);
3989 pixel_width += char_width/2;
3991 char_width = find_char_width (text, &mark, &tab_mark);
3993 pixel_width += (char_width+1)/2;
3998 find_mouse_cursor (GtkSText* text, gint x, gint y)
4001 GList* cache = text->line_start_cache;
4005 pixel_height = - text->first_cut_pixels;
4007 for (; cache; cache = cache->next)
4009 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
4011 if (y < pixel_height || !cache->next)
4013 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
4015 find_cursor (text, FALSE);
4022 /**********************************************************************/
4024 /**********************************************************************/
4027 free_cache (GtkSText* text)
4029 GList* cache = text->line_start_cache;
4034 cache = cache->prev;
4036 text->line_start_cache = cache;
4039 for (; cache; cache = cache->next)
4040 g_mem_chunk_free (params_mem_chunk, cache->data);
4042 g_list_free (text->line_start_cache);
4044 text->line_start_cache = NULL;
4048 remove_cache_line (GtkSText* text, GList* member)
4055 if (member == text->line_start_cache)
4056 text->line_start_cache = text->line_start_cache->next;
4059 member->prev->next = member->next;
4062 member->next->prev = member->prev;
4064 list = member->next;
4066 g_mem_chunk_free (params_mem_chunk, member->data);
4067 g_list_free_1 (member);
4072 /**********************************************************************/
4074 /**********************************************************************/
4077 move_cursor_buffer_ver (GtkSText *text, int dir)
4079 undraw_cursor (text, FALSE);
4083 scroll_int (text, text->vadj->upper);
4084 text->cursor_mark = find_this_line_start_mark (text,
4086 &text->cursor_mark);
4090 scroll_int (text, - text->vadj->value);
4091 text->cursor_mark = find_this_line_start_mark (text,
4093 &text->cursor_mark);
4096 find_cursor (text, TRUE);
4097 draw_cursor (text, FALSE);
4101 move_cursor_page_ver (GtkSText *text, int dir)
4103 scroll_int (text, dir * text->vadj->page_increment);
4107 move_cursor_ver (GtkSText *text, int count)
4110 GtkSPropertyMark mark;
4113 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
4114 offset = text->cursor_mark.index - mark.index;
4116 if (offset > text->cursor_virtual_x)
4117 text->cursor_virtual_x = offset;
4121 if (mark.index == 0)
4124 decrement_mark (&mark);
4125 mark = find_this_line_start_mark (text, mark.index, &mark);
4129 mark = text->cursor_mark;
4131 while (!LAST_INDEX(text, mark) && GTK_STEXT_INDEX(text, mark.index) != LINE_DELIM)
4132 advance_mark (&mark);
4134 if (LAST_INDEX(text, mark))
4137 advance_mark (&mark);
4140 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
4141 if (LAST_INDEX(text, mark) ||
4142 GTK_STEXT_INDEX(text, mark.index) == LINE_DELIM)
4145 undraw_cursor (text, FALSE);
4147 text->cursor_mark = mark;
4149 find_cursor (text, TRUE);
4151 draw_cursor (text, FALSE);
4155 move_cursor_hor (GtkSText *text, int count)
4157 /* count should be +-1. */
4158 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
4159 (count < 0 && text->cursor_mark.index < (- count)) ||
4163 text->cursor_virtual_x = 0;
4165 undraw_cursor (text, FALSE);
4167 move_mark_n (&text->cursor_mark, count);
4169 find_cursor (text, TRUE);
4171 draw_cursor (text, FALSE);
4174 /* SYLPHEED - the default cursor movement of GtkText is aeons ahead of the
4175 * current technology level of the current generation of programmers and
4176 * end users. so sylpheed has to take a more mundane approach to editing.
4179 * move_cursor_to_display_row_end() (end of display line)
4180 * move_cursor_to_display_row_home() (home of display line)
4181 * move_cursor_to_display_row_up() (line up)
4182 * move_cursor_to_display_row_down() (line down)
4186 static void move_cursor_to_display_row_end(GtkSText *text)
4188 LineParams lp = CACHE_DATA(text->current_line);
4189 int to_move = (lp.end.index - text->cursor_mark.index);
4191 /* advance this much */
4193 move_cursor_hor(text, to_move);
4197 static void move_cursor_to_display_row_start(GtkSText *text)
4199 LineParams lp = CACHE_DATA(text->current_line);
4200 int to_move = (text->cursor_mark.index - lp.start.index);
4202 move_cursor_hor(text, -to_move);
4212 static gint back_display_row_fetcher(GtkSText *text,
4216 if (data->start == params->start.index
4217 && data->end == params->end.index) {
4227 static void move_cursor_to_display_row_up(GtkSText *text)
4233 GtkSPropertyMark mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
4236 if (mark.index == 0) {
4240 /* we need to previous DISPLAY row not the previous BUFFER line, so we go one line back,
4241 * and iterate over the lines until we have a LineParams that matches the original */
4243 lp = CACHE_DATA(text->current_line);
4244 col = (text->cursor_mark.index - lp.start.index);
4245 data.start = lp.start.index;
4246 data.end = lp.end.index;
4248 /* get the previous line */
4249 if (mark.index - 1 > 0) {
4250 decrement_mark(&mark);
4251 if (mark.index - 1 > 0) {
4252 mark = find_this_line_start_mark(text, mark.index -1, &mark);
4256 /* let's get the previous display line */
4257 line_params_iterate(text, &mark, NULL, FALSE, &data,
4258 (LineIteratorFunction)back_display_row_fetcher);
4261 if (col < text->persist_column) col = text->persist_column;
4262 else text->persist_column = col;
4263 new_index = data.lp.start.index + col;
4264 if (new_index > data.lp.end.index) {
4265 new_index = data.lp.end.index;
4267 /* and move the cursor */
4268 gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);
4279 static gint forward_display_row_fetcher(GtkSText *text,
4285 data->completed = TRUE;
4288 else if (data->start == lp->start.index && data->end == lp->end.index) {
4297 static void move_cursor_to_display_row_down (GtkSText *text)
4302 GtkSPropertyMark mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
4303 fdrf data = { FALSE, FALSE };
4305 lp = CACHE_DATA(text->current_line);
4306 col = (text->cursor_mark.index - lp.start.index);
4308 data.start = lp.start.index;
4309 data.end = lp.end.index;
4311 /* find the next DISPLAY line */
4312 line_params_iterate(text, &mark, NULL, FALSE, &data,
4313 (LineIteratorFunction)forward_display_row_fetcher);
4315 if (data.completed) {
4316 if (col < text->persist_column) col = text->persist_column;
4317 else text->persist_column = col;
4318 new_index = data.lp.start.index + col;
4319 if (new_index > data.lp.end.index) {
4320 new_index = data.lp.end.index;
4322 /* and move the cursor */
4323 gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);
4327 static void reset_persist_col_pos(GtkSText *text)
4329 text->persist_column = 0;
4333 gtk_stext_move_cursor (GtkEditable *editable,
4340 move_cursor_hor (GTK_STEXT (editable), 1);
4345 move_cursor_hor (GTK_STEXT (editable), -1);
4351 move_cursor_ver (GTK_STEXT (editable), 1);
4356 move_cursor_ver (GTK_STEXT (editable), -1);
4361 gtk_stext_move_forward_character (GtkSText *text)
4363 move_cursor_hor (text, 1);
4367 gtk_stext_move_backward_character (GtkSText *text)
4369 move_cursor_hor (text, -1);
4373 gtk_stext_move_next_line (GtkSText *text)
4375 move_cursor_ver (text, 1);
4379 gtk_stext_move_previous_line (GtkSText *text)
4381 move_cursor_ver (text, -1);
4385 gtk_stext_move_word (GtkEditable *editable,
4391 gtk_stext_move_forward_word (GTK_STEXT (editable));
4396 gtk_stext_move_backward_word (GTK_STEXT (editable));
4401 gtk_stext_move_forward_word (GtkSText *text)
4403 text->cursor_virtual_x = 0;
4405 undraw_cursor (text, FALSE);
4407 if (text->use_wchar)
4409 while (!LAST_INDEX (text, text->cursor_mark) &&
4410 !gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4411 advance_mark (&text->cursor_mark);
4413 while (!LAST_INDEX (text, text->cursor_mark) &&
4414 gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4415 advance_mark (&text->cursor_mark);
4419 while (!LAST_INDEX (text, text->cursor_mark) &&
4420 !isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4421 advance_mark (&text->cursor_mark);
4423 while (!LAST_INDEX (text, text->cursor_mark) &&
4424 isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4425 advance_mark (&text->cursor_mark);
4428 find_cursor (text, TRUE);
4429 draw_cursor (text, FALSE);
4433 gtk_stext_move_backward_word (GtkSText *text)
4435 text->cursor_virtual_x = 0;
4437 undraw_cursor (text, FALSE);
4439 if (text->use_wchar)
4441 while ((text->cursor_mark.index > 0) &&
4442 !gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4443 decrement_mark (&text->cursor_mark);
4445 while ((text->cursor_mark.index > 0) &&
4446 gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4447 decrement_mark (&text->cursor_mark);
4451 while ((text->cursor_mark.index > 0) &&
4452 !isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4453 decrement_mark (&text->cursor_mark);
4455 while ((text->cursor_mark.index > 0) &&
4456 isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4457 decrement_mark (&text->cursor_mark);
4460 find_cursor (text, TRUE);
4461 draw_cursor (text, FALSE);
4465 gtk_stext_move_page (GtkEditable *editable,
4470 scroll_int (GTK_STEXT (editable),
4471 y * GTK_STEXT(editable)->vadj->page_increment);
4475 gtk_stext_move_to_row (GtkEditable *editable,
4481 gtk_stext_move_to_column (GtkEditable *editable,
4486 text = GTK_STEXT (editable);
4488 text->cursor_virtual_x = 0; /* FIXME */
4490 undraw_cursor (text, FALSE);
4492 /* Move to the beginning of the line */
4493 while ((text->cursor_mark.index > 0) &&
4494 (GTK_STEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4495 decrement_mark (&text->cursor_mark);
4497 while (!LAST_INDEX (text, text->cursor_mark) &&
4498 (GTK_STEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4502 else if (column == 0)
4505 advance_mark (&text->cursor_mark);
4508 find_cursor (text, TRUE);
4509 draw_cursor (text, FALSE);
4513 gtk_stext_move_beginning_of_line (GtkSText *text)
4515 gtk_stext_move_to_column (GTK_EDITABLE (text), 0);
4520 gtk_stext_move_end_of_line (GtkSText *text)
4522 gtk_stext_move_to_column (GTK_EDITABLE (text), -1);
4526 gtk_stext_kill_char (GtkEditable *editable,
4531 text = GTK_STEXT (editable);
4533 if (editable->selection_start_pos != editable->selection_end_pos)
4534 gtk_editable_delete_selection (editable);
4539 if (text->point.index + 1 <= TEXT_LENGTH (text))
4540 gtk_editable_delete_text (editable, text->point.index, text->point.index + 1);
4544 if (text->point.index > 0)
4545 gtk_editable_delete_text (editable, text->point.index - 1, text->point.index);
4551 gtk_stext_delete_forward_character (GtkSText *text)
4553 gtk_stext_kill_char (GTK_EDITABLE (text), 1);
4557 gtk_stext_delete_backward_character (GtkSText *text)
4559 gtk_stext_kill_char (GTK_EDITABLE (text), -1);
4563 gtk_stext_kill_word (GtkEditable *editable,
4566 if (editable->selection_start_pos != editable->selection_end_pos)
4567 gtk_editable_delete_selection (editable);
4570 gint old_pos = editable->current_pos;
4573 gtk_stext_move_word (editable, 1);
4574 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
4578 gtk_stext_move_word (editable, -1);
4579 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
4585 gtk_stext_delete_forward_word (GtkSText *text)
4587 gtk_stext_kill_word (GTK_EDITABLE (text), 1);
4591 gtk_stext_delete_backward_word (GtkSText *text)
4593 gtk_stext_kill_word (GTK_EDITABLE (text), -1);
4597 gtk_stext_kill_line (GtkEditable *editable,
4600 gint old_pos = editable->current_pos;
4603 gtk_stext_move_to_column (editable, -1);
4604 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
4608 gtk_stext_move_to_column (editable, 0);
4609 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
4614 gtk_stext_delete_line (GtkSText *text)
4616 gtk_stext_move_to_column (GTK_EDITABLE (text), 0);
4617 gtk_stext_kill_line (GTK_EDITABLE (text), 1);
4621 gtk_stext_delete_to_line_end (GtkSText *text)
4623 gtk_stext_kill_line (GTK_EDITABLE (text), 1);
4627 gtk_stext_select_word (GtkSText *text, guint32 time)
4632 GtkEditable *editable;
4633 editable = GTK_EDITABLE (text);
4635 gtk_stext_move_backward_word (text);
4636 start_pos = text->cursor_mark.index;
4638 gtk_stext_move_forward_word (text);
4639 end_pos = text->cursor_mark.index;
4641 editable->has_selection = TRUE;
4642 gtk_stext_set_selection (editable, start_pos, end_pos);
4643 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
4647 gtk_stext_select_line (GtkSText *text, guint32 time)
4652 GtkEditable *editable;
4653 editable = GTK_EDITABLE (text);
4655 gtk_stext_move_beginning_of_line (text);
4656 start_pos = text->cursor_mark.index;
4658 gtk_stext_move_end_of_line (text);
4659 gtk_stext_move_forward_character (text);
4660 end_pos = text->cursor_mark.index;
4662 editable->has_selection = TRUE;
4663 gtk_stext_set_selection (editable, start_pos, end_pos);
4664 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
4667 /**********************************************************************/
4669 /**********************************************************************/
4672 adjust_adj (GtkSText* text, GtkAdjustment* adj)
4676 gdk_window_get_size (text->text_area, NULL, &height);
4678 adj->step_increment = MIN (adj->upper, (float) SCROLL_PIXELS);
4679 adj->page_increment = MIN (adj->upper, height - (float) KEY_SCROLL_PIXELS);
4680 adj->page_size = MIN (adj->upper, height);
4681 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4682 adj->value = MAX (adj->value, 0.0);
4684 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4688 set_vertical_scroll_iterator (GtkSText* text, LineParams* lp, void* data)
4690 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4692 if ((text->first_line_start_index >= lp->start.index) &&
4693 (text->first_line_start_index <= lp->end.index))
4695 svdata->mark = lp->start;
4697 if (text->first_line_start_index == lp->start.index)
4699 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4703 text->first_onscreen_ver_pixel = svdata->pixel_height;
4704 text->first_cut_pixels = 0;
4707 text->vadj->value = (float) text->first_onscreen_ver_pixel;
4710 svdata->pixel_height += LINE_HEIGHT (*lp);
4716 set_vertical_scroll_find_iterator (GtkSText* text, LineParams* lp, void* data)
4718 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4721 if (svdata->pixel_height <= (gint) text->vadj->value &&
4722 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4724 svdata->mark = lp->start;
4726 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4727 text->first_onscreen_ver_pixel = svdata->pixel_height;
4728 text->first_line_start_index = lp->start.index;
4734 svdata->pixel_height += LINE_HEIGHT (*lp);
4742 static GtkSPropertyMark
4743 set_vertical_scroll (GtkSText* text)
4745 GtkSPropertyMark mark = find_mark (text, 0);
4746 SetVerticalScrollData data;
4750 data.pixel_height = 0;
4751 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4753 text->vadj->upper = (float) data.pixel_height;
4754 orig_value = (gint) text->vadj->value;
4756 gdk_window_get_size (text->text_area, NULL, &height);
4758 text->vadj->step_increment = MIN (text->vadj->upper, (float) SCROLL_PIXELS);
4759 text->vadj->page_increment = MIN (text->vadj->upper, height - (float) KEY_SCROLL_PIXELS);
4760 text->vadj->page_size = MIN (text->vadj->upper, height);
4761 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4762 text->vadj->value = MAX (text->vadj->value, 0.0);
4764 text->last_ver_value = (gint)text->vadj->value;
4766 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4768 if (text->vadj->value != orig_value)
4770 /* We got clipped, and don't really know which line to put first. */
4771 data.pixel_height = 0;
4772 data.last_didnt_wrap = TRUE;
4774 line_params_iterate (text, &mark, NULL,
4776 set_vertical_scroll_find_iterator);
4783 scroll_int (GtkSText* text, gint diff)
4787 text->vadj->value += diff;
4789 upper = text->vadj->upper - text->vadj->page_size;
4790 text->vadj->value = MIN (text->vadj->value, upper);
4791 text->vadj->value = MAX (text->vadj->value, 0.0);
4793 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4797 process_exposes (GtkSText *text)
4801 /* Make sure graphics expose events are processed before scrolling
4804 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4806 gtk_widget_event (GTK_WIDGET (text), event);
4807 if (event->expose.count == 0)
4809 gdk_event_free (event);
4812 gdk_event_free (event);
4816 static gint last_visible_line_height (GtkSText* text)
4818 GList *cache = text->line_start_cache;
4821 gdk_window_get_size (text->text_area, NULL, &height);
4823 for (; cache->next; cache = cache->next)
4824 if (pixel_height_of(text, cache->next) > height)
4828 return pixel_height_of(text, cache) - 1;
4833 static gint first_visible_line_height (GtkSText* text)
4835 if (text->first_cut_pixels)
4836 return pixel_height_of(text, text->line_start_cache) + 1;
4842 scroll_down (GtkSText* text, gint diff0)
4848 text->first_onscreen_ver_pixel += diff0;
4852 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4854 text->first_cut_pixels += 1;
4858 text->first_cut_pixels = 0;
4860 text->line_start_cache = text->line_start_cache->next;
4861 g_assert (text->line_start_cache);
4863 text->first_line_start_index =
4864 CACHE_DATA(text->line_start_cache).start.index;
4866 if (!text->line_start_cache->next)
4867 fetch_lines_forward (text, 1);
4873 gdk_window_get_size (text->text_area, &width, &height);
4874 if (height > real_diff)
4875 gdk_draw_pixmap (text->text_area,
4883 height - real_diff);
4886 rect.y = MAX (0, height - real_diff);
4888 rect.height = MIN (height, real_diff);
4890 expose_text (text, &rect, FALSE);
4891 gtk_stext_draw_focus ( (GtkWidget *) text);
4893 if (text->current_line)
4897 text->cursor_pos_y -= real_diff;
4898 cursor_min = drawn_cursor_min(text);
4901 find_mouse_cursor (text, text->cursor_pos_x,
4902 first_visible_line_height (text));
4905 if (height > real_diff)
4906 process_exposes (text);
4910 scroll_up (GtkSText* text, gint diff0)
4916 text->first_onscreen_ver_pixel += diff0;
4920 g_assert (text->line_start_cache);
4922 if (text->first_cut_pixels > 0)
4924 text->first_cut_pixels -= 1;
4928 if (!text->line_start_cache->prev)
4929 fetch_lines_backward (text);
4931 text->line_start_cache = text->line_start_cache->prev;
4933 text->first_line_start_index =
4934 CACHE_DATA(text->line_start_cache).start.index;
4936 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4942 gdk_window_get_size (text->text_area, &width, &height);
4943 if (height > real_diff)
4944 gdk_draw_pixmap (text->text_area,
4952 height - real_diff);
4957 rect.height = MIN (height, real_diff);
4959 expose_text (text, &rect, FALSE);
4960 gtk_stext_draw_focus ( (GtkWidget *) text);
4962 if (text->current_line)
4967 text->cursor_pos_y += real_diff;
4968 cursor_max = drawn_cursor_max(text);
4969 gdk_window_get_size (text->text_area, NULL, &height);
4971 if (cursor_max >= height)
4972 find_mouse_cursor (text, text->cursor_pos_x,
4973 last_visible_line_height (text));
4976 if (height > real_diff)
4977 process_exposes (text);
4980 /**********************************************************************/
4982 /**********************************************************************/
4984 /* Assumes mark starts a line. Calculates the height, width, and
4985 * displayable character count of a single DISPLAYABLE line. That
4986 * means that in line-wrap mode, this does may not compute the
4987 * properties of an entire line. */
4989 find_line_params (GtkSText* text,
4990 const GtkSPropertyMark* mark,
4991 const PrevTabCont *tab_cont,
4992 PrevTabCont *next_cont)
4995 TabStopMark tab_mark = tab_cont->tab_start;
4996 guint max_display_pixels;
5001 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
5003 if (text->wrap_rmargin) {
5004 /* SYLPHEED - since sylpheed is a mail program, assume we work with
5005 * fixed fonts. only assume we're using one font! */
5006 font = MARK_CURRENT_FONT(text, mark);
5008 /* SYLPHEED - ok for multi byte charsets to check for ASCII char?
5010 if (text->use_wchar)
5011 ch_width = gdk_char_width_wc(font, 'W');
5013 ch_width = gdk_char_width(font, 'W');
5015 /* SYLPHEED - max_display_chars has the rmargin in pixels
5017 max_display_pixels = text->wrap_rmargin * ch_width;
5020 if (GTK_EDITABLE (text)->editable || !text->word_wrap)
5021 max_display_pixels -= LINE_WRAP_ROOM;
5024 lp.tab_cont = *tab_cont;
5027 lp.pixel_width = tab_cont->pixel_offset;
5028 lp.displayable_chars = 0;
5030 lp.font_descent = 0;
5032 init_tab_cont (text, next_cont);
5034 while (!LAST_INDEX(text, lp.end))
5036 g_assert (lp.end.property);
5038 ch = GTK_STEXT_INDEX (text, lp.end.index);
5039 font = MARK_CURRENT_FONT (text, &lp.end);
5041 if (ch == LINE_DELIM)
5043 /* Newline doesn't count in computation of line height, even
5044 * if its in a bigger font than the rest of the line. Unless,
5045 * of course, there are no other characters. */
5047 if (!lp.font_ascent && !lp.font_descent)
5049 lp.font_ascent = font->ascent;
5050 lp.font_descent = font->descent;
5053 lp.tab_cont_next = *next_cont;
5058 ch_width = find_char_width (text, &lp.end, &tab_mark);
5060 if ((ch_width + lp.pixel_width > max_display_pixels) &&
5061 (lp.end.index > lp.start.index))
5065 if (text->line_wrap)
5067 next_cont->tab_start = tab_mark;
5068 next_cont->pixel_offset = 0;
5072 /* Here's the tough case, a tab is wrapping. */
5073 gint pixels_avail = max_display_pixels - lp.pixel_width;
5074 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
5075 gint spaces_avail = pixels_avail / space_width;
5077 if (spaces_avail == 0)
5079 decrement_mark (&lp.end);
5083 advance_tab_mark (text, &next_cont->tab_start, '\t');
5084 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
5086 lp.displayable_chars += 1;
5091 if (text->word_wrap)
5093 GtkSPropertyMark saved_mark = lp.end;
5094 guint saved_characters = lp.displayable_chars;
5096 lp.displayable_chars += 1;
5098 if (text->use_wchar)
5100 while (!gdk_iswspace (GTK_STEXT_INDEX (text, lp.end.index)) &&
5101 (lp.end.index > lp.start.index))
5103 decrement_mark (&lp.end);
5104 lp.displayable_chars -= 1;
5109 while (!isspace(GTK_STEXT_INDEX (text, lp.end.index)) &&
5110 (lp.end.index > lp.start.index))
5112 decrement_mark (&lp.end);
5113 lp.displayable_chars -= 1;
5117 /* If whole line is one word, revert to char wrapping */
5118 if (lp.end.index == lp.start.index)
5120 lp.end = saved_mark;
5121 lp.displayable_chars = saved_characters;
5122 decrement_mark (&lp.end);
5127 /* Don't include this character, it will wrap. */
5128 decrement_mark (&lp.end);
5132 lp.tab_cont_next = *next_cont;
5139 lp.displayable_chars += 1;
5142 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
5143 lp.font_descent = MAX (font->descent, lp.font_descent);
5144 lp.pixel_width += ch_width;
5146 advance_mark(&lp.end);
5147 advance_tab_mark (text, &tab_mark, ch);
5150 if (LAST_INDEX(text, lp.start))
5152 /* Special case, empty last line. */
5153 font = MARK_CURRENT_FONT (text, &lp.end);
5155 lp.font_ascent = font->ascent;
5156 lp.font_descent = font->descent;
5159 lp.tab_cont_next = *next_cont;
5165 expand_scratch_buffer (GtkSText* text, guint len)
5167 if (len >= text->scratch_buffer_len)
5171 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
5173 if (text->use_wchar)
5175 if (!text->scratch_buffer.wc)
5176 text->scratch_buffer.wc = g_new (GdkWChar, i);
5178 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
5179 i * sizeof (GdkWChar));
5183 if (!text->scratch_buffer.ch)
5184 text->scratch_buffer.ch = g_new (guchar, i);
5186 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
5189 text->scratch_buffer_len = i;
5193 /* Side effect: modifies text->gc
5197 draw_bg_rect (GtkSText* text, GtkSPropertyMark *mark,
5198 gint x, gint y, gint width, gint height,
5199 gboolean already_cleared)
5201 GtkEditable *editable = GTK_EDITABLE(text);
5203 if ((mark->index >= MIN(editable->selection_start_pos, editable->selection_end_pos) &&
5204 mark->index < MAX(editable->selection_start_pos, editable->selection_end_pos)))
5206 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
5207 editable->has_selection ?
5208 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
5210 NULL, GTK_WIDGET(text), "text",
5211 x, y, width, height);
5213 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
5214 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
5216 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
5218 gdk_draw_rectangle (text->text_area,
5220 TRUE, x, y, width, height);
5222 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
5229 rect.height = height;
5231 clear_area (text, &rect);
5233 else if (!already_cleared)
5234 gdk_window_clear_area (text->text_area, x, y, width, height);
5238 draw_line (GtkSText* text,
5239 gint pixel_start_height,
5242 GdkGCValues gc_values;
5245 guint running_offset = lp->tab_cont.pixel_offset;
5246 union { GdkWChar *wc; guchar *ch; } buffer;
5249 GtkEditable *editable = GTK_EDITABLE(text);
5251 guint selection_start_pos = MIN (editable->selection_start_pos, editable->selection_end_pos);
5252 guint selection_end_pos = MAX (editable->selection_start_pos, editable->selection_end_pos);
5254 GtkSPropertyMark mark = lp->start;
5255 TabStopMark tab_mark = lp->tab_cont.tab_start;
5256 gint pixel_height = pixel_start_height + lp->font_ascent;
5257 guint chars = lp->displayable_chars;
5259 /* First provide a contiguous segment of memory. This makes reading
5260 * the code below *much* easier, and only incurs the cost of copying
5261 * when the line being displayed spans the gap. */
5262 if (mark.index <= text->gap_position &&
5263 mark.index + chars > text->gap_position)
5265 expand_scratch_buffer (text, chars);
5267 if (text->use_wchar)
5269 for (i = 0; i < chars; i += 1)
5270 text->scratch_buffer.wc[i] = GTK_STEXT_INDEX(text, mark.index + i);
5271 buffer.wc = text->scratch_buffer.wc;
5275 for (i = 0; i < chars; i += 1)
5276 text->scratch_buffer.ch[i] = GTK_STEXT_INDEX(text, mark.index + i);
5277 buffer.ch = text->scratch_buffer.ch;
5282 if (text->use_wchar)
5284 if (mark.index >= text->gap_position)
5285 buffer.wc = text->text.wc + mark.index + text->gap_size;
5287 buffer.wc = text->text.wc + mark.index;
5291 if (mark.index >= text->gap_position)
5292 buffer.ch = text->text.ch + mark.index + text->gap_size;
5294 buffer.ch = text->text.ch + mark.index;
5299 if (running_offset > 0)
5301 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
5302 LINE_HEIGHT (*lp), TRUE);
5308 if ((text->use_wchar && buffer.wc[0] != '\t') ||
5309 (!text->use_wchar && buffer.ch[0] != '\t'))
5311 union { GdkWChar *wc; guchar *ch; } next_tab;
5316 if (text->use_wchar)
5317 for (i=0; i<chars; i++)
5319 if (buffer.wc[i] == '\t')
5321 next_tab.wc = buffer.wc + i;
5326 next_tab.ch = memchr (buffer.ch, '\t', chars);
5328 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
5330 if (text->use_wchar)
5333 len = MIN (len, next_tab.wc - buffer.wc);
5338 len = MIN (len, next_tab.ch - buffer.ch);
5341 if (mark.index < selection_start_pos)
5342 len = MIN (len, selection_start_pos - mark.index);
5343 else if (mark.index < selection_end_pos)
5344 len = MIN (len, selection_end_pos - mark.index);
5346 font = MARK_CURRENT_FONT (text, &mark);
5347 if (font->type == GDK_FONT_FONT)
5349 gdk_gc_set_font (text->gc, font);
5350 gdk_gc_get_values (text->gc, &gc_values);
5351 if (text->use_wchar)
5352 pixel_width = gdk_text_width_wc (gc_values.font,
5355 pixel_width = gdk_text_width (gc_values.font,
5360 if (text->use_wchar)
5361 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
5363 pixel_width = gdk_text_width (font, buffer.ch, len);
5366 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
5367 pixel_width, LINE_HEIGHT (*lp), TRUE);
5369 if ((mark.index >= selection_start_pos) &&
5370 (mark.index < selection_end_pos))
5372 if (editable->has_selection)
5373 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_SELECTED];
5375 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_ACTIVE];
5379 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
5383 if (text->use_wchar)
5384 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
5391 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
5398 running_offset += pixel_width;
5400 advance_tab_mark_n (text, &tab_mark, len);
5404 gint pixels_remaining;
5410 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
5411 if (GTK_EDITABLE (text)->editable || !text->word_wrap)
5412 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
5414 pixels_remaining -= running_offset;
5416 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
5418 spaces_avail = pixels_remaining / space_width;
5419 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
5421 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
5422 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
5424 running_offset += tab_mark.to_next_tab *
5425 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
5427 advance_tab_mark (text, &tab_mark, '\t');
5430 advance_mark_n (&mark, len);
5431 if (text->use_wchar)
5440 draw_line_wrap (GtkSText* text, guint height /* baseline height */)
5447 if (!GTK_EDITABLE (text)->editable && text->word_wrap)
5450 /* SYLPHEED - don't draw ugly word wrapping thing if
5451 * our wrap margin is set */
5452 if (text->wrap_rmargin > 0) {
5456 if (text->line_wrap)
5458 bitmap = text->line_wrap_bitmap;
5459 bitmap_width = line_wrap_width;
5460 bitmap_height = line_wrap_height;
5464 bitmap = text->line_arrow_bitmap;
5465 bitmap_width = line_arrow_width;
5466 bitmap_height = line_arrow_height;
5469 gdk_window_get_size (text->text_area, &width, NULL);
5470 width -= LINE_WRAP_ROOM;
5472 gdk_gc_set_stipple (text->gc,
5475 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
5477 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5479 gdk_gc_set_ts_origin (text->gc,
5481 height - bitmap_height - 1);
5483 gdk_draw_rectangle (text->text_area,
5487 height - bitmap_height - 1 /* one pixel above the baseline. */,
5491 gdk_gc_set_ts_origin (text->gc, 0, 0);
5493 gdk_gc_set_fill (text->gc, GDK_SOLID);
5497 undraw_cursor (GtkSText* text, gint absolute)
5499 GtkEditable *editable = (GtkEditable *)text;
5501 TDEBUG (("in undraw_cursor\n"));
5504 text->cursor_drawn_level = 0;
5506 if ((text->cursor_drawn_level ++ == 0) &&
5507 (editable->selection_start_pos == editable->selection_end_pos) &&
5508 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5511 gint pixel_width, pixel_height;
5514 g_assert(text->cursor_mark.property);
5516 gc = gdk_gc_new(text->text_area);
5518 gdk_gc_copy(gc, text->gc);
5520 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5523 * changed the cursor to a real block (TM)
5525 if (text->cursor_type == STEXT_CURSOR_BLOCK) {
5526 if (text->use_wchar)
5527 pixel_width = gdk_char_width_wc(font, text->cursor_char);
5529 pixel_width = gdk_char_width(font, text->cursor_char);
5531 pixel_height = LINE_HEIGHT(CACHE_DATA(text->current_line));
5533 draw_bg_rect (text, &text->cursor_mark,
5535 text->cursor_pos_y - (pixel_height + 1),
5541 draw_bg_rect (text, &text->cursor_mark,
5542 text->cursor_pos_x - 1,
5543 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5544 2, font->ascent + 1, FALSE);
5548 if (text->cursor_char)
5550 if (font->type == GDK_FONT_FONT)
5551 gdk_gc_set_font (text->gc, font);
5553 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5555 gdk_draw_text_wc (text->text_area, font,
5558 text->cursor_pos_y - text->cursor_char_offset,
5563 gdk_gc_copy(text->gc, gc);
5569 drawn_cursor_min (GtkSText* text)
5573 g_assert(text->cursor_mark.property);
5575 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5577 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5581 drawn_cursor_max (GtkSText* text)
5585 g_assert(text->cursor_mark.property);
5587 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5589 return text->cursor_pos_y - text->cursor_char_offset;
5593 draw_cursor (GtkSText* text, gint absolute)
5595 GtkEditable *editable = (GtkEditable *)text;
5596 gint pixel_width, pixel_height;
5598 TDEBUG (("in draw_cursor\n"));
5601 text->cursor_drawn_level = 1;
5603 if ((--text->cursor_drawn_level == 0) &&
5604 editable->editable &&
5605 (editable->selection_start_pos == editable->selection_end_pos) &&
5606 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5611 g_assert (text->cursor_mark.property);
5613 gc = gdk_gc_new(text->text_area);
5615 gdk_gc_copy(gc, text->gc);
5616 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5617 if (text->cursor_type == STEXT_CURSOR_BLOCK) {
5619 * changed the cursor to a real block (TM)
5621 if (text->use_wchar) {
5622 pixel_width = gdk_char_width_wc(font, text->cursor_char);
5625 pixel_width = gdk_char_width(font, text->cursor_char);
5629 pixel_height = LINE_HEIGHT(CACHE_DATA(text->current_line));
5630 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5631 gdk_gc_set_function(text->gc, GDK_INVERT);
5632 gdk_draw_rectangle(text->text_area, text->gc, TRUE,
5634 text->cursor_pos_y - pixel_height,
5639 gdk_gc_set_line_attributes(text->gc, 2, GDK_LINE_SOLID, GDK_CAP_NOT_LAST, GDK_JOIN_MITER);
5640 gdk_gc_set_foreground(text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5641 gdk_draw_line(text->text_area, text->gc, text->cursor_pos_x,
5642 text->cursor_pos_y - text->cursor_char_offset,
5644 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5647 gdk_gc_copy(text->gc, gc);
5653 create_bg_gc (GtkSText *text)
5657 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5658 values.fill = GDK_TILED;
5660 return gdk_gc_new_with_values (text->text_area, &values,
5661 GDK_GC_FILL | GDK_GC_TILE);
5665 clear_area (GtkSText *text, GdkRectangle *area)
5667 GtkWidget *widget = GTK_WIDGET (text);
5673 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5675 gdk_gc_set_ts_origin (text->bg_gc,
5676 (- (gint)text->first_onscreen_hor_pixel) % width,
5677 (- (gint)text->first_onscreen_ver_pixel) % height);
5679 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5680 area->x, area->y, area->width, area->height);
5683 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5687 expose_text (GtkSText* text, GdkRectangle *area, gboolean cursor)
5689 GList *cache = text->line_start_cache;
5690 gint pixels = - text->first_cut_pixels;
5691 gint min_y = MAX (0, area->y);
5692 gint max_y = MAX (0, area->y + area->height);
5695 gdk_window_get_size (text->text_area, NULL, &height);
5696 max_y = MIN (max_y, height);
5698 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5700 clear_area (text, area);
5702 for (; pixels < height; cache = cache->next)
5704 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5706 draw_line (text, pixels, &CACHE_DATA(cache));
5708 if (CACHE_DATA(cache).wraps)
5709 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5712 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5714 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5715 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5717 /* We undraw and draw the cursor here to get the drawn
5718 * level right ... FIXME - maybe the second parameter
5719 * of draw_cursor should work differently
5721 undraw_cursor (text, FALSE);
5722 draw_cursor (text, FALSE);
5726 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5730 fetch_lines_forward (text, 1);
5739 gtk_stext_update_text (GtkEditable *editable,
5743 GtkSText *text = GTK_STEXT (editable);
5745 GList *cache = text->line_start_cache;
5746 gint pixels = - text->first_cut_pixels;
5752 end_pos = TEXT_LENGTH (text);
5754 if (end_pos < start_pos)
5757 gdk_window_get_size (text->text_area, &width, &height);
5763 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5765 for (; pixels < height; cache = cache->next)
5767 if (CACHE_DATA(cache).start.index < end_pos)
5769 if (CACHE_DATA(cache).end.index >= start_pos)
5772 area.y = MAX(0,pixels);
5773 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5779 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5783 fetch_lines_forward (text, 1);
5791 expose_text (text, &area, TRUE);
5795 recompute_geometry (GtkSText* text)
5797 GtkSPropertyMark mark, start_mark;
5804 mark = start_mark = set_vertical_scroll (text);
5806 /* We need a real start of a line when calling fetch_lines().
5807 * not the start of a wrapped line.
5809 while (mark.index > 0 &&
5810 GTK_STEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5811 decrement_mark (&mark);
5813 gdk_window_get_size (text->text_area, &width, &height);
5815 /* Fetch an entire line, to make sure that we get all the text
5816 * we backed over above, in addition to enough text to fill up
5817 * the space vertically
5820 new_lines = fetch_lines (text,
5826 mark = CACHE_DATA (g_list_last (new_lines)).end;
5827 if (!LAST_INDEX (text, mark))
5829 advance_mark (&mark);
5831 new_lines = g_list_concat (new_lines,
5836 height + text->first_cut_pixels));
5839 /* Now work forward to the actual first onscreen line */
5841 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5842 new_lines = new_lines->next;
5844 text->line_start_cache = new_lines;
5846 find_cursor (text, TRUE);
5849 /**********************************************************************/
5851 /**********************************************************************/
5854 gtk_stext_set_selection (GtkEditable *editable,
5858 GtkSText *text = GTK_STEXT (editable);
5860 guint start1, end1, start2, end2;
5863 end = TEXT_LENGTH (text);
5865 start1 = MIN(start,end);
5866 end1 = MAX(start,end);
5867 start2 = MIN(editable->selection_start_pos, editable->selection_end_pos);
5868 end2 = MAX(editable->selection_start_pos, editable->selection_end_pos);
5870 if (start2 < start1)
5874 tmp = start1; start1 = start2; start2 = tmp;
5875 tmp = end1; end1 = end2; end2 = tmp;
5878 undraw_cursor (text, FALSE);
5879 editable->selection_start_pos = start;
5880 editable->selection_end_pos = end;
5881 draw_cursor (text, FALSE);
5883 /* Expose only what changed */
5885 if (start1 < start2)
5886 gtk_stext_update_text (editable, start1, MIN(end1, start2));
5889 gtk_stext_update_text (editable, MAX(end1, start2), end2);
5890 else if (end2 < end1)
5891 gtk_stext_update_text (editable, end2, end1);
5899 static gint stext_blink_timer_proc(GtkSText *text)
5901 if (text->cursor_state_on) {
5902 text->cursor_state_on = FALSE;
5903 undraw_cursor(text, TRUE);
5904 /* kill this timer... */
5905 gtk_timeout_remove(text->cursor_timer_id);
5906 text->cursor_timer_id = gtk_timeout_add(text->cursor_off_ms, (GtkFunction) stext_blink_timer_proc, (gpointer) text);
5909 text->cursor_state_on = TRUE;
5910 draw_cursor(text, TRUE);
5911 /* kill this timer... */
5912 gtk_timeout_remove(text->cursor_timer_id);
5913 text->cursor_timer_id = gtk_timeout_add(text->cursor_on_ms, (GtkFunction) stext_blink_timer_proc, (gpointer) text);
5918 static gint stext_idle_timer_proc(GtkSText *text)
5920 /* make sure the cursor timer is off */
5921 if (text->cursor_timer_id) {
5922 gtk_timeout_remove(text->cursor_timer_id);
5923 text->cursor_timer_id = 0;
5925 /* assuming it's always on when calling this function ... */
5926 text->cursor_state_on = TRUE;
5927 text->cursor_timer_id = gtk_timeout_add(text->cursor_on_ms, (GtkFunction) stext_blink_timer_proc, (gpointer) text);
5928 /* make sure we kill the timer (could perhaps make this function return FALSE (not documented in
5929 * the current docs). should check the source. */
5930 gtk_idle_remove( text->cursor_idle_time_timer_id );
5931 text->cursor_idle_time_timer_id = 0;
5935 static void gtk_stext_enable_blink (GtkSText *text)
5937 if (text->cursor_timer_on) {
5938 gtk_stext_disable_blink(text);
5939 text->cursor_idle_time_timer_id = gtk_idle_add((GtkFunction) stext_idle_timer_proc, (gpointer) text);
5943 static void gtk_stext_disable_blink (GtkSText *text)
5945 if (text->cursor_timer_on) {
5946 if (text->cursor_idle_time_timer_id) {
5947 gtk_idle_remove( text->cursor_idle_time_timer_id );
5948 text->cursor_idle_time_timer_id = 0;
5950 if (text->cursor_timer_id) {
5951 gtk_timeout_remove( text->cursor_timer_id );
5952 text->cursor_timer_id = 0;
5954 draw_cursor(text, TRUE);
5958 void gtk_stext_set_blink(GtkSText *text, gboolean blinkin_on)
5960 if (text->cursor_timer_on != blinkin_on) {
5961 if (text->cursor_timer_on) {
5962 /* text widget already created? */
5963 if (text->cursor_visible) {
5964 gtk_stext_disable_blink(text);
5966 text->cursor_timer_on = FALSE;
5969 if (text->cursor_visible) {
5970 gtk_stext_enable_blink(text);
5972 text->cursor_timer_on = TRUE;
5978 void gtk_stext_set_wrap_rmargin (GtkSText *text, gint rmargin)
5980 /* not particularly impressive, but it does the job. */
5981 /* TODO: currently only allowed to set this after a
5982 * gtk_stext_new() */
5983 text->wrap_rmargin = rmargin >= 0 ? rmargin : 0;
5986 /**********************************************************************/
5988 /**********************************************************************/
5990 #ifdef DEBUG_GTK_STEXT
5992 gtk_stext_show_cache_line (GtkSText *text, GList *cache,
5993 const char* what, const char* func, gint line)
5995 LineParams *lp = &CACHE_DATA(cache);
5998 if (cache == text->line_start_cache)
5999 g_message ("Line Start Cache: ");
6001 if (cache == text->current_line)
6002 g_message("Current Line: ");
6004 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
6012 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
6013 g_message ("%c", GTK_STEXT_INDEX (text, i));
6019 gtk_stext_show_cache (GtkSText *text, const char* func, gint line)
6021 GList *l = text->line_start_cache;
6027 /* back up to the absolute beginning of the line cache */
6031 g_message ("*** line cache ***\n");
6032 for (; l; l = l->next)
6033 gtk_stext_show_cache_line (text, l, "all", func, line);
6037 gtk_stext_assert_mark (GtkSText *text,
6038 GtkSPropertyMark *mark,
6039 GtkSPropertyMark *before,
6040 GtkSPropertyMark *after,
6045 GtkSPropertyMark correct_mark = find_mark (text, mark->index);
6047 if (mark->offset != correct_mark.offset ||
6048 mark->property != correct_mark.property)
6049 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
6053 gtk_stext_assert (GtkSText *text,
6057 GList* cache = text->line_start_cache;
6058 GtkSPropertyMark* before_mark = NULL;
6059 GtkSPropertyMark* after_mark = NULL;
6061 gtk_stext_show_props (text, msg, line);
6063 for (; cache->prev; cache = cache->prev)
6066 g_message ("*** line markers ***\n");
6068 for (; cache; cache = cache->next)
6070 after_mark = &CACHE_DATA(cache).end;
6071 gtk_stext_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
6072 before_mark = &CACHE_DATA(cache).start;
6075 after_mark = &CACHE_DATA(cache->next).start;
6079 gtk_stext_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
6080 before_mark = &CACHE_DATA(cache).end;
6085 gtk_stext_show_adj (GtkSText *text,
6091 g_message ("*** adjustment ***\n");
6093 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
6100 adj->step_increment,
6101 adj->page_increment,
6106 gtk_stext_show_props (GtkSText *text,
6110 GList* props = text->text_properties;
6113 g_message ("%s:%d: ", msg, line);
6115 for (; props; props = props->next)
6117 TextProperty *p = (TextProperty*)props->data;
6119 proplen += p->length;
6121 g_message ("[%d,%p,", p->length, p);
6122 if (p->flags & PROPERTY_FONT)
6123 g_message ("%p,", p->font);
6126 if (p->flags & PROPERTY_FOREGROUND)
6127 g_message ("%ld, ", p->fore_color.pixel);
6130 if (p->flags & PROPERTY_BACKGROUND)
6131 g_message ("%ld] ", p->back_color.pixel);
6138 if (proplen - 1 != TEXT_LENGTH(text))
6139 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);