1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 * Modified by the Sylpheed Team and others 2001-2002. Interesting
29 * parts are marked using comment block following this one.
30 * This modification is based on the GtkText of GTK 1.2.10
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>
54 #define line_arrow_width 6
55 #define line_arrow_height 9
56 static unsigned char line_arrow_bits[] = {
57 0x00, 0x00, 0x04, 0x0c, 0x18, 0x3f, 0x18, 0x0c, 0x04};
60 #define line_wrap_width 6
61 #define line_wrap_height 9
62 static unsigned char line_wrap_bits[] = {
63 0x1e, 0x3e, 0x30, 0x30, 0x39, 0x1f, 0x0f, 0x0f, 0x1f, };
66 * compile time settings
68 #define INITIAL_BUFFER_SIZE 1024
69 #define INITIAL_LINE_CACHE_SIZE 256
70 #define MIN_GAP_SIZE 256
73 * intending to introduce a paragraph delimiter '\r' because
74 * '\r' are being stripped by sylpheed
76 #define LINE_DELIM '\n'
77 #define MIN_TEXT_WIDTH_LINES 20
78 #define MIN_TEXT_HEIGHT_LINES 10
79 #define TEXT_BORDER_ROOM 1
80 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
81 #define DEFAULT_TAB_STOP_WIDTH 4
82 #define SCROLL_PIXELS 5
83 #define KEY_SCROLL_PIXELS 10
84 #define SCROLL_TIME 100
85 #define FREEZE_LENGTH 1024
86 /* Freeze text when inserting or deleting more than this many characters */
89 * could also mark paragraphs using marks, but don't know anything yet
90 * about consistency when characters are removed
92 #define SET_PROPERTY_MARK(m, p, o) do { \
93 (m)->property = (p); \
96 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
97 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
98 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
99 (mark)->property->prev->data \
101 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
102 #define MARK_LIST_PTR(mark) ((mark)->property)
103 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
104 #define MARK_OFFSET(mark) ((mark)->offset)
105 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
108 #define MARK_CURRENT_FONT(text, mark) \
109 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
110 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
111 GTK_WIDGET (text)->style->font)
112 #define MARK_CURRENT_FORE(text, mark) \
113 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
114 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
115 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
116 #define MARK_CURRENT_BACK(text, mark) \
117 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
118 &MARK_CURRENT_PROPERTY(mark)->back_color : \
119 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
120 #define MARK_CURRENT_TEXT_FONT(text, mark) \
121 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
122 MARK_CURRENT_PROPERTY(mark)->font : \
125 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
126 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
127 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
128 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
129 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
130 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
131 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
132 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
142 typedef struct _TextProperty TextProperty;
143 typedef struct _TabStopMark TabStopMark;
144 typedef struct _PrevTabCont PrevTabCont;
145 typedef struct _FetchLinesData FetchLinesData;
146 typedef struct _LineParams LineParams;
147 typedef struct _SetVerticalScrollData SetVerticalScrollData;
152 typedef gint (*LineIteratorFunction) (GtkSText* text, LineParams* lp, void* data);
160 struct _SetVerticalScrollData {
162 gint last_didnt_wrap;
163 gint last_line_start;
164 GtkSPropertyMark mark;
169 /* The actual font. */
173 gint16 char_widths[256];
177 PROPERTY_FONT = 1 << 0,
178 PROPERTY_FOREGROUND = 1 << 1,
179 PROPERTY_BACKGROUND = 1 << 2
187 /* Background Color. */
190 /* Foreground Color. */
193 /* Show which properties are set */
194 TextPropertyFlags flags;
196 /* Length of this property. */
202 GList* tab_stops; /* Index into list containing the next tab position. If
203 * NULL, using default widths. */
210 TabStopMark tab_start;
213 struct _FetchLinesData
226 guint displayable_chars;
229 PrevTabCont tab_cont;
230 PrevTabCont tab_cont_next;
232 GtkSPropertyMark start;
233 GtkSPropertyMark end;
237 static void gtk_stext_class_init (GtkSTextClass *klass);
238 static void gtk_stext_set_arg (GtkObject *object,
241 static void gtk_stext_get_arg (GtkObject *object,
244 static void gtk_stext_init (GtkSText *text);
245 static void gtk_stext_destroy (GtkObject *object);
246 static void gtk_stext_finalize (GtkObject *object);
247 static void gtk_stext_realize (GtkWidget *widget);
248 static void gtk_stext_unrealize (GtkWidget *widget);
249 static void gtk_stext_style_set (GtkWidget *widget,
250 GtkStyle *previous_style);
251 static void gtk_stext_state_changed (GtkWidget *widget,
252 GtkStateType previous_state);
253 static void gtk_stext_draw_focus (GtkWidget *widget);
254 static void gtk_stext_size_request (GtkWidget *widget,
255 GtkRequisition *requisition);
256 static void gtk_stext_size_allocate (GtkWidget *widget,
257 GtkAllocation *allocation);
258 static void gtk_stext_adjustment (GtkAdjustment *adjustment,
260 static void gtk_stext_disconnect (GtkAdjustment *adjustment,
263 static void gtk_stext_insert_text (GtkEditable *editable,
264 const gchar *new_text,
265 gint new_text_length,
267 static void gtk_stext_delete_text (GtkEditable *editable,
270 static void gtk_stext_update_text (GtkEditable *editable,
273 static gchar *gtk_stext_get_chars (GtkEditable *editable,
276 static void gtk_stext_set_selection (GtkEditable *editable,
279 static void gtk_stext_real_set_editable(GtkEditable *editable,
280 gboolean is_editable);
283 static void gtk_stext_draw (GtkWidget *widget,
285 static gint gtk_stext_expose (GtkWidget *widget,
286 GdkEventExpose *event);
287 static gint gtk_stext_button_press (GtkWidget *widget,
288 GdkEventButton *event);
289 static gint gtk_stext_button_release (GtkWidget *widget,
290 GdkEventButton *event);
291 static gint gtk_stext_motion_notify (GtkWidget *widget,
292 GdkEventMotion *event);
293 static gint gtk_stext_key_press (GtkWidget *widget,
295 static gint gtk_stext_focus_in (GtkWidget *widget,
296 GdkEventFocus *event);
297 static gint gtk_stext_focus_out (GtkWidget *widget,
298 GdkEventFocus *event);
300 static void move_gap (GtkSText* text, guint index);
301 static void make_forward_space (GtkSText* text, guint len);
303 /* Property management */
304 static GtkSTextFont* get_text_font (GdkFont* gfont);
305 static void text_font_unref (GtkSTextFont *text_font);
307 static void insert_text_property (GtkSText* text, GdkFont* font,
308 GdkColor *fore, GdkColor* back, guint len);
309 static TextProperty* new_text_property (GtkSText *text, GdkFont* font,
310 GdkColor* fore, GdkColor* back, guint length);
311 static void destroy_text_property (TextProperty *prop);
312 static void init_properties (GtkSText *text);
313 static void realize_property (GtkSText *text, TextProperty *prop);
314 static void realize_properties (GtkSText *text);
315 static void unrealize_property (GtkSText *text, TextProperty *prop);
316 static void unrealize_properties (GtkSText *text);
318 static void delete_text_property (GtkSText* text, guint len);
320 static guint pixel_height_of (GtkSText* text, GList* cache_line);
322 /* Property Movement and Size Computations */
323 static void advance_mark (GtkSPropertyMark* mark);
324 static void decrement_mark (GtkSPropertyMark* mark);
325 static void advance_mark_n (GtkSPropertyMark* mark, gint n);
326 static void decrement_mark_n (GtkSPropertyMark* mark, gint n);
327 static void move_mark_n (GtkSPropertyMark* mark, gint n);
328 static GtkSPropertyMark find_mark (GtkSText* text, guint mark_position);
329 static GtkSPropertyMark find_mark_near (GtkSText* text, guint mark_position, const GtkSPropertyMark* near);
330 static void find_line_containing_point (GtkSText* text, guint point,
334 static void compute_lines_pixels (GtkSText* text, guint char_count,
335 guint *lines, guint *pixels);
337 static gint total_line_height (GtkSText* text,
340 static LineParams find_line_params (GtkSText* text,
341 const GtkSPropertyMark *mark,
342 const PrevTabCont *tab_cont,
343 PrevTabCont *next_cont);
344 static void recompute_geometry (GtkSText* text);
345 static void insert_expose (GtkSText* text, guint old_pixels, gint nchars, guint new_line_count);
346 static void delete_expose (GtkSText* text,
350 static GdkGC *create_bg_gc (GtkSText *text);
351 static void clear_area (GtkSText *text, GdkRectangle *area);
352 static void draw_line (GtkSText* text,
355 static void draw_line_wrap (GtkSText* text,
357 static void draw_cursor (GtkSText* text, gint absolute);
358 static void undraw_cursor (GtkSText* text, gint absolute);
359 static gint drawn_cursor_min (GtkSText* text);
360 static gint drawn_cursor_max (GtkSText* text);
361 static void expose_text (GtkSText* text, GdkRectangle *area, gboolean cursor);
363 /* Search and Placement. */
364 static void find_cursor (GtkSText* text,
366 static void find_cursor_at_line (GtkSText* text,
367 const LineParams* start_line,
369 static void find_mouse_cursor (GtkSText* text, gint x, gint y);
372 static void adjust_adj (GtkSText* text, GtkAdjustment* adj);
373 static void scroll_up (GtkSText* text, gint diff);
374 static void scroll_down (GtkSText* text, gint diff);
375 static void scroll_int (GtkSText* text, gint diff);
377 static void process_exposes (GtkSText *text);
379 /* Cache Management. */
380 static void free_cache (GtkSText* text);
381 static GList* remove_cache_line (GtkSText* text, GList* list);
384 static void move_cursor_buffer_ver (GtkSText *text, int dir);
385 static void move_cursor_page_ver (GtkSText *text, int dir);
386 static void move_cursor_ver (GtkSText *text, int count);
387 static void move_cursor_hor (GtkSText *text, int count);
391 static void move_cursor_to_display_row_end (GtkSText *text);
392 static void move_cursor_to_display_row_start (GtkSText *text);
393 static void move_cursor_to_display_row_up (GtkSText *text);
394 static void move_cursor_to_display_row_down (GtkSText *text);
395 static void reset_persist_col_pos (GtkSText *text);
397 /* Binding actions */
398 static void gtk_stext_move_cursor (GtkEditable *editable,
401 static void gtk_stext_move_word (GtkEditable *editable,
403 static void gtk_stext_move_page (GtkEditable *editable,
406 static void gtk_stext_move_to_row (GtkEditable *editable,
408 static void gtk_stext_move_to_column (GtkEditable *editable,
410 static void gtk_stext_kill_char (GtkEditable *editable,
412 static void gtk_stext_kill_word (GtkEditable *editable,
414 static void gtk_stext_kill_line (GtkEditable *editable,
419 static void gtk_stext_move_forward_character (GtkSText *text);
420 static void gtk_stext_move_backward_character (GtkSText *text);
421 static void gtk_stext_move_forward_word (GtkSText *text);
422 static void gtk_stext_move_backward_word (GtkSText *text);
423 static void gtk_stext_move_beginning_of_line (GtkSText *text);
424 static void gtk_stext_move_end_of_line (GtkSText *text);
425 static void gtk_stext_move_next_line (GtkSText *text);
426 static void gtk_stext_move_previous_line (GtkSText *text);
428 static void gtk_stext_delete_forward_character (GtkSText *text);
429 static void gtk_stext_delete_backward_character (GtkSText *text);
430 static void gtk_stext_delete_forward_word (GtkSText *text);
431 static void gtk_stext_delete_backward_word (GtkSText *text);
432 static void gtk_stext_delete_line (GtkSText *text);
433 static void gtk_stext_delete_to_line_end (GtkSText *text);
435 static void gtk_stext_select_word (GtkSText *text,
437 static void gtk_stext_select_line (GtkSText *text,
440 static void gtk_stext_set_position (GtkEditable *editable,
446 static void gtk_stext_enable_blink (GtkSText *text);
447 static void gtk_stext_disable_blink (GtkSText *text);
449 /* #define DEBUG_GTK_STEXT */
451 #if defined(DEBUG_GTK_STEXT) && defined(__GNUC__)
452 /* Debugging utilities. */
453 static void gtk_stext_assert_mark (GtkSText *text,
454 GtkSPropertyMark *mark,
455 GtkSPropertyMark *before,
456 GtkSPropertyMark *after,
461 static void gtk_stext_assert (GtkSText *text,
464 static void gtk_stext_show_cache_line (GtkSText *text, GList *cache,
465 const char* what, const char* func, gint line);
466 static void gtk_stext_show_cache (GtkSText *text, const char* func, gint line);
467 static void gtk_stext_show_adj (GtkSText *text,
472 static void gtk_stext_show_props (GtkSText* test,
476 #define TDEBUG(args) g_message args
477 #define TEXT_ASSERT(text) gtk_stext_assert (text,__PRETTY_FUNCTION__,__LINE__)
478 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_stext_assert_mark (text,mark, \
479 __PRETTY_FUNCTION__,msg,__LINE__)
480 #define TEXT_SHOW(text) gtk_stext_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
481 #define TEXT_SHOW_LINE(text,line,msg) gtk_stext_show_cache_line (text,line,msg,\
482 __PRETTY_FUNCTION__,__LINE__)
483 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_stext_show_adj (text,adj,msg, \
484 __PRETTY_FUNCTION__,__LINE__)
487 #define TEXT_ASSERT(text)
488 #define TEXT_ASSERT_MARK(text,mark,msg)
489 #define TEXT_SHOW(text)
490 #define TEXT_SHOW_LINE(text,line,msg)
491 #define TEXT_SHOW_ADJ(text,adj,msg)
495 #if defined(AHX_DEBUG)
496 # define XDEBUG(args) g_message args
498 # define XDEBUG(args)
499 #endif /* AHX_DEBUG */
501 /* Memory Management. */
502 static GMemChunk *params_mem_chunk = NULL;
503 static GMemChunk *text_property_chunk = NULL;
505 static GtkWidgetClass *parent_class = NULL;
509 static const GtkTextFunction control_keys[26] =
511 (GtkTextFunction)gtk_stext_move_beginning_of_line, /* a */
512 (GtkTextFunction)gtk_stext_move_backward_character, /* b */
513 (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
514 (GtkTextFunction)gtk_stext_delete_forward_character, /* d */
515 (GtkTextFunction)gtk_stext_move_end_of_line, /* e */
516 (GtkTextFunction)gtk_stext_move_forward_character, /* f */
518 (GtkTextFunction)gtk_stext_delete_backward_character,/* h */
521 (GtkTextFunction)gtk_stext_delete_to_line_end, /* k */
524 (GtkTextFunction)gtk_stext_move_next_line, /* n */
526 (GtkTextFunction)gtk_stext_move_previous_line, /* p */
531 (GtkTextFunction)gtk_stext_delete_line, /* u */
532 (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
533 (GtkTextFunction)gtk_stext_delete_backward_word, /* w */
534 (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
539 static const GtkTextFunction alt_keys[26] =
542 (GtkTextFunction)gtk_stext_move_backward_word, /* b */
544 (GtkTextFunction)gtk_stext_delete_forward_word, /* d */
546 (GtkTextFunction)gtk_stext_move_forward_word, /* f */
571 /**********************************************************************/
573 /**********************************************************************/
576 gtk_stext_get_type (void)
578 static GtkType text_type = 0;
582 static const GtkTypeInfo text_info =
586 sizeof (GtkSTextClass),
587 (GtkClassInitFunc) gtk_stext_class_init,
588 (GtkObjectInitFunc) gtk_stext_init,
589 /* reserved_1 */ NULL,
590 /* reserved_2 */ NULL,
591 (GtkClassInitFunc) NULL,
594 text_type = gtk_type_unique (GTK_TYPE_EDITABLE, &text_info);
601 gtk_stext_class_init (GtkSTextClass *class)
603 GtkObjectClass *object_class;
604 GtkWidgetClass *widget_class;
605 GtkEditableClass *editable_class;
607 object_class = (GtkObjectClass*) class;
608 widget_class = (GtkWidgetClass*) class;
609 editable_class = (GtkEditableClass*) class;
610 parent_class = gtk_type_class (GTK_TYPE_EDITABLE);
612 gtk_object_add_arg_type ("GtkSText::hadjustment",
614 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
616 gtk_object_add_arg_type ("GtkSText::vadjustment",
618 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
620 gtk_object_add_arg_type ("GtkSText::line_wrap",
624 gtk_object_add_arg_type ("GtkSText::word_wrap",
629 object_class->set_arg = gtk_stext_set_arg;
630 object_class->get_arg = gtk_stext_get_arg;
631 object_class->destroy = gtk_stext_destroy;
632 object_class->finalize = gtk_stext_finalize;
634 widget_class->realize = gtk_stext_realize;
635 widget_class->unrealize = gtk_stext_unrealize;
636 widget_class->style_set = gtk_stext_style_set;
637 widget_class->state_changed = gtk_stext_state_changed;
638 widget_class->draw_focus = gtk_stext_draw_focus;
639 widget_class->size_request = gtk_stext_size_request;
640 widget_class->size_allocate = gtk_stext_size_allocate;
641 widget_class->draw = gtk_stext_draw;
642 widget_class->expose_event = gtk_stext_expose;
643 widget_class->button_press_event = gtk_stext_button_press;
644 widget_class->button_release_event = gtk_stext_button_release;
645 widget_class->motion_notify_event = gtk_stext_motion_notify;
646 widget_class->key_press_event = gtk_stext_key_press;
647 widget_class->focus_in_event = gtk_stext_focus_in;
648 widget_class->focus_out_event = gtk_stext_focus_out;
650 widget_class->set_scroll_adjustments_signal =
651 gtk_signal_new ("set_scroll_adjustments",
654 GTK_SIGNAL_OFFSET (GtkSTextClass, set_scroll_adjustments),
655 gtk_marshal_NONE__POINTER_POINTER,
656 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
658 editable_class->set_editable = gtk_stext_real_set_editable;
659 editable_class->insert_text = gtk_stext_insert_text;
660 editable_class->delete_text = gtk_stext_delete_text;
662 editable_class->move_cursor = gtk_stext_move_cursor;
663 editable_class->move_word = gtk_stext_move_word;
664 editable_class->move_page = gtk_stext_move_page;
665 editable_class->move_to_row = gtk_stext_move_to_row;
666 editable_class->move_to_column = gtk_stext_move_to_column;
668 editable_class->kill_char = gtk_stext_kill_char;
669 editable_class->kill_word = gtk_stext_kill_word;
670 editable_class->kill_line = gtk_stext_kill_line;
672 editable_class->update_text = gtk_stext_update_text;
673 editable_class->get_chars = gtk_stext_get_chars;
674 editable_class->set_selection = gtk_stext_set_selection;
675 editable_class->set_position = gtk_stext_set_position;
677 class->set_scroll_adjustments = gtk_stext_set_adjustments;
681 gtk_stext_set_arg (GtkObject *object,
687 text = GTK_STEXT (object);
691 case ARG_HADJUSTMENT:
692 gtk_stext_set_adjustments (text,
693 GTK_VALUE_POINTER (*arg),
696 case ARG_VADJUSTMENT:
697 gtk_stext_set_adjustments (text,
699 GTK_VALUE_POINTER (*arg));
702 gtk_stext_set_line_wrap (text, GTK_VALUE_BOOL (*arg));
705 gtk_stext_set_word_wrap (text, GTK_VALUE_BOOL (*arg));
713 gtk_stext_get_arg (GtkObject *object,
719 text = GTK_STEXT (object);
723 case ARG_HADJUSTMENT:
724 GTK_VALUE_POINTER (*arg) = text->hadj;
726 case ARG_VADJUSTMENT:
727 GTK_VALUE_POINTER (*arg) = text->vadj;
730 GTK_VALUE_BOOL (*arg) = text->line_wrap;
733 GTK_VALUE_BOOL (*arg) = text->word_wrap;
736 arg->type = GTK_TYPE_INVALID;
742 gtk_stext_init (GtkSText *text)
744 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
746 text->text_area = NULL;
751 text->line_wrap_bitmap = NULL;
752 text->line_arrow_bitmap = NULL;
754 text->use_wchar = FALSE;
755 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
756 text->text_len = INITIAL_BUFFER_SIZE;
758 text->scratch_buffer.ch = NULL;
759 text->scratch_buffer_len = 0;
761 text->freeze_count = 0;
763 if (!params_mem_chunk)
764 params_mem_chunk = g_mem_chunk_new ("LineParams",
766 256 * sizeof (LineParams),
769 text->default_tab_width = 4;
770 text->tab_stops = NULL;
772 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
773 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
775 text->line_start_cache = NULL;
776 text->first_cut_pixels = 0;
778 text->line_wrap = TRUE;
779 text->word_wrap = FALSE;
784 text->current_font = NULL;
787 * timer for blinking cursor
789 text->cursor_visible = FALSE; /* don't know whether gtktext stores this somewhere */
790 text->cursor_timer_on = TRUE;
791 text->cursor_off_ms = 500;
792 text->cursor_on_ms = 500;
793 text->cursor_timer_id = 0;
794 text->cursor_idle_time_timer_id = 0;
795 text->wrap_rmargin = 0;
796 text->cursor_type = GTK_STEXT_CURSOR_LINE;
797 text->persist_column = 0;
799 init_properties (text);
801 GTK_EDITABLE (text)->editable = FALSE;
803 gtk_editable_set_position (GTK_EDITABLE (text), 0);
807 gtk_stext_new (GtkAdjustment *hadj,
813 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
815 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
817 text = gtk_widget_new (GTK_TYPE_STEXT,
823 * force widget name to be GtkText so it silently adapts
824 * the GtkText widget's style...
826 gtk_widget_set_name (text, "GtkText");
827 gtk_widget_ensure_style (text);
833 gtk_stext_set_word_wrap (GtkSText *text,
836 g_return_if_fail (text != NULL);
837 g_return_if_fail (GTK_IS_STEXT (text));
839 text->word_wrap = (word_wrap != FALSE);
841 if (GTK_WIDGET_REALIZED (text))
843 recompute_geometry (text);
844 gtk_widget_queue_draw (GTK_WIDGET (text));
849 gtk_stext_set_line_wrap (GtkSText *text,
852 g_return_if_fail (text != NULL);
853 g_return_if_fail (GTK_IS_STEXT (text));
855 text->line_wrap = (line_wrap != FALSE);
857 if (GTK_WIDGET_REALIZED (text))
859 recompute_geometry (text);
860 gtk_widget_queue_draw (GTK_WIDGET (text));
865 gtk_stext_set_editable (GtkSText *text,
866 gboolean is_editable)
868 g_return_if_fail (text != NULL);
869 g_return_if_fail (GTK_IS_STEXT (text));
871 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
875 gtk_stext_real_set_editable (GtkEditable *editable,
876 gboolean is_editable)
880 g_return_if_fail (editable != NULL);
881 g_return_if_fail (GTK_IS_STEXT (editable));
883 text = GTK_STEXT (editable);
885 editable->editable = (is_editable != FALSE);
887 if (GTK_WIDGET_REALIZED (text))
889 recompute_geometry (text);
890 gtk_widget_queue_draw (GTK_WIDGET (text));
895 gtk_stext_set_adjustments (GtkSText *text,
899 g_return_if_fail (text != NULL);
900 g_return_if_fail (GTK_IS_STEXT (text));
902 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
904 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
906 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
908 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
910 if (text->hadj && (text->hadj != hadj))
912 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
913 gtk_object_unref (GTK_OBJECT (text->hadj));
916 if (text->vadj && (text->vadj != vadj))
918 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
919 gtk_object_unref (GTK_OBJECT (text->vadj));
922 if (text->hadj != hadj)
925 gtk_object_ref (GTK_OBJECT (text->hadj));
926 gtk_object_sink (GTK_OBJECT (text->hadj));
928 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
929 (GtkSignalFunc) gtk_stext_adjustment,
931 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
932 (GtkSignalFunc) gtk_stext_adjustment,
934 gtk_signal_connect (GTK_OBJECT (text->hadj), "disconnect",
935 (GtkSignalFunc) gtk_stext_disconnect,
937 gtk_stext_adjustment (hadj, text);
940 if (text->vadj != vadj)
943 gtk_object_ref (GTK_OBJECT (text->vadj));
944 gtk_object_sink (GTK_OBJECT (text->vadj));
946 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
947 (GtkSignalFunc) gtk_stext_adjustment,
949 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
950 (GtkSignalFunc) gtk_stext_adjustment,
952 gtk_signal_connect (GTK_OBJECT (text->vadj), "disconnect",
953 (GtkSignalFunc) gtk_stext_disconnect,
955 gtk_stext_adjustment (vadj, text);
960 gtk_stext_set_point (GtkSText *text,
963 g_return_if_fail (text != NULL);
964 g_return_if_fail (GTK_IS_STEXT (text));
965 g_return_if_fail (index <= TEXT_LENGTH (text));
967 text->point = find_mark (text, index);
971 gtk_stext_get_point (GtkSText *text)
973 g_return_val_if_fail (text != NULL, 0);
974 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
976 return text->point.index;
980 gtk_stext_get_length (GtkSText *text)
982 g_return_val_if_fail (text != NULL, 0);
983 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
985 return TEXT_LENGTH (text);
989 gtk_stext_freeze (GtkSText *text)
991 g_return_if_fail (text != NULL);
992 g_return_if_fail (GTK_IS_STEXT (text));
994 text->freeze_count++;
995 undraw_cursor (text, FALSE);
999 gtk_stext_thaw (GtkSText *text)
1001 g_return_if_fail (text != NULL);
1002 g_return_if_fail (GTK_IS_STEXT (text));
1004 if (text->freeze_count)
1005 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
1007 recompute_geometry (text);
1008 gtk_widget_queue_draw (GTK_WIDGET (text));
1010 draw_cursor (text, FALSE);
1015 gtk_stext_compact_buffer (GtkSText *text)
1017 g_return_if_fail (text != NULL);
1018 g_return_if_fail (GTK_IS_STEXT (text));
1019 move_gap (text, gtk_stext_get_length(text));
1023 gtk_stext_insert (GtkSText *text,
1030 GtkEditable *editable = GTK_EDITABLE (text);
1031 gboolean frozen = FALSE;
1033 gint new_line_count = 1;
1034 guint old_height = 0;
1039 g_return_if_fail (text != NULL);
1040 g_return_if_fail (GTK_IS_STEXT (text));
1042 g_return_if_fail (chars != NULL);
1045 if (!nchars || !chars)
1047 nchars = strlen (chars);
1051 if (!text->freeze_count && (length > FREEZE_LENGTH))
1053 gtk_stext_freeze (text);
1057 if (!text->freeze_count && (text->line_start_cache != NULL))
1059 find_line_containing_point (text, text->point.index, TRUE);
1060 old_height = total_line_height (text, text->current_line, 1);
1063 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
1066 widget = GTK_WIDGET (text);
1067 gtk_widget_ensure_style (widget);
1068 if ((widget->style) && (widget->style->font->type == GDK_FONT_FONTSET))
1070 text->use_wchar = TRUE;
1071 g_free (text->text.ch);
1072 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
1073 text->text_len = INITIAL_BUFFER_SIZE;
1074 if (text->scratch_buffer.ch)
1075 g_free (text->scratch_buffer.ch);
1076 text->scratch_buffer.wc = NULL;
1077 text->scratch_buffer_len = 0;
1081 move_gap (text, text->point.index);
1082 make_forward_space (text, length);
1084 if (text->use_wchar)
1086 char *chars_nt = (char *)chars;
1089 chars_nt = g_new (char, length+1);
1090 memcpy (chars_nt, chars, length);
1091 chars_nt[length] = 0;
1093 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
1095 if (chars_nt != chars)
1103 memcpy(text->text.ch + text->gap_position, chars, length);
1106 if (!text->freeze_count && (text->line_start_cache != NULL))
1108 if (text->use_wchar)
1110 for (i=0; i<numwcs; i++)
1111 if (text->text.wc[text->gap_position + i] == '\n')
1116 for (i=0; i<numwcs; i++)
1117 if (text->text.ch[text->gap_position + i] == '\n')
1124 insert_text_property (text, font, fore, back, numwcs);
1126 text->gap_size -= numwcs;
1127 text->gap_position += numwcs;
1129 if (text->point.index < text->first_line_start_index)
1130 text->first_line_start_index += numwcs;
1131 if (text->point.index < editable->selection_start_pos)
1132 editable->selection_start_pos += numwcs;
1133 if (text->point.index < editable->selection_end_pos)
1134 editable->selection_end_pos += numwcs;
1135 /* We'll reset the cursor later anyways if we aren't frozen */
1136 if (text->point.index < text->cursor_mark.index)
1137 text->cursor_mark.index += numwcs;
1139 advance_mark_n (&text->point, numwcs);
1141 if (!text->freeze_count && (text->line_start_cache != NULL))
1142 insert_expose (text, old_height, numwcs, new_line_count);
1146 gtk_stext_thaw (text);
1150 gtk_stext_backward_delete (GtkSText *text,
1153 g_return_val_if_fail (text != NULL, 0);
1154 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
1156 if (nchars > text->point.index || nchars <= 0)
1159 gtk_stext_set_point (text, text->point.index - nchars);
1161 return gtk_stext_forward_delete (text, nchars);
1165 gtk_stext_forward_delete (GtkSText *text,
1168 guint old_lines, old_height;
1169 GtkEditable *editable = GTK_EDITABLE (text);
1170 gboolean frozen = FALSE;
1172 g_return_val_if_fail (text != NULL, 0);
1173 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
1175 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1178 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1180 gtk_stext_freeze (text);
1184 if (!text->freeze_count && text->line_start_cache != NULL)
1186 /* We need to undraw the cursor here, since we may later
1187 * delete the cursor's property
1189 undraw_cursor (text, FALSE);
1190 find_line_containing_point (text, text->point.index, TRUE);
1191 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1194 /* FIXME, or resizing after deleting will be odd */
1195 if (text->point.index < text->first_line_start_index)
1197 if (text->point.index + nchars >= text->first_line_start_index)
1199 text->first_line_start_index = text->point.index;
1200 while ((text->first_line_start_index > 0) &&
1201 (GTK_STEXT_INDEX (text, text->first_line_start_index - 1)
1203 text->first_line_start_index -= 1;
1207 text->first_line_start_index -= nchars;
1210 if (text->point.index < editable->selection_start_pos)
1211 editable->selection_start_pos -=
1212 MIN(nchars, editable->selection_start_pos - text->point.index);
1213 if (text->point.index < editable->selection_end_pos)
1214 editable->selection_end_pos -=
1215 MIN(nchars, editable->selection_end_pos - text->point.index);
1216 /* We'll reset the cursor later anyways if we aren't frozen */
1217 if (text->point.index < text->cursor_mark.index)
1218 move_mark_n (&text->cursor_mark,
1219 -MIN(nchars, text->cursor_mark.index - text->point.index));
1221 move_gap (text, text->point.index);
1223 text->gap_size += nchars;
1225 delete_text_property (text, nchars);
1227 if (!text->freeze_count && (text->line_start_cache != NULL))
1229 delete_expose (text, nchars, old_lines, old_height);
1230 draw_cursor (text, FALSE);
1234 gtk_stext_thaw (text);
1240 gtk_stext_set_position (GtkEditable *editable,
1243 GtkSText *text = (GtkSText *) editable;
1245 undraw_cursor (text, FALSE);
1246 text->cursor_mark = find_mark (text, position);
1247 find_cursor (text, TRUE);
1248 draw_cursor (text, FALSE);
1249 gtk_editable_select_region (editable, 0, 0);
1252 /* SYLPHEED - set_position used. Need to find out whether there's
1253 * a better way to handle this */
1254 static void gtk_stext_set_position_X (GtkEditable *editable,
1257 GtkSText *text = (GtkSText *) editable;
1259 undraw_cursor (text, FALSE);
1260 text->cursor_mark = find_mark (text, position);
1261 find_cursor (text, TRUE);
1262 draw_cursor (text, FALSE);
1266 gtk_stext_get_chars (GtkEditable *editable,
1274 g_return_val_if_fail (editable != NULL, NULL);
1275 g_return_val_if_fail (GTK_IS_STEXT (editable), NULL);
1276 text = GTK_STEXT (editable);
1279 end_pos = TEXT_LENGTH (text);
1281 if ((start_pos < 0) ||
1282 (end_pos > TEXT_LENGTH (text)) ||
1283 (end_pos < start_pos))
1286 move_gap (text, TEXT_LENGTH (text));
1287 make_forward_space (text, 1);
1289 if (text->use_wchar)
1292 ch = text->text.wc[end_pos];
1293 text->text.wc[end_pos] = 0;
1294 retval = gdk_wcstombs (text->text.wc + start_pos);
1295 text->text.wc[end_pos] = ch;
1300 ch = text->text.ch[end_pos];
1301 text->text.ch[end_pos] = 0;
1302 retval = g_strdup (text->text.ch + start_pos);
1303 text->text.ch[end_pos] = ch;
1311 gtk_stext_destroy (GtkObject *object)
1315 g_return_if_fail (object != NULL);
1316 g_return_if_fail (GTK_IS_STEXT (object));
1318 text = (GtkSText*) object;
1320 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1321 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1325 gtk_timeout_remove (text->timer);
1332 gtk_stext_disable_blink (text);
1334 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1338 gtk_stext_finalize (GtkObject *object)
1343 g_return_if_fail (object != NULL);
1344 g_return_if_fail (GTK_IS_STEXT (object));
1346 text = (GtkSText *)object;
1348 gtk_object_unref (GTK_OBJECT (text->hadj));
1349 gtk_object_unref (GTK_OBJECT (text->vadj));
1351 /* Clean up the internal structures */
1352 if (text->use_wchar)
1353 g_free (text->text.wc);
1355 g_free (text->text.ch);
1357 tmp_list = text->text_properties;
1360 destroy_text_property (tmp_list->data);
1361 tmp_list = tmp_list->next;
1364 if (text->current_font)
1365 text_font_unref (text->current_font);
1367 g_list_free (text->text_properties);
1369 if (text->use_wchar)
1371 if (text->scratch_buffer.wc)
1372 g_free (text->scratch_buffer.wc);
1376 if (text->scratch_buffer.ch)
1377 g_free (text->scratch_buffer.ch);
1380 g_list_free (text->tab_stops);
1382 GTK_OBJECT_CLASS(parent_class)->finalize (object);
1386 gtk_stext_realize (GtkWidget *widget)
1389 GtkEditable *editable;
1390 GdkWindowAttr attributes;
1391 gint attributes_mask;
1393 g_return_if_fail (widget != NULL);
1394 g_return_if_fail (GTK_IS_STEXT (widget));
1396 text = GTK_STEXT (widget);
1397 editable = GTK_EDITABLE (widget);
1398 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1400 attributes.window_type = GDK_WINDOW_CHILD;
1401 attributes.x = widget->allocation.x;
1402 attributes.y = widget->allocation.y;
1403 attributes.width = widget->allocation.width;
1404 attributes.height = widget->allocation.height;
1405 attributes.wclass = GDK_INPUT_OUTPUT;
1406 attributes.visual = gtk_widget_get_visual (widget);
1407 attributes.colormap = gtk_widget_get_colormap (widget);
1408 attributes.event_mask = gtk_widget_get_events (widget);
1409 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1410 GDK_BUTTON_PRESS_MASK |
1411 GDK_BUTTON_RELEASE_MASK |
1412 GDK_BUTTON_MOTION_MASK |
1413 GDK_ENTER_NOTIFY_MASK |
1414 GDK_LEAVE_NOTIFY_MASK |
1415 GDK_KEY_PRESS_MASK);
1416 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1418 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1419 gdk_window_set_user_data (widget->window, text);
1421 attributes.x = (widget->style->klass->xthickness + TEXT_BORDER_ROOM);
1422 attributes.y = (widget->style->klass->ythickness + TEXT_BORDER_ROOM);
1423 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1424 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1426 attributes.cursor = gdk_cursor_new (GDK_XTERM);
1427 attributes_mask |= GDK_WA_CURSOR;
1429 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1430 gdk_window_set_user_data (text->text_area, text);
1432 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1434 widget->style = gtk_style_attach (widget->style, widget->window);
1436 /* Can't call gtk_style_set_background here because it's handled specially */
1437 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1438 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1440 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1441 text->bg_gc = create_bg_gc (text);
1443 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1444 (gchar*) line_wrap_bits,
1448 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1449 (gchar*) line_arrow_bits,
1453 text->gc = gdk_gc_new (text->text_area);
1454 gdk_gc_set_exposures (text->gc, TRUE);
1455 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1457 #ifdef USE_GTKGDK_XIM
1458 if (gdk_im_ready () && (editable->ic_attr = gdk_ic_attr_new ()) != NULL)
1461 GdkColormap *colormap;
1463 GdkICAttr *attr = editable->ic_attr;
1464 GdkICAttributesType attrmask = GDK_IC_ALL_REQ;
1466 GdkIMStyle supported_style = GDK_IM_PREEDIT_NONE |
1467 GDK_IM_PREEDIT_NOTHING |
1468 GDK_IM_PREEDIT_POSITION |
1469 GDK_IM_STATUS_NONE |
1470 GDK_IM_STATUS_NOTHING;
1472 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1473 supported_style &= ~GDK_IM_PREEDIT_POSITION;
1475 attr->style = style = gdk_im_decide_style (supported_style);
1476 attr->client_window = text->text_area;
1478 if ((colormap = gtk_widget_get_colormap (widget)) !=
1479 gtk_widget_get_default_colormap ())
1481 attrmask |= GDK_IC_PREEDIT_COLORMAP;
1482 attr->preedit_colormap = colormap;
1485 switch (style & GDK_IM_PREEDIT_MASK)
1487 case GDK_IM_PREEDIT_POSITION:
1488 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1490 g_warning ("over-the-spot style requires fontset");
1494 attrmask |= GDK_IC_PREEDIT_POSITION_REQ;
1495 gdk_window_get_size (text->text_area, &width, &height);
1496 attr->spot_location.x = 0;
1497 attr->spot_location.y = height;
1498 attr->preedit_area.x = 0;
1499 attr->preedit_area.y = 0;
1500 attr->preedit_area.width = width;
1501 attr->preedit_area.height = height;
1502 attr->preedit_fontset = widget->style->font;
1506 editable->ic = gdk_ic_new (attr, attrmask);
1508 if (editable->ic == NULL)
1509 g_warning ("Can't create input context.");
1512 mask = gdk_window_get_events (text->text_area);
1513 mask |= gdk_ic_get_events (editable->ic);
1514 gdk_window_set_events (text->text_area, mask);
1516 if (GTK_WIDGET_HAS_FOCUS (widget))
1517 gdk_im_begin (editable->ic, text->text_area);
1522 realize_properties (text);
1523 gdk_window_show (text->text_area);
1524 init_properties (text);
1526 if (editable->selection_start_pos != editable->selection_end_pos)
1527 gtk_editable_claim_selection (editable, TRUE, GDK_CURRENT_TIME);
1529 recompute_geometry (text);
1533 gtk_stext_style_set (GtkWidget *widget,
1534 GtkStyle *previous_style)
1536 GtkSText *text = GTK_STEXT (widget);
1538 if (GTK_WIDGET_REALIZED (widget))
1540 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1541 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1545 gdk_gc_destroy (text->bg_gc);
1549 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1550 text->bg_gc = create_bg_gc (text);
1552 recompute_geometry (text);
1555 if (text->current_font)
1556 text_font_unref (text->current_font);
1557 text->current_font = get_text_font (widget->style->font);
1561 gtk_stext_state_changed (GtkWidget *widget,
1562 GtkStateType previous_state)
1564 GtkSText *text = GTK_STEXT (widget);
1566 if (GTK_WIDGET_REALIZED (widget))
1568 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1569 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1574 gtk_stext_unrealize (GtkWidget *widget)
1578 g_return_if_fail (widget != NULL);
1579 g_return_if_fail (GTK_IS_STEXT (widget));
1581 text = GTK_STEXT (widget);
1583 #ifdef USE_GTKGDK_XIM
1584 if (GTK_EDITABLE (widget)->ic)
1586 gdk_ic_destroy (GTK_EDITABLE (widget)->ic);
1587 GTK_EDITABLE (widget)->ic = NULL;
1589 if (GTK_EDITABLE (widget)->ic_attr)
1591 gdk_ic_attr_destroy (GTK_EDITABLE (widget)->ic_attr);
1592 GTK_EDITABLE (widget)->ic_attr = NULL;
1596 gdk_window_set_user_data (text->text_area, NULL);
1597 gdk_window_destroy (text->text_area);
1598 text->text_area = NULL;
1600 gdk_gc_destroy (text->gc);
1605 gdk_gc_destroy (text->bg_gc);
1609 gdk_pixmap_unref (text->line_wrap_bitmap);
1610 gdk_pixmap_unref (text->line_arrow_bitmap);
1612 unrealize_properties (text);
1616 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1617 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1621 clear_focus_area (GtkSText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1623 GtkWidget *widget = GTK_WIDGET (text);
1626 gint ythick = TEXT_BORDER_ROOM + widget->style->klass->ythickness;
1627 gint xthick = TEXT_BORDER_ROOM + widget->style->klass->xthickness;
1631 if (area_width == 0 || area_height == 0)
1634 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1636 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1638 gdk_gc_set_ts_origin (text->bg_gc,
1639 (- (gint)text->first_onscreen_hor_pixel + xthick) % width,
1640 (- (gint)text->first_onscreen_ver_pixel + ythick) % height);
1645 gc = widget->style->bg_gc[widget->state];
1648 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1649 area_x, area_y, area_width, area_height);
1653 gtk_stext_draw_focus (GtkWidget *widget)
1659 g_return_if_fail (widget != NULL);
1660 g_return_if_fail (GTK_IS_STEXT (widget));
1662 text = GTK_STEXT (widget);
1664 if (GTK_WIDGET_DRAWABLE (widget))
1666 gint ythick = widget->style->klass->ythickness;
1667 gint xthick = widget->style->klass->xthickness;
1668 gint xextra = TEXT_BORDER_ROOM;
1669 gint yextra = TEXT_BORDER_ROOM;
1671 TDEBUG (("in gtk_stext_draw_focus\n"));
1675 width = widget->allocation.width;
1676 height = widget->allocation.height;
1678 if (GTK_WIDGET_HAS_FOCUS (widget))
1687 gtk_paint_focus (widget->style, widget->window,
1688 NULL, widget, "text",
1690 widget->allocation.width - 1,
1691 widget->allocation.height - 1);
1694 gtk_paint_shadow (widget->style, widget->window,
1695 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1696 NULL, widget, "text",
1697 x, y, width, height);
1701 width -= 2 * xthick;
1702 height -= 2 * ythick;
1705 clear_focus_area (text, x, y, width, yextra);
1707 clear_focus_area (text, x, y + yextra,
1708 xextra, y + height - 2 * yextra);
1710 clear_focus_area (text, x + width - xextra, y + yextra,
1711 xextra, height - 2 * ythick);
1713 clear_focus_area (text, x, x + height - yextra, width, yextra);
1717 TDEBUG (("in gtk_stext_draw_focus (undrawable !!!)\n"));
1722 gtk_stext_size_request (GtkWidget *widget,
1723 GtkRequisition *requisition)
1730 g_return_if_fail (widget != NULL);
1731 g_return_if_fail (GTK_IS_STEXT (widget));
1732 g_return_if_fail (requisition != NULL);
1734 xthickness = widget->style->klass->xthickness + TEXT_BORDER_ROOM;
1735 ythickness = widget->style->klass->ythickness + TEXT_BORDER_ROOM;
1737 char_height = MIN_TEXT_HEIGHT_LINES * (widget->style->font->ascent +
1738 widget->style->font->descent);
1740 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (widget->style->font,
1741 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1745 requisition->width = char_width + xthickness * 2;
1746 requisition->height = char_height + ythickness * 2;
1750 gtk_stext_size_allocate (GtkWidget *widget,
1751 GtkAllocation *allocation)
1754 GtkEditable *editable;
1756 g_return_if_fail (widget != NULL);
1757 g_return_if_fail (GTK_IS_STEXT (widget));
1758 g_return_if_fail (allocation != NULL);
1760 text = GTK_STEXT (widget);
1761 editable = GTK_EDITABLE (widget);
1763 widget->allocation = *allocation;
1764 if (GTK_WIDGET_REALIZED (widget))
1766 gdk_window_move_resize (widget->window,
1767 allocation->x, allocation->y,
1768 allocation->width, allocation->height);
1770 gdk_window_move_resize (text->text_area,
1771 widget->style->klass->xthickness + TEXT_BORDER_ROOM,
1772 widget->style->klass->ythickness + TEXT_BORDER_ROOM,
1773 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->klass->xthickness +
1774 (gint)TEXT_BORDER_ROOM) * 2),
1775 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->klass->ythickness +
1776 (gint)TEXT_BORDER_ROOM) * 2));
1778 #ifdef USE_GTKGDK_XIM
1779 if (editable->ic && (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
1783 gdk_window_get_size (text->text_area, &width, &height);
1784 editable->ic_attr->preedit_area.width = width;
1785 editable->ic_attr->preedit_area.height = height;
1787 gdk_ic_set_attr (editable->ic,
1788 editable->ic_attr, GDK_IC_PREEDIT_AREA);
1792 recompute_geometry (text);
1797 gtk_stext_draw (GtkWidget *widget,
1800 g_return_if_fail (widget != NULL);
1801 g_return_if_fail (GTK_IS_STEXT (widget));
1802 g_return_if_fail (area != NULL);
1804 if (GTK_WIDGET_DRAWABLE (widget))
1806 expose_text (GTK_STEXT (widget), area, TRUE);
1807 gtk_widget_draw_focus (widget);
1812 gtk_stext_expose (GtkWidget *widget,
1813 GdkEventExpose *event)
1815 g_return_val_if_fail (widget != NULL, FALSE);
1816 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1817 g_return_val_if_fail (event != NULL, FALSE);
1819 if (event->window == GTK_STEXT (widget)->text_area)
1821 TDEBUG (("in gtk_stext_expose (expose)\n"));
1822 expose_text (GTK_STEXT (widget), &event->area, TRUE);
1824 else if (event->count == 0)
1826 TDEBUG (("in gtk_stext_expose (focus)\n"));
1827 gtk_widget_draw_focus (widget);
1834 gtk_stext_scroll_timeout (gpointer data)
1837 GdkEventMotion event;
1839 GdkModifierType mask;
1841 GDK_THREADS_ENTER ();
1843 text = GTK_STEXT (data);
1846 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1848 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1855 gtk_stext_motion_notify (GTK_WIDGET (text), &event);
1858 GDK_THREADS_LEAVE ();
1864 gtk_stext_button_press (GtkWidget *widget,
1865 GdkEventButton *event)
1868 GtkEditable *editable;
1869 static GdkAtom ctext_atom = GDK_NONE;
1871 g_return_val_if_fail (widget != NULL, FALSE);
1872 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1873 g_return_val_if_fail (event != NULL, FALSE);
1875 if (ctext_atom == GDK_NONE)
1876 ctext_atom = gdk_atom_intern ("COMPOUND_TEXT", FALSE);
1878 text = GTK_STEXT (widget);
1879 editable = GTK_EDITABLE (widget);
1881 if (text->button && (event->button != text->button))
1884 text->button = event->button;
1886 if (!GTK_WIDGET_HAS_FOCUS (widget))
1887 gtk_widget_grab_focus (widget);
1889 if (event->button == 1)
1891 switch (event->type)
1893 case GDK_BUTTON_PRESS:
1894 gtk_grab_add (widget);
1896 undraw_cursor (text, FALSE);
1897 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1898 draw_cursor (text, FALSE);
1900 /* Set it now, so we display things right. We'll unset it
1901 * later if things don't work out */
1902 editable->has_selection = TRUE;
1903 gtk_stext_set_selection (GTK_EDITABLE(text),
1904 text->cursor_mark.index,
1905 text->cursor_mark.index);
1909 case GDK_2BUTTON_PRESS:
1910 gtk_stext_select_word (text, event->time);
1913 case GDK_3BUTTON_PRESS:
1914 gtk_stext_select_line (text, event->time);
1921 else if (event->type == GDK_BUTTON_PRESS)
1923 if ((event->button == 2) && editable->editable)
1925 if (editable->selection_start_pos == editable->selection_end_pos ||
1926 editable->has_selection)
1928 undraw_cursor (text, FALSE);
1929 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1930 draw_cursor (text, FALSE);
1934 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1935 ctext_atom, event->time);
1939 gtk_grab_add (widget);
1941 undraw_cursor (text, FALSE);
1942 find_mouse_cursor (text, event->x, event->y);
1943 draw_cursor (text, FALSE);
1945 gtk_stext_set_selection (GTK_EDITABLE(text),
1946 text->cursor_mark.index,
1947 text->cursor_mark.index);
1949 editable->has_selection = FALSE;
1950 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1951 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1959 gtk_stext_button_release (GtkWidget *widget,
1960 GdkEventButton *event)
1963 GtkEditable *editable;
1964 g_return_val_if_fail (widget != NULL, FALSE);
1965 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1966 g_return_val_if_fail (event != NULL, FALSE);
1968 text = GTK_STEXT (widget);
1970 gtk_grab_remove (widget);
1972 if (text->button != event->button)
1979 gtk_timeout_remove (text->timer);
1983 if (event->button == 1)
1985 text = GTK_STEXT (widget);
1986 editable = GTK_EDITABLE (widget);
1988 gtk_grab_remove (widget);
1990 editable->has_selection = FALSE;
1991 if (editable->selection_start_pos != editable->selection_end_pos)
1993 if (gtk_selection_owner_set (widget,
1994 GDK_SELECTION_PRIMARY,
1996 editable->has_selection = TRUE;
1998 gtk_stext_update_text (editable, editable->selection_start_pos,
1999 editable->selection_end_pos);
2003 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
2004 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
2007 else if (event->button == 3)
2009 gtk_grab_remove (widget);
2012 undraw_cursor (text, FALSE);
2013 find_cursor (text, TRUE);
2014 draw_cursor (text, FALSE);
2020 gtk_stext_motion_notify (GtkWidget *widget,
2021 GdkEventMotion *event)
2026 GdkModifierType mask;
2028 g_return_val_if_fail (widget != NULL, FALSE);
2029 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2030 g_return_val_if_fail (event != NULL, FALSE);
2032 text = GTK_STEXT (widget);
2036 mask = event->state;
2037 if (event->is_hint || (text->text_area != event->window))
2039 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
2042 if ((text->button == 0) ||
2043 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
2046 gdk_window_get_size (text->text_area, NULL, &height);
2048 if ((y < 0) || (y > height))
2050 if (text->timer == 0)
2052 text->timer = gtk_timeout_add (SCROLL_TIME,
2053 gtk_stext_scroll_timeout,
2057 scroll_int (text, y/2);
2059 scroll_int (text, (y - height)/2);
2065 undraw_cursor (GTK_STEXT (widget), FALSE);
2066 find_mouse_cursor (GTK_STEXT (widget), x, y);
2067 draw_cursor (GTK_STEXT (widget), FALSE);
2069 gtk_stext_set_selection (GTK_EDITABLE(text),
2070 GTK_EDITABLE(text)->selection_start_pos,
2071 text->cursor_mark.index);
2077 gtk_stext_insert_text (GtkEditable *editable,
2078 const gchar *new_text,
2079 gint new_text_length,
2082 GtkSText *text = GTK_STEXT (editable);
2084 GdkColor *fore, *back;
2086 TextProperty *property;
2088 gtk_stext_set_point (text, *position);
2090 property = MARK_CURRENT_PROPERTY (&text->point);
2091 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
2092 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
2093 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
2095 gtk_stext_insert (text, font, fore, back, new_text, new_text_length);
2097 *position = text->point.index;
2101 gtk_stext_delete_text (GtkEditable *editable,
2107 g_return_if_fail (start_pos >= 0);
2109 text = GTK_STEXT (editable);
2111 gtk_stext_set_point (text, start_pos);
2113 end_pos = TEXT_LENGTH (text);
2115 if (end_pos > start_pos)
2116 gtk_stext_forward_delete (text, end_pos - start_pos);
2120 gtk_stext_key_press (GtkWidget *widget,
2124 GtkEditable *editable;
2129 g_return_val_if_fail (widget != NULL, FALSE);
2130 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2131 g_return_val_if_fail (event != NULL, FALSE);
2135 text = GTK_STEXT (widget);
2136 editable = GTK_EDITABLE (widget);
2138 key = event->keyval;
2141 if ((GTK_EDITABLE(text)->editable == FALSE))
2143 switch (event->keyval)
2146 if (event->state & GDK_CONTROL_MASK)
2147 scroll_int (text, -text->vadj->value);
2152 if (event->state & GDK_CONTROL_MASK)
2153 scroll_int (text, +text->vadj->upper);
2157 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
2158 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
2159 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
2160 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
2162 if (event->state & GDK_CONTROL_MASK)
2163 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2174 gint extend_selection;
2176 guint initial_pos = editable->current_pos;
2178 text->point = find_mark (text, text->cursor_mark.index);
2180 extend_selection = (event->state & GDK_SHIFT_MASK) &&
2181 (event->keyval != GDK_Return);
2182 extend_start = FALSE;
2184 if (extend_selection)
2186 editable->has_selection = TRUE;
2188 if (editable->selection_start_pos == editable->selection_end_pos)
2190 editable->selection_start_pos = text->point.index;
2191 editable->selection_end_pos = text->point.index;
2194 extend_start = (text->point.index == editable->selection_start_pos);
2195 /* SYLPHEED: cursor */
2196 gtk_stext_disable_blink (text);
2200 gtk_stext_enable_blink (text);
2206 if (event->keyval != GDK_Up && event->keyval != GDK_Down) {
2207 reset_persist_col_pos(text);
2210 switch (event->keyval)
2213 if (event->state & GDK_CONTROL_MASK) {
2214 if (text->wrap_rmargin == 0) {
2215 /* SYLPHEED: old behaviour */
2216 move_cursor_buffer_ver (text, -1);
2219 /* SYLPHEED: contrived, but "trusty" */
2220 move_cursor_buffer_ver(text, -1);
2221 move_cursor_to_display_row_start(text);
2225 if (text->wrap_rmargin > 0) {
2226 /* SYLPHEED: line start */
2227 move_cursor_to_display_row_start(text);
2230 gtk_stext_move_beginning_of_line (text);
2235 if (event->state & GDK_CONTROL_MASK) {
2236 /* SYLPHEED: a little bit contrived... */
2237 if (text->wrap_rmargin == 0) {
2239 move_cursor_buffer_ver (text, +1);
2242 move_cursor_buffer_ver(text, +1);
2243 move_cursor_to_display_row_end(text);
2247 if (text->wrap_rmargin > 0) {
2248 /* SYLPHEED: line end */
2249 move_cursor_to_display_row_end(text);
2252 gtk_stext_move_end_of_line(text);
2257 move_cursor_page_ver (text, -1);
2259 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
2260 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
2262 if (text->wrap_rmargin > 0) {
2265 move_cursor_to_display_row_up(text);
2268 move_cursor_ver (text, -1);
2272 move_cursor_to_display_row_down(text);
2273 /* move_cursor_ver (text, +1); */
2276 if (event->state & GDK_CONTROL_MASK)
2277 gtk_stext_move_backward_word (text);
2279 move_cursor_hor (text, -1);
2282 if (event->state & GDK_CONTROL_MASK)
2283 gtk_stext_move_forward_word (text);
2285 move_cursor_hor (text, +1);
2289 if (event->state & GDK_CONTROL_MASK)
2290 gtk_stext_delete_backward_word (text);
2292 gtk_stext_delete_backward_character (text);
2295 gtk_stext_delete_line (text);
2298 if (event->state & GDK_SHIFT_MASK)
2300 extend_selection = FALSE;
2301 gtk_editable_paste_clipboard (editable);
2303 else if (event->state & GDK_CONTROL_MASK)
2305 gtk_editable_copy_clipboard (editable);
2309 /* gtk_toggle_insert(text) -- IMPLEMENT */
2313 if (event->state & GDK_CONTROL_MASK)
2314 gtk_stext_delete_forward_word (text);
2315 else if (event->state & GDK_SHIFT_MASK)
2317 extend_selection = FALSE;
2318 gtk_editable_cut_clipboard (editable);
2321 gtk_stext_delete_forward_character (text);
2324 position = text->point.index;
2325 gtk_editable_insert_text (editable, "\t", 1, &position);
2328 if (event->state & GDK_CONTROL_MASK)
2329 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2332 position = text->point.index;
2333 gtk_editable_insert_text (editable, "\n", 1, &position);
2337 /* Don't insert literally */
2345 if (event->state & GDK_CONTROL_MASK)
2347 if ((key >= 'A') && (key <= 'Z'))
2350 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2352 (* control_keys[(int) (key - 'a')]) (editable, event->time);
2358 else if (event->state & GDK_MOD1_MASK)
2360 if ((key >= 'A') && (key <= 'Z'))
2363 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2365 (* alt_keys[(int) (key - 'a')]) (editable, event->time);
2371 else if (event->length > 0)
2373 if (!(event->state & GDK_CONTROL_MASK) &&
2374 !(event->state & GDK_MOD1_MASK) &&
2375 (event->length > 0))
2377 extend_selection = FALSE;
2379 gtk_editable_delete_selection (editable);
2380 position = text->point.index;
2381 gtk_editable_insert_text (editable, event->string, event->length, &position);
2389 if (return_val && (editable->current_pos != initial_pos))
2391 if (extend_selection)
2393 if (editable->current_pos < editable->selection_start_pos)
2394 gtk_stext_set_selection (editable, editable->current_pos,
2395 editable->selection_end_pos);
2396 else if (editable->current_pos > editable->selection_end_pos)
2397 gtk_stext_set_selection (editable, editable->selection_start_pos,
2398 editable->current_pos);
2402 gtk_stext_set_selection (editable, editable->current_pos,
2403 editable->selection_end_pos);
2405 gtk_stext_set_selection (editable, editable->selection_start_pos,
2406 editable->current_pos);
2410 gtk_stext_set_selection (editable, 0, 0);
2412 gtk_editable_claim_selection (editable,
2413 editable->selection_start_pos != editable->selection_end_pos,
2422 gtk_stext_focus_in (GtkWidget *widget,
2423 GdkEventFocus *event)
2425 g_return_val_if_fail (widget != NULL, FALSE);
2426 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2427 g_return_val_if_fail (event != NULL, FALSE);
2429 TDEBUG (("in gtk_stext_focus_in\n"));
2431 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
2432 gtk_widget_draw_focus (widget);
2434 #ifdef USE_GTKGDK_XIM
2435 if (GTK_EDITABLE(widget)->ic)
2436 gdk_im_begin (GTK_EDITABLE(widget)->ic, GTK_STEXT(widget)->text_area);
2439 draw_cursor (GTK_STEXT(widget), TRUE);
2440 /* SYLPHEED: cursor */
2441 GTK_STEXT(widget)->cursor_visible = TRUE;
2442 gtk_stext_enable_blink (GTK_STEXT(widget));
2448 gtk_stext_focus_out (GtkWidget *widget,
2449 GdkEventFocus *event)
2451 g_return_val_if_fail (widget != NULL, FALSE);
2452 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2453 g_return_val_if_fail (event != NULL, FALSE);
2455 TDEBUG (("in gtk_stext_focus_out\n"));
2457 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
2458 gtk_widget_draw_focus (widget);
2461 * should disable blink before undrawing the cursor - disabling blink
2462 * redraws the cursor.
2464 gtk_stext_disable_blink (GTK_STEXT(widget));
2465 undraw_cursor (GTK_STEXT(widget), TRUE);
2466 GTK_STEXT(widget)->cursor_visible = FALSE;
2468 #ifdef USE_GTKGDK_XIM
2476 gtk_stext_adjustment (GtkAdjustment *adjustment,
2481 g_return_if_fail (adjustment != NULL);
2482 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2483 g_return_if_fail (text != NULL);
2484 g_return_if_fail (GTK_IS_STEXT (text));
2486 /* Clamp the value here, because we'll get really confused
2487 * if someone tries to move the adjusment outside of the
2490 old_val = adjustment->value;
2492 adjustment->value = MIN (adjustment->value, adjustment->upper - adjustment->page_size);
2493 adjustment->value = MAX (adjustment->value, 0.0);
2495 if (adjustment->value != old_val)
2497 gtk_signal_handler_block_by_func (GTK_OBJECT (adjustment),
2498 GTK_SIGNAL_FUNC (gtk_stext_adjustment),
2500 gtk_adjustment_changed (adjustment);
2501 gtk_signal_handler_unblock_by_func (GTK_OBJECT (adjustment),
2502 GTK_SIGNAL_FUNC (gtk_stext_adjustment),
2506 /* Just ignore it if we haven't been size-allocated and realized yet */
2507 if (text->line_start_cache == NULL)
2510 if (adjustment == text->hadj)
2512 g_warning ("horizontal scrolling not implemented");
2516 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2520 undraw_cursor (text, FALSE);
2523 scroll_down (text, diff);
2524 else /* if (diff < 0) */
2525 scroll_up (text, diff);
2527 draw_cursor (text, FALSE);
2529 text->last_ver_value = adjustment->value;
2535 gtk_stext_disconnect (GtkAdjustment *adjustment,
2538 g_return_if_fail (adjustment != NULL);
2539 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2540 g_return_if_fail (text != NULL);
2541 g_return_if_fail (GTK_IS_STEXT (text));
2543 if (adjustment == text->hadj)
2544 gtk_stext_set_adjustments (text, NULL, text->vadj);
2545 if (adjustment == text->vadj)
2546 gtk_stext_set_adjustments (text, text->hadj, NULL);
2550 static GtkSPropertyMark
2551 find_this_line_start_mark (GtkSText* text, guint point_position, const GtkSPropertyMark* near)
2553 GtkSPropertyMark mark;
2555 mark = find_mark_near (text, point_position, near);
2557 while (mark.index > 0 &&
2558 GTK_STEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2559 decrement_mark (&mark);
2565 init_tab_cont (GtkSText* text, PrevTabCont* tab_cont)
2567 tab_cont->pixel_offset = 0;
2568 tab_cont->tab_start.tab_stops = text->tab_stops;
2569 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2571 if (!tab_cont->tab_start.to_next_tab)
2572 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2576 line_params_iterate (GtkSText* text,
2577 const GtkSPropertyMark* mark0,
2578 const PrevTabCont* tab_mark0,
2581 LineIteratorFunction iter)
2582 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2583 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2584 * for each line following MARK, iteration continues unless ITER_CALL
2587 GtkSPropertyMark mark = *mark0;
2588 PrevTabCont tab_conts[2];
2589 LineParams *lp, lpbuf;
2590 gint tab_cont_index = 0;
2593 tab_conts[0] = *tab_mark0;
2595 init_tab_cont (text, tab_conts);
2600 lp = g_chunk_new (LineParams, params_mem_chunk);
2604 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2605 tab_conts + (tab_cont_index + 1) % 2);
2607 if ((*iter) (text, lp, data))
2610 if (LAST_INDEX (text, lp->end))
2614 advance_mark (&mark);
2615 tab_cont_index = (tab_cont_index + 1) % 2;
2620 fetch_lines_iterator (GtkSText* text, LineParams* lp, void* data)
2622 FetchLinesData *fldata = (FetchLinesData*) data;
2624 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2626 switch (fldata->fl_type)
2628 case FetchLinesCount:
2629 if (!text->line_wrap || !lp->wraps)
2632 if (fldata->data >= fldata->data_max)
2636 case FetchLinesPixels:
2638 fldata->data += LINE_HEIGHT(*lp);
2640 if (fldata->data >= fldata->data_max)
2650 fetch_lines (GtkSText* text,
2651 const GtkSPropertyMark* mark0,
2652 const PrevTabCont* tab_cont0,
2656 FetchLinesData fl_data;
2658 fl_data.new_lines = NULL;
2660 fl_data.data_max = data;
2661 fl_data.fl_type = fl_type;
2663 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2665 return g_list_reverse (fl_data.new_lines);
2669 fetch_lines_backward (GtkSText* text)
2671 GList* new_lines = NULL, *new_line_start;
2672 GtkSPropertyMark mark;
2674 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2677 mark = find_this_line_start_mark (text,
2678 CACHE_DATA(text->line_start_cache).start.index - 1,
2679 &CACHE_DATA(text->line_start_cache).start);
2681 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2683 while (new_line_start->next)
2684 new_line_start = new_line_start->next;
2686 new_line_start->next = text->line_start_cache;
2687 text->line_start_cache->prev = new_line_start;
2691 fetch_lines_forward (GtkSText* text, gint line_count)
2693 GtkSPropertyMark mark;
2694 GList* line = text->line_start_cache;
2699 mark = CACHE_DATA(line).end;
2701 if (LAST_INDEX (text, mark))
2704 advance_mark(&mark);
2706 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2709 line->next->prev = line;
2712 /* Compute the number of lines, and vertical pixels for n characters
2713 * starting from the point
2716 compute_lines_pixels (GtkSText* text, guint char_count,
2717 guint *lines, guint *pixels)
2719 GList *line = text->current_line;
2720 gint chars_left = char_count;
2725 /* If chars_left == 0, that means we're joining two lines in a
2726 * deletion, so add in the values for the next line as well
2728 for (; line && chars_left >= 0; line = line->next)
2730 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2732 if (line == text->current_line)
2733 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2735 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2737 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2741 chars_left = 0; /* force another loop */
2744 fetch_lines_forward (text, 1);
2749 total_line_height (GtkSText* text, GList* line, gint line_count)
2753 for (; line && line_count > 0; line = line->next)
2755 height += LINE_HEIGHT(CACHE_DATA(line));
2757 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2761 fetch_lines_forward (text, line_count);
2768 swap_lines (GtkSText* text, GList* old, GList* new, guint old_line_count)
2770 if (old == text->line_start_cache)
2774 for (; old_line_count > 0; old_line_count -= 1)
2776 while (text->line_start_cache &&
2778 CACHE_DATA(text->line_start_cache).wraps)
2779 remove_cache_line(text, text->line_start_cache);
2781 remove_cache_line(text, text->line_start_cache);
2784 last = g_list_last (new);
2786 last->next = text->line_start_cache;
2788 if (text->line_start_cache)
2789 text->line_start_cache->prev = last;
2791 text->line_start_cache = new;
2797 g_assert (old->prev);
2801 for (; old_line_count > 0; old_line_count -= 1)
2803 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2804 old = remove_cache_line (text, old);
2806 old = remove_cache_line (text, old);
2812 last = g_list_last (new);
2822 correct_cache_delete (GtkSText* text, gint nchars, gint lines)
2824 GList* cache = text->current_line;
2827 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2830 for (; cache; cache = cache->next)
2832 GtkSPropertyMark *start = &CACHE_DATA(cache).start;
2833 GtkSPropertyMark *end = &CACHE_DATA(cache).end;
2835 start->index -= nchars;
2836 end->index -= nchars;
2838 if (LAST_INDEX (text, text->point) &&
2839 start->index == text->point.index)
2840 *start = text->point;
2841 else if (start->property == text->point.property)
2842 start->offset = start->index - (text->point.index - text->point.offset);
2844 if (LAST_INDEX (text, text->point) &&
2845 end->index == text->point.index)
2847 if (end->property == text->point.property)
2848 end->offset = end->index - (text->point.index - text->point.offset);
2850 /*TEXT_ASSERT_MARK(text, start, "start");*/
2851 /*TEXT_ASSERT_MARK(text, end, "end");*/
2856 delete_expose (GtkSText* text, guint nchars, guint old_lines, guint old_pixels)
2858 GtkWidget *widget = GTK_WIDGET (text);
2861 guint new_pixels = 0;
2863 GList* new_line = NULL;
2866 text->cursor_virtual_x = 0;
2868 correct_cache_delete (text, nchars, old_lines);
2870 pixel_height = pixel_height_of(text, text->current_line) -
2871 LINE_HEIGHT(CACHE_DATA(text->current_line));
2873 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2874 CACHE_DATA(text->current_line).start = text->point;
2876 new_line = fetch_lines (text,
2877 &CACHE_DATA(text->current_line).start,
2878 &CACHE_DATA(text->current_line).tab_cont,
2882 swap_lines (text, text->current_line, new_line, old_lines);
2884 text->current_line = new_line;
2886 new_pixels = total_line_height (text, new_line, 1);
2888 gdk_window_get_size (text->text_area, &width, &height);
2890 if (old_pixels != new_pixels)
2892 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2894 gdk_draw_pixmap (text->text_area,
2898 pixel_height + old_pixels,
2900 pixel_height + new_pixels,
2904 text->vadj->upper += new_pixels;
2905 text->vadj->upper -= old_pixels;
2906 adjust_adj (text, text->vadj);
2910 rect.y = pixel_height;
2912 rect.height = new_pixels;
2914 expose_text (text, &rect, FALSE);
2915 gtk_stext_draw_focus ( (GtkWidget *) text);
2917 text->cursor_mark = text->point;
2919 find_cursor (text, TRUE);
2921 if (old_pixels != new_pixels)
2923 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2926 rect.y = pixel_height + new_pixels;
2928 rect.height = height - rect.y;
2930 expose_text (text, &rect, FALSE);
2933 process_exposes (text);
2940 /* note, the point has already been moved forward */
2942 correct_cache_insert (GtkSText* text, gint nchars)
2945 GtkSPropertyMark *start;
2946 GtkSPropertyMark *end;
2948 /* If we inserted a property exactly at the beginning of the
2949 * line, we have to correct here, or fetch_lines will
2952 start = &CACHE_DATA(text->current_line).start;
2953 if (start->index == text->point.index - nchars)
2955 *start = text->point;
2956 move_mark_n (start, -nchars);
2959 /* Now correct the offsets, and check for start or end marks that
2960 * are after the point, yet point to a property before the point's
2961 * property. This indicates that they are meant to point to the
2962 * second half of a property we split in insert_text_property(), so
2963 * we fix them up that way.
2965 cache = text->current_line->next;
2967 for (; cache; cache = cache->next)
2969 start = &CACHE_DATA(cache).start;
2970 end = &CACHE_DATA(cache).end;
2972 if (LAST_INDEX (text, text->point) &&
2973 start->index == text->point.index)
2974 *start = text->point;
2977 if (start->property == text->point.property)
2979 start->offset += nchars;
2980 start->index += nchars;
2982 else if (start->property->next &&
2983 (start->property->next->next == text->point.property))
2985 /* We split the property, and this is the second half */
2986 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2987 start->index += nchars;
2988 start->property = text->point.property;
2991 start->index += nchars;
2994 if (LAST_INDEX (text, text->point) &&
2995 end->index == text->point.index)
2999 if (end->property == text->point.property)
3001 end->offset += nchars;
3002 end->index += nchars;
3004 else if (end->property->next &&
3005 (end->property->next->next == text->point.property))
3007 /* We split the property, and this is the second half */
3008 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
3009 end->index += nchars;
3010 end->property = text->point.property;
3013 end->index += nchars;
3016 /*TEXT_ASSERT_MARK(text, start, "start");*/
3017 /*TEXT_ASSERT_MARK(text, end, "end");*/
3023 insert_expose (GtkSText* text, guint old_pixels, gint nchars,
3024 guint new_line_count)
3026 GtkWidget *widget = GTK_WIDGET (text);
3029 guint new_pixels = 0;
3031 GList* new_lines = NULL;
3034 text->cursor_virtual_x = 0;
3036 undraw_cursor (text, FALSE);
3038 correct_cache_insert (text, nchars);
3040 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
3042 pixel_height = pixel_height_of(text, text->current_line) -
3043 LINE_HEIGHT(CACHE_DATA(text->current_line));
3045 new_lines = fetch_lines (text,
3046 &CACHE_DATA(text->current_line).start,
3047 &CACHE_DATA(text->current_line).tab_cont,
3051 swap_lines (text, text->current_line, new_lines, 1);
3053 text->current_line = new_lines;
3055 new_pixels = total_line_height (text, new_lines, new_line_count);
3057 gdk_window_get_size (text->text_area, &width, &height);
3059 if (old_pixels != new_pixels)
3061 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
3063 gdk_draw_pixmap (text->text_area,
3067 pixel_height + old_pixels,
3069 pixel_height + new_pixels,
3071 height + (old_pixels - new_pixels) - pixel_height);
3074 text->vadj->upper += new_pixels;
3075 text->vadj->upper -= old_pixels;
3076 adjust_adj (text, text->vadj);
3080 rect.y = pixel_height;
3082 rect.height = new_pixels;
3084 expose_text (text, &rect, FALSE);
3085 gtk_stext_draw_focus ( (GtkWidget *) text);
3087 text->cursor_mark = text->point;
3089 find_cursor (text, TRUE);
3091 draw_cursor (text, FALSE);
3093 if (old_pixels != new_pixels)
3095 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
3098 rect.y = pixel_height + new_pixels;
3100 rect.height = height - rect.y;
3102 expose_text (text, &rect, FALSE);
3105 process_exposes (text);
3108 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
3113 /* Text property functions */
3116 font_hash (gconstpointer font)
3118 return gdk_font_id ((const GdkFont*) font);
3121 static GHashTable *font_cache_table = NULL;
3123 static GtkSTextFont*
3124 get_text_font (GdkFont* gfont)
3129 if (!font_cache_table)
3130 font_cache_table = g_hash_table_new (font_hash, (GCompareFunc) gdk_font_equal);
3132 tf = g_hash_table_lookup (font_cache_table, gfont);
3140 tf = g_new (GtkSTextFont, 1);
3143 tf->gdk_font = gfont;
3144 gdk_font_ref (gfont);
3146 for(i = 0; i < 256; i += 1)
3147 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
3149 g_hash_table_insert (font_cache_table, gfont, tf);
3155 text_font_unref (GtkSTextFont *text_font)
3157 text_font->ref_count--;
3158 if (text_font->ref_count == 0)
3160 g_hash_table_remove (font_cache_table, text_font->gdk_font);
3161 gdk_font_unref (text_font->gdk_font);
3167 text_properties_equal (TextProperty* prop, GdkFont* font, GdkColor *fore, GdkColor *back)
3169 if (prop->flags & PROPERTY_FONT)
3172 GtkSTextFont *text_font;
3177 text_font = get_text_font (font);
3179 retval = (prop->font == text_font);
3180 text_font_unref (text_font);
3189 if (prop->flags & PROPERTY_FOREGROUND)
3191 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
3198 if (prop->flags & PROPERTY_BACKGROUND)
3200 if (!back || !gdk_color_equal (&prop->back_color, back))
3211 realize_property (GtkSText *text, TextProperty *prop)
3213 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3215 if (prop->flags & PROPERTY_FOREGROUND)
3216 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
3218 if (prop->flags & PROPERTY_BACKGROUND)
3219 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
3223 realize_properties (GtkSText *text)
3225 GList *tmp_list = text->text_properties;
3229 realize_property (text, tmp_list->data);
3231 tmp_list = tmp_list->next;
3236 unrealize_property (GtkSText *text, TextProperty *prop)
3238 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3240 if (prop->flags & PROPERTY_FOREGROUND)
3241 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
3243 if (prop->flags & PROPERTY_BACKGROUND)
3244 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
3248 unrealize_properties (GtkSText *text)
3250 GList *tmp_list = text->text_properties;
3254 unrealize_property (text, tmp_list->data);
3256 tmp_list = tmp_list->next;
3260 static TextProperty*
3261 new_text_property (GtkSText *text, GdkFont *font, GdkColor* fore,
3262 GdkColor* back, guint length)
3266 if (text_property_chunk == NULL)
3268 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
3269 sizeof(TextProperty),
3270 1024*sizeof(TextProperty),
3274 prop = g_chunk_new(TextProperty, text_property_chunk);
3279 prop->flags |= PROPERTY_FONT;
3280 prop->font = get_text_font (font);
3287 prop->flags |= PROPERTY_FOREGROUND;
3288 prop->fore_color = *fore;
3293 prop->flags |= PROPERTY_BACKGROUND;
3294 prop->back_color = *back;
3297 prop->length = length;
3299 if (GTK_WIDGET_REALIZED (text))
3300 realize_property (text, prop);
3306 destroy_text_property (TextProperty *prop)
3309 text_font_unref (prop->font);
3311 g_mem_chunk_free (text_property_chunk, prop);
3314 /* Flop the memory between the point and the gap around like a
3317 move_gap (GtkSText* text, guint index)
3319 if (text->gap_position < index)
3321 gint diff = index - text->gap_position;
3323 if (text->use_wchar)
3324 g_memmove (text->text.wc + text->gap_position,
3325 text->text.wc + text->gap_position + text->gap_size,
3326 diff*sizeof (GdkWChar));
3328 g_memmove (text->text.ch + text->gap_position,
3329 text->text.ch + text->gap_position + text->gap_size,
3332 text->gap_position = index;
3334 else if (text->gap_position > index)
3336 gint diff = text->gap_position - index;
3338 if (text->use_wchar)
3339 g_memmove (text->text.wc + index + text->gap_size,
3340 text->text.wc + index,
3341 diff*sizeof (GdkWChar));
3343 g_memmove (text->text.ch + index + text->gap_size,
3344 text->text.ch + index,
3347 text->gap_position = index;
3351 /* Increase the gap size. */
3353 make_forward_space (GtkSText* text, guint len)
3355 if (text->gap_size < len)
3357 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3359 if (sum >= text->text_len)
3363 while (i <= sum) i <<= 1;
3365 if (text->use_wchar)
3366 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3367 i*sizeof(GdkWChar));
3369 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3373 if (text->use_wchar)
3374 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3375 text->text.wc + text->gap_position + text->gap_size,
3376 (text->text_end - (text->gap_position + text->gap_size))
3379 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3380 text->text.ch + text->gap_position + text->gap_size,
3381 text->text_end - (text->gap_position + text->gap_size));
3383 text->text_end += len*2;
3384 text->gap_size += len*2;
3388 /* Inserts into the text property list a list element that guarantees
3389 * that for len characters following the point, text has the correct
3390 * property. does not move point. adjusts text_properties_point and
3391 * text_properties_point_offset relative to the current value of
3394 insert_text_property (GtkSText* text, GdkFont* font,
3395 GdkColor *fore, GdkColor* back, guint len)
3397 GtkSPropertyMark *mark = &text->point;
3398 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3399 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3401 if (MARK_OFFSET(mark) == 0)
3403 /* Point is on the boundary of two properties.
3404 * If it is the same as either, grow, else insert
3407 if (text_properties_equal(forward_prop, font, fore, back))
3409 /* Grow the property in front of us. */
3411 MARK_PROPERTY_LENGTH(mark) += len;
3413 else if (backward_prop &&
3414 text_properties_equal(backward_prop, font, fore, back))
3416 /* Grow property behind us, point property and offset
3419 SET_PROPERTY_MARK (&text->point,
3420 MARK_PREV_LIST_PTR (mark),
3421 backward_prop->length);
3423 backward_prop->length += len;
3425 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3426 (forward_prop->length == 1))
3428 /* Next property just has last position, take it over */
3430 if (GTK_WIDGET_REALIZED (text))
3431 unrealize_property (text, forward_prop);
3433 forward_prop->flags = 0;
3436 forward_prop->flags |= PROPERTY_FONT;
3437 forward_prop->font = get_text_font (font);
3440 forward_prop->font = NULL;
3444 forward_prop->flags |= PROPERTY_FOREGROUND;
3445 forward_prop->fore_color = *fore;
3449 forward_prop->flags |= PROPERTY_BACKGROUND;
3450 forward_prop->back_color = *back;
3452 forward_prop->length += len;
3454 if (GTK_WIDGET_REALIZED (text))
3455 realize_property (text, forward_prop);
3459 /* Splice a new property into the list. */
3461 GList* new_prop = g_list_alloc();
3463 new_prop->next = MARK_LIST_PTR(mark);
3464 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3465 new_prop->next->prev = new_prop;
3468 new_prop->prev->next = new_prop;
3470 new_prop->data = new_text_property (text, font, fore, back, len);
3472 SET_PROPERTY_MARK (mark, new_prop, 0);
3477 /* The following will screw up the line_start cache,
3478 * we'll fix it up in correct_cache_insert
3481 /* In the middle of forward_prop, if properties are equal,
3482 * just add to its length, else split it into two and splice
3484 if (text_properties_equal (forward_prop, font, fore, back))
3486 forward_prop->length += len;
3488 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3489 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3491 /* Inserting before only the last position in the text */
3494 forward_prop->length -= 1;
3496 new_prop = g_list_alloc();
3497 new_prop->data = new_text_property (text, font, fore, back, len+1);
3498 new_prop->prev = MARK_LIST_PTR(mark);
3499 new_prop->next = NULL;
3500 MARK_NEXT_LIST_PTR(mark) = new_prop;
3502 SET_PROPERTY_MARK (mark, new_prop, 0);
3506 GList* new_prop = g_list_alloc();
3507 GList* new_prop_forward = g_list_alloc();
3508 gint old_length = forward_prop->length;
3509 GList* next = MARK_NEXT_LIST_PTR(mark);
3511 /* Set the new lengths according to where they are split. Construct
3512 * two new properties. */
3513 forward_prop->length = MARK_OFFSET(mark);
3515 new_prop_forward->data =
3516 new_text_property(text,
3517 forward_prop->flags & PROPERTY_FONT ?
3518 forward_prop->font->gdk_font : NULL,
3519 forward_prop->flags & PROPERTY_FOREGROUND ?
3520 &forward_prop->fore_color : NULL,
3521 forward_prop->flags & PROPERTY_BACKGROUND ?
3522 &forward_prop->back_color : NULL,
3523 old_length - forward_prop->length);
3525 new_prop->data = new_text_property(text, font, fore, back, len);
3527 /* Now splice things in. */
3528 MARK_NEXT_LIST_PTR(mark) = new_prop;
3529 new_prop->prev = MARK_LIST_PTR(mark);
3531 new_prop->next = new_prop_forward;
3532 new_prop_forward->prev = new_prop;
3534 new_prop_forward->next = next;
3537 next->prev = new_prop_forward;
3539 SET_PROPERTY_MARK (mark, new_prop, 0);
3543 while (text->text_properties_end->next)
3544 text->text_properties_end = text->text_properties_end->next;
3546 while (text->text_properties->prev)
3547 text->text_properties = text->text_properties->prev;
3551 delete_text_property (GtkSText* text, guint nchars)
3553 /* Delete nchars forward from point. */
3555 /* Deleting text properties is problematical, because we
3556 * might be storing around marks pointing to a property.
3558 * The marks in question and how we handle them are:
3560 * point: We know the new value, since it will be at the
3561 * end of the deleted text, and we move it there
3563 * cursor: We just remove the mark and set it equal to the
3564 * point after the operation.
3565 * line-start cache: We replace most affected lines.
3566 * The current line gets used to fetch the new
3567 * lines so, if necessary, (delete at the beginning
3568 * of a line) we fix it up by setting it equal to the
3576 for(; nchars; nchars -= 1)
3578 prop = MARK_CURRENT_PROPERTY(&text->point);
3582 if (prop->length == 0)
3584 tmp = MARK_LIST_PTR (&text->point);
3586 is_first = tmp == text->text_properties;
3588 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3589 text->point.offset = 0;
3591 if (GTK_WIDGET_REALIZED (text))
3592 unrealize_property (text, prop);
3594 destroy_text_property (prop);
3595 g_list_free_1 (tmp);
3597 prop = MARK_CURRENT_PROPERTY (&text->point);
3600 text->text_properties = MARK_LIST_PTR (&text->point);
3602 g_assert (prop->length != 0);
3604 else if (prop->length == text->point.offset)
3606 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3607 text->point.offset = 0;
3611 /* Check to see if we have just the single final position remaining
3612 * along in a property; if so, combine it with the previous property
3614 if (LAST_INDEX (text, text->point) &&
3615 (MARK_OFFSET (&text->point) == 0) &&
3616 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3618 tmp = MARK_LIST_PTR (&text->point);
3619 prop = MARK_CURRENT_PROPERTY(&text->point);
3621 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3622 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3623 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3625 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3627 if (GTK_WIDGET_REALIZED (text))
3628 unrealize_property (text, prop);
3630 destroy_text_property (prop);
3631 g_list_free_1 (tmp);
3636 init_properties (GtkSText *text)
3638 if (!text->text_properties)
3640 text->text_properties = g_list_alloc();
3641 text->text_properties->next = NULL;
3642 text->text_properties->prev = NULL;
3643 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3644 text->text_properties_end = text->text_properties;
3646 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3648 text->point.index = 0;
3653 /**********************************************************************/
3654 /* Property Movement */
3655 /**********************************************************************/
3658 move_mark_n (GtkSPropertyMark* mark, gint n)
3661 advance_mark_n(mark, n);
3663 decrement_mark_n(mark, -n);
3667 advance_mark (GtkSPropertyMark* mark)
3669 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3673 if (prop->length > mark->offset + 1)
3677 mark->property = MARK_NEXT_LIST_PTR (mark);
3683 advance_mark_n (GtkSPropertyMark* mark, gint n)
3690 i = 0; /* otherwise it migth not be init. */
3691 prop = MARK_CURRENT_PROPERTY(mark);
3693 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3694 /* to make it easier */
3695 n += (mark->offset);
3696 mark->index -= mark->offset;
3698 /* first we take seven-mile-leaps to get to the right text
3700 while ((n-i) > prop->length - 1) {
3702 mark->index += prop->length;
3703 mark->property = MARK_NEXT_LIST_PTR (mark);
3704 prop = MARK_CURRENT_PROPERTY (mark);
3708 /* and then the rest */
3709 mark->index += n - i;
3710 mark->offset += n - i;
3714 decrement_mark (GtkSPropertyMark* mark)
3718 if (mark->offset > 0)
3722 mark->property = MARK_PREV_LIST_PTR (mark);
3723 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3728 decrement_mark_n (GtkSPropertyMark* mark, gint n)
3732 while (mark->offset < n) {
3733 /* jump to end of prev */
3734 n -= mark->offset + 1;
3735 mark->index -= mark->offset + 1;
3736 mark->property = MARK_PREV_LIST_PTR (mark);
3737 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3745 static GtkSPropertyMark
3746 find_mark (GtkSText* text, guint mark_position)
3748 return find_mark_near (text, mark_position, &text->point);
3752 * You can also start from the end, what a drag.
3754 static GtkSPropertyMark
3755 find_mark_near (GtkSText* text, guint mark_position, const GtkSPropertyMark* near)
3760 GtkSPropertyMark mark;
3763 diffa = mark_position + 1;
3765 diffa = mark_position - near->index;
3767 diffb = mark_position;
3779 mark.property = text->text_properties;
3783 move_mark_n (&mark, mark_position - mark.index);
3788 /* This routine must be called with scroll == FALSE, only when
3789 * point is at least partially on screen
3793 find_line_containing_point (GtkSText* text, guint point,
3799 text->current_line = NULL;
3803 /* Scroll backwards until the point is on screen
3805 while (CACHE_DATA(text->line_start_cache).start.index > point)
3806 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3808 /* Now additionally try to make sure that the point is fully on screen
3812 while (text->first_cut_pixels != 0 &&
3813 text->line_start_cache->next &&
3814 CACHE_DATA(text->line_start_cache->next).start.index > point)
3815 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3818 gdk_window_get_size (text->text_area, NULL, &height);
3820 for (cache = text->line_start_cache; cache; cache = cache->next)
3824 if (CACHE_DATA(cache).end.index >= point ||
3825 LAST_INDEX(text, CACHE_DATA(cache).end))
3827 text->current_line = cache; /* LOOK HERE, this proc has an
3828 * important side effect. */
3832 TEXT_SHOW_LINE (text, cache, "cache");
3834 if (cache->next == NULL)
3835 fetch_lines_forward (text, 1);
3839 lph = pixel_height_of (text, cache->next);
3841 /* Scroll the bottom of the line is on screen, or until
3842 * the line is the first onscreen line.
3844 while (cache->next != text->line_start_cache && lph > height)
3846 TEXT_SHOW_LINE (text, cache, "cache");
3847 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3848 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3849 lph = pixel_height_of (text, cache->next);
3854 g_assert_not_reached (); /* Must set text->current_line here */
3858 pixel_height_of (GtkSText* text, GList* cache_line)
3860 gint pixels = - text->first_cut_pixels;
3861 GList *cache = text->line_start_cache;
3864 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3866 if (cache->data == cache_line->data)
3869 cache = cache->next;
3875 /**********************************************************************/
3876 /* Search and Placement */
3877 /**********************************************************************/
3880 find_char_width (GtkSText* text, const GtkSPropertyMark *mark, const TabStopMark *tab_mark)
3883 gint16* char_widths;
3885 if (LAST_INDEX (text, *mark))
3888 ch = GTK_STEXT_INDEX (text, mark->index);
3889 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3893 return tab_mark->to_next_tab * char_widths[' '];
3895 else if (!text->use_wchar)
3897 return char_widths[ch];
3901 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3906 advance_tab_mark (GtkSText* text, TabStopMark* tab_mark, GdkWChar ch)
3908 if (tab_mark->to_next_tab == 1 || ch == '\t')
3910 if (tab_mark->tab_stops->next)
3912 tab_mark->tab_stops = tab_mark->tab_stops->next;
3913 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3917 tab_mark->to_next_tab = text->default_tab_width;
3922 tab_mark->to_next_tab -= 1;
3927 advance_tab_mark_n (GtkSText* text, TabStopMark* tab_mark, gint n)
3931 advance_tab_mark (text, tab_mark, 0);
3935 find_cursor_at_line (GtkSText* text, const LineParams* start_line, gint pixel_height)
3938 GtkEditable *editable = (GtkEditable *)text;
3940 GtkSPropertyMark mark = start_line->start;
3941 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3942 gint pixel_width = LINE_START_PIXEL (*start_line);
3944 while (mark.index < text->cursor_mark.index)
3946 pixel_width += find_char_width (text, &mark, &tab_mark);
3948 advance_tab_mark (text, &tab_mark, GTK_STEXT_INDEX(text, mark.index));
3949 advance_mark (&mark);
3952 text->cursor_pos_x = pixel_width;
3953 text->cursor_pos_y = pixel_height;
3954 text->cursor_char_offset = start_line->font_descent;
3955 text->cursor_mark = mark;
3957 ch = LAST_INDEX (text, mark) ?
3958 LINE_DELIM : GTK_STEXT_INDEX (text, mark.index);
3960 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3961 text->cursor_char = 0;
3963 text->cursor_char = ch;
3965 #ifdef USE_GTKGDK_XIM
3966 if (GTK_WIDGET_HAS_FOCUS(text) && gdk_im_ready() && editable->ic &&
3967 (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
3969 GdkICAttributesType mask = GDK_IC_SPOT_LOCATION |
3970 GDK_IC_PREEDIT_FOREGROUND |
3971 GDK_IC_PREEDIT_BACKGROUND;
3973 editable->ic_attr->spot_location.x = text->cursor_pos_x;
3974 editable->ic_attr->spot_location.y
3975 = text->cursor_pos_y - text->cursor_char_offset;
3976 editable->ic_attr->preedit_foreground = *MARK_CURRENT_FORE (text, &mark);
3977 editable->ic_attr->preedit_background = *MARK_CURRENT_BACK (text, &mark);
3979 if (MARK_CURRENT_FONT (text, &mark)->type == GDK_FONT_FONTSET)
3981 mask |= GDK_IC_PREEDIT_FONTSET;
3982 editable->ic_attr->preedit_fontset = MARK_CURRENT_FONT (text, &mark);
3985 gdk_ic_set_attr (editable->ic, editable->ic_attr, mask);
3991 find_cursor (GtkSText* text, gboolean scroll)
3993 if (GTK_WIDGET_REALIZED (text))
3995 find_line_containing_point (text, text->cursor_mark.index, scroll);
3997 if (text->current_line)
3998 find_cursor_at_line (text,
3999 &CACHE_DATA(text->current_line),
4000 pixel_height_of(text, text->current_line));
4003 GTK_EDITABLE (text)->current_pos = text->cursor_mark.index;
4007 find_mouse_cursor_at_line (GtkSText *text, const LineParams* lp,
4008 guint line_pixel_height,
4011 GtkSPropertyMark mark = lp->start;
4012 TabStopMark tab_mark = lp->tab_cont.tab_start;
4014 gint char_width = find_char_width(text, &mark, &tab_mark);
4015 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
4017 text->cursor_pos_y = line_pixel_height;
4021 GdkWChar ch = LAST_INDEX (text, mark) ?
4022 LINE_DELIM : GTK_STEXT_INDEX (text, mark.index);
4024 if (button_x < pixel_width || mark.index == lp->end.index)
4026 text->cursor_pos_x = pixel_width - (char_width+1)/2;
4027 text->cursor_mark = mark;
4028 text->cursor_char_offset = lp->font_descent;
4030 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
4031 text->cursor_char = 0;
4033 text->cursor_char = ch;
4038 advance_tab_mark (text, &tab_mark, ch);
4039 advance_mark (&mark);
4041 pixel_width += char_width/2;
4043 char_width = find_char_width (text, &mark, &tab_mark);
4045 pixel_width += (char_width+1)/2;
4050 find_mouse_cursor (GtkSText* text, gint x, gint y)
4053 GList* cache = text->line_start_cache;
4057 pixel_height = - text->first_cut_pixels;
4059 for (; cache; cache = cache->next)
4061 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
4063 if (y < pixel_height || !cache->next)
4065 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
4067 find_cursor (text, FALSE);
4074 /**********************************************************************/
4076 /**********************************************************************/
4079 free_cache (GtkSText* text)
4081 GList* cache = text->line_start_cache;
4086 cache = cache->prev;
4088 text->line_start_cache = cache;
4091 for (; cache; cache = cache->next)
4092 g_mem_chunk_free (params_mem_chunk, cache->data);
4094 g_list_free (text->line_start_cache);
4096 text->line_start_cache = NULL;
4100 remove_cache_line (GtkSText* text, GList* member)
4107 if (member == text->line_start_cache)
4108 text->line_start_cache = text->line_start_cache->next;
4111 member->prev->next = member->next;
4114 member->next->prev = member->prev;
4116 list = member->next;
4118 g_mem_chunk_free (params_mem_chunk, member->data);
4119 g_list_free_1 (member);
4124 /**********************************************************************/
4126 /**********************************************************************/
4129 move_cursor_buffer_ver (GtkSText *text, int dir)
4131 undraw_cursor (text, FALSE);
4135 scroll_int (text, text->vadj->upper);
4136 text->cursor_mark = find_this_line_start_mark (text,
4138 &text->cursor_mark);
4142 scroll_int (text, - text->vadj->value);
4143 text->cursor_mark = find_this_line_start_mark (text,
4145 &text->cursor_mark);
4148 find_cursor (text, TRUE);
4149 draw_cursor (text, FALSE);
4153 move_cursor_page_ver (GtkSText *text, int dir)
4155 scroll_int (text, dir * text->vadj->page_increment);
4159 move_cursor_ver (GtkSText *text, int count)
4162 GtkSPropertyMark mark;
4165 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
4166 offset = text->cursor_mark.index - mark.index;
4168 if (offset > text->cursor_virtual_x)
4169 text->cursor_virtual_x = offset;
4173 if (mark.index == 0)
4176 decrement_mark (&mark);
4177 mark = find_this_line_start_mark (text, mark.index, &mark);
4181 mark = text->cursor_mark;
4183 while (!LAST_INDEX(text, mark) && GTK_STEXT_INDEX(text, mark.index) != LINE_DELIM)
4184 advance_mark (&mark);
4186 if (LAST_INDEX(text, mark))
4189 advance_mark (&mark);
4192 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
4193 if (LAST_INDEX(text, mark) ||
4194 GTK_STEXT_INDEX(text, mark.index) == LINE_DELIM)
4197 undraw_cursor (text, FALSE);
4199 text->cursor_mark = mark;
4201 find_cursor (text, TRUE);
4203 draw_cursor (text, FALSE);
4207 move_cursor_hor (GtkSText *text, int count)
4209 /* count should be +-1. */
4210 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
4211 (count < 0 && text->cursor_mark.index < (- count)) ||
4215 text->cursor_virtual_x = 0;
4217 undraw_cursor (text, FALSE);
4219 move_mark_n (&text->cursor_mark, count);
4221 find_cursor (text, TRUE);
4223 draw_cursor (text, FALSE);
4226 /* SYLPHEED - the default cursor movement of GtkText is aeons ahead of the
4227 * current technology level of the current generation of programmers and
4228 * end users. so sylpheed has to take a more mundane approach to editing.
4231 * move_cursor_to_display_row_end() (end of display line)
4232 * move_cursor_to_display_row_home() (home of display line)
4233 * move_cursor_to_display_row_up() (line up)
4234 * move_cursor_to_display_row_down() (line down)
4238 * SYLPHEED_TODO: for some reason the line fetcher also returns markers
4239 * of just one character! should investigate this... -- alfons
4242 static void move_cursor_to_display_row_end(GtkSText *text)
4244 LineParams lp = CACHE_DATA(text->current_line);
4245 int to_move = (lp.end.index - text->cursor_mark.index);
4247 /* advance this much */
4249 move_cursor_hor(text, to_move);
4253 static void move_cursor_to_display_row_start(GtkSText *text)
4255 LineParams lp = CACHE_DATA(text->current_line);
4256 int to_move = (text->cursor_mark.index - lp.start.index);
4258 move_cursor_hor(text, -to_move);
4263 static gboolean range_intersect(guint x1, guint x2, guint y1, guint y2)
4266 if (x1 > x2) { tmp = x1; x1 = x2; x2 = tmp; }
4267 if (y1 > y2) { tmp = y1; y1 = y2; y1 = tmp; }
4268 if (y1 < x1) { tmp = x1; x1 = y1; y1 = tmp; tmp = x2; x2 = y2; y2 = tmp; }
4278 static gint back_display_row_fetcher(GtkSText *text,
4282 if (range_intersect(data->start, data->end, params->start.index, params->end.index)) {
4283 XDEBUG( ("%s(%d) - FOUND search (%d, %d), current (%d, %d)", __FILE__, __LINE__,
4284 data->start, data->end,
4285 params->start.index, params->end.index) );
4290 XDEBUG( ("%s(%d) - NEXT search (%d, %d), current (%d, %d)", __FILE__, __LINE__,
4291 data->start, data->end,
4292 params->start.index, params->end.index) );
4298 static void move_cursor_to_display_row_up(GtkSText *text)
4304 GtkSPropertyMark mark;
4306 mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
4309 if (mark.index == 0) {
4310 XDEBUG ( ("%s(%d) top of buffer", __FILE__, __LINE__) );
4313 /* we need previous DISPLAY row not the previous BUFFER line, so we go to start
4314 * of paragraph, and iterate over the lines until we have a LineParams that matches
4316 lp = CACHE_DATA(text->current_line);
4317 col = (text->cursor_mark.index - lp.start.index);
4318 data.start = lp.start.index;
4319 data.end = lp.end.index;
4321 /* get the previous line */
4322 if (mark.index != 0) {
4323 decrement_mark(&mark);
4324 if (mark.index != 0) {
4325 GtkSPropertyMark smark = mark;
4326 XDEBUG( ("%s(%d) finding line start mark", __FILE__, __LINE__) );
4327 mark = find_this_line_start_mark(text, smark.index -1, &smark);
4331 /* let's get the previous display line */
4332 XDEBUG( ("%s(%d) iterating to get display lines", __FILE__, __LINE__) );
4333 line_params_iterate(text, &mark, NULL, FALSE, &data,
4334 (LineIteratorFunction)back_display_row_fetcher);
4335 XDEBUG( ("%s(%d) done iterating. found = %d", __FILE__, __LINE__, data.found) );
4338 if (col < text->persist_column) col = text->persist_column;
4339 else text->persist_column = col;
4340 new_index = data.lp.start.index + col;
4341 XDEBUG( ("%s(%d) - new index = %d", __FILE__, __LINE__, new_index) );
4342 if (new_index > data.lp.end.index) {
4343 new_index = data.lp.end.index;
4345 /* and move the cursor */
4346 XDEBUG( ("%s(%d) - setting index", __FILE__, __LINE__) );
4347 gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);
4358 #if defined(AHX_DEBUG)
4359 static void print_line(GtkSText *text, guint start, guint end)
4361 gchar *buf = alloca(2048), *walk = buf;
4363 memset(buf, 0, 2048);
4364 for (; start <= end; start++) {
4365 *walk++ = GTK_STEXT_INDEX(text, start);
4367 XDEBUG( ("%s", buf) );
4371 static gint forward_display_row_fetcher(GtkSText *text,
4376 XDEBUG( ("%s(%d) - FW RETURNS. search (%d, %d), current (%d, %d)",
4377 __FILE__, __LINE__, data->start, data->end, lp->start.index, lp->end.index) );
4379 data->completed = TRUE;
4380 print_line(text, lp->start.index, lp->end.index);
4383 else if (range_intersect(data->start, data->end, lp->start.index, lp->end.index)) {
4384 XDEBUG( ("%s(%d) - FW FOUND IT. search (%d, %d), current (%d, %d)",
4385 __FILE__, __LINE__, data->start, data->end, lp->start.index, lp->end.index) );
4394 static void move_cursor_to_display_row_down (GtkSText *text)
4399 GtkSPropertyMark mark;
4400 fdrf data = { FALSE, FALSE };
4402 mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
4403 lp = CACHE_DATA(text->current_line);
4404 col = (text->cursor_mark.index - lp.start.index);
4406 data.start = lp.start.index;
4407 data.end = lp.end.index;
4409 /* find the next DISPLAY line */
4410 XDEBUG( ("%s(%d) - FW iterating", __FILE__, __LINE__) ) ;
4411 line_params_iterate(text, &mark, NULL, FALSE, &data,
4412 (LineIteratorFunction)forward_display_row_fetcher);
4413 XDEBUG( ("%s(%d) - FW done iterating", __FILE__, __LINE__) );
4415 if (data.completed) {
4416 if (col < text->persist_column) col = text->persist_column;
4417 else text->persist_column = col;
4418 new_index = data.lp.start.index + col;
4419 if (new_index > data.lp.end.index) {
4420 new_index = data.lp.end.index;
4422 /* and move the cursor */
4423 XDEBUG( ("%s(%d) - FW set pos %d", __FILE__, __LINE__, new_index) );
4424 gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);
4428 static void reset_persist_col_pos(GtkSText *text)
4430 text->persist_column = 0;
4434 gtk_stext_move_cursor (GtkEditable *editable,
4441 move_cursor_hor (GTK_STEXT (editable), 1);
4446 move_cursor_hor (GTK_STEXT (editable), -1);
4452 move_cursor_ver (GTK_STEXT (editable), 1);
4457 move_cursor_ver (GTK_STEXT (editable), -1);
4462 gtk_stext_move_forward_character (GtkSText *text)
4464 move_cursor_hor (text, 1);
4468 gtk_stext_move_backward_character (GtkSText *text)
4470 move_cursor_hor (text, -1);
4474 gtk_stext_move_next_line (GtkSText *text)
4476 move_cursor_ver (text, 1);
4480 gtk_stext_move_previous_line (GtkSText *text)
4482 move_cursor_ver (text, -1);
4486 gtk_stext_move_word (GtkEditable *editable,
4492 gtk_stext_move_forward_word (GTK_STEXT (editable));
4497 gtk_stext_move_backward_word (GTK_STEXT (editable));
4502 gtk_stext_move_forward_word (GtkSText *text)
4504 text->cursor_virtual_x = 0;
4506 undraw_cursor (text, FALSE);
4508 if (text->use_wchar)
4510 while (!LAST_INDEX (text, text->cursor_mark) &&
4511 !gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4512 advance_mark (&text->cursor_mark);
4514 while (!LAST_INDEX (text, text->cursor_mark) &&
4515 gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4516 advance_mark (&text->cursor_mark);
4520 while (!LAST_INDEX (text, text->cursor_mark) &&
4521 !isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4522 advance_mark (&text->cursor_mark);
4524 while (!LAST_INDEX (text, text->cursor_mark) &&
4525 isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4526 advance_mark (&text->cursor_mark);
4529 find_cursor (text, TRUE);
4530 draw_cursor (text, FALSE);
4534 gtk_stext_move_backward_word (GtkSText *text)
4536 text->cursor_virtual_x = 0;
4538 undraw_cursor (text, FALSE);
4540 if (text->use_wchar)
4542 while ((text->cursor_mark.index > 0) &&
4543 !gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4544 decrement_mark (&text->cursor_mark);
4546 while ((text->cursor_mark.index > 0) &&
4547 gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4548 decrement_mark (&text->cursor_mark);
4552 while ((text->cursor_mark.index > 0) &&
4553 !isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4554 decrement_mark (&text->cursor_mark);
4556 while ((text->cursor_mark.index > 0) &&
4557 isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4558 decrement_mark (&text->cursor_mark);
4561 find_cursor (text, TRUE);
4562 draw_cursor (text, FALSE);
4566 gtk_stext_move_page (GtkEditable *editable,
4571 scroll_int (GTK_STEXT (editable),
4572 y * GTK_STEXT(editable)->vadj->page_increment);
4576 gtk_stext_move_to_row (GtkEditable *editable,
4582 gtk_stext_move_to_column (GtkEditable *editable,
4587 text = GTK_STEXT (editable);
4589 text->cursor_virtual_x = 0; /* FIXME */
4591 undraw_cursor (text, FALSE);
4593 /* Move to the beginning of the line */
4594 while ((text->cursor_mark.index > 0) &&
4595 (GTK_STEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4596 decrement_mark (&text->cursor_mark);
4598 while (!LAST_INDEX (text, text->cursor_mark) &&
4599 (GTK_STEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4603 else if (column == 0)
4606 advance_mark (&text->cursor_mark);
4609 find_cursor (text, TRUE);
4610 draw_cursor (text, FALSE);
4614 gtk_stext_move_beginning_of_line (GtkSText *text)
4616 gtk_stext_move_to_column (GTK_EDITABLE (text), 0);
4621 gtk_stext_move_end_of_line (GtkSText *text)
4623 gtk_stext_move_to_column (GTK_EDITABLE (text), -1);
4627 gtk_stext_kill_char (GtkEditable *editable,
4632 text = GTK_STEXT (editable);
4634 if (editable->selection_start_pos != editable->selection_end_pos)
4635 gtk_editable_delete_selection (editable);
4640 if (text->point.index + 1 <= TEXT_LENGTH (text))
4641 gtk_editable_delete_text (editable, text->point.index, text->point.index + 1);
4645 if (text->point.index > 0)
4646 gtk_editable_delete_text (editable, text->point.index - 1, text->point.index);
4652 gtk_stext_delete_forward_character (GtkSText *text)
4654 gtk_stext_kill_char (GTK_EDITABLE (text), 1);
4658 gtk_stext_delete_backward_character (GtkSText *text)
4660 gtk_stext_kill_char (GTK_EDITABLE (text), -1);
4664 gtk_stext_kill_word (GtkEditable *editable,
4667 if (editable->selection_start_pos != editable->selection_end_pos)
4668 gtk_editable_delete_selection (editable);
4671 gint old_pos = editable->current_pos;
4674 gtk_stext_move_word (editable, 1);
4675 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
4679 gtk_stext_move_word (editable, -1);
4680 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
4686 gtk_stext_delete_forward_word (GtkSText *text)
4688 gtk_stext_kill_word (GTK_EDITABLE (text), 1);
4692 gtk_stext_delete_backward_word (GtkSText *text)
4694 gtk_stext_kill_word (GTK_EDITABLE (text), -1);
4698 gtk_stext_kill_line (GtkEditable *editable,
4701 gint old_pos = editable->current_pos;
4704 gtk_stext_move_to_column (editable, -1);
4705 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
4709 gtk_stext_move_to_column (editable, 0);
4710 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
4715 gtk_stext_delete_line (GtkSText *text)
4717 gtk_stext_move_to_column (GTK_EDITABLE (text), 0);
4718 if (GTK_STEXT_INDEX(text, GTK_EDITABLE (text)->current_pos) == LINE_DELIM)
4720 gtk_stext_kill_char (GTK_EDITABLE (text), 1);
4724 gtk_stext_kill_line (GTK_EDITABLE (text), 1);
4729 gtk_stext_delete_to_line_end (GtkSText *text)
4731 if (GTK_STEXT_INDEX(text, GTK_EDITABLE (text)->current_pos) == LINE_DELIM)
4733 gtk_stext_kill_char (GTK_EDITABLE (text), 1);
4737 gtk_stext_kill_line (GTK_EDITABLE (text), 1);
4742 gtk_stext_select_word (GtkSText *text, guint32 time)
4747 GtkEditable *editable;
4748 editable = GTK_EDITABLE (text);
4750 gtk_stext_move_backward_word (text);
4751 start_pos = text->cursor_mark.index;
4753 gtk_stext_move_forward_word (text);
4754 end_pos = text->cursor_mark.index;
4756 editable->has_selection = TRUE;
4757 gtk_stext_set_selection (editable, start_pos, end_pos);
4758 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
4762 gtk_stext_select_line (GtkSText *text, guint32 time)
4767 GtkEditable *editable;
4768 editable = GTK_EDITABLE (text);
4770 gtk_stext_move_beginning_of_line (text);
4771 start_pos = text->cursor_mark.index;
4773 gtk_stext_move_end_of_line (text);
4774 gtk_stext_move_forward_character (text);
4775 end_pos = text->cursor_mark.index;
4777 editable->has_selection = TRUE;
4778 gtk_stext_set_selection (editable, start_pos, end_pos);
4779 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
4782 /**********************************************************************/
4784 /**********************************************************************/
4787 adjust_adj (GtkSText* text, GtkAdjustment* adj)
4791 gdk_window_get_size (text->text_area, NULL, &height);
4793 adj->step_increment = MIN (adj->upper, (float) SCROLL_PIXELS);
4794 adj->page_increment = MIN (adj->upper, height - (float) KEY_SCROLL_PIXELS);
4795 adj->page_size = MIN (adj->upper, height);
4796 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4797 adj->value = MAX (adj->value, 0.0);
4799 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4803 set_vertical_scroll_iterator (GtkSText* text, LineParams* lp, void* data)
4805 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4807 if ((text->first_line_start_index >= lp->start.index) &&
4808 (text->first_line_start_index <= lp->end.index))
4810 svdata->mark = lp->start;
4812 if (text->first_line_start_index == lp->start.index)
4814 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4818 text->first_onscreen_ver_pixel = svdata->pixel_height;
4819 text->first_cut_pixels = 0;
4822 text->vadj->value = (float) text->first_onscreen_ver_pixel;
4825 svdata->pixel_height += LINE_HEIGHT (*lp);
4831 set_vertical_scroll_find_iterator (GtkSText* text, LineParams* lp, void* data)
4833 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4836 if (svdata->pixel_height <= (gint) text->vadj->value &&
4837 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4839 svdata->mark = lp->start;
4841 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4842 text->first_onscreen_ver_pixel = svdata->pixel_height;
4843 text->first_line_start_index = lp->start.index;
4849 svdata->pixel_height += LINE_HEIGHT (*lp);
4857 static GtkSPropertyMark
4858 set_vertical_scroll (GtkSText* text)
4860 GtkSPropertyMark mark = find_mark (text, 0);
4861 SetVerticalScrollData data;
4865 data.pixel_height = 0;
4866 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4868 text->vadj->upper = (float) data.pixel_height;
4869 orig_value = (gint) text->vadj->value;
4871 gdk_window_get_size (text->text_area, NULL, &height);
4873 text->vadj->step_increment = MIN (text->vadj->upper, (float) SCROLL_PIXELS);
4874 text->vadj->page_increment = MIN (text->vadj->upper, height - (float) KEY_SCROLL_PIXELS);
4875 text->vadj->page_size = MIN (text->vadj->upper, height);
4876 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4877 text->vadj->value = MAX (text->vadj->value, 0.0);
4879 text->last_ver_value = (gint)text->vadj->value;
4881 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4883 if (text->vadj->value != orig_value)
4885 /* We got clipped, and don't really know which line to put first. */
4886 data.pixel_height = 0;
4887 data.last_didnt_wrap = TRUE;
4889 line_params_iterate (text, &mark, NULL,
4891 set_vertical_scroll_find_iterator);
4898 scroll_int (GtkSText* text, gint diff)
4902 text->vadj->value += diff;
4904 upper = text->vadj->upper - text->vadj->page_size;
4905 text->vadj->value = MIN (text->vadj->value, upper);
4906 text->vadj->value = MAX (text->vadj->value, 0.0);
4908 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4912 process_exposes (GtkSText *text)
4916 /* Make sure graphics expose events are processed before scrolling
4919 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4921 gtk_widget_event (GTK_WIDGET (text), event);
4922 if (event->expose.count == 0)
4924 gdk_event_free (event);
4927 gdk_event_free (event);
4931 static gint last_visible_line_height (GtkSText* text)
4933 GList *cache = text->line_start_cache;
4936 gdk_window_get_size (text->text_area, NULL, &height);
4938 for (; cache->next; cache = cache->next)
4939 if (pixel_height_of(text, cache->next) > height)
4943 return pixel_height_of(text, cache) - 1;
4948 static gint first_visible_line_height (GtkSText* text)
4950 if (text->first_cut_pixels)
4951 return pixel_height_of(text, text->line_start_cache) + 1;
4957 scroll_down (GtkSText* text, gint diff0)
4963 text->first_onscreen_ver_pixel += diff0;
4967 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4969 text->first_cut_pixels += 1;
4973 text->first_cut_pixels = 0;
4975 text->line_start_cache = text->line_start_cache->next;
4976 g_assert (text->line_start_cache);
4978 text->first_line_start_index =
4979 CACHE_DATA(text->line_start_cache).start.index;
4981 if (!text->line_start_cache->next)
4982 fetch_lines_forward (text, 1);
4988 gdk_window_get_size (text->text_area, &width, &height);
4989 if (height > real_diff)
4990 gdk_draw_pixmap (text->text_area,
4998 height - real_diff);
5001 rect.y = MAX (0, height - real_diff);
5003 rect.height = MIN (height, real_diff);
5005 expose_text (text, &rect, FALSE);
5006 gtk_stext_draw_focus ( (GtkWidget *) text);
5008 if (text->current_line)
5012 text->cursor_pos_y -= real_diff;
5013 cursor_min = drawn_cursor_min(text);
5016 find_mouse_cursor (text, text->cursor_pos_x,
5017 first_visible_line_height (text));
5020 if (height > real_diff)
5021 process_exposes (text);
5025 scroll_up (GtkSText* text, gint diff0)
5031 text->first_onscreen_ver_pixel += diff0;
5035 g_assert (text->line_start_cache);
5037 if (text->first_cut_pixels > 0)
5039 text->first_cut_pixels -= 1;
5043 if (!text->line_start_cache->prev)
5044 fetch_lines_backward (text);
5046 text->line_start_cache = text->line_start_cache->prev;
5048 text->first_line_start_index =
5049 CACHE_DATA(text->line_start_cache).start.index;
5051 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
5057 gdk_window_get_size (text->text_area, &width, &height);
5058 if (height > real_diff)
5059 gdk_draw_pixmap (text->text_area,
5067 height - real_diff);
5072 rect.height = MIN (height, real_diff);
5074 expose_text (text, &rect, FALSE);
5075 gtk_stext_draw_focus ( (GtkWidget *) text);
5077 if (text->current_line)
5082 text->cursor_pos_y += real_diff;
5083 cursor_max = drawn_cursor_max(text);
5084 gdk_window_get_size (text->text_area, NULL, &height);
5086 if (cursor_max >= height)
5087 find_mouse_cursor (text, text->cursor_pos_x,
5088 last_visible_line_height (text));
5091 if (height > real_diff)
5092 process_exposes (text);
5095 /**********************************************************************/
5097 /**********************************************************************/
5099 /* Assumes mark starts a line. Calculates the height, width, and
5100 * displayable character count of a single DISPLAYABLE line. That
5101 * means that in line-wrap mode, this does may not compute the
5102 * properties of an entire line. */
5104 find_line_params (GtkSText* text,
5105 const GtkSPropertyMark* mark,
5106 const PrevTabCont *tab_cont,
5107 PrevTabCont *next_cont)
5110 TabStopMark tab_mark = tab_cont->tab_start;
5111 guint max_display_pixels;
5116 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
5118 if (text->wrap_rmargin) {
5119 /* SYLPHEED - since sylpheed is a mail program, assume we work with
5120 * fixed fonts. only assume we're using one font! */
5121 font = MARK_CURRENT_FONT(text, mark);
5123 /* SYLPHEED - ok for multi byte charsets to check for ASCII char?
5125 if (text->use_wchar)
5126 ch_width = gdk_char_width_wc(font, 'W');
5128 ch_width = gdk_char_width(font, 'W');
5130 /* SYLPHEED - max_display_chars has the rmargin in pixels
5132 max_display_pixels = text->wrap_rmargin * ch_width;
5135 /* SYLPHEED - we don't draw ugly word wrapping thing
5136 * if our wrap margin is set */
5137 if (!text->wrap_rmargin &&
5138 ((GTK_EDITABLE (text)->editable || !text->word_wrap)))
5139 max_display_pixels -= LINE_WRAP_ROOM;
5142 lp.tab_cont = *tab_cont;
5145 lp.pixel_width = tab_cont->pixel_offset;
5146 lp.displayable_chars = 0;
5148 lp.font_descent = 0;
5150 init_tab_cont (text, next_cont);
5152 while (!LAST_INDEX(text, lp.end))
5154 g_assert (lp.end.property);
5156 ch = GTK_STEXT_INDEX (text, lp.end.index);
5157 font = MARK_CURRENT_FONT (text, &lp.end);
5159 if (ch == LINE_DELIM)
5161 /* Newline doesn't count in computation of line height, even
5162 * if its in a bigger font than the rest of the line. Unless,
5163 * of course, there are no other characters. */
5165 if (!lp.font_ascent && !lp.font_descent)
5167 lp.font_ascent = font->ascent;
5168 lp.font_descent = font->descent;
5171 lp.tab_cont_next = *next_cont;
5176 ch_width = find_char_width (text, &lp.end, &tab_mark);
5178 if ((ch_width + lp.pixel_width > max_display_pixels) &&
5179 (lp.end.index > lp.start.index))
5183 if (text->line_wrap)
5185 next_cont->tab_start = tab_mark;
5186 next_cont->pixel_offset = 0;
5190 /* Here's the tough case, a tab is wrapping. */
5191 gint pixels_avail = max_display_pixels - lp.pixel_width;
5192 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
5193 gint spaces_avail = pixels_avail / space_width;
5195 if (spaces_avail == 0)
5197 decrement_mark (&lp.end);
5201 advance_tab_mark (text, &next_cont->tab_start, '\t');
5202 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
5204 lp.displayable_chars += 1;
5209 if (text->word_wrap)
5211 GtkSPropertyMark saved_mark = lp.end;
5212 guint saved_characters = lp.displayable_chars;
5214 lp.displayable_chars += 1;
5216 if (text->use_wchar)
5218 while (!gdk_iswspace (GTK_STEXT_INDEX (text, lp.end.index)) &&
5219 (lp.end.index > lp.start.index))
5221 decrement_mark (&lp.end);
5222 lp.displayable_chars -= 1;
5227 while (!isspace(GTK_STEXT_INDEX (text, lp.end.index)) &&
5228 (lp.end.index > lp.start.index))
5230 decrement_mark (&lp.end);
5231 lp.displayable_chars -= 1;
5235 /* If whole line is one word, revert to char wrapping */
5236 if (lp.end.index == lp.start.index)
5238 /* SYLPHEED: don't wrap URLs */
5239 if (gtkut_stext_is_uri_string(text, lp.end.index,
5240 gtk_stext_get_length(text)))
5242 lp.end = saved_mark;
5243 lp.displayable_chars = saved_characters + 1;
5248 lp.end = saved_mark;
5249 lp.displayable_chars = saved_characters;
5250 decrement_mark (&lp.end);
5255 /* Don't include this character, it will wrap. */
5256 decrement_mark (&lp.end);
5260 lp.tab_cont_next = *next_cont;
5267 lp.displayable_chars += 1;
5271 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
5272 lp.font_descent = MAX (font->descent, lp.font_descent);
5273 lp.pixel_width += ch_width;
5275 advance_mark(&lp.end);
5276 advance_tab_mark (text, &tab_mark, ch);
5279 if (LAST_INDEX(text, lp.start))
5281 /* Special case, empty last line. */
5282 font = MARK_CURRENT_FONT (text, &lp.end);
5284 lp.font_ascent = font->ascent;
5285 lp.font_descent = font->descent;
5288 lp.tab_cont_next = *next_cont;
5294 expand_scratch_buffer (GtkSText* text, guint len)
5296 if (len >= text->scratch_buffer_len)
5300 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
5302 if (text->use_wchar)
5304 if (!text->scratch_buffer.wc)
5305 text->scratch_buffer.wc = g_new (GdkWChar, i);
5307 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
5308 i * sizeof (GdkWChar));
5312 if (!text->scratch_buffer.ch)
5313 text->scratch_buffer.ch = g_new (guchar, i);
5315 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
5318 text->scratch_buffer_len = i;
5322 /* Side effect: modifies text->gc
5326 draw_bg_rect (GtkSText* text, GtkSPropertyMark *mark,
5327 gint x, gint y, gint width, gint height,
5328 gboolean already_cleared)
5330 GtkEditable *editable = GTK_EDITABLE(text);
5332 if ((mark->index >= MIN(editable->selection_start_pos, editable->selection_end_pos) &&
5333 mark->index < MAX(editable->selection_start_pos, editable->selection_end_pos)))
5335 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
5336 editable->has_selection ?
5337 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
5339 NULL, GTK_WIDGET(text), "text",
5340 x, y, width, height);
5342 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
5343 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
5345 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
5347 gdk_draw_rectangle (text->text_area,
5349 TRUE, x, y, width, height);
5351 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
5358 rect.height = height;
5360 clear_area (text, &rect);
5362 else if (!already_cleared)
5363 gdk_window_clear_area (text->text_area, x, y, width, height);
5367 draw_line (GtkSText* text,
5368 gint pixel_start_height,
5371 GdkGCValues gc_values;
5374 guint running_offset = lp->tab_cont.pixel_offset;
5375 union { GdkWChar *wc; guchar *ch; } buffer;
5378 GtkEditable *editable = GTK_EDITABLE(text);
5380 guint selection_start_pos = MIN (editable->selection_start_pos, editable->selection_end_pos);
5381 guint selection_end_pos = MAX (editable->selection_start_pos, editable->selection_end_pos);
5383 GtkSPropertyMark mark = lp->start;
5384 TabStopMark tab_mark = lp->tab_cont.tab_start;
5385 gint pixel_height = pixel_start_height + lp->font_ascent;
5386 guint chars = lp->displayable_chars;
5388 /* First provide a contiguous segment of memory. This makes reading
5389 * the code below *much* easier, and only incurs the cost of copying
5390 * when the line being displayed spans the gap. */
5391 if (mark.index <= text->gap_position &&
5392 mark.index + chars > text->gap_position)
5394 expand_scratch_buffer (text, chars);
5396 if (text->use_wchar)
5398 for (i = 0; i < chars; i += 1)
5399 text->scratch_buffer.wc[i] = GTK_STEXT_INDEX(text, mark.index + i);
5400 buffer.wc = text->scratch_buffer.wc;
5404 for (i = 0; i < chars; i += 1)
5405 text->scratch_buffer.ch[i] = GTK_STEXT_INDEX(text, mark.index + i);
5406 buffer.ch = text->scratch_buffer.ch;
5411 if (text->use_wchar)
5413 if (mark.index >= text->gap_position)
5414 buffer.wc = text->text.wc + mark.index + text->gap_size;
5416 buffer.wc = text->text.wc + mark.index;
5420 if (mark.index >= text->gap_position)
5421 buffer.ch = text->text.ch + mark.index + text->gap_size;
5423 buffer.ch = text->text.ch + mark.index;
5428 if (running_offset > 0)
5430 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
5431 LINE_HEIGHT (*lp), TRUE);
5437 if ((text->use_wchar && buffer.wc[0] != '\t') ||
5438 (!text->use_wchar && buffer.ch[0] != '\t'))
5440 union { GdkWChar *wc; guchar *ch; } next_tab;
5445 if (text->use_wchar)
5446 for (i=0; i<chars; i++)
5448 if (buffer.wc[i] == '\t')
5450 next_tab.wc = buffer.wc + i;
5455 next_tab.ch = memchr (buffer.ch, '\t', chars);
5457 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
5459 if (text->use_wchar)
5462 len = MIN (len, next_tab.wc - buffer.wc);
5467 len = MIN (len, next_tab.ch - buffer.ch);
5470 if (mark.index < selection_start_pos)
5471 len = MIN (len, selection_start_pos - mark.index);
5472 else if (mark.index < selection_end_pos)
5473 len = MIN (len, selection_end_pos - mark.index);
5475 font = MARK_CURRENT_FONT (text, &mark);
5476 if (font->type == GDK_FONT_FONT)
5478 gdk_gc_set_font (text->gc, font);
5479 gdk_gc_get_values (text->gc, &gc_values);
5480 if (text->use_wchar)
5481 pixel_width = gdk_text_width_wc (gc_values.font,
5484 pixel_width = gdk_text_width (gc_values.font,
5489 if (text->use_wchar)
5490 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
5492 pixel_width = gdk_text_width (font, buffer.ch, len);
5495 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
5496 pixel_width, LINE_HEIGHT (*lp), TRUE);
5498 if ((mark.index >= selection_start_pos) &&
5499 (mark.index < selection_end_pos))
5501 if (editable->has_selection)
5502 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_SELECTED];
5504 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_ACTIVE];
5508 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
5512 if (text->use_wchar)
5513 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
5520 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
5527 running_offset += pixel_width;
5529 advance_tab_mark_n (text, &tab_mark, len);
5533 gint pixels_remaining;
5539 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
5540 if (GTK_EDITABLE (text)->editable || !text->word_wrap)
5541 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
5543 pixels_remaining -= running_offset;
5545 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
5547 spaces_avail = pixels_remaining / space_width;
5548 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
5550 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
5551 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
5553 running_offset += tab_mark.to_next_tab *
5554 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
5556 advance_tab_mark (text, &tab_mark, '\t');
5559 advance_mark_n (&mark, len);
5560 if (text->use_wchar)
5569 draw_line_wrap (GtkSText* text, guint height /* baseline height */)
5576 if (!GTK_EDITABLE (text)->editable && text->word_wrap)
5579 /* SYLPHEED - don't draw ugly word wrapping thing if
5580 * our wrap margin is set */
5581 if (text->wrap_rmargin > 0) {
5585 if (text->line_wrap)
5587 bitmap = text->line_wrap_bitmap;
5588 bitmap_width = line_wrap_width;
5589 bitmap_height = line_wrap_height;
5593 bitmap = text->line_arrow_bitmap;
5594 bitmap_width = line_arrow_width;
5595 bitmap_height = line_arrow_height;
5598 gdk_window_get_size (text->text_area, &width, NULL);
5599 width -= LINE_WRAP_ROOM;
5601 gdk_gc_set_stipple (text->gc,
5604 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
5606 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5608 gdk_gc_set_ts_origin (text->gc,
5610 height - bitmap_height - 1);
5612 gdk_draw_rectangle (text->text_area,
5616 height - bitmap_height - 1 /* one pixel above the baseline. */,
5620 gdk_gc_set_ts_origin (text->gc, 0, 0);
5622 gdk_gc_set_fill (text->gc, GDK_SOLID);
5626 undraw_cursor (GtkSText* text, gint absolute)
5628 GtkEditable *editable = (GtkEditable *)text;
5630 TDEBUG (("in undraw_cursor\n"));
5633 text->cursor_drawn_level = 0;
5635 if ((text->cursor_drawn_level ++ == 0) &&
5636 (editable->selection_start_pos == editable->selection_end_pos) &&
5637 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5643 g_assert(text->cursor_mark.property);
5645 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5648 * changed the cursor to a real block (TM)
5650 if (text->cursor_type == GTK_STEXT_CURSOR_BLOCK)
5652 if (text->cursor_char)
5654 if (text->use_wchar)
5655 pixel_width = gdk_char_width_wc (font, text->cursor_char);
5657 pixel_width = gdk_char_width (font, (guchar)text->cursor_char);
5661 pixel_width = gdk_char_width (font, 'W');
5664 pixel_height = LINE_HEIGHT(CACHE_DATA(text->current_line));
5666 draw_bg_rect (text, &text->cursor_mark,
5668 text->cursor_pos_y - (pixel_height + 1),
5675 draw_bg_rect (text, &text->cursor_mark,
5677 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5678 2, font->descent + font->ascent + 1, FALSE);
5681 if (text->cursor_char)
5683 if (font->type == GDK_FONT_FONT)
5684 gdk_gc_set_font (text->gc, font);
5686 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5688 if (text->use_wchar)
5689 gdk_draw_text_wc (text->text_area, font,
5692 text->cursor_pos_y - text->cursor_char_offset,
5697 guchar ch = text->cursor_char;
5698 gdk_draw_text (text->text_area, font,
5701 text->cursor_pos_y - text->cursor_char_offset,
5710 drawn_cursor_min (GtkSText* text)
5714 g_assert(text->cursor_mark.property);
5716 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5718 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5722 drawn_cursor_max (GtkSText* text)
5726 g_assert(text->cursor_mark.property);
5728 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5730 return text->cursor_pos_y - text->cursor_char_offset;
5734 draw_cursor (GtkSText* text, gint absolute)
5736 GtkEditable *editable = (GtkEditable *)text;
5738 TDEBUG (("in draw_cursor\n"));
5741 text->cursor_drawn_level = 1;
5743 if ((--text->cursor_drawn_level == 0) &&
5744 editable->editable &&
5745 (editable->selection_start_pos == editable->selection_end_pos) &&
5746 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5752 g_assert (text->cursor_mark.property);
5754 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5757 * change the cursor to a real block (TM)
5759 if (text->cursor_type == GTK_STEXT_CURSOR_BLOCK)
5761 if (text->cursor_char)
5763 if (text->use_wchar)
5764 pixel_width = gdk_char_width_wc (font, text->cursor_char);
5766 pixel_width = gdk_char_width (font, (guchar)text->cursor_char);
5770 pixel_width = gdk_char_width (font, 'W');
5773 pixel_height = LINE_HEIGHT(CACHE_DATA(text->current_line));
5775 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5776 gdk_gc_set_function (text->gc, GDK_INVERT);
5777 gdk_draw_rectangle (text->text_area, text->gc, TRUE,
5779 text->cursor_pos_y - pixel_height,
5782 gdk_gc_set_function (text->gc, GDK_COPY);
5786 gdk_gc_set_line_attributes(text->gc, 2, GDK_LINE_SOLID, GDK_CAP_NOT_LAST, GDK_JOIN_MITER);
5787 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5789 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x + 1,
5790 text->cursor_pos_y + font->descent - text->cursor_char_offset,
5791 text->cursor_pos_x + 1,
5792 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5798 create_bg_gc (GtkSText *text)
5802 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5803 values.fill = GDK_TILED;
5805 return gdk_gc_new_with_values (text->text_area, &values,
5806 GDK_GC_FILL | GDK_GC_TILE);
5810 clear_area (GtkSText *text, GdkRectangle *area)
5812 GtkWidget *widget = GTK_WIDGET (text);
5818 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5820 gdk_gc_set_ts_origin (text->bg_gc,
5821 (- (gint)text->first_onscreen_hor_pixel) % width,
5822 (- (gint)text->first_onscreen_ver_pixel) % height);
5824 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5825 area->x, area->y, area->width, area->height);
5828 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5832 expose_text (GtkSText* text, GdkRectangle *area, gboolean cursor)
5834 GList *cache = text->line_start_cache;
5835 gint pixels = - text->first_cut_pixels;
5836 gint min_y = MAX (0, area->y);
5837 gint max_y = MAX (0, area->y + area->height);
5840 gdk_window_get_size (text->text_area, NULL, &height);
5841 max_y = MIN (max_y, height);
5843 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5845 clear_area (text, area);
5847 for (; pixels < height; cache = cache->next)
5849 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5851 draw_line (text, pixels, &CACHE_DATA(cache));
5853 if (CACHE_DATA(cache).wraps)
5854 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5857 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5859 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5860 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5862 /* We undraw and draw the cursor here to get the drawn
5863 * level right ... FIXME - maybe the second parameter
5864 * of draw_cursor should work differently
5866 undraw_cursor (text, FALSE);
5867 draw_cursor (text, FALSE);
5871 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5875 fetch_lines_forward (text, 1);
5884 gtk_stext_update_text (GtkEditable *editable,
5888 GtkSText *text = GTK_STEXT (editable);
5890 GList *cache = text->line_start_cache;
5891 gint pixels = - text->first_cut_pixels;
5897 end_pos = TEXT_LENGTH (text);
5899 if (end_pos < start_pos)
5902 gdk_window_get_size (text->text_area, &width, &height);
5908 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5910 for (; pixels < height; cache = cache->next)
5912 if (CACHE_DATA(cache).start.index < end_pos)
5914 if (CACHE_DATA(cache).end.index >= start_pos)
5917 area.y = MAX(0,pixels);
5918 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5924 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5928 fetch_lines_forward (text, 1);
5936 expose_text (text, &area, TRUE);
5940 recompute_geometry (GtkSText* text)
5942 GtkSPropertyMark mark, start_mark;
5949 mark = start_mark = set_vertical_scroll (text);
5951 /* We need a real start of a line when calling fetch_lines().
5952 * not the start of a wrapped line.
5954 while (mark.index > 0 &&
5955 GTK_STEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5956 decrement_mark (&mark);
5958 gdk_window_get_size (text->text_area, &width, &height);
5960 /* Fetch an entire line, to make sure that we get all the text
5961 * we backed over above, in addition to enough text to fill up
5962 * the space vertically
5965 new_lines = fetch_lines (text,
5971 mark = CACHE_DATA (g_list_last (new_lines)).end;
5972 if (!LAST_INDEX (text, mark))
5974 advance_mark (&mark);
5976 new_lines = g_list_concat (new_lines,
5981 height + text->first_cut_pixels));
5984 /* Now work forward to the actual first onscreen line */
5986 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5987 new_lines = new_lines->next;
5989 text->line_start_cache = new_lines;
5991 find_cursor (text, TRUE);
5994 /**********************************************************************/
5996 /**********************************************************************/
5999 gtk_stext_set_selection (GtkEditable *editable,
6003 GtkSText *text = GTK_STEXT (editable);
6005 guint start1, end1, start2, end2;
6008 end = TEXT_LENGTH (text);
6010 start1 = MIN(start,end);
6011 end1 = MAX(start,end);
6012 start2 = MIN(editable->selection_start_pos, editable->selection_end_pos);
6013 end2 = MAX(editable->selection_start_pos, editable->selection_end_pos);
6015 if (start2 < start1)
6019 tmp = start1; start1 = start2; start2 = tmp;
6020 tmp = end1; end1 = end2; end2 = tmp;
6023 undraw_cursor (text, FALSE);
6024 editable->selection_start_pos = start;
6025 editable->selection_end_pos = end;
6026 draw_cursor (text, FALSE);
6028 /* Expose only what changed */
6030 if (start1 < start2)
6031 gtk_stext_update_text (editable, start1, MIN(end1, start2));
6034 gtk_stext_update_text (editable, MAX(end1, start2), end2);
6035 else if (end2 < end1)
6036 gtk_stext_update_text (editable, end2, end1);
6045 stext_blink_timer_proc (GtkSText *text)
6047 if (text->cursor_state_on)
6049 text->cursor_state_on = FALSE;
6050 undraw_cursor (text, TRUE);
6051 /* kill this timer... */
6052 gtk_timeout_remove (text->cursor_timer_id);
6053 text->cursor_timer_id = gtk_timeout_add (text->cursor_off_ms,
6054 (GtkFunction) stext_blink_timer_proc,
6059 text->cursor_state_on = TRUE;
6060 draw_cursor (text, TRUE);
6061 /* kill this timer... */
6062 gtk_timeout_remove (text->cursor_timer_id);
6063 text->cursor_timer_id = gtk_timeout_add (text->cursor_on_ms,
6064 (GtkFunction) stext_blink_timer_proc,
6072 stext_idle_timer_proc (GtkSText *text)
6074 /* make sure the cursor timer is off */
6075 if (text->cursor_timer_id)
6077 gtk_timeout_remove (text->cursor_timer_id);
6078 text->cursor_timer_id = 0;
6081 /* assuming it's always on when calling this function ... */
6082 text->cursor_state_on = TRUE;
6083 text->cursor_timer_id = gtk_timeout_add (text->cursor_on_ms,
6084 (GtkFunction) stext_blink_timer_proc,
6086 /* make sure we kill the timer (could perhaps make this function return
6087 FALSE (not documented in the current docs). should check the source. */
6088 gtk_idle_remove (text->cursor_idle_time_timer_id);
6089 text->cursor_idle_time_timer_id = 0;
6095 gtk_stext_enable_blink (GtkSText *text)
6097 if (text->cursor_timer_on)
6099 gtk_stext_disable_blink (text);
6100 text->cursor_idle_time_timer_id = gtk_idle_add ((GtkFunction) stext_idle_timer_proc,
6106 gtk_stext_disable_blink (GtkSText *text)
6108 if (text->cursor_timer_on)
6110 if (text->cursor_idle_time_timer_id)
6112 gtk_idle_remove (text->cursor_idle_time_timer_id);
6113 text->cursor_idle_time_timer_id = 0;
6115 if (text->cursor_timer_id)
6117 gtk_timeout_remove (text->cursor_timer_id);
6118 text->cursor_timer_id = 0;
6120 draw_cursor(text, TRUE);
6125 gtk_stext_set_blink (GtkSText *text, gboolean blinking_on)
6127 if (text->cursor_timer_on != blinking_on)
6129 if (text->cursor_timer_on)
6131 /* text widget already created? */
6132 if (text->cursor_visible)
6134 gtk_stext_disable_blink (text);
6136 text->cursor_timer_on = FALSE;
6140 if (text->cursor_visible)
6142 gtk_stext_enable_blink (text);
6144 text->cursor_timer_on = TRUE;
6150 void gtk_stext_set_wrap_rmargin (GtkSText *text, gint rmargin)
6152 /* not particularly impressive, but it does the job. */
6153 /* TODO: currently only allowed to set this after a
6154 * gtk_stext_new() */
6155 text->wrap_rmargin = rmargin >= 0 ? rmargin : 0;
6158 /**********************************************************************/
6160 /**********************************************************************/
6162 #ifdef DEBUG_GTK_STEXT
6164 gtk_stext_show_cache_line (GtkSText *text, GList *cache,
6165 const char* what, const char* func, gint line)
6167 LineParams *lp = &CACHE_DATA(cache);
6170 if (cache == text->line_start_cache)
6171 g_message ("Line Start Cache: ");
6173 if (cache == text->current_line)
6174 g_message("Current Line: ");
6176 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
6184 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
6185 g_message ("%c", GTK_STEXT_INDEX (text, i));
6191 gtk_stext_show_cache (GtkSText *text, const char* func, gint line)
6193 GList *l = text->line_start_cache;
6199 /* back up to the absolute beginning of the line cache */
6203 g_message ("*** line cache ***\n");
6204 for (; l; l = l->next)
6205 gtk_stext_show_cache_line (text, l, "all", func, line);
6209 gtk_stext_assert_mark (GtkSText *text,
6210 GtkSPropertyMark *mark,
6211 GtkSPropertyMark *before,
6212 GtkSPropertyMark *after,
6217 GtkSPropertyMark correct_mark = find_mark (text, mark->index);
6219 if (mark->offset != correct_mark.offset ||
6220 mark->property != correct_mark.property)
6221 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
6225 gtk_stext_assert (GtkSText *text,
6229 GList* cache = text->line_start_cache;
6230 GtkSPropertyMark* before_mark = NULL;
6231 GtkSPropertyMark* after_mark = NULL;
6233 gtk_stext_show_props (text, msg, line);
6235 for (; cache->prev; cache = cache->prev)
6238 g_message ("*** line markers ***\n");
6240 for (; cache; cache = cache->next)
6242 after_mark = &CACHE_DATA(cache).end;
6243 gtk_stext_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
6244 before_mark = &CACHE_DATA(cache).start;
6247 after_mark = &CACHE_DATA(cache->next).start;
6251 gtk_stext_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
6252 before_mark = &CACHE_DATA(cache).end;
6257 gtk_stext_show_adj (GtkSText *text,
6263 g_message ("*** adjustment ***\n");
6265 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
6272 adj->step_increment,
6273 adj->page_increment,
6278 gtk_stext_show_props (GtkSText *text,
6282 GList* props = text->text_properties;
6285 g_message ("%s:%d: ", msg, line);
6287 for (; props; props = props->next)
6289 TextProperty *p = (TextProperty*)props->data;
6291 proplen += p->length;
6293 g_message ("[%d,%p,", p->length, p);
6294 if (p->flags & PROPERTY_FONT)
6295 g_message ("%p,", p->font);
6298 if (p->flags & PROPERTY_FOREGROUND)
6299 g_message ("%ld, ", p->fore_color.pixel);
6302 if (p->flags & PROPERTY_BACKGROUND)
6303 g_message ("%ld] ", p->back_color.pixel);
6310 if (proplen - 1 != TEXT_LENGTH(text))
6311 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);