1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 * Modified by the Sylpheed Team and others 2001. Interesting
29 * parts are marked using comment block following this one.
30 * This modification is based on the 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>
53 * compile time settings
55 #define INITIAL_BUFFER_SIZE 1024
56 #define INITIAL_LINE_CACHE_SIZE 256
57 #define MIN_GAP_SIZE 256
60 * intending to introduce a paragraph delimiter '\r' because
61 * '\r' are being stripped by sylpheed
63 #define LINE_DELIM '\n'
64 #define MIN_TEXT_WIDTH_LINES 20
65 #define MIN_TEXT_HEIGHT_LINES 10
66 #define TEXT_BORDER_ROOM 1
67 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
68 #define DEFAULT_TAB_STOP_WIDTH 4
69 #define SCROLL_PIXELS 5
70 #define KEY_SCROLL_PIXELS 10
71 #define SCROLL_TIME 100
72 #define FREEZE_LENGTH 1024
75 /* Freeze text when inserting or deleting more than this many characters */
78 * could also mark paragraphs using marks, but don't know anything yet
79 * about consistency when characters are removed
81 #define SET_PROPERTY_MARK(m, p, o) do { \
82 (m)->property = (p); \
85 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
86 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
87 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
88 (mark)->property->prev->data \
90 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
91 #define MARK_LIST_PTR(mark) ((mark)->property)
92 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
93 #define MARK_OFFSET(mark) ((mark)->offset)
94 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
97 #define MARK_CURRENT_FONT(text, mark) \
98 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
99 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
100 GTK_WIDGET (text)->style->font)
101 #define MARK_CURRENT_FORE(text, mark) \
102 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
103 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
104 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
105 #define MARK_CURRENT_BACK(text, mark) \
106 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
107 &MARK_CURRENT_PROPERTY(mark)->back_color : \
108 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
109 #define MARK_CURRENT_TEXT_FONT(text, mark) \
110 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
111 MARK_CURRENT_PROPERTY(mark)->font : \
114 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
115 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
116 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
117 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
118 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
119 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
120 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
121 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
131 typedef struct _TextProperty TextProperty;
132 typedef struct _TabStopMark TabStopMark;
133 typedef struct _PrevTabCont PrevTabCont;
134 typedef struct _FetchLinesData FetchLinesData;
135 typedef struct _LineParams LineParams;
136 typedef struct _SetVerticalScrollData SetVerticalScrollData;
141 typedef gint (*LineIteratorFunction) (GtkSText* text, LineParams* lp, void* data);
149 struct _SetVerticalScrollData {
151 gint last_didnt_wrap;
152 gint last_line_start;
153 GtkSPropertyMark mark;
158 /* The actual font. */
162 gint16 char_widths[256];
166 PROPERTY_FONT = 1 << 0,
167 PROPERTY_FOREGROUND = 1 << 1,
168 PROPERTY_BACKGROUND = 1 << 2
176 /* Background Color. */
179 /* Foreground Color. */
182 /* Show which properties are set */
183 TextPropertyFlags flags;
185 /* Length of this property. */
191 GList* tab_stops; /* Index into list containing the next tab position. If
192 * NULL, using default widths. */
199 TabStopMark tab_start;
202 struct _FetchLinesData
215 guint displayable_chars;
218 PrevTabCont tab_cont;
219 PrevTabCont tab_cont_next;
221 GtkSPropertyMark start;
222 GtkSPropertyMark end;
226 static void gtk_stext_class_init (GtkSTextClass *klass);
227 static void gtk_stext_set_arg (GtkObject *object,
230 static void gtk_stext_get_arg (GtkObject *object,
233 static void gtk_stext_init (GtkSText *text);
234 static void gtk_stext_destroy (GtkObject *object);
235 static void gtk_stext_finalize (GtkObject *object);
236 static void gtk_stext_realize (GtkWidget *widget);
237 static void gtk_stext_unrealize (GtkWidget *widget);
238 static void gtk_stext_style_set (GtkWidget *widget,
239 GtkStyle *previous_style);
240 static void gtk_stext_state_changed (GtkWidget *widget,
241 GtkStateType previous_state);
242 static void gtk_stext_draw_focus (GtkWidget *widget);
243 static void gtk_stext_size_request (GtkWidget *widget,
244 GtkRequisition *requisition);
245 static void gtk_stext_size_allocate (GtkWidget *widget,
246 GtkAllocation *allocation);
247 static void gtk_stext_adjustment (GtkAdjustment *adjustment,
249 static void gtk_stext_disconnect (GtkAdjustment *adjustment,
252 static void gtk_stext_insert_text (GtkEditable *editable,
253 const gchar *new_text,
254 gint new_text_length,
256 static void gtk_stext_delete_text (GtkEditable *editable,
259 static void gtk_stext_update_text (GtkEditable *editable,
262 static gchar *gtk_stext_get_chars (GtkEditable *editable,
265 static void gtk_stext_set_selection (GtkEditable *editable,
268 static void gtk_stext_real_set_editable (GtkEditable *editable,
269 gboolean is_editable);
272 static void gtk_stext_draw (GtkWidget *widget,
274 static gint gtk_stext_expose (GtkWidget *widget,
275 GdkEventExpose *event);
276 static gint gtk_stext_button_press (GtkWidget *widget,
277 GdkEventButton *event);
278 static gint gtk_stext_button_release (GtkWidget *widget,
279 GdkEventButton *event);
280 static gint gtk_stext_motion_notify (GtkWidget *widget,
281 GdkEventMotion *event);
282 static gint gtk_stext_key_press (GtkWidget *widget,
284 static gint gtk_stext_focus_in (GtkWidget *widget,
285 GdkEventFocus *event);
286 static gint gtk_stext_focus_out (GtkWidget *widget,
287 GdkEventFocus *event);
289 static void move_gap (GtkSText* text, guint index);
290 static void make_forward_space (GtkSText* text, guint len);
292 /* Property management */
293 static GtkSTextFont* get_text_font (GdkFont* gfont);
294 static void text_font_unref (GtkSTextFont *text_font);
296 static void insert_text_property (GtkSText* text, GdkFont* font,
297 GdkColor *fore, GdkColor* back, guint len);
298 static TextProperty* new_text_property (GtkSText *text, GdkFont* font,
299 GdkColor* fore, GdkColor* back, guint length);
300 static void destroy_text_property (TextProperty *prop);
301 static void init_properties (GtkSText *text);
302 static void realize_property (GtkSText *text, TextProperty *prop);
303 static void realize_properties (GtkSText *text);
304 static void unrealize_property (GtkSText *text, TextProperty *prop);
305 static void unrealize_properties (GtkSText *text);
307 static void delete_text_property (GtkSText* text, guint len);
309 static guint pixel_height_of (GtkSText* text, GList* cache_line);
311 /* Property Movement and Size Computations */
312 static void advance_mark (GtkSPropertyMark* mark);
313 static void decrement_mark (GtkSPropertyMark* mark);
314 static void advance_mark_n (GtkSPropertyMark* mark, gint n);
315 static void decrement_mark_n (GtkSPropertyMark* mark, gint n);
316 static void move_mark_n (GtkSPropertyMark* mark, gint n);
317 static GtkSPropertyMark find_mark (GtkSText* text, guint mark_position);
318 static GtkSPropertyMark find_mark_near (GtkSText* text, guint mark_position, const GtkSPropertyMark* near);
319 static void find_line_containing_point (GtkSText* text, guint point,
323 static void compute_lines_pixels (GtkSText* text, guint char_count,
324 guint *lines, guint *pixels);
326 static gint total_line_height (GtkSText* text,
329 static LineParams find_line_params (GtkSText* text,
330 const GtkSPropertyMark *mark,
331 const PrevTabCont *tab_cont,
332 PrevTabCont *next_cont);
333 static void recompute_geometry (GtkSText* text);
334 static void insert_expose (GtkSText* text, guint old_pixels, gint nchars, guint new_line_count);
335 static void delete_expose (GtkSText* text,
339 static GdkGC *create_bg_gc (GtkSText *text);
340 static void clear_area (GtkSText *text, GdkRectangle *area);
341 static void draw_line (GtkSText* text,
344 static void draw_line_wrap (GtkSText* text,
346 static void draw_cursor (GtkSText* text, gint absolute);
347 static void undraw_cursor (GtkSText* text, gint absolute);
348 static gint drawn_cursor_min (GtkSText* text);
349 static gint drawn_cursor_max (GtkSText* text);
350 static void expose_text (GtkSText* text, GdkRectangle *area, gboolean cursor);
352 /* Search and Placement. */
353 static void find_cursor (GtkSText* text,
355 static void find_cursor_at_line (GtkSText* text,
356 const LineParams* start_line,
358 static void find_mouse_cursor (GtkSText* text, gint x, gint y);
361 static void adjust_adj (GtkSText* text, GtkAdjustment* adj);
362 static void scroll_up (GtkSText* text, gint diff);
363 static void scroll_down (GtkSText* text, gint diff);
364 static void scroll_int (GtkSText* text, gint diff);
366 static void process_exposes (GtkSText *text);
368 /* Cache Management. */
369 static void free_cache (GtkSText* text);
370 static GList* remove_cache_line (GtkSText* text, GList* list);
373 static void move_cursor_buffer_ver (GtkSText *text, int dir);
374 static void move_cursor_page_ver (GtkSText *text, int dir);
375 static void move_cursor_ver (GtkSText *text, int count);
376 static void move_cursor_hor (GtkSText *text, int count);
380 static void move_cursor_to_display_row_end (GtkSText *text);
381 static void move_cursor_to_display_row_start (GtkSText *text);
382 static void move_cursor_to_display_row_up (GtkSText *text);
383 static void move_cursor_to_display_row_down (GtkSText *text);
384 static void reset_persist_col_pos (GtkSText *text);
386 /* Binding actions */
387 static void gtk_stext_move_cursor (GtkEditable *editable,
390 static void gtk_stext_move_word (GtkEditable *editable,
392 static void gtk_stext_move_page (GtkEditable *editable,
395 static void gtk_stext_move_to_row (GtkEditable *editable,
397 static void gtk_stext_move_to_column (GtkEditable *editable,
399 static void gtk_stext_kill_char (GtkEditable *editable,
401 static void gtk_stext_kill_word (GtkEditable *editable,
403 static void gtk_stext_kill_line (GtkEditable *editable,
407 static void gtk_stext_move_forward_character (GtkSText *text);
408 static void gtk_stext_move_backward_character (GtkSText *text);
409 static void gtk_stext_move_forward_word (GtkSText *text);
410 static void gtk_stext_move_backward_word (GtkSText *text);
411 static void gtk_stext_move_beginning_of_line (GtkSText *text);
412 static void gtk_stext_move_end_of_line (GtkSText *text);
413 static void gtk_stext_move_next_line (GtkSText *text);
414 static void gtk_stext_move_previous_line (GtkSText *text);
416 static void gtk_stext_delete_forward_character (GtkSText *text);
417 static void gtk_stext_delete_backward_character (GtkSText *text);
418 static void gtk_stext_delete_forward_word (GtkSText *text);
419 static void gtk_stext_delete_backward_word (GtkSText *text);
420 static void gtk_stext_delete_line (GtkSText *text);
421 static void gtk_stext_delete_to_line_end (GtkSText *text);
422 static void gtk_stext_select_word (GtkSText *text,
424 static void gtk_stext_select_line (GtkSText *text,
427 static void gtk_stext_set_position (GtkEditable *editable,
434 static void gtk_stext_enable_blink (GtkSText *text);
435 static void gtk_stext_disable_blink (GtkSText *text);
437 /* #define DEBUG_GTK_STEXT */
439 #if defined(DEBUG_GTK_STEXT) && defined(__GNUC__)
440 /* Debugging utilities. */
441 static void gtk_stext_assert_mark (GtkSText *text,
442 GtkSPropertyMark *mark,
443 GtkSPropertyMark *before,
444 GtkSPropertyMark *after,
449 static void gtk_stext_assert (GtkSText *text,
452 static void gtk_stext_show_cache_line (GtkSText *text, GList *cache,
453 const char* what, const char* func, gint line);
454 static void gtk_stext_show_cache (GtkSText *text, const char* func, gint line);
455 static void gtk_stext_show_adj (GtkSText *text,
460 static void gtk_stext_show_props (GtkSText* test,
464 #define TDEBUG(args) g_message args
465 #define TEXT_ASSERT(text) gtk_stext_assert (text,__PRETTY_FUNCTION__,__LINE__)
466 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_stext_assert_mark (text,mark, \
467 __PRETTY_FUNCTION__,msg,__LINE__)
468 #define TEXT_SHOW(text) gtk_stext_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
469 #define TEXT_SHOW_LINE(text,line,msg) gtk_stext_show_cache_line (text,line,msg,\
470 __PRETTY_FUNCTION__,__LINE__)
471 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_stext_show_adj (text,adj,msg, \
472 __PRETTY_FUNCTION__,__LINE__)
475 #define TEXT_ASSERT(text)
476 #define TEXT_ASSERT_MARK(text,mark,msg)
477 #define TEXT_SHOW(text)
478 #define TEXT_SHOW_LINE(text,line,msg)
479 #define TEXT_SHOW_ADJ(text,adj,msg)
483 #if defined(AHX_DEBUG)
484 # define XDEBUG(args) g_message args
486 # define XDEBUG(args)
487 #endif /* AHX_DEBUG */
489 /* Memory Management. */
490 static GMemChunk *params_mem_chunk = NULL;
491 static GMemChunk *text_property_chunk = NULL;
493 static GtkWidgetClass *parent_class = NULL;
496 static const GtkTextFunction control_keys[26] =
498 (GtkTextFunction)gtk_stext_move_beginning_of_line, /* a */
499 (GtkTextFunction)gtk_stext_move_backward_character, /* b */
501 /* (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
502 (GtkTextFunction)gtk_stext_delete_forward_character, /* d */
503 (GtkTextFunction)gtk_stext_move_end_of_line, /* e */
504 (GtkTextFunction)gtk_stext_move_forward_character, /* f */
506 (GtkTextFunction)gtk_stext_delete_backward_character, /* h */
509 (GtkTextFunction)gtk_stext_delete_to_line_end, /* k */
512 (GtkTextFunction)gtk_stext_move_next_line, /* n */
514 (GtkTextFunction)gtk_stext_move_previous_line, /* p */
519 (GtkTextFunction)gtk_stext_delete_line, /* u */
521 /* (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
522 (GtkTextFunction)gtk_stext_delete_backward_word, /* w */
524 /* (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
529 static const GtkTextFunction alt_keys[26] =
532 (GtkTextFunction)gtk_stext_move_backward_word, /* b */
534 (GtkTextFunction)gtk_stext_delete_forward_word, /* d */
536 (GtkTextFunction)gtk_stext_move_forward_word, /* f */
561 #define line_arrow_width 6
562 #define line_arrow_height 9
563 static unsigned char line_arrow_bits[] = {
564 0x00, 0x00, 0x04, 0x0c, 0x18, 0x3f, 0x18, 0x0c, 0x04};
567 #define line_wrap_width 6
568 #define line_wrap_height 9
569 static unsigned char line_wrap_bits[] = {
570 0x1e, 0x3e, 0x30, 0x30, 0x39, 0x1f, 0x0f, 0x0f, 0x1f, };
572 /**********************************************************************/
574 /**********************************************************************/
577 gtk_stext_get_type (void)
579 static GtkType text_type = 0;
583 static const GtkTypeInfo text_info =
587 sizeof (GtkSTextClass),
588 (GtkClassInitFunc) gtk_stext_class_init,
589 (GtkObjectInitFunc) gtk_stext_init,
590 /* reserved_1 */ NULL,
591 /* reserved_2 */ NULL,
592 (GtkClassInitFunc) NULL,
595 text_type = gtk_type_unique (GTK_TYPE_EDITABLE, &text_info);
602 gtk_stext_class_init (GtkSTextClass *class)
604 GtkObjectClass *object_class;
605 GtkWidgetClass *widget_class;
606 GtkEditableClass *editable_class;
608 object_class = (GtkObjectClass*) class;
609 widget_class = (GtkWidgetClass*) class;
610 editable_class = (GtkEditableClass*) class;
611 parent_class = gtk_type_class (GTK_TYPE_EDITABLE);
613 gtk_object_add_arg_type ("GtkSText::hadjustment",
615 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
617 gtk_object_add_arg_type ("GtkSText::vadjustment",
619 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
621 gtk_object_add_arg_type ("GtkSText::line_wrap",
625 gtk_object_add_arg_type ("GtkSText::word_wrap",
630 object_class->set_arg = gtk_stext_set_arg;
631 object_class->get_arg = gtk_stext_get_arg;
632 object_class->destroy = gtk_stext_destroy;
633 object_class->finalize = gtk_stext_finalize;
635 widget_class->realize = gtk_stext_realize;
636 widget_class->unrealize = gtk_stext_unrealize;
637 widget_class->style_set = gtk_stext_style_set;
638 widget_class->state_changed = gtk_stext_state_changed;
639 widget_class->draw_focus = gtk_stext_draw_focus;
640 widget_class->size_request = gtk_stext_size_request;
641 widget_class->size_allocate = gtk_stext_size_allocate;
642 widget_class->draw = gtk_stext_draw;
643 widget_class->expose_event = gtk_stext_expose;
644 widget_class->button_press_event = gtk_stext_button_press;
645 widget_class->button_release_event = gtk_stext_button_release;
646 widget_class->motion_notify_event = gtk_stext_motion_notify;
647 widget_class->key_press_event = gtk_stext_key_press;
648 widget_class->focus_in_event = gtk_stext_focus_in;
649 widget_class->focus_out_event = gtk_stext_focus_out;
651 widget_class->set_scroll_adjustments_signal =
652 gtk_signal_new ("set_scroll_adjustments",
655 GTK_SIGNAL_OFFSET (GtkSTextClass, set_scroll_adjustments),
656 gtk_marshal_NONE__POINTER_POINTER,
657 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
659 editable_class->set_editable = gtk_stext_real_set_editable;
660 editable_class->insert_text = gtk_stext_insert_text;
661 editable_class->delete_text = gtk_stext_delete_text;
663 editable_class->move_cursor = gtk_stext_move_cursor;
664 editable_class->move_word = gtk_stext_move_word;
665 editable_class->move_page = gtk_stext_move_page;
666 editable_class->move_to_row = gtk_stext_move_to_row;
667 editable_class->move_to_column = gtk_stext_move_to_column;
669 editable_class->kill_char = gtk_stext_kill_char;
670 editable_class->kill_word = gtk_stext_kill_word;
671 editable_class->kill_line = gtk_stext_kill_line;
673 editable_class->update_text = gtk_stext_update_text;
674 editable_class->get_chars = gtk_stext_get_chars;
675 editable_class->set_selection = gtk_stext_set_selection;
676 editable_class->set_position = gtk_stext_set_position;
678 class->set_scroll_adjustments = gtk_stext_set_adjustments;
682 gtk_stext_set_arg (GtkObject *object,
688 text = GTK_STEXT (object);
692 case ARG_HADJUSTMENT:
693 gtk_stext_set_adjustments (text,
694 GTK_VALUE_POINTER (*arg),
697 case ARG_VADJUSTMENT:
698 gtk_stext_set_adjustments (text,
700 GTK_VALUE_POINTER (*arg));
703 gtk_stext_set_line_wrap (text, GTK_VALUE_BOOL (*arg));
706 gtk_stext_set_word_wrap (text, GTK_VALUE_BOOL (*arg));
714 gtk_stext_get_arg (GtkObject *object,
720 text = GTK_STEXT (object);
724 case ARG_HADJUSTMENT:
725 GTK_VALUE_POINTER (*arg) = text->hadj;
727 case ARG_VADJUSTMENT:
728 GTK_VALUE_POINTER (*arg) = text->vadj;
731 GTK_VALUE_BOOL (*arg) = text->line_wrap;
734 GTK_VALUE_BOOL (*arg) = text->word_wrap;
737 arg->type = GTK_TYPE_INVALID;
743 gtk_stext_init (GtkSText *text)
745 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
747 text->text_area = NULL;
752 text->line_wrap_bitmap = NULL;
753 text->line_arrow_bitmap = NULL;
755 text->use_wchar = FALSE;
756 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
757 text->text_len = INITIAL_BUFFER_SIZE;
759 text->scratch_buffer.ch = NULL;
760 text->scratch_buffer_len = 0;
762 text->freeze_count = 0;
764 if (!params_mem_chunk)
765 params_mem_chunk = g_mem_chunk_new ("LineParams",
767 256 * sizeof (LineParams),
770 text->default_tab_width = 4;
771 text->tab_stops = NULL;
773 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
774 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
776 text->line_start_cache = NULL;
777 text->first_cut_pixels = 0;
779 text->line_wrap = TRUE;
780 text->word_wrap = FALSE;
785 text->current_font = NULL;
788 * timer for blinking cursor
790 text->cursor_visible = FALSE; /* don't know whether gtktext stores this somewhere */
791 text->cursor_timer_on = TRUE;
792 text->cursor_off_ms = 500;
793 text->cursor_on_ms = 500;
794 text->cursor_timer_id = 0;
795 text->cursor_idle_time_timer_id = 0;
796 text->wrap_rmargin = 0;
797 text->cursor_type = GTK_STEXT_CURSOR_LINE;
798 text->persist_column = 0;
800 init_properties (text);
802 GTK_EDITABLE (text)->editable = FALSE;
804 gtk_editable_set_position (GTK_EDITABLE (text), 0);
808 gtk_stext_new (GtkAdjustment *hadj,
814 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
816 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
818 text = gtk_widget_new (GTK_TYPE_STEXT,
824 * force widget name to be GtkText so it silently adapts
825 * the GtkText widget's style...
827 gtk_widget_set_name(text, "GtkText");
828 gtk_widget_ensure_style(text);
834 gtk_stext_set_word_wrap (GtkSText *text,
837 g_return_if_fail (text != NULL);
838 g_return_if_fail (GTK_IS_STEXT (text));
840 text->word_wrap = (word_wrap != FALSE);
842 if (GTK_WIDGET_REALIZED (text))
844 recompute_geometry (text);
845 gtk_widget_queue_draw (GTK_WIDGET (text));
850 gtk_stext_set_line_wrap (GtkSText *text,
853 g_return_if_fail (text != NULL);
854 g_return_if_fail (GTK_IS_STEXT (text));
856 text->line_wrap = (line_wrap != FALSE);
858 if (GTK_WIDGET_REALIZED (text))
860 recompute_geometry (text);
861 gtk_widget_queue_draw (GTK_WIDGET (text));
866 gtk_stext_set_editable (GtkSText *text,
867 gboolean is_editable)
869 g_return_if_fail (text != NULL);
870 g_return_if_fail (GTK_IS_STEXT (text));
872 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
876 gtk_stext_real_set_editable (GtkEditable *editable,
877 gboolean is_editable)
881 g_return_if_fail (editable != NULL);
882 g_return_if_fail (GTK_IS_STEXT (editable));
884 text = GTK_STEXT (editable);
886 editable->editable = (is_editable != FALSE);
888 if (GTK_WIDGET_REALIZED (text))
890 recompute_geometry (text);
891 gtk_widget_queue_draw (GTK_WIDGET (text));
896 gtk_stext_set_adjustments (GtkSText *text,
900 g_return_if_fail (text != NULL);
901 g_return_if_fail (GTK_IS_STEXT (text));
903 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
905 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
907 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
909 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
911 if (text->hadj && (text->hadj != hadj))
913 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
914 gtk_object_unref (GTK_OBJECT (text->hadj));
917 if (text->vadj && (text->vadj != vadj))
919 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
920 gtk_object_unref (GTK_OBJECT (text->vadj));
923 if (text->hadj != hadj)
926 gtk_object_ref (GTK_OBJECT (text->hadj));
927 gtk_object_sink (GTK_OBJECT (text->hadj));
929 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
930 (GtkSignalFunc) gtk_stext_adjustment,
932 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
933 (GtkSignalFunc) gtk_stext_adjustment,
935 gtk_signal_connect (GTK_OBJECT (text->hadj), "disconnect",
936 (GtkSignalFunc) gtk_stext_disconnect,
938 gtk_stext_adjustment (hadj, text);
941 if (text->vadj != vadj)
944 gtk_object_ref (GTK_OBJECT (text->vadj));
945 gtk_object_sink (GTK_OBJECT (text->vadj));
947 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
948 (GtkSignalFunc) gtk_stext_adjustment,
950 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
951 (GtkSignalFunc) gtk_stext_adjustment,
953 gtk_signal_connect (GTK_OBJECT (text->vadj), "disconnect",
954 (GtkSignalFunc) gtk_stext_disconnect,
956 gtk_stext_adjustment (vadj, text);
961 gtk_stext_set_point (GtkSText *text,
964 g_return_if_fail (text != NULL);
965 g_return_if_fail (GTK_IS_STEXT (text));
966 g_return_if_fail (index <= TEXT_LENGTH (text));
968 text->point = find_mark (text, index);
972 gtk_stext_get_point (GtkSText *text)
974 g_return_val_if_fail (text != NULL, 0);
975 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
977 return text->point.index;
981 gtk_stext_get_length (GtkSText *text)
983 g_return_val_if_fail (text != NULL, 0);
984 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
986 return TEXT_LENGTH (text);
990 gtk_stext_freeze (GtkSText *text)
992 g_return_if_fail (text != NULL);
993 g_return_if_fail (GTK_IS_STEXT (text));
995 text->freeze_count++;
996 undraw_cursor (text, FALSE);
1000 gtk_stext_thaw (GtkSText *text)
1002 g_return_if_fail (text != NULL);
1003 g_return_if_fail (GTK_IS_STEXT (text));
1005 if (text->freeze_count)
1006 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
1008 recompute_geometry (text);
1009 gtk_widget_queue_draw (GTK_WIDGET (text));
1011 draw_cursor (text, FALSE);
1016 gtk_stext_compact_buffer (GtkSText *text)
1018 g_return_if_fail (text != NULL);
1019 g_return_if_fail (GTK_IS_STEXT (text));
1020 move_gap (text, gtk_stext_get_length(text));
1024 gtk_stext_insert (GtkSText *text,
1031 GtkEditable *editable = GTK_EDITABLE (text);
1032 gboolean frozen = FALSE;
1034 gint new_line_count = 1;
1035 guint old_height = 0;
1040 g_return_if_fail (text != NULL);
1041 g_return_if_fail (GTK_IS_STEXT (text));
1043 g_return_if_fail (chars != NULL);
1046 if (!nchars || !chars)
1048 nchars = strlen (chars);
1052 if (!text->freeze_count && (length > FREEZE_LENGTH))
1054 gtk_stext_freeze (text);
1058 if (!text->freeze_count && (text->line_start_cache != NULL))
1060 find_line_containing_point (text, text->point.index, TRUE);
1061 old_height = total_line_height (text, text->current_line, 1);
1064 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
1067 widget = GTK_WIDGET (text);
1068 gtk_widget_ensure_style (widget);
1069 if ((widget->style) && (widget->style->font->type == GDK_FONT_FONTSET))
1071 text->use_wchar = TRUE;
1072 g_free (text->text.ch);
1073 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
1074 text->text_len = INITIAL_BUFFER_SIZE;
1075 if (text->scratch_buffer.ch)
1076 g_free (text->scratch_buffer.ch);
1077 text->scratch_buffer.wc = NULL;
1078 text->scratch_buffer_len = 0;
1082 move_gap (text, text->point.index);
1083 make_forward_space (text, length);
1085 if (text->use_wchar)
1087 char *chars_nt = (char *)chars;
1090 chars_nt = g_new (char, length+1);
1091 memcpy (chars_nt, chars, length);
1092 chars_nt[length] = 0;
1094 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
1096 if (chars_nt != chars)
1104 memcpy(text->text.ch + text->gap_position, chars, length);
1107 if (!text->freeze_count && (text->line_start_cache != NULL))
1109 if (text->use_wchar)
1111 for (i=0; i<numwcs; i++)
1112 if (text->text.wc[text->gap_position + i] == '\n')
1117 for (i=0; i<numwcs; i++)
1118 if (text->text.ch[text->gap_position + i] == '\n')
1125 insert_text_property (text, font, fore, back, numwcs);
1127 text->gap_size -= numwcs;
1128 text->gap_position += numwcs;
1130 if (text->point.index < text->first_line_start_index)
1131 text->first_line_start_index += numwcs;
1132 if (text->point.index < editable->selection_start_pos)
1133 editable->selection_start_pos += numwcs;
1134 if (text->point.index < editable->selection_end_pos)
1135 editable->selection_end_pos += numwcs;
1136 /* We'll reset the cursor later anyways if we aren't frozen */
1137 if (text->point.index < text->cursor_mark.index)
1138 text->cursor_mark.index += numwcs;
1140 advance_mark_n (&text->point, numwcs);
1142 if (!text->freeze_count && (text->line_start_cache != NULL))
1143 insert_expose (text, old_height, numwcs, new_line_count);
1147 gtk_stext_thaw (text);
1151 gtk_stext_backward_delete (GtkSText *text,
1154 g_return_val_if_fail (text != NULL, 0);
1155 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
1157 if (nchars > text->point.index || nchars <= 0)
1160 gtk_stext_set_point (text, text->point.index - nchars);
1162 return gtk_stext_forward_delete (text, nchars);
1166 gtk_stext_forward_delete (GtkSText *text,
1169 guint old_lines, old_height;
1170 GtkEditable *editable = GTK_EDITABLE (text);
1171 gboolean frozen = FALSE;
1173 g_return_val_if_fail (text != NULL, 0);
1174 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
1176 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1179 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1181 gtk_stext_freeze (text);
1185 if (!text->freeze_count && text->line_start_cache != NULL)
1187 /* We need to undraw the cursor here, since we may later
1188 * delete the cursor's property
1190 undraw_cursor (text, FALSE);
1191 find_line_containing_point (text, text->point.index, TRUE);
1192 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1195 /* FIXME, or resizing after deleting will be odd */
1196 if (text->point.index < text->first_line_start_index)
1198 if (text->point.index + nchars >= text->first_line_start_index)
1200 text->first_line_start_index = text->point.index;
1201 while ((text->first_line_start_index > 0) &&
1202 (GTK_STEXT_INDEX (text, text->first_line_start_index - 1)
1204 text->first_line_start_index -= 1;
1208 text->first_line_start_index -= nchars;
1211 if (text->point.index < editable->selection_start_pos)
1212 editable->selection_start_pos -=
1213 MIN(nchars, editable->selection_start_pos - text->point.index);
1214 if (text->point.index < editable->selection_end_pos)
1215 editable->selection_end_pos -=
1216 MIN(nchars, editable->selection_end_pos - text->point.index);
1217 /* We'll reset the cursor later anyways if we aren't frozen */
1218 if (text->point.index < text->cursor_mark.index)
1219 move_mark_n (&text->cursor_mark,
1220 -MIN(nchars, text->cursor_mark.index - text->point.index));
1222 move_gap (text, text->point.index);
1224 text->gap_size += nchars;
1226 delete_text_property (text, nchars);
1228 if (!text->freeze_count && (text->line_start_cache != NULL))
1230 delete_expose (text, nchars, old_lines, old_height);
1231 draw_cursor (text, FALSE);
1235 gtk_stext_thaw (text);
1241 gtk_stext_set_position (GtkEditable *editable,
1244 GtkSText *text = (GtkSText *) editable;
1246 undraw_cursor (text, FALSE);
1247 text->cursor_mark = find_mark (text, position);
1248 find_cursor (text, TRUE);
1249 draw_cursor (text, FALSE);
1250 gtk_editable_select_region (editable, 0, 0);
1253 /* SYLPHEED - set_position used. Need to find out whether there's
1254 * a better way to handle this */
1255 static void gtk_stext_set_position_X (GtkEditable *editable,
1258 GtkSText *text = (GtkSText *) editable;
1260 undraw_cursor (text, FALSE);
1261 text->cursor_mark = find_mark (text, position);
1262 find_cursor (text, TRUE);
1263 draw_cursor (text, FALSE);
1267 gtk_stext_get_chars (GtkEditable *editable,
1275 g_return_val_if_fail (editable != NULL, NULL);
1276 g_return_val_if_fail (GTK_IS_STEXT (editable), NULL);
1277 text = GTK_STEXT (editable);
1280 end_pos = TEXT_LENGTH (text);
1282 if ((start_pos < 0) ||
1283 (end_pos > TEXT_LENGTH (text)) ||
1284 (end_pos < start_pos))
1287 move_gap (text, TEXT_LENGTH (text));
1288 make_forward_space (text, 1);
1290 if (text->use_wchar)
1293 ch = text->text.wc[end_pos];
1294 text->text.wc[end_pos] = 0;
1295 retval = gdk_wcstombs (text->text.wc + start_pos);
1296 text->text.wc[end_pos] = ch;
1301 ch = text->text.ch[end_pos];
1302 text->text.ch[end_pos] = 0;
1303 retval = g_strdup (text->text.ch + start_pos);
1304 text->text.ch[end_pos] = ch;
1312 gtk_stext_destroy (GtkObject *object)
1316 g_return_if_fail (object != NULL);
1317 g_return_if_fail (GTK_IS_STEXT (object));
1319 text = (GtkSText*) object;
1321 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1322 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1326 gtk_timeout_remove (text->timer);
1333 gtk_stext_disable_blink (text);
1335 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1339 gtk_stext_finalize (GtkObject *object)
1344 g_return_if_fail (object != NULL);
1345 g_return_if_fail (GTK_IS_STEXT (object));
1347 text = (GtkSText *)object;
1349 gtk_object_unref (GTK_OBJECT (text->hadj));
1350 gtk_object_unref (GTK_OBJECT (text->vadj));
1352 /* Clean up the internal structures */
1353 if (text->use_wchar)
1354 g_free (text->text.wc);
1356 g_free (text->text.ch);
1358 tmp_list = text->text_properties;
1361 destroy_text_property (tmp_list->data);
1362 tmp_list = tmp_list->next;
1365 if (text->current_font)
1366 text_font_unref (text->current_font);
1368 g_list_free (text->text_properties);
1370 if (text->use_wchar)
1372 if (text->scratch_buffer.wc)
1373 g_free (text->scratch_buffer.wc);
1377 if (text->scratch_buffer.ch)
1378 g_free (text->scratch_buffer.ch);
1381 g_list_free (text->tab_stops);
1383 GTK_OBJECT_CLASS(parent_class)->finalize (object);
1387 gtk_stext_realize (GtkWidget *widget)
1390 GtkEditable *editable;
1391 GdkWindowAttr attributes;
1392 gint attributes_mask;
1394 g_return_if_fail (widget != NULL);
1395 g_return_if_fail (GTK_IS_STEXT (widget));
1397 text = GTK_STEXT (widget);
1398 editable = GTK_EDITABLE (widget);
1399 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1401 attributes.window_type = GDK_WINDOW_CHILD;
1402 attributes.x = widget->allocation.x;
1403 attributes.y = widget->allocation.y;
1404 attributes.width = widget->allocation.width;
1405 attributes.height = widget->allocation.height;
1406 attributes.wclass = GDK_INPUT_OUTPUT;
1407 attributes.visual = gtk_widget_get_visual (widget);
1408 attributes.colormap = gtk_widget_get_colormap (widget);
1409 attributes.event_mask = gtk_widget_get_events (widget);
1410 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1411 GDK_BUTTON_PRESS_MASK |
1412 GDK_BUTTON_RELEASE_MASK |
1413 GDK_BUTTON_MOTION_MASK |
1414 GDK_ENTER_NOTIFY_MASK |
1415 GDK_LEAVE_NOTIFY_MASK |
1416 GDK_KEY_PRESS_MASK);
1417 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1419 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1420 gdk_window_set_user_data (widget->window, text);
1422 attributes.x = (widget->style->klass->xthickness + TEXT_BORDER_ROOM);
1423 attributes.y = (widget->style->klass->ythickness + TEXT_BORDER_ROOM);
1424 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1425 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1427 attributes.cursor = gdk_cursor_new (GDK_XTERM);
1428 attributes_mask |= GDK_WA_CURSOR;
1430 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1431 gdk_window_set_user_data (text->text_area, text);
1433 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1435 widget->style = gtk_style_attach (widget->style, widget->window);
1437 /* Can't call gtk_style_set_background here because it's handled specially */
1438 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1439 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1441 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1442 text->bg_gc = create_bg_gc (text);
1444 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1445 (gchar*) line_wrap_bits,
1449 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1450 (gchar*) line_arrow_bits,
1454 text->gc = gdk_gc_new (text->text_area);
1455 gdk_gc_set_exposures (text->gc, TRUE);
1456 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1458 #ifdef USE_GTKGDK_XIM
1459 if (gdk_im_ready () && (editable->ic_attr = gdk_ic_attr_new ()) != NULL)
1462 GdkColormap *colormap;
1464 GdkICAttr *attr = editable->ic_attr;
1465 GdkICAttributesType attrmask = GDK_IC_ALL_REQ;
1467 GdkIMStyle supported_style = GDK_IM_PREEDIT_NONE |
1468 GDK_IM_PREEDIT_NOTHING |
1469 GDK_IM_PREEDIT_POSITION |
1470 GDK_IM_STATUS_NONE |
1471 GDK_IM_STATUS_NOTHING;
1473 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1474 supported_style &= ~GDK_IM_PREEDIT_POSITION;
1476 attr->style = style = gdk_im_decide_style (supported_style);
1477 attr->client_window = text->text_area;
1479 if ((colormap = gtk_widget_get_colormap (widget)) !=
1480 gtk_widget_get_default_colormap ())
1482 attrmask |= GDK_IC_PREEDIT_COLORMAP;
1483 attr->preedit_colormap = colormap;
1486 switch (style & GDK_IM_PREEDIT_MASK)
1488 case GDK_IM_PREEDIT_POSITION:
1489 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1491 g_warning ("over-the-spot style requires fontset");
1495 attrmask |= GDK_IC_PREEDIT_POSITION_REQ;
1496 gdk_window_get_size (text->text_area, &width, &height);
1497 attr->spot_location.x = 0;
1498 attr->spot_location.y = height;
1499 attr->preedit_area.x = 0;
1500 attr->preedit_area.y = 0;
1501 attr->preedit_area.width = width;
1502 attr->preedit_area.height = height;
1503 attr->preedit_fontset = widget->style->font;
1507 editable->ic = gdk_ic_new (attr, attrmask);
1509 if (editable->ic == NULL)
1510 g_warning ("Can't create input context.");
1513 mask = gdk_window_get_events (text->text_area);
1514 mask |= gdk_ic_get_events (editable->ic);
1515 gdk_window_set_events (text->text_area, mask);
1517 if (GTK_WIDGET_HAS_FOCUS (widget))
1518 gdk_im_begin (editable->ic, text->text_area);
1523 realize_properties (text);
1524 gdk_window_show (text->text_area);
1525 init_properties (text);
1527 if (editable->selection_start_pos != editable->selection_end_pos)
1528 gtk_editable_claim_selection (editable, TRUE, GDK_CURRENT_TIME);
1530 recompute_geometry (text);
1534 gtk_stext_style_set (GtkWidget *widget,
1535 GtkStyle *previous_style)
1537 GtkSText *text = GTK_STEXT (widget);
1539 if (GTK_WIDGET_REALIZED (widget))
1541 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1542 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1546 gdk_gc_destroy (text->bg_gc);
1550 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1551 text->bg_gc = create_bg_gc (text);
1553 recompute_geometry (text);
1556 if (text->current_font)
1557 text_font_unref (text->current_font);
1558 text->current_font = get_text_font (widget->style->font);
1562 gtk_stext_state_changed (GtkWidget *widget,
1563 GtkStateType previous_state)
1565 GtkSText *text = GTK_STEXT (widget);
1567 if (GTK_WIDGET_REALIZED (widget))
1569 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1570 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1575 gtk_stext_unrealize (GtkWidget *widget)
1579 g_return_if_fail (widget != NULL);
1580 g_return_if_fail (GTK_IS_STEXT (widget));
1582 text = GTK_STEXT (widget);
1584 #ifdef USE_GTKGDK_XIM
1585 if (GTK_EDITABLE (widget)->ic)
1587 gdk_ic_destroy (GTK_EDITABLE (widget)->ic);
1588 GTK_EDITABLE (widget)->ic = NULL;
1590 if (GTK_EDITABLE (widget)->ic_attr)
1592 gdk_ic_attr_destroy (GTK_EDITABLE (widget)->ic_attr);
1593 GTK_EDITABLE (widget)->ic_attr = NULL;
1597 gdk_window_set_user_data (text->text_area, NULL);
1598 gdk_window_destroy (text->text_area);
1599 text->text_area = NULL;
1601 gdk_gc_destroy (text->gc);
1606 gdk_gc_destroy (text->bg_gc);
1610 gdk_pixmap_unref (text->line_wrap_bitmap);
1611 gdk_pixmap_unref (text->line_arrow_bitmap);
1613 unrealize_properties (text);
1617 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1618 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1622 clear_focus_area (GtkSText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1624 GtkWidget *widget = GTK_WIDGET (text);
1627 gint ythick = TEXT_BORDER_ROOM + widget->style->klass->ythickness;
1628 gint xthick = TEXT_BORDER_ROOM + widget->style->klass->xthickness;
1632 if (area_width == 0 || area_height == 0)
1635 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1637 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1639 gdk_gc_set_ts_origin (text->bg_gc,
1640 (- (gint)text->first_onscreen_hor_pixel + xthick) % width,
1641 (- (gint)text->first_onscreen_ver_pixel + ythick) % height);
1646 gc = widget->style->bg_gc[widget->state];
1649 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1650 area_x, area_y, area_width, area_height);
1654 gtk_stext_draw_focus (GtkWidget *widget)
1660 g_return_if_fail (widget != NULL);
1661 g_return_if_fail (GTK_IS_STEXT (widget));
1663 text = GTK_STEXT (widget);
1665 if (GTK_WIDGET_DRAWABLE (widget))
1667 gint ythick = widget->style->klass->ythickness;
1668 gint xthick = widget->style->klass->xthickness;
1669 gint xextra = TEXT_BORDER_ROOM;
1670 gint yextra = TEXT_BORDER_ROOM;
1672 TDEBUG (("in gtk_stext_draw_focus\n"));
1676 width = widget->allocation.width;
1677 height = widget->allocation.height;
1679 if (GTK_WIDGET_HAS_FOCUS (widget))
1688 gtk_paint_focus (widget->style, widget->window,
1689 NULL, widget, "text",
1691 widget->allocation.width - 1,
1692 widget->allocation.height - 1);
1695 gtk_paint_shadow (widget->style, widget->window,
1696 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1697 NULL, widget, "text",
1698 x, y, width, height);
1702 width -= 2 * xthick;
1703 height -= 2 * ythick;
1706 clear_focus_area (text, x, y, width, yextra);
1708 clear_focus_area (text, x, y + yextra,
1709 xextra, y + height - 2 * yextra);
1711 clear_focus_area (text, x + width - xextra, y + yextra,
1712 xextra, height - 2 * ythick);
1714 clear_focus_area (text, x, x + height - yextra, width, yextra);
1718 TDEBUG (("in gtk_stext_draw_focus (undrawable !!!)\n"));
1723 gtk_stext_size_request (GtkWidget *widget,
1724 GtkRequisition *requisition)
1731 g_return_if_fail (widget != NULL);
1732 g_return_if_fail (GTK_IS_STEXT (widget));
1733 g_return_if_fail (requisition != NULL);
1735 xthickness = widget->style->klass->xthickness + TEXT_BORDER_ROOM;
1736 ythickness = widget->style->klass->ythickness + TEXT_BORDER_ROOM;
1738 char_height = MIN_TEXT_HEIGHT_LINES * (widget->style->font->ascent +
1739 widget->style->font->descent);
1741 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (widget->style->font,
1742 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1746 requisition->width = char_width + xthickness * 2;
1747 requisition->height = char_height + ythickness * 2;
1751 gtk_stext_size_allocate (GtkWidget *widget,
1752 GtkAllocation *allocation)
1755 GtkEditable *editable;
1757 g_return_if_fail (widget != NULL);
1758 g_return_if_fail (GTK_IS_STEXT (widget));
1759 g_return_if_fail (allocation != NULL);
1761 text = GTK_STEXT (widget);
1762 editable = GTK_EDITABLE (widget);
1764 widget->allocation = *allocation;
1765 if (GTK_WIDGET_REALIZED (widget))
1767 gdk_window_move_resize (widget->window,
1768 allocation->x, allocation->y,
1769 allocation->width, allocation->height);
1771 gdk_window_move_resize (text->text_area,
1772 widget->style->klass->xthickness + TEXT_BORDER_ROOM,
1773 widget->style->klass->ythickness + TEXT_BORDER_ROOM,
1774 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->klass->xthickness +
1775 (gint)TEXT_BORDER_ROOM) * 2),
1776 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->klass->ythickness +
1777 (gint)TEXT_BORDER_ROOM) * 2));
1779 #ifdef USE_GTKGDK_XIM
1780 if (editable->ic && (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
1784 gdk_window_get_size (text->text_area, &width, &height);
1785 editable->ic_attr->preedit_area.width = width;
1786 editable->ic_attr->preedit_area.height = height;
1788 gdk_ic_set_attr (editable->ic,
1789 editable->ic_attr, GDK_IC_PREEDIT_AREA);
1793 recompute_geometry (text);
1798 gtk_stext_draw (GtkWidget *widget,
1801 g_return_if_fail (widget != NULL);
1802 g_return_if_fail (GTK_IS_STEXT (widget));
1803 g_return_if_fail (area != NULL);
1805 if (GTK_WIDGET_DRAWABLE (widget))
1807 expose_text (GTK_STEXT (widget), area, TRUE);
1808 gtk_widget_draw_focus (widget);
1813 gtk_stext_expose (GtkWidget *widget,
1814 GdkEventExpose *event)
1816 g_return_val_if_fail (widget != NULL, FALSE);
1817 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1818 g_return_val_if_fail (event != NULL, FALSE);
1820 if (event->window == GTK_STEXT (widget)->text_area)
1822 TDEBUG (("in gtk_stext_expose (expose)\n"));
1823 expose_text (GTK_STEXT (widget), &event->area, TRUE);
1825 else if (event->count == 0)
1827 TDEBUG (("in gtk_stext_expose (focus)\n"));
1828 gtk_widget_draw_focus (widget);
1835 gtk_stext_scroll_timeout (gpointer data)
1838 GdkEventMotion event;
1840 GdkModifierType mask;
1842 GDK_THREADS_ENTER ();
1844 text = GTK_STEXT (data);
1847 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1849 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1856 gtk_stext_motion_notify (GTK_WIDGET (text), &event);
1859 GDK_THREADS_LEAVE ();
1865 gtk_stext_button_press (GtkWidget *widget,
1866 GdkEventButton *event)
1869 GtkEditable *editable;
1870 static GdkAtom ctext_atom = GDK_NONE;
1872 g_return_val_if_fail (widget != NULL, FALSE);
1873 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1874 g_return_val_if_fail (event != NULL, FALSE);
1876 if (ctext_atom == GDK_NONE)
1877 ctext_atom = gdk_atom_intern ("COMPOUND_TEXT", FALSE);
1879 text = GTK_STEXT (widget);
1880 editable = GTK_EDITABLE (widget);
1882 if (text->button && (event->button != text->button))
1885 text->button = event->button;
1887 if (!GTK_WIDGET_HAS_FOCUS (widget))
1888 gtk_widget_grab_focus (widget);
1890 if (event->button == 1)
1892 switch (event->type)
1894 case GDK_BUTTON_PRESS:
1895 gtk_grab_add (widget);
1897 undraw_cursor (text, FALSE);
1898 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1899 draw_cursor (text, FALSE);
1901 /* Set it now, so we display things right. We'll unset it
1902 * later if things don't work out */
1903 editable->has_selection = TRUE;
1904 gtk_stext_set_selection (GTK_EDITABLE(text),
1905 text->cursor_mark.index,
1906 text->cursor_mark.index);
1910 case GDK_2BUTTON_PRESS:
1911 gtk_stext_select_word (text, event->time);
1914 case GDK_3BUTTON_PRESS:
1915 gtk_stext_select_line (text, event->time);
1922 else if (event->type == GDK_BUTTON_PRESS)
1924 if ((event->button == 2) && editable->editable)
1926 if (editable->selection_start_pos == editable->selection_end_pos ||
1927 editable->has_selection)
1929 undraw_cursor (text, FALSE);
1930 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1931 draw_cursor (text, FALSE);
1935 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1936 ctext_atom, event->time);
1940 gtk_grab_add (widget);
1942 undraw_cursor (text, FALSE);
1943 find_mouse_cursor (text, event->x, event->y);
1944 draw_cursor (text, FALSE);
1946 gtk_stext_set_selection (GTK_EDITABLE(text),
1947 text->cursor_mark.index,
1948 text->cursor_mark.index);
1950 editable->has_selection = FALSE;
1951 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1952 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1960 gtk_stext_button_release (GtkWidget *widget,
1961 GdkEventButton *event)
1964 GtkEditable *editable;
1965 g_return_val_if_fail (widget != NULL, FALSE);
1966 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1967 g_return_val_if_fail (event != NULL, FALSE);
1969 text = GTK_STEXT (widget);
1971 gtk_grab_remove (widget);
1973 if (text->button != event->button)
1980 gtk_timeout_remove (text->timer);
1984 if (event->button == 1)
1986 text = GTK_STEXT (widget);
1987 editable = GTK_EDITABLE (widget);
1989 gtk_grab_remove (widget);
1991 editable->has_selection = FALSE;
1992 if (editable->selection_start_pos != editable->selection_end_pos)
1994 if (gtk_selection_owner_set (widget,
1995 GDK_SELECTION_PRIMARY,
1997 editable->has_selection = TRUE;
1999 gtk_stext_update_text (editable, editable->selection_start_pos,
2000 editable->selection_end_pos);
2004 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
2005 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
2008 else if (event->button == 3)
2010 gtk_grab_remove (widget);
2013 undraw_cursor (text, FALSE);
2014 find_cursor (text, TRUE);
2015 draw_cursor (text, FALSE);
2021 gtk_stext_motion_notify (GtkWidget *widget,
2022 GdkEventMotion *event)
2027 GdkModifierType mask;
2029 g_return_val_if_fail (widget != NULL, FALSE);
2030 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2031 g_return_val_if_fail (event != NULL, FALSE);
2033 text = GTK_STEXT (widget);
2037 mask = event->state;
2038 if (event->is_hint || (text->text_area != event->window))
2040 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
2043 if ((text->button == 0) ||
2044 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
2047 gdk_window_get_size (text->text_area, NULL, &height);
2049 if ((y < 0) || (y > height))
2051 if (text->timer == 0)
2053 text->timer = gtk_timeout_add (SCROLL_TIME,
2054 gtk_stext_scroll_timeout,
2058 scroll_int (text, y/2);
2060 scroll_int (text, (y - height)/2);
2066 undraw_cursor (GTK_STEXT (widget), FALSE);
2067 find_mouse_cursor (GTK_STEXT (widget), x, y);
2068 draw_cursor (GTK_STEXT (widget), FALSE);
2070 gtk_stext_set_selection (GTK_EDITABLE(text),
2071 GTK_EDITABLE(text)->selection_start_pos,
2072 text->cursor_mark.index);
2078 gtk_stext_insert_text (GtkEditable *editable,
2079 const gchar *new_text,
2080 gint new_text_length,
2083 GtkSText *text = GTK_STEXT (editable);
2085 GdkColor *fore, *back;
2087 TextProperty *property;
2089 gtk_stext_set_point (text, *position);
2091 property = MARK_CURRENT_PROPERTY (&text->point);
2092 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
2093 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
2094 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
2096 gtk_stext_insert (text, font, fore, back, new_text, new_text_length);
2098 *position = text->point.index;
2102 gtk_stext_delete_text (GtkEditable *editable,
2108 g_return_if_fail (start_pos >= 0);
2110 text = GTK_STEXT (editable);
2112 gtk_stext_set_point (text, start_pos);
2114 end_pos = TEXT_LENGTH (text);
2116 if (end_pos > start_pos)
2117 gtk_stext_forward_delete (text, end_pos - start_pos);
2121 gtk_stext_key_press (GtkWidget *widget,
2125 GtkEditable *editable;
2130 g_return_val_if_fail (widget != NULL, FALSE);
2131 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2132 g_return_val_if_fail (event != NULL, FALSE);
2136 text = GTK_STEXT (widget);
2137 editable = GTK_EDITABLE (widget);
2139 key = event->keyval;
2142 if ((GTK_EDITABLE(text)->editable == FALSE))
2144 switch (event->keyval)
2147 if (event->state & GDK_CONTROL_MASK)
2148 scroll_int (text, -text->vadj->value);
2153 if (event->state & GDK_CONTROL_MASK)
2154 scroll_int (text, +text->vadj->upper);
2158 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
2159 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
2160 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
2161 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
2163 if (event->state & GDK_CONTROL_MASK)
2164 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2175 gint extend_selection;
2177 guint initial_pos = editable->current_pos;
2179 text->point = find_mark (text, text->cursor_mark.index);
2181 extend_selection = event->state & GDK_SHIFT_MASK;
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 */
2344 if (event->state & GDK_CONTROL_MASK)
2346 if ((key >= 'A') && (key <= 'Z'))
2349 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2351 (* control_keys[(int) (key - 'a')]) (editable, event->time);
2357 else if (event->state & GDK_MOD1_MASK)
2359 if ((key >= 'A') && (key <= 'Z'))
2362 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2364 (* alt_keys[(int) (key - 'a')]) (editable, event->time);
2370 else if (event->length > 0)
2372 extend_selection = FALSE;
2374 gtk_editable_delete_selection (editable);
2375 position = text->point.index;
2376 gtk_editable_insert_text (editable, event->string, event->length, &position);
2384 if (return_val && (editable->current_pos != initial_pos))
2386 if (extend_selection)
2388 if (editable->current_pos < editable->selection_start_pos)
2389 gtk_stext_set_selection (editable, editable->current_pos,
2390 editable->selection_end_pos);
2391 else if (editable->current_pos > editable->selection_end_pos)
2392 gtk_stext_set_selection (editable, editable->selection_start_pos,
2393 editable->current_pos);
2397 gtk_stext_set_selection (editable, editable->current_pos,
2398 editable->selection_end_pos);
2400 gtk_stext_set_selection (editable, editable->selection_start_pos,
2401 editable->current_pos);
2405 gtk_stext_set_selection (editable, 0, 0);
2407 gtk_editable_claim_selection (editable,
2408 editable->selection_start_pos != editable->selection_end_pos,
2417 gtk_stext_focus_in (GtkWidget *widget,
2418 GdkEventFocus *event)
2420 g_return_val_if_fail (widget != NULL, FALSE);
2421 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2422 g_return_val_if_fail (event != NULL, FALSE);
2424 TDEBUG (("in gtk_stext_focus_in\n"));
2426 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
2427 gtk_widget_draw_focus (widget);
2429 #ifdef USE_GTKGDK_XIM
2430 if (GTK_EDITABLE(widget)->ic)
2431 gdk_im_begin (GTK_EDITABLE(widget)->ic, GTK_STEXT(widget)->text_area);
2434 draw_cursor (GTK_STEXT(widget), TRUE);
2435 /* SYLPHEED: cursor */
2436 GTK_STEXT(widget)->cursor_visible = TRUE;
2437 gtk_stext_enable_blink (GTK_STEXT(widget));
2443 gtk_stext_focus_out (GtkWidget *widget,
2444 GdkEventFocus *event)
2446 g_return_val_if_fail (widget != NULL, FALSE);
2447 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2448 g_return_val_if_fail (event != NULL, FALSE);
2450 TDEBUG (("in gtk_stext_focus_out\n"));
2452 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
2453 gtk_widget_draw_focus (widget);
2456 * should disable blink before undrawing the cursor - disabling blink
2457 * redraws the cursor.
2459 gtk_stext_disable_blink(GTK_STEXT(widget));
2460 undraw_cursor (GTK_STEXT(widget), TRUE);
2461 GTK_STEXT(widget)->cursor_visible = FALSE;
2463 #ifdef USE_GTKGDK_XIM
2471 gtk_stext_adjustment (GtkAdjustment *adjustment,
2476 g_return_if_fail (adjustment != NULL);
2477 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2478 g_return_if_fail (text != NULL);
2479 g_return_if_fail (GTK_IS_STEXT (text));
2481 /* Clamp the value here, because we'll get really confused
2482 * if someone tries to move the adjusment outside of the
2485 old_val = adjustment->value;
2487 adjustment->value = MIN (adjustment->value, adjustment->upper - adjustment->page_size);
2488 adjustment->value = MAX (adjustment->value, 0.0);
2490 if (adjustment->value != old_val)
2492 gtk_signal_handler_block_by_func (GTK_OBJECT (adjustment),
2493 GTK_SIGNAL_FUNC (gtk_stext_adjustment),
2495 gtk_adjustment_changed (adjustment);
2496 gtk_signal_handler_unblock_by_func (GTK_OBJECT (adjustment),
2497 GTK_SIGNAL_FUNC (gtk_stext_adjustment),
2501 /* Just ignore it if we haven't been size-allocated and realized yet */
2502 if (text->line_start_cache == NULL)
2505 if (adjustment == text->hadj)
2507 g_warning ("horizontal scrolling not implemented");
2511 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2515 undraw_cursor (text, FALSE);
2518 scroll_down (text, diff);
2519 else /* if (diff < 0) */
2520 scroll_up (text, diff);
2522 draw_cursor (text, FALSE);
2524 text->last_ver_value = adjustment->value;
2530 gtk_stext_disconnect (GtkAdjustment *adjustment,
2533 g_return_if_fail (adjustment != NULL);
2534 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2535 g_return_if_fail (text != NULL);
2536 g_return_if_fail (GTK_IS_STEXT (text));
2538 if (adjustment == text->hadj)
2539 gtk_stext_set_adjustments (text, NULL, text->vadj);
2540 if (adjustment == text->vadj)
2541 gtk_stext_set_adjustments (text, text->hadj, NULL);
2545 static GtkSPropertyMark
2546 find_this_line_start_mark (GtkSText* text, guint point_position, const GtkSPropertyMark* near)
2548 GtkSPropertyMark mark;
2550 mark = find_mark_near (text, point_position, near);
2552 while (mark.index > 0 &&
2553 GTK_STEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2554 decrement_mark (&mark);
2560 init_tab_cont (GtkSText* text, PrevTabCont* tab_cont)
2562 tab_cont->pixel_offset = 0;
2563 tab_cont->tab_start.tab_stops = text->tab_stops;
2564 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2566 if (!tab_cont->tab_start.to_next_tab)
2567 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2571 line_params_iterate (GtkSText* text,
2572 const GtkSPropertyMark* mark0,
2573 const PrevTabCont* tab_mark0,
2576 LineIteratorFunction iter)
2577 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2578 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2579 * for each line following MARK, iteration continues unless ITER_CALL
2582 GtkSPropertyMark mark = *mark0;
2583 PrevTabCont tab_conts[2];
2584 LineParams *lp, lpbuf;
2585 gint tab_cont_index = 0;
2588 tab_conts[0] = *tab_mark0;
2590 init_tab_cont (text, tab_conts);
2595 lp = g_chunk_new (LineParams, params_mem_chunk);
2599 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2600 tab_conts + (tab_cont_index + 1) % 2);
2602 if ((*iter) (text, lp, data))
2605 if (LAST_INDEX (text, lp->end))
2609 advance_mark (&mark);
2610 tab_cont_index = (tab_cont_index + 1) % 2;
2615 fetch_lines_iterator (GtkSText* text, LineParams* lp, void* data)
2617 FetchLinesData *fldata = (FetchLinesData*) data;
2619 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2621 switch (fldata->fl_type)
2623 case FetchLinesCount:
2624 if (!text->line_wrap || !lp->wraps)
2627 if (fldata->data >= fldata->data_max)
2631 case FetchLinesPixels:
2633 fldata->data += LINE_HEIGHT(*lp);
2635 if (fldata->data >= fldata->data_max)
2645 fetch_lines (GtkSText* text,
2646 const GtkSPropertyMark* mark0,
2647 const PrevTabCont* tab_cont0,
2651 FetchLinesData fl_data;
2653 fl_data.new_lines = NULL;
2655 fl_data.data_max = data;
2656 fl_data.fl_type = fl_type;
2658 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2660 return g_list_reverse (fl_data.new_lines);
2664 fetch_lines_backward (GtkSText* text)
2666 GList* new_lines = NULL, *new_line_start;
2667 GtkSPropertyMark mark;
2669 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2672 mark = find_this_line_start_mark (text,
2673 CACHE_DATA(text->line_start_cache).start.index - 1,
2674 &CACHE_DATA(text->line_start_cache).start);
2676 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2678 while (new_line_start->next)
2679 new_line_start = new_line_start->next;
2681 new_line_start->next = text->line_start_cache;
2682 text->line_start_cache->prev = new_line_start;
2686 fetch_lines_forward (GtkSText* text, gint line_count)
2688 GtkSPropertyMark mark;
2689 GList* line = text->line_start_cache;
2694 mark = CACHE_DATA(line).end;
2696 if (LAST_INDEX (text, mark))
2699 advance_mark(&mark);
2701 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2704 line->next->prev = line;
2707 /* Compute the number of lines, and vertical pixels for n characters
2708 * starting from the point
2711 compute_lines_pixels (GtkSText* text, guint char_count,
2712 guint *lines, guint *pixels)
2714 GList *line = text->current_line;
2715 gint chars_left = char_count;
2720 /* If chars_left == 0, that means we're joining two lines in a
2721 * deletion, so add in the values for the next line as well
2723 for (; line && chars_left >= 0; line = line->next)
2725 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2727 if (line == text->current_line)
2728 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2730 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2732 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2736 chars_left = 0; /* force another loop */
2739 fetch_lines_forward (text, 1);
2744 total_line_height (GtkSText* text, GList* line, gint line_count)
2748 for (; line && line_count > 0; line = line->next)
2750 height += LINE_HEIGHT(CACHE_DATA(line));
2752 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2756 fetch_lines_forward (text, line_count);
2763 swap_lines (GtkSText* text, GList* old, GList* new, guint old_line_count)
2765 if (old == text->line_start_cache)
2769 for (; old_line_count > 0; old_line_count -= 1)
2771 while (text->line_start_cache &&
2773 CACHE_DATA(text->line_start_cache).wraps)
2774 remove_cache_line(text, text->line_start_cache);
2776 remove_cache_line(text, text->line_start_cache);
2779 last = g_list_last (new);
2781 last->next = text->line_start_cache;
2783 if (text->line_start_cache)
2784 text->line_start_cache->prev = last;
2786 text->line_start_cache = new;
2792 g_assert (old->prev);
2796 for (; old_line_count > 0; old_line_count -= 1)
2798 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2799 old = remove_cache_line (text, old);
2801 old = remove_cache_line (text, old);
2807 last = g_list_last (new);
2817 correct_cache_delete (GtkSText* text, gint nchars, gint lines)
2819 GList* cache = text->current_line;
2822 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2825 for (; cache; cache = cache->next)
2827 GtkSPropertyMark *start = &CACHE_DATA(cache).start;
2828 GtkSPropertyMark *end = &CACHE_DATA(cache).end;
2830 start->index -= nchars;
2831 end->index -= nchars;
2833 if (LAST_INDEX (text, text->point) &&
2834 start->index == text->point.index)
2835 *start = text->point;
2836 else if (start->property == text->point.property)
2837 start->offset = start->index - (text->point.index - text->point.offset);
2839 if (LAST_INDEX (text, text->point) &&
2840 end->index == text->point.index)
2842 if (end->property == text->point.property)
2843 end->offset = end->index - (text->point.index - text->point.offset);
2845 /*TEXT_ASSERT_MARK(text, start, "start");*/
2846 /*TEXT_ASSERT_MARK(text, end, "end");*/
2851 delete_expose (GtkSText* text, guint nchars, guint old_lines, guint old_pixels)
2853 GtkWidget *widget = GTK_WIDGET (text);
2856 guint new_pixels = 0;
2858 GList* new_line = NULL;
2861 text->cursor_virtual_x = 0;
2863 correct_cache_delete (text, nchars, old_lines);
2865 pixel_height = pixel_height_of(text, text->current_line) -
2866 LINE_HEIGHT(CACHE_DATA(text->current_line));
2868 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2869 CACHE_DATA(text->current_line).start = text->point;
2871 new_line = fetch_lines (text,
2872 &CACHE_DATA(text->current_line).start,
2873 &CACHE_DATA(text->current_line).tab_cont,
2877 swap_lines (text, text->current_line, new_line, old_lines);
2879 text->current_line = new_line;
2881 new_pixels = total_line_height (text, new_line, 1);
2883 gdk_window_get_size (text->text_area, &width, &height);
2885 if (old_pixels != new_pixels)
2887 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2889 gdk_draw_pixmap (text->text_area,
2893 pixel_height + old_pixels,
2895 pixel_height + new_pixels,
2899 text->vadj->upper += new_pixels;
2900 text->vadj->upper -= old_pixels;
2901 adjust_adj (text, text->vadj);
2905 rect.y = pixel_height;
2907 rect.height = new_pixels;
2909 expose_text (text, &rect, FALSE);
2910 gtk_stext_draw_focus ( (GtkWidget *) text);
2912 text->cursor_mark = text->point;
2914 find_cursor (text, TRUE);
2916 if (old_pixels != new_pixels)
2918 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2921 rect.y = pixel_height + new_pixels;
2923 rect.height = height - rect.y;
2925 expose_text (text, &rect, FALSE);
2928 process_exposes (text);
2935 /* note, the point has already been moved forward */
2937 correct_cache_insert (GtkSText* text, gint nchars)
2940 GtkSPropertyMark *start;
2941 GtkSPropertyMark *end;
2943 /* If we inserted a property exactly at the beginning of the
2944 * line, we have to correct here, or fetch_lines will
2947 start = &CACHE_DATA(text->current_line).start;
2948 if (start->index == text->point.index - nchars)
2950 *start = text->point;
2951 move_mark_n (start, -nchars);
2954 /* Now correct the offsets, and check for start or end marks that
2955 * are after the point, yet point to a property before the point's
2956 * property. This indicates that they are meant to point to the
2957 * second half of a property we split in insert_text_property(), so
2958 * we fix them up that way.
2960 cache = text->current_line->next;
2962 for (; cache; cache = cache->next)
2964 start = &CACHE_DATA(cache).start;
2965 end = &CACHE_DATA(cache).end;
2967 if (LAST_INDEX (text, text->point) &&
2968 start->index == text->point.index)
2969 *start = text->point;
2972 if (start->property == text->point.property)
2974 start->offset += nchars;
2975 start->index += nchars;
2977 else if (start->property->next &&
2978 (start->property->next->next == text->point.property))
2980 /* We split the property, and this is the second half */
2981 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2982 start->index += nchars;
2983 start->property = text->point.property;
2986 start->index += nchars;
2989 if (LAST_INDEX (text, text->point) &&
2990 end->index == text->point.index)
2994 if (end->property == text->point.property)
2996 end->offset += nchars;
2997 end->index += nchars;
2999 else if (end->property->next &&
3000 (end->property->next->next == text->point.property))
3002 /* We split the property, and this is the second half */
3003 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
3004 end->index += nchars;
3005 end->property = text->point.property;
3008 end->index += nchars;
3011 /*TEXT_ASSERT_MARK(text, start, "start");*/
3012 /*TEXT_ASSERT_MARK(text, end, "end");*/
3018 insert_expose (GtkSText* text, guint old_pixels, gint nchars,
3019 guint new_line_count)
3021 GtkWidget *widget = GTK_WIDGET (text);
3024 guint new_pixels = 0;
3026 GList* new_lines = NULL;
3029 text->cursor_virtual_x = 0;
3031 undraw_cursor (text, FALSE);
3033 correct_cache_insert (text, nchars);
3035 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
3037 pixel_height = pixel_height_of(text, text->current_line) -
3038 LINE_HEIGHT(CACHE_DATA(text->current_line));
3040 new_lines = fetch_lines (text,
3041 &CACHE_DATA(text->current_line).start,
3042 &CACHE_DATA(text->current_line).tab_cont,
3046 swap_lines (text, text->current_line, new_lines, 1);
3048 text->current_line = new_lines;
3050 new_pixels = total_line_height (text, new_lines, new_line_count);
3052 gdk_window_get_size (text->text_area, &width, &height);
3054 if (old_pixels != new_pixels)
3056 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
3058 gdk_draw_pixmap (text->text_area,
3062 pixel_height + old_pixels,
3064 pixel_height + new_pixels,
3066 height + (old_pixels - new_pixels) - pixel_height);
3069 text->vadj->upper += new_pixels;
3070 text->vadj->upper -= old_pixels;
3071 adjust_adj (text, text->vadj);
3075 rect.y = pixel_height;
3077 rect.height = new_pixels;
3079 expose_text (text, &rect, FALSE);
3080 gtk_stext_draw_focus ( (GtkWidget *) text);
3082 text->cursor_mark = text->point;
3084 find_cursor (text, TRUE);
3086 draw_cursor (text, FALSE);
3088 if (old_pixels != new_pixels)
3090 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
3093 rect.y = pixel_height + new_pixels;
3095 rect.height = height - rect.y;
3097 expose_text (text, &rect, FALSE);
3100 process_exposes (text);
3103 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
3108 /* Text property functions */
3111 font_hash (gconstpointer font)
3113 return gdk_font_id ((const GdkFont*) font);
3116 static GHashTable *font_cache_table = NULL;
3118 static GtkSTextFont*
3119 get_text_font (GdkFont* gfont)
3124 if (!font_cache_table)
3125 font_cache_table = g_hash_table_new (font_hash, (GCompareFunc) gdk_font_equal);
3127 tf = g_hash_table_lookup (font_cache_table, gfont);
3135 tf = g_new (GtkSTextFont, 1);
3138 tf->gdk_font = gfont;
3139 gdk_font_ref (gfont);
3141 for(i = 0; i < 256; i += 1)
3142 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
3144 g_hash_table_insert (font_cache_table, gfont, tf);
3150 text_font_unref (GtkSTextFont *text_font)
3152 text_font->ref_count--;
3153 if (text_font->ref_count == 0)
3155 g_hash_table_remove (font_cache_table, text_font->gdk_font);
3156 gdk_font_unref (text_font->gdk_font);
3162 text_properties_equal (TextProperty* prop, GdkFont* font, GdkColor *fore, GdkColor *back)
3164 if (prop->flags & PROPERTY_FONT)
3167 GtkSTextFont *text_font;
3172 text_font = get_text_font (font);
3174 retval = (prop->font == text_font);
3175 text_font_unref (text_font);
3184 if (prop->flags & PROPERTY_FOREGROUND)
3186 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
3193 if (prop->flags & PROPERTY_BACKGROUND)
3195 if (!back || !gdk_color_equal (&prop->back_color, back))
3206 realize_property (GtkSText *text, TextProperty *prop)
3208 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3210 if (prop->flags & PROPERTY_FOREGROUND)
3211 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
3213 if (prop->flags & PROPERTY_BACKGROUND)
3214 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
3218 realize_properties (GtkSText *text)
3220 GList *tmp_list = text->text_properties;
3224 realize_property (text, tmp_list->data);
3226 tmp_list = tmp_list->next;
3231 unrealize_property (GtkSText *text, TextProperty *prop)
3233 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3235 if (prop->flags & PROPERTY_FOREGROUND)
3236 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
3238 if (prop->flags & PROPERTY_BACKGROUND)
3239 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
3243 unrealize_properties (GtkSText *text)
3245 GList *tmp_list = text->text_properties;
3249 unrealize_property (text, tmp_list->data);
3251 tmp_list = tmp_list->next;
3255 static TextProperty*
3256 new_text_property (GtkSText *text, GdkFont *font, GdkColor* fore,
3257 GdkColor* back, guint length)
3261 if (text_property_chunk == NULL)
3263 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
3264 sizeof(TextProperty),
3265 1024*sizeof(TextProperty),
3269 prop = g_chunk_new(TextProperty, text_property_chunk);
3274 prop->flags |= PROPERTY_FONT;
3275 prop->font = get_text_font (font);
3282 prop->flags |= PROPERTY_FOREGROUND;
3283 prop->fore_color = *fore;
3288 prop->flags |= PROPERTY_BACKGROUND;
3289 prop->back_color = *back;
3292 prop->length = length;
3294 if (GTK_WIDGET_REALIZED (text))
3295 realize_property (text, prop);
3301 destroy_text_property (TextProperty *prop)
3304 text_font_unref (prop->font);
3306 g_mem_chunk_free (text_property_chunk, prop);
3309 /* Flop the memory between the point and the gap around like a
3312 move_gap (GtkSText* text, guint index)
3314 if (text->gap_position < index)
3316 gint diff = index - text->gap_position;
3318 if (text->use_wchar)
3319 g_memmove (text->text.wc + text->gap_position,
3320 text->text.wc + text->gap_position + text->gap_size,
3321 diff*sizeof (GdkWChar));
3323 g_memmove (text->text.ch + text->gap_position,
3324 text->text.ch + text->gap_position + text->gap_size,
3327 text->gap_position = index;
3329 else if (text->gap_position > index)
3331 gint diff = text->gap_position - index;
3333 if (text->use_wchar)
3334 g_memmove (text->text.wc + index + text->gap_size,
3335 text->text.wc + index,
3336 diff*sizeof (GdkWChar));
3338 g_memmove (text->text.ch + index + text->gap_size,
3339 text->text.ch + index,
3342 text->gap_position = index;
3346 /* Increase the gap size. */
3348 make_forward_space (GtkSText* text, guint len)
3350 if (text->gap_size < len)
3352 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3354 if (sum >= text->text_len)
3358 while (i <= sum) i <<= 1;
3360 if (text->use_wchar)
3361 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3362 i*sizeof(GdkWChar));
3364 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3368 if (text->use_wchar)
3369 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3370 text->text.wc + text->gap_position + text->gap_size,
3371 (text->text_end - (text->gap_position + text->gap_size))
3374 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3375 text->text.ch + text->gap_position + text->gap_size,
3376 text->text_end - (text->gap_position + text->gap_size));
3378 text->text_end += len*2;
3379 text->gap_size += len*2;
3383 /* Inserts into the text property list a list element that guarantees
3384 * that for len characters following the point, text has the correct
3385 * property. does not move point. adjusts text_properties_point and
3386 * text_properties_point_offset relative to the current value of
3389 insert_text_property (GtkSText* text, GdkFont* font,
3390 GdkColor *fore, GdkColor* back, guint len)
3392 GtkSPropertyMark *mark = &text->point;
3393 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3394 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3396 if (MARK_OFFSET(mark) == 0)
3398 /* Point is on the boundary of two properties.
3399 * If it is the same as either, grow, else insert
3402 if (text_properties_equal(forward_prop, font, fore, back))
3404 /* Grow the property in front of us. */
3406 MARK_PROPERTY_LENGTH(mark) += len;
3408 else if (backward_prop &&
3409 text_properties_equal(backward_prop, font, fore, back))
3411 /* Grow property behind us, point property and offset
3414 SET_PROPERTY_MARK (&text->point,
3415 MARK_PREV_LIST_PTR (mark),
3416 backward_prop->length);
3418 backward_prop->length += len;
3420 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3421 (forward_prop->length == 1))
3423 /* Next property just has last position, take it over */
3425 if (GTK_WIDGET_REALIZED (text))
3426 unrealize_property (text, forward_prop);
3428 forward_prop->flags = 0;
3431 forward_prop->flags |= PROPERTY_FONT;
3432 forward_prop->font = get_text_font (font);
3435 forward_prop->font = NULL;
3439 forward_prop->flags |= PROPERTY_FOREGROUND;
3440 forward_prop->fore_color = *fore;
3444 forward_prop->flags |= PROPERTY_BACKGROUND;
3445 forward_prop->back_color = *back;
3447 forward_prop->length += len;
3449 if (GTK_WIDGET_REALIZED (text))
3450 realize_property (text, forward_prop);
3454 /* Splice a new property into the list. */
3456 GList* new_prop = g_list_alloc();
3458 new_prop->next = MARK_LIST_PTR(mark);
3459 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3460 new_prop->next->prev = new_prop;
3463 new_prop->prev->next = new_prop;
3465 new_prop->data = new_text_property (text, font, fore, back, len);
3467 SET_PROPERTY_MARK (mark, new_prop, 0);
3472 /* The following will screw up the line_start cache,
3473 * we'll fix it up in correct_cache_insert
3476 /* In the middle of forward_prop, if properties are equal,
3477 * just add to its length, else split it into two and splice
3479 if (text_properties_equal (forward_prop, font, fore, back))
3481 forward_prop->length += len;
3483 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3484 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3486 /* Inserting before only the last position in the text */
3489 forward_prop->length -= 1;
3491 new_prop = g_list_alloc();
3492 new_prop->data = new_text_property (text, font, fore, back, len+1);
3493 new_prop->prev = MARK_LIST_PTR(mark);
3494 new_prop->next = NULL;
3495 MARK_NEXT_LIST_PTR(mark) = new_prop;
3497 SET_PROPERTY_MARK (mark, new_prop, 0);
3501 GList* new_prop = g_list_alloc();
3502 GList* new_prop_forward = g_list_alloc();
3503 gint old_length = forward_prop->length;
3504 GList* next = MARK_NEXT_LIST_PTR(mark);
3506 /* Set the new lengths according to where they are split. Construct
3507 * two new properties. */
3508 forward_prop->length = MARK_OFFSET(mark);
3510 new_prop_forward->data =
3511 new_text_property(text,
3512 forward_prop->flags & PROPERTY_FONT ?
3513 forward_prop->font->gdk_font : NULL,
3514 forward_prop->flags & PROPERTY_FOREGROUND ?
3515 &forward_prop->fore_color : NULL,
3516 forward_prop->flags & PROPERTY_BACKGROUND ?
3517 &forward_prop->back_color : NULL,
3518 old_length - forward_prop->length);
3520 new_prop->data = new_text_property(text, font, fore, back, len);
3522 /* Now splice things in. */
3523 MARK_NEXT_LIST_PTR(mark) = new_prop;
3524 new_prop->prev = MARK_LIST_PTR(mark);
3526 new_prop->next = new_prop_forward;
3527 new_prop_forward->prev = new_prop;
3529 new_prop_forward->next = next;
3532 next->prev = new_prop_forward;
3534 SET_PROPERTY_MARK (mark, new_prop, 0);
3538 while (text->text_properties_end->next)
3539 text->text_properties_end = text->text_properties_end->next;
3541 while (text->text_properties->prev)
3542 text->text_properties = text->text_properties->prev;
3546 delete_text_property (GtkSText* text, guint nchars)
3548 /* Delete nchars forward from point. */
3550 /* Deleting text properties is problematical, because we
3551 * might be storing around marks pointing to a property.
3553 * The marks in question and how we handle them are:
3555 * point: We know the new value, since it will be at the
3556 * end of the deleted text, and we move it there
3558 * cursor: We just remove the mark and set it equal to the
3559 * point after the operation.
3560 * line-start cache: We replace most affected lines.
3561 * The current line gets used to fetch the new
3562 * lines so, if necessary, (delete at the beginning
3563 * of a line) we fix it up by setting it equal to the
3571 for(; nchars; nchars -= 1)
3573 prop = MARK_CURRENT_PROPERTY(&text->point);
3577 if (prop->length == 0)
3579 tmp = MARK_LIST_PTR (&text->point);
3581 is_first = tmp == text->text_properties;
3583 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3584 text->point.offset = 0;
3586 if (GTK_WIDGET_REALIZED (text))
3587 unrealize_property (text, prop);
3589 destroy_text_property (prop);
3590 g_list_free_1 (tmp);
3592 prop = MARK_CURRENT_PROPERTY (&text->point);
3595 text->text_properties = MARK_LIST_PTR (&text->point);
3597 g_assert (prop->length != 0);
3599 else if (prop->length == text->point.offset)
3601 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3602 text->point.offset = 0;
3606 /* Check to see if we have just the single final position remaining
3607 * along in a property; if so, combine it with the previous property
3609 if (LAST_INDEX (text, text->point) &&
3610 (MARK_OFFSET (&text->point) == 0) &&
3611 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3613 tmp = MARK_LIST_PTR (&text->point);
3614 prop = MARK_CURRENT_PROPERTY(&text->point);
3616 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3617 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3618 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3620 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3622 if (GTK_WIDGET_REALIZED (text))
3623 unrealize_property (text, prop);
3625 destroy_text_property (prop);
3626 g_list_free_1 (tmp);
3631 init_properties (GtkSText *text)
3633 if (!text->text_properties)
3635 text->text_properties = g_list_alloc();
3636 text->text_properties->next = NULL;
3637 text->text_properties->prev = NULL;
3638 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3639 text->text_properties_end = text->text_properties;
3641 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3643 text->point.index = 0;
3648 /**********************************************************************/
3649 /* Property Movement */
3650 /**********************************************************************/
3653 move_mark_n (GtkSPropertyMark* mark, gint n)
3656 advance_mark_n(mark, n);
3658 decrement_mark_n(mark, -n);
3662 advance_mark (GtkSPropertyMark* mark)
3664 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3668 if (prop->length > mark->offset + 1)
3672 mark->property = MARK_NEXT_LIST_PTR (mark);
3678 advance_mark_n (GtkSPropertyMark* mark, gint n)
3685 i = 0; /* otherwise it migth not be init. */
3686 prop = MARK_CURRENT_PROPERTY(mark);
3688 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3689 /* to make it easier */
3690 n += (mark->offset);
3691 mark->index -= mark->offset;
3693 /* first we take seven-mile-leaps to get to the right text
3695 while ((n-i) > prop->length - 1) {
3697 mark->index += prop->length;
3698 mark->property = MARK_NEXT_LIST_PTR (mark);
3699 prop = MARK_CURRENT_PROPERTY (mark);
3703 /* and then the rest */
3704 mark->index += n - i;
3705 mark->offset += n - i;
3709 decrement_mark (GtkSPropertyMark* mark)
3713 if (mark->offset > 0)
3717 mark->property = MARK_PREV_LIST_PTR (mark);
3718 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3723 decrement_mark_n (GtkSPropertyMark* mark, gint n)
3727 while (mark->offset < n) {
3728 /* jump to end of prev */
3729 n -= mark->offset + 1;
3730 mark->index -= mark->offset + 1;
3731 mark->property = MARK_PREV_LIST_PTR (mark);
3732 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3740 static GtkSPropertyMark
3741 find_mark (GtkSText* text, guint mark_position)
3743 return find_mark_near (text, mark_position, &text->point);
3747 * You can also start from the end, what a drag.
3749 static GtkSPropertyMark
3750 find_mark_near (GtkSText* text, guint mark_position, const GtkSPropertyMark* near)
3755 GtkSPropertyMark mark;
3758 diffa = mark_position + 1;
3760 diffa = mark_position - near->index;
3762 diffb = mark_position;
3774 mark.property = text->text_properties;
3778 move_mark_n (&mark, mark_position - mark.index);
3783 /* This routine must be called with scroll == FALSE, only when
3784 * point is at least partially on screen
3788 find_line_containing_point (GtkSText* text, guint point,
3794 text->current_line = NULL;
3798 /* Scroll backwards until the point is on screen
3800 while (CACHE_DATA(text->line_start_cache).start.index > point)
3801 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3803 /* Now additionally try to make sure that the point is fully on screen
3807 while (text->first_cut_pixels != 0 &&
3808 text->line_start_cache->next &&
3809 CACHE_DATA(text->line_start_cache->next).start.index > point)
3810 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3813 gdk_window_get_size (text->text_area, NULL, &height);
3815 for (cache = text->line_start_cache; cache; cache = cache->next)
3819 if (CACHE_DATA(cache).end.index >= point ||
3820 LAST_INDEX(text, CACHE_DATA(cache).end))
3822 text->current_line = cache; /* LOOK HERE, this proc has an
3823 * important side effect. */
3827 TEXT_SHOW_LINE (text, cache, "cache");
3829 if (cache->next == NULL)
3830 fetch_lines_forward (text, 1);
3834 lph = pixel_height_of (text, cache->next);
3836 /* Scroll the bottom of the line is on screen, or until
3837 * the line is the first onscreen line.
3839 while (cache->next != text->line_start_cache && lph > height)
3841 TEXT_SHOW_LINE (text, cache, "cache");
3842 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3843 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3844 lph = pixel_height_of (text, cache->next);
3849 g_assert_not_reached (); /* Must set text->current_line here */
3853 pixel_height_of (GtkSText* text, GList* cache_line)
3855 gint pixels = - text->first_cut_pixels;
3856 GList *cache = text->line_start_cache;
3859 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3861 if (cache->data == cache_line->data)
3864 cache = cache->next;
3870 /**********************************************************************/
3871 /* Search and Placement */
3872 /**********************************************************************/
3875 find_char_width (GtkSText* text, const GtkSPropertyMark *mark, const TabStopMark *tab_mark)
3878 gint16* char_widths;
3880 if (LAST_INDEX (text, *mark))
3883 ch = GTK_STEXT_INDEX (text, mark->index);
3884 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3888 return tab_mark->to_next_tab * char_widths[' '];
3890 else if (!text->use_wchar)
3892 return char_widths[ch];
3896 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3901 advance_tab_mark (GtkSText* text, TabStopMark* tab_mark, GdkWChar ch)
3903 if (tab_mark->to_next_tab == 1 || ch == '\t')
3905 if (tab_mark->tab_stops->next)
3907 tab_mark->tab_stops = tab_mark->tab_stops->next;
3908 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3912 tab_mark->to_next_tab = text->default_tab_width;
3917 tab_mark->to_next_tab -= 1;
3922 advance_tab_mark_n (GtkSText* text, TabStopMark* tab_mark, gint n)
3926 advance_tab_mark (text, tab_mark, 0);
3930 find_cursor_at_line (GtkSText* text, const LineParams* start_line, gint pixel_height)
3933 GtkEditable *editable = (GtkEditable *)text;
3935 GtkSPropertyMark mark = start_line->start;
3936 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3937 gint pixel_width = LINE_START_PIXEL (*start_line);
3939 while (mark.index < text->cursor_mark.index)
3941 pixel_width += find_char_width (text, &mark, &tab_mark);
3943 advance_tab_mark (text, &tab_mark, GTK_STEXT_INDEX(text, mark.index));
3944 advance_mark (&mark);
3947 text->cursor_pos_x = pixel_width;
3948 text->cursor_pos_y = pixel_height;
3949 text->cursor_char_offset = start_line->font_descent;
3950 text->cursor_mark = mark;
3952 ch = LAST_INDEX (text, mark) ?
3953 LINE_DELIM : GTK_STEXT_INDEX (text, mark.index);
3955 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3956 text->cursor_char = 0;
3958 text->cursor_char = ch;
3960 #ifdef USE_GTKGDK_XIM
3961 if (GTK_WIDGET_HAS_FOCUS(text) && gdk_im_ready() && editable->ic &&
3962 (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
3964 GdkICAttributesType mask = GDK_IC_SPOT_LOCATION |
3965 GDK_IC_PREEDIT_FOREGROUND |
3966 GDK_IC_PREEDIT_BACKGROUND;
3968 editable->ic_attr->spot_location.x = text->cursor_pos_x;
3969 editable->ic_attr->spot_location.y
3970 = text->cursor_pos_y - text->cursor_char_offset;
3971 editable->ic_attr->preedit_foreground = *MARK_CURRENT_FORE (text, &mark);
3972 editable->ic_attr->preedit_background = *MARK_CURRENT_BACK (text, &mark);
3974 if (MARK_CURRENT_FONT (text, &mark)->type == GDK_FONT_FONTSET)
3976 mask |= GDK_IC_PREEDIT_FONTSET;
3977 editable->ic_attr->preedit_fontset = MARK_CURRENT_FONT (text, &mark);
3980 gdk_ic_set_attr (editable->ic, editable->ic_attr, mask);
3986 find_cursor (GtkSText* text, gboolean scroll)
3988 if (GTK_WIDGET_REALIZED (text))
3990 find_line_containing_point (text, text->cursor_mark.index, scroll);
3992 if (text->current_line)
3993 find_cursor_at_line (text,
3994 &CACHE_DATA(text->current_line),
3995 pixel_height_of(text, text->current_line));
3998 GTK_EDITABLE (text)->current_pos = text->cursor_mark.index;
4002 find_mouse_cursor_at_line (GtkSText *text, const LineParams* lp,
4003 guint line_pixel_height,
4006 GtkSPropertyMark mark = lp->start;
4007 TabStopMark tab_mark = lp->tab_cont.tab_start;
4009 gint char_width = find_char_width(text, &mark, &tab_mark);
4010 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
4012 text->cursor_pos_y = line_pixel_height;
4016 GdkWChar ch = LAST_INDEX (text, mark) ?
4017 LINE_DELIM : GTK_STEXT_INDEX (text, mark.index);
4019 if (button_x < pixel_width || mark.index == lp->end.index)
4021 text->cursor_pos_x = pixel_width - (char_width+1)/2;
4022 text->cursor_mark = mark;
4023 text->cursor_char_offset = lp->font_descent;
4025 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
4026 text->cursor_char = 0;
4028 text->cursor_char = ch;
4033 advance_tab_mark (text, &tab_mark, ch);
4034 advance_mark (&mark);
4036 pixel_width += char_width/2;
4038 char_width = find_char_width (text, &mark, &tab_mark);
4040 pixel_width += (char_width+1)/2;
4045 find_mouse_cursor (GtkSText* text, gint x, gint y)
4048 GList* cache = text->line_start_cache;
4052 pixel_height = - text->first_cut_pixels;
4054 for (; cache; cache = cache->next)
4056 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
4058 if (y < pixel_height || !cache->next)
4060 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
4062 find_cursor (text, FALSE);
4069 /**********************************************************************/
4071 /**********************************************************************/
4074 free_cache (GtkSText* text)
4076 GList* cache = text->line_start_cache;
4081 cache = cache->prev;
4083 text->line_start_cache = cache;
4086 for (; cache; cache = cache->next)
4087 g_mem_chunk_free (params_mem_chunk, cache->data);
4089 g_list_free (text->line_start_cache);
4091 text->line_start_cache = NULL;
4095 remove_cache_line (GtkSText* text, GList* member)
4102 if (member == text->line_start_cache)
4103 text->line_start_cache = text->line_start_cache->next;
4106 member->prev->next = member->next;
4109 member->next->prev = member->prev;
4111 list = member->next;
4113 g_mem_chunk_free (params_mem_chunk, member->data);
4114 g_list_free_1 (member);
4119 /**********************************************************************/
4121 /**********************************************************************/
4124 move_cursor_buffer_ver (GtkSText *text, int dir)
4126 undraw_cursor (text, FALSE);
4130 scroll_int (text, text->vadj->upper);
4131 text->cursor_mark = find_this_line_start_mark (text,
4133 &text->cursor_mark);
4137 scroll_int (text, - text->vadj->value);
4138 text->cursor_mark = find_this_line_start_mark (text,
4140 &text->cursor_mark);
4143 find_cursor (text, TRUE);
4144 draw_cursor (text, FALSE);
4148 move_cursor_page_ver (GtkSText *text, int dir)
4150 scroll_int (text, dir * text->vadj->page_increment);
4154 move_cursor_ver (GtkSText *text, int count)
4157 GtkSPropertyMark mark;
4160 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
4161 offset = text->cursor_mark.index - mark.index;
4163 if (offset > text->cursor_virtual_x)
4164 text->cursor_virtual_x = offset;
4168 if (mark.index == 0)
4171 decrement_mark (&mark);
4172 mark = find_this_line_start_mark (text, mark.index, &mark);
4176 mark = text->cursor_mark;
4178 while (!LAST_INDEX(text, mark) && GTK_STEXT_INDEX(text, mark.index) != LINE_DELIM)
4179 advance_mark (&mark);
4181 if (LAST_INDEX(text, mark))
4184 advance_mark (&mark);
4187 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
4188 if (LAST_INDEX(text, mark) ||
4189 GTK_STEXT_INDEX(text, mark.index) == LINE_DELIM)
4192 undraw_cursor (text, FALSE);
4194 text->cursor_mark = mark;
4196 find_cursor (text, TRUE);
4198 draw_cursor (text, FALSE);
4202 move_cursor_hor (GtkSText *text, int count)
4204 /* count should be +-1. */
4205 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
4206 (count < 0 && text->cursor_mark.index < (- count)) ||
4210 text->cursor_virtual_x = 0;
4212 undraw_cursor (text, FALSE);
4214 move_mark_n (&text->cursor_mark, count);
4216 find_cursor (text, TRUE);
4218 draw_cursor (text, FALSE);
4221 /* SYLPHEED - the default cursor movement of GtkText is aeons ahead of the
4222 * current technology level of the current generation of programmers and
4223 * end users. so sylpheed has to take a more mundane approach to editing.
4226 * move_cursor_to_display_row_end() (end of display line)
4227 * move_cursor_to_display_row_home() (home of display line)
4228 * move_cursor_to_display_row_up() (line up)
4229 * move_cursor_to_display_row_down() (line down)
4233 * SYLPHEED_TODO: for some reason the line fetcher also returns markers
4234 * of just one character! should investigate this... -- alfons
4237 static void move_cursor_to_display_row_end(GtkSText *text)
4239 LineParams lp = CACHE_DATA(text->current_line);
4240 int to_move = (lp.end.index - text->cursor_mark.index);
4242 /* advance this much */
4244 move_cursor_hor(text, to_move);
4248 static void move_cursor_to_display_row_start(GtkSText *text)
4250 LineParams lp = CACHE_DATA(text->current_line);
4251 int to_move = (text->cursor_mark.index - lp.start.index);
4253 move_cursor_hor(text, -to_move);
4258 static gboolean range_intersect(guint x1, guint x2, guint y1, guint y2)
4261 if (x1 > x2) { tmp = x1; x1 = x2; x2 = tmp; }
4262 if (y1 > y2) { tmp = y1; y1 = y2; y1 = tmp; }
4263 if (y1 < x1) { tmp = x1; x1 = y1; y1 = tmp; tmp = x2; x2 = y2; y2 = tmp; }
4273 static gint back_display_row_fetcher(GtkSText *text,
4277 if (range_intersect(data->start, data->end, params->start.index, params->end.index)) {
4278 XDEBUG( ("%s(%d) - FOUND search (%d, %d), current (%d, %d)", __FILE__, __LINE__,
4279 data->start, data->end,
4280 params->start.index, params->end.index) );
4285 XDEBUG( ("%s(%d) - NEXT search (%d, %d), current (%d, %d)", __FILE__, __LINE__,
4286 data->start, data->end,
4287 params->start.index, params->end.index) );
4293 static void move_cursor_to_display_row_up(GtkSText *text)
4299 GtkSPropertyMark mark;
4301 mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
4304 if (mark.index == 0) {
4305 XDEBUG ( ("%s(%d) top of buffer", __FILE__, __LINE__) );
4308 /* we need previous DISPLAY row not the previous BUFFER line, so we go to start
4309 * of paragraph, and iterate over the lines until we have a LineParams that matches
4311 lp = CACHE_DATA(text->current_line);
4312 col = (text->cursor_mark.index - lp.start.index);
4313 data.start = lp.start.index;
4314 data.end = lp.end.index;
4316 /* get the previous line */
4317 if (mark.index != 0) {
4318 decrement_mark(&mark);
4319 if (mark.index != 0) {
4320 GtkSPropertyMark smark = mark;
4321 XDEBUG( ("%s(%d) finding line start mark", __FILE__, __LINE__) );
4322 mark = find_this_line_start_mark(text, smark.index -1, &smark);
4326 /* let's get the previous display line */
4327 XDEBUG( ("%s(%d) iterating to get display lines", __FILE__, __LINE__) );
4328 line_params_iterate(text, &mark, NULL, FALSE, &data,
4329 (LineIteratorFunction)back_display_row_fetcher);
4330 XDEBUG( ("%s(%d) done iterating. found = %d", __FILE__, __LINE__, data.found) );
4333 if (col < text->persist_column) col = text->persist_column;
4334 else text->persist_column = col;
4335 new_index = data.lp.start.index + col;
4336 XDEBUG( ("%s(%d) - new index = %d", __FILE__, __LINE__, new_index) );
4337 if (new_index > data.lp.end.index) {
4338 new_index = data.lp.end.index;
4340 /* and move the cursor */
4341 XDEBUG( ("%s(%d) - setting index", __FILE__, __LINE__) );
4342 gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);
4353 #if defined(AHX_DEBUG)
4354 static void print_line(GtkSText *text, guint start, guint end)
4356 gchar *buf = alloca(2048), *walk = buf;
4358 memset(buf, 0, 2048);
4359 for (; start <= end; start++) {
4360 *walk++ = GTK_STEXT_INDEX(text, start);
4362 XDEBUG( ("%s", buf) );
4366 static gint forward_display_row_fetcher(GtkSText *text,
4371 XDEBUG( ("%s(%d) - FW RETURNS. search (%d, %d), current (%d, %d)",
4372 __FILE__, __LINE__, data->start, data->end, lp->start.index, lp->end.index) );
4374 data->completed = TRUE;
4375 print_line(text, lp->start.index, lp->end.index);
4378 else if (range_intersect(data->start, data->end, lp->start.index, lp->end.index)) {
4379 XDEBUG( ("%s(%d) - FW FOUND IT. search (%d, %d), current (%d, %d)",
4380 __FILE__, __LINE__, data->start, data->end, lp->start.index, lp->end.index) );
4389 static void move_cursor_to_display_row_down (GtkSText *text)
4394 GtkSPropertyMark mark;
4395 fdrf data = { FALSE, FALSE };
4397 mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
4398 lp = CACHE_DATA(text->current_line);
4399 col = (text->cursor_mark.index - lp.start.index);
4401 data.start = lp.start.index;
4402 data.end = lp.end.index;
4404 /* find the next DISPLAY line */
4405 XDEBUG( ("%s(%d) - FW iterating", __FILE__, __LINE__) ) ;
4406 line_params_iterate(text, &mark, NULL, FALSE, &data,
4407 (LineIteratorFunction)forward_display_row_fetcher);
4408 XDEBUG( ("%s(%d) - FW done iterating", __FILE__, __LINE__) );
4410 if (data.completed) {
4411 if (col < text->persist_column) col = text->persist_column;
4412 else text->persist_column = col;
4413 new_index = data.lp.start.index + col;
4414 if (new_index > data.lp.end.index) {
4415 new_index = data.lp.end.index;
4417 /* and move the cursor */
4418 XDEBUG( ("%s(%d) - FW set pos %d", __FILE__, __LINE__, new_index) );
4419 gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);
4423 static void reset_persist_col_pos(GtkSText *text)
4425 text->persist_column = 0;
4429 gtk_stext_move_cursor (GtkEditable *editable,
4436 move_cursor_hor (GTK_STEXT (editable), 1);
4441 move_cursor_hor (GTK_STEXT (editable), -1);
4447 move_cursor_ver (GTK_STEXT (editable), 1);
4452 move_cursor_ver (GTK_STEXT (editable), -1);
4457 gtk_stext_move_forward_character (GtkSText *text)
4459 move_cursor_hor (text, 1);
4463 gtk_stext_move_backward_character (GtkSText *text)
4465 move_cursor_hor (text, -1);
4469 gtk_stext_move_next_line (GtkSText *text)
4471 move_cursor_ver (text, 1);
4475 gtk_stext_move_previous_line (GtkSText *text)
4477 move_cursor_ver (text, -1);
4481 gtk_stext_move_word (GtkEditable *editable,
4487 gtk_stext_move_forward_word (GTK_STEXT (editable));
4492 gtk_stext_move_backward_word (GTK_STEXT (editable));
4497 gtk_stext_move_forward_word (GtkSText *text)
4499 text->cursor_virtual_x = 0;
4501 undraw_cursor (text, FALSE);
4503 if (text->use_wchar)
4505 while (!LAST_INDEX (text, text->cursor_mark) &&
4506 !gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4507 advance_mark (&text->cursor_mark);
4509 while (!LAST_INDEX (text, text->cursor_mark) &&
4510 gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4511 advance_mark (&text->cursor_mark);
4515 while (!LAST_INDEX (text, text->cursor_mark) &&
4516 !isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4517 advance_mark (&text->cursor_mark);
4519 while (!LAST_INDEX (text, text->cursor_mark) &&
4520 isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4521 advance_mark (&text->cursor_mark);
4524 find_cursor (text, TRUE);
4525 draw_cursor (text, FALSE);
4529 gtk_stext_move_backward_word (GtkSText *text)
4531 text->cursor_virtual_x = 0;
4533 undraw_cursor (text, FALSE);
4535 if (text->use_wchar)
4537 while ((text->cursor_mark.index > 0) &&
4538 !gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4539 decrement_mark (&text->cursor_mark);
4541 while ((text->cursor_mark.index > 0) &&
4542 gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4543 decrement_mark (&text->cursor_mark);
4547 while ((text->cursor_mark.index > 0) &&
4548 !isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4549 decrement_mark (&text->cursor_mark);
4551 while ((text->cursor_mark.index > 0) &&
4552 isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4553 decrement_mark (&text->cursor_mark);
4556 find_cursor (text, TRUE);
4557 draw_cursor (text, FALSE);
4561 gtk_stext_move_page (GtkEditable *editable,
4566 scroll_int (GTK_STEXT (editable),
4567 y * GTK_STEXT(editable)->vadj->page_increment);
4571 gtk_stext_move_to_row (GtkEditable *editable,
4577 gtk_stext_move_to_column (GtkEditable *editable,
4582 text = GTK_STEXT (editable);
4584 text->cursor_virtual_x = 0; /* FIXME */
4586 undraw_cursor (text, FALSE);
4588 /* Move to the beginning of the line */
4589 while ((text->cursor_mark.index > 0) &&
4590 (GTK_STEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4591 decrement_mark (&text->cursor_mark);
4593 while (!LAST_INDEX (text, text->cursor_mark) &&
4594 (GTK_STEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4598 else if (column == 0)
4601 advance_mark (&text->cursor_mark);
4604 find_cursor (text, TRUE);
4605 draw_cursor (text, FALSE);
4609 gtk_stext_move_beginning_of_line (GtkSText *text)
4611 gtk_stext_move_to_column (GTK_EDITABLE (text), 0);
4616 gtk_stext_move_end_of_line (GtkSText *text)
4618 gtk_stext_move_to_column (GTK_EDITABLE (text), -1);
4622 gtk_stext_kill_char (GtkEditable *editable,
4627 text = GTK_STEXT (editable);
4629 if (editable->selection_start_pos != editable->selection_end_pos)
4630 gtk_editable_delete_selection (editable);
4635 if (text->point.index + 1 <= TEXT_LENGTH (text))
4636 gtk_editable_delete_text (editable, text->point.index, text->point.index + 1);
4640 if (text->point.index > 0)
4641 gtk_editable_delete_text (editable, text->point.index - 1, text->point.index);
4647 gtk_stext_delete_forward_character (GtkSText *text)
4649 gtk_stext_kill_char (GTK_EDITABLE (text), 1);
4653 gtk_stext_delete_backward_character (GtkSText *text)
4655 gtk_stext_kill_char (GTK_EDITABLE (text), -1);
4659 gtk_stext_kill_word (GtkEditable *editable,
4662 if (editable->selection_start_pos != editable->selection_end_pos)
4663 gtk_editable_delete_selection (editable);
4666 gint old_pos = editable->current_pos;
4669 gtk_stext_move_word (editable, 1);
4670 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
4674 gtk_stext_move_word (editable, -1);
4675 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
4681 gtk_stext_delete_forward_word (GtkSText *text)
4683 gtk_stext_kill_word (GTK_EDITABLE (text), 1);
4687 gtk_stext_delete_backward_word (GtkSText *text)
4689 gtk_stext_kill_word (GTK_EDITABLE (text), -1);
4693 gtk_stext_kill_line (GtkEditable *editable,
4696 gint old_pos = editable->current_pos;
4699 gtk_stext_move_to_column (editable, -1);
4700 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
4704 gtk_stext_move_to_column (editable, 0);
4705 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
4710 gtk_stext_delete_line (GtkSText *text)
4712 gtk_stext_move_to_column (GTK_EDITABLE (text), 0);
4713 gtk_stext_kill_line (GTK_EDITABLE (text), 1);
4717 gtk_stext_delete_to_line_end (GtkSText *text)
4719 gtk_stext_kill_line (GTK_EDITABLE (text), 1);
4723 gtk_stext_select_word (GtkSText *text, guint32 time)
4728 GtkEditable *editable;
4729 editable = GTK_EDITABLE (text);
4731 gtk_stext_move_backward_word (text);
4732 start_pos = text->cursor_mark.index;
4734 gtk_stext_move_forward_word (text);
4735 end_pos = text->cursor_mark.index;
4737 editable->has_selection = TRUE;
4738 gtk_stext_set_selection (editable, start_pos, end_pos);
4739 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
4743 gtk_stext_select_line (GtkSText *text, guint32 time)
4748 GtkEditable *editable;
4749 editable = GTK_EDITABLE (text);
4751 gtk_stext_move_beginning_of_line (text);
4752 start_pos = text->cursor_mark.index;
4754 gtk_stext_move_end_of_line (text);
4755 gtk_stext_move_forward_character (text);
4756 end_pos = text->cursor_mark.index;
4758 editable->has_selection = TRUE;
4759 gtk_stext_set_selection (editable, start_pos, end_pos);
4760 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
4763 /**********************************************************************/
4765 /**********************************************************************/
4768 adjust_adj (GtkSText* text, GtkAdjustment* adj)
4772 gdk_window_get_size (text->text_area, NULL, &height);
4774 adj->step_increment = MIN (adj->upper, (float) SCROLL_PIXELS);
4775 adj->page_increment = MIN (adj->upper, height - (float) KEY_SCROLL_PIXELS);
4776 adj->page_size = MIN (adj->upper, height);
4777 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4778 adj->value = MAX (adj->value, 0.0);
4780 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4784 set_vertical_scroll_iterator (GtkSText* text, LineParams* lp, void* data)
4786 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4788 if ((text->first_line_start_index >= lp->start.index) &&
4789 (text->first_line_start_index <= lp->end.index))
4791 svdata->mark = lp->start;
4793 if (text->first_line_start_index == lp->start.index)
4795 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4799 text->first_onscreen_ver_pixel = svdata->pixel_height;
4800 text->first_cut_pixels = 0;
4803 text->vadj->value = (float) text->first_onscreen_ver_pixel;
4806 svdata->pixel_height += LINE_HEIGHT (*lp);
4812 set_vertical_scroll_find_iterator (GtkSText* text, LineParams* lp, void* data)
4814 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4817 if (svdata->pixel_height <= (gint) text->vadj->value &&
4818 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4820 svdata->mark = lp->start;
4822 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4823 text->first_onscreen_ver_pixel = svdata->pixel_height;
4824 text->first_line_start_index = lp->start.index;
4830 svdata->pixel_height += LINE_HEIGHT (*lp);
4838 static GtkSPropertyMark
4839 set_vertical_scroll (GtkSText* text)
4841 GtkSPropertyMark mark = find_mark (text, 0);
4842 SetVerticalScrollData data;
4846 data.pixel_height = 0;
4847 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4849 text->vadj->upper = (float) data.pixel_height;
4850 orig_value = (gint) text->vadj->value;
4852 gdk_window_get_size (text->text_area, NULL, &height);
4854 text->vadj->step_increment = MIN (text->vadj->upper, (float) SCROLL_PIXELS);
4855 text->vadj->page_increment = MIN (text->vadj->upper, height - (float) KEY_SCROLL_PIXELS);
4856 text->vadj->page_size = MIN (text->vadj->upper, height);
4857 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4858 text->vadj->value = MAX (text->vadj->value, 0.0);
4860 text->last_ver_value = (gint)text->vadj->value;
4862 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4864 if (text->vadj->value != orig_value)
4866 /* We got clipped, and don't really know which line to put first. */
4867 data.pixel_height = 0;
4868 data.last_didnt_wrap = TRUE;
4870 line_params_iterate (text, &mark, NULL,
4872 set_vertical_scroll_find_iterator);
4879 scroll_int (GtkSText* text, gint diff)
4883 text->vadj->value += diff;
4885 upper = text->vadj->upper - text->vadj->page_size;
4886 text->vadj->value = MIN (text->vadj->value, upper);
4887 text->vadj->value = MAX (text->vadj->value, 0.0);
4889 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4893 process_exposes (GtkSText *text)
4897 /* Make sure graphics expose events are processed before scrolling
4900 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4902 gtk_widget_event (GTK_WIDGET (text), event);
4903 if (event->expose.count == 0)
4905 gdk_event_free (event);
4908 gdk_event_free (event);
4912 static gint last_visible_line_height (GtkSText* text)
4914 GList *cache = text->line_start_cache;
4917 gdk_window_get_size (text->text_area, NULL, &height);
4919 for (; cache->next; cache = cache->next)
4920 if (pixel_height_of(text, cache->next) > height)
4924 return pixel_height_of(text, cache) - 1;
4929 static gint first_visible_line_height (GtkSText* text)
4931 if (text->first_cut_pixels)
4932 return pixel_height_of(text, text->line_start_cache) + 1;
4938 scroll_down (GtkSText* text, gint diff0)
4944 text->first_onscreen_ver_pixel += diff0;
4948 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4950 text->first_cut_pixels += 1;
4954 text->first_cut_pixels = 0;
4956 text->line_start_cache = text->line_start_cache->next;
4957 g_assert (text->line_start_cache);
4959 text->first_line_start_index =
4960 CACHE_DATA(text->line_start_cache).start.index;
4962 if (!text->line_start_cache->next)
4963 fetch_lines_forward (text, 1);
4969 gdk_window_get_size (text->text_area, &width, &height);
4970 if (height > real_diff)
4971 gdk_draw_pixmap (text->text_area,
4979 height - real_diff);
4982 rect.y = MAX (0, height - real_diff);
4984 rect.height = MIN (height, real_diff);
4986 expose_text (text, &rect, FALSE);
4987 gtk_stext_draw_focus ( (GtkWidget *) text);
4989 if (text->current_line)
4993 text->cursor_pos_y -= real_diff;
4994 cursor_min = drawn_cursor_min(text);
4997 find_mouse_cursor (text, text->cursor_pos_x,
4998 first_visible_line_height (text));
5001 if (height > real_diff)
5002 process_exposes (text);
5006 scroll_up (GtkSText* text, gint diff0)
5012 text->first_onscreen_ver_pixel += diff0;
5016 g_assert (text->line_start_cache);
5018 if (text->first_cut_pixels > 0)
5020 text->first_cut_pixels -= 1;
5024 if (!text->line_start_cache->prev)
5025 fetch_lines_backward (text);
5027 text->line_start_cache = text->line_start_cache->prev;
5029 text->first_line_start_index =
5030 CACHE_DATA(text->line_start_cache).start.index;
5032 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
5038 gdk_window_get_size (text->text_area, &width, &height);
5039 if (height > real_diff)
5040 gdk_draw_pixmap (text->text_area,
5048 height - real_diff);
5053 rect.height = MIN (height, real_diff);
5055 expose_text (text, &rect, FALSE);
5056 gtk_stext_draw_focus ( (GtkWidget *) text);
5058 if (text->current_line)
5063 text->cursor_pos_y += real_diff;
5064 cursor_max = drawn_cursor_max(text);
5065 gdk_window_get_size (text->text_area, NULL, &height);
5067 if (cursor_max >= height)
5068 find_mouse_cursor (text, text->cursor_pos_x,
5069 last_visible_line_height (text));
5072 if (height > real_diff)
5073 process_exposes (text);
5076 /**********************************************************************/
5078 /**********************************************************************/
5080 /* Assumes mark starts a line. Calculates the height, width, and
5081 * displayable character count of a single DISPLAYABLE line. That
5082 * means that in line-wrap mode, this does may not compute the
5083 * properties of an entire line. */
5085 find_line_params (GtkSText* text,
5086 const GtkSPropertyMark* mark,
5087 const PrevTabCont *tab_cont,
5088 PrevTabCont *next_cont)
5091 TabStopMark tab_mark = tab_cont->tab_start;
5092 guint max_display_pixels;
5097 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
5099 if (text->wrap_rmargin) {
5100 /* SYLPHEED - since sylpheed is a mail program, assume we work with
5101 * fixed fonts. only assume we're using one font! */
5102 font = MARK_CURRENT_FONT(text, mark);
5104 /* SYLPHEED - ok for multi byte charsets to check for ASCII char?
5106 if (text->use_wchar)
5107 ch_width = gdk_char_width_wc(font, 'W');
5109 ch_width = gdk_char_width(font, 'W');
5111 /* SYLPHEED - max_display_chars has the rmargin in pixels
5113 max_display_pixels = text->wrap_rmargin * ch_width;
5116 /* SYLPHEED - we don't draw ugly word wrapping thing
5117 * if our wrap margin is set */
5118 if (!text->wrap_rmargin &&
5119 ((GTK_EDITABLE (text)->editable || !text->word_wrap)))
5120 max_display_pixels -= LINE_WRAP_ROOM;
5123 lp.tab_cont = *tab_cont;
5126 lp.pixel_width = tab_cont->pixel_offset;
5127 lp.displayable_chars = 0;
5129 lp.font_descent = 0;
5131 init_tab_cont (text, next_cont);
5133 while (!LAST_INDEX(text, lp.end))
5135 g_assert (lp.end.property);
5137 ch = GTK_STEXT_INDEX (text, lp.end.index);
5138 font = MARK_CURRENT_FONT (text, &lp.end);
5140 if (ch == LINE_DELIM)
5142 /* Newline doesn't count in computation of line height, even
5143 * if its in a bigger font than the rest of the line. Unless,
5144 * of course, there are no other characters. */
5146 if (!lp.font_ascent && !lp.font_descent)
5148 lp.font_ascent = font->ascent;
5149 lp.font_descent = font->descent;
5152 lp.tab_cont_next = *next_cont;
5157 ch_width = find_char_width (text, &lp.end, &tab_mark);
5159 if ((ch_width + lp.pixel_width > max_display_pixels) &&
5160 (lp.end.index > lp.start.index))
5164 if (text->line_wrap)
5166 next_cont->tab_start = tab_mark;
5167 next_cont->pixel_offset = 0;
5171 /* Here's the tough case, a tab is wrapping. */
5172 gint pixels_avail = max_display_pixels - lp.pixel_width;
5173 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
5174 gint spaces_avail = pixels_avail / space_width;
5176 if (spaces_avail == 0)
5178 decrement_mark (&lp.end);
5182 advance_tab_mark (text, &next_cont->tab_start, '\t');
5183 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
5185 lp.displayable_chars += 1;
5190 if (text->word_wrap)
5192 GtkSPropertyMark saved_mark = lp.end;
5193 guint saved_characters = lp.displayable_chars;
5195 lp.displayable_chars += 1;
5197 if (text->use_wchar)
5199 while (!gdk_iswspace (GTK_STEXT_INDEX (text, lp.end.index)) &&
5200 (lp.end.index > lp.start.index))
5202 decrement_mark (&lp.end);
5203 lp.displayable_chars -= 1;
5208 while (!isspace(GTK_STEXT_INDEX (text, lp.end.index)) &&
5209 (lp.end.index > lp.start.index))
5211 decrement_mark (&lp.end);
5212 lp.displayable_chars -= 1;
5216 /* If whole line is one word, revert to char wrapping */
5217 if (lp.end.index == lp.start.index)
5219 /* SYLPHEED: don't wrap URLs */
5220 if (gtkut_stext_is_uri_string(text, lp.end.index,
5221 gtk_stext_get_length(text)))
5223 lp.end = saved_mark;
5224 lp.displayable_chars = saved_characters + 1;
5229 lp.end = saved_mark;
5230 lp.displayable_chars = saved_characters;
5231 decrement_mark (&lp.end);
5236 /* Don't include this character, it will wrap. */
5237 decrement_mark (&lp.end);
5241 lp.tab_cont_next = *next_cont;
5248 lp.displayable_chars += 1;
5252 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
5253 lp.font_descent = MAX (font->descent, lp.font_descent);
5254 lp.pixel_width += ch_width;
5256 advance_mark(&lp.end);
5257 advance_tab_mark (text, &tab_mark, ch);
5260 if (LAST_INDEX(text, lp.start))
5262 /* Special case, empty last line. */
5263 font = MARK_CURRENT_FONT (text, &lp.end);
5265 lp.font_ascent = font->ascent;
5266 lp.font_descent = font->descent;
5269 lp.tab_cont_next = *next_cont;
5275 expand_scratch_buffer (GtkSText* text, guint len)
5277 if (len >= text->scratch_buffer_len)
5281 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
5283 if (text->use_wchar)
5285 if (!text->scratch_buffer.wc)
5286 text->scratch_buffer.wc = g_new (GdkWChar, i);
5288 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
5289 i * sizeof (GdkWChar));
5293 if (!text->scratch_buffer.ch)
5294 text->scratch_buffer.ch = g_new (guchar, i);
5296 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
5299 text->scratch_buffer_len = i;
5303 /* Side effect: modifies text->gc
5307 draw_bg_rect (GtkSText* text, GtkSPropertyMark *mark,
5308 gint x, gint y, gint width, gint height,
5309 gboolean already_cleared)
5311 GtkEditable *editable = GTK_EDITABLE(text);
5313 if ((mark->index >= MIN(editable->selection_start_pos, editable->selection_end_pos) &&
5314 mark->index < MAX(editable->selection_start_pos, editable->selection_end_pos)))
5316 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
5317 editable->has_selection ?
5318 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
5320 NULL, GTK_WIDGET(text), "text",
5321 x, y, width, height);
5323 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
5324 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
5326 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
5328 gdk_draw_rectangle (text->text_area,
5330 TRUE, x, y, width, height);
5332 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
5339 rect.height = height;
5341 clear_area (text, &rect);
5343 else if (!already_cleared)
5344 gdk_window_clear_area (text->text_area, x, y, width, height);
5348 draw_line (GtkSText* text,
5349 gint pixel_start_height,
5352 GdkGCValues gc_values;
5355 guint running_offset = lp->tab_cont.pixel_offset;
5356 union { GdkWChar *wc; guchar *ch; } buffer;
5359 GtkEditable *editable = GTK_EDITABLE(text);
5361 guint selection_start_pos = MIN (editable->selection_start_pos, editable->selection_end_pos);
5362 guint selection_end_pos = MAX (editable->selection_start_pos, editable->selection_end_pos);
5364 GtkSPropertyMark mark = lp->start;
5365 TabStopMark tab_mark = lp->tab_cont.tab_start;
5366 gint pixel_height = pixel_start_height + lp->font_ascent;
5367 guint chars = lp->displayable_chars;
5369 /* First provide a contiguous segment of memory. This makes reading
5370 * the code below *much* easier, and only incurs the cost of copying
5371 * when the line being displayed spans the gap. */
5372 if (mark.index <= text->gap_position &&
5373 mark.index + chars > text->gap_position)
5375 expand_scratch_buffer (text, chars);
5377 if (text->use_wchar)
5379 for (i = 0; i < chars; i += 1)
5380 text->scratch_buffer.wc[i] = GTK_STEXT_INDEX(text, mark.index + i);
5381 buffer.wc = text->scratch_buffer.wc;
5385 for (i = 0; i < chars; i += 1)
5386 text->scratch_buffer.ch[i] = GTK_STEXT_INDEX(text, mark.index + i);
5387 buffer.ch = text->scratch_buffer.ch;
5392 if (text->use_wchar)
5394 if (mark.index >= text->gap_position)
5395 buffer.wc = text->text.wc + mark.index + text->gap_size;
5397 buffer.wc = text->text.wc + mark.index;
5401 if (mark.index >= text->gap_position)
5402 buffer.ch = text->text.ch + mark.index + text->gap_size;
5404 buffer.ch = text->text.ch + mark.index;
5409 if (running_offset > 0)
5411 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
5412 LINE_HEIGHT (*lp), TRUE);
5418 if ((text->use_wchar && buffer.wc[0] != '\t') ||
5419 (!text->use_wchar && buffer.ch[0] != '\t'))
5421 union { GdkWChar *wc; guchar *ch; } next_tab;
5426 if (text->use_wchar)
5427 for (i=0; i<chars; i++)
5429 if (buffer.wc[i] == '\t')
5431 next_tab.wc = buffer.wc + i;
5436 next_tab.ch = memchr (buffer.ch, '\t', chars);
5438 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
5440 if (text->use_wchar)
5443 len = MIN (len, next_tab.wc - buffer.wc);
5448 len = MIN (len, next_tab.ch - buffer.ch);
5451 if (mark.index < selection_start_pos)
5452 len = MIN (len, selection_start_pos - mark.index);
5453 else if (mark.index < selection_end_pos)
5454 len = MIN (len, selection_end_pos - mark.index);
5456 font = MARK_CURRENT_FONT (text, &mark);
5457 if (font->type == GDK_FONT_FONT)
5459 gdk_gc_set_font (text->gc, font);
5460 gdk_gc_get_values (text->gc, &gc_values);
5461 if (text->use_wchar)
5462 pixel_width = gdk_text_width_wc (gc_values.font,
5465 pixel_width = gdk_text_width (gc_values.font,
5470 if (text->use_wchar)
5471 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
5473 pixel_width = gdk_text_width (font, buffer.ch, len);
5476 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
5477 pixel_width, LINE_HEIGHT (*lp), TRUE);
5479 if ((mark.index >= selection_start_pos) &&
5480 (mark.index < selection_end_pos))
5482 if (editable->has_selection)
5483 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_SELECTED];
5485 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_ACTIVE];
5489 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
5493 if (text->use_wchar)
5494 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
5501 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
5508 running_offset += pixel_width;
5510 advance_tab_mark_n (text, &tab_mark, len);
5514 gint pixels_remaining;
5520 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
5521 if (GTK_EDITABLE (text)->editable || !text->word_wrap)
5522 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
5524 pixels_remaining -= running_offset;
5526 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
5528 spaces_avail = pixels_remaining / space_width;
5529 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
5531 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
5532 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
5534 running_offset += tab_mark.to_next_tab *
5535 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
5537 advance_tab_mark (text, &tab_mark, '\t');
5540 advance_mark_n (&mark, len);
5541 if (text->use_wchar)
5550 draw_line_wrap (GtkSText* text, guint height /* baseline height */)
5557 if (!GTK_EDITABLE (text)->editable && text->word_wrap)
5560 /* SYLPHEED - don't draw ugly word wrapping thing if
5561 * our wrap margin is set */
5562 if (text->wrap_rmargin > 0) {
5566 if (text->line_wrap)
5568 bitmap = text->line_wrap_bitmap;
5569 bitmap_width = line_wrap_width;
5570 bitmap_height = line_wrap_height;
5574 bitmap = text->line_arrow_bitmap;
5575 bitmap_width = line_arrow_width;
5576 bitmap_height = line_arrow_height;
5579 gdk_window_get_size (text->text_area, &width, NULL);
5580 width -= LINE_WRAP_ROOM;
5582 gdk_gc_set_stipple (text->gc,
5585 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
5587 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5589 gdk_gc_set_ts_origin (text->gc,
5591 height - bitmap_height - 1);
5593 gdk_draw_rectangle (text->text_area,
5597 height - bitmap_height - 1 /* one pixel above the baseline. */,
5601 gdk_gc_set_ts_origin (text->gc, 0, 0);
5603 gdk_gc_set_fill (text->gc, GDK_SOLID);
5607 undraw_cursor (GtkSText* text, gint absolute)
5609 GtkEditable *editable = (GtkEditable *)text;
5611 TDEBUG (("in undraw_cursor\n"));
5614 text->cursor_drawn_level = 0;
5616 if ((text->cursor_drawn_level ++ == 0) &&
5617 (editable->selection_start_pos == editable->selection_end_pos) &&
5618 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5624 g_assert(text->cursor_mark.property);
5626 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5629 * changed the cursor to a real block (TM)
5631 if (text->cursor_type == GTK_STEXT_CURSOR_BLOCK)
5633 if (text->cursor_char)
5635 if (text->use_wchar)
5636 pixel_width = gdk_char_width_wc (font, text->cursor_char);
5638 pixel_width = gdk_char_width (font, (guchar)text->cursor_char);
5642 pixel_width = gdk_char_width (font, 'W');
5645 pixel_height = LINE_HEIGHT(CACHE_DATA(text->current_line));
5647 draw_bg_rect (text, &text->cursor_mark,
5649 text->cursor_pos_y - (pixel_height + 1),
5656 draw_bg_rect (text, &text->cursor_mark,
5658 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5659 2, font->descent + font->ascent + 1, FALSE);
5662 if (text->cursor_char)
5664 if (font->type == GDK_FONT_FONT)
5665 gdk_gc_set_font (text->gc, font);
5667 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5669 if (text->use_wchar)
5670 gdk_draw_text_wc (text->text_area, font,
5673 text->cursor_pos_y - text->cursor_char_offset,
5678 guchar ch = text->cursor_char;
5679 gdk_draw_text (text->text_area, font,
5682 text->cursor_pos_y - text->cursor_char_offset,
5691 drawn_cursor_min (GtkSText* text)
5695 g_assert(text->cursor_mark.property);
5697 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5699 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5703 drawn_cursor_max (GtkSText* text)
5707 g_assert(text->cursor_mark.property);
5709 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5711 return text->cursor_pos_y - text->cursor_char_offset;
5715 draw_cursor (GtkSText* text, gint absolute)
5717 GtkEditable *editable = (GtkEditable *)text;
5719 TDEBUG (("in draw_cursor\n"));
5722 text->cursor_drawn_level = 1;
5724 if ((--text->cursor_drawn_level == 0) &&
5725 editable->editable &&
5726 (editable->selection_start_pos == editable->selection_end_pos) &&
5727 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5733 g_assert (text->cursor_mark.property);
5735 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5738 * change the cursor to a real block (TM)
5740 if (text->cursor_type == GTK_STEXT_CURSOR_BLOCK)
5742 if (text->cursor_char)
5744 if (text->use_wchar)
5745 pixel_width = gdk_char_width_wc (font, text->cursor_char);
5747 pixel_width = gdk_char_width (font, (guchar)text->cursor_char);
5751 pixel_width = gdk_char_width (font, 'W');
5754 pixel_height = LINE_HEIGHT(CACHE_DATA(text->current_line));
5756 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5757 gdk_gc_set_function (text->gc, GDK_INVERT);
5758 gdk_draw_rectangle (text->text_area, text->gc, TRUE,
5760 text->cursor_pos_y - pixel_height,
5763 gdk_gc_set_function (text->gc, GDK_COPY);
5767 gdk_gc_set_line_attributes(text->gc, 2, GDK_LINE_SOLID, GDK_CAP_NOT_LAST, GDK_JOIN_MITER);
5768 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5770 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x + 1,
5771 text->cursor_pos_y + font->descent - text->cursor_char_offset,
5772 text->cursor_pos_x + 1,
5773 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5779 create_bg_gc (GtkSText *text)
5783 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5784 values.fill = GDK_TILED;
5786 return gdk_gc_new_with_values (text->text_area, &values,
5787 GDK_GC_FILL | GDK_GC_TILE);
5791 clear_area (GtkSText *text, GdkRectangle *area)
5793 GtkWidget *widget = GTK_WIDGET (text);
5799 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5801 gdk_gc_set_ts_origin (text->bg_gc,
5802 (- (gint)text->first_onscreen_hor_pixel) % width,
5803 (- (gint)text->first_onscreen_ver_pixel) % height);
5805 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5806 area->x, area->y, area->width, area->height);
5809 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5813 expose_text (GtkSText* text, GdkRectangle *area, gboolean cursor)
5815 GList *cache = text->line_start_cache;
5816 gint pixels = - text->first_cut_pixels;
5817 gint min_y = MAX (0, area->y);
5818 gint max_y = MAX (0, area->y + area->height);
5821 gdk_window_get_size (text->text_area, NULL, &height);
5822 max_y = MIN (max_y, height);
5824 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5826 clear_area (text, area);
5828 for (; pixels < height; cache = cache->next)
5830 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5832 draw_line (text, pixels, &CACHE_DATA(cache));
5834 if (CACHE_DATA(cache).wraps)
5835 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5838 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5840 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5841 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5843 /* We undraw and draw the cursor here to get the drawn
5844 * level right ... FIXME - maybe the second parameter
5845 * of draw_cursor should work differently
5847 undraw_cursor (text, FALSE);
5848 draw_cursor (text, FALSE);
5852 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5856 fetch_lines_forward (text, 1);
5865 gtk_stext_update_text (GtkEditable *editable,
5869 GtkSText *text = GTK_STEXT (editable);
5871 GList *cache = text->line_start_cache;
5872 gint pixels = - text->first_cut_pixels;
5878 end_pos = TEXT_LENGTH (text);
5880 if (end_pos < start_pos)
5883 gdk_window_get_size (text->text_area, &width, &height);
5889 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5891 for (; pixels < height; cache = cache->next)
5893 if (CACHE_DATA(cache).start.index < end_pos)
5895 if (CACHE_DATA(cache).end.index >= start_pos)
5898 area.y = MAX(0,pixels);
5899 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5905 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5909 fetch_lines_forward (text, 1);
5917 expose_text (text, &area, TRUE);
5921 recompute_geometry (GtkSText* text)
5923 GtkSPropertyMark mark, start_mark;
5930 mark = start_mark = set_vertical_scroll (text);
5932 /* We need a real start of a line when calling fetch_lines().
5933 * not the start of a wrapped line.
5935 while (mark.index > 0 &&
5936 GTK_STEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5937 decrement_mark (&mark);
5939 gdk_window_get_size (text->text_area, &width, &height);
5941 /* Fetch an entire line, to make sure that we get all the text
5942 * we backed over above, in addition to enough text to fill up
5943 * the space vertically
5946 new_lines = fetch_lines (text,
5952 mark = CACHE_DATA (g_list_last (new_lines)).end;
5953 if (!LAST_INDEX (text, mark))
5955 advance_mark (&mark);
5957 new_lines = g_list_concat (new_lines,
5962 height + text->first_cut_pixels));
5965 /* Now work forward to the actual first onscreen line */
5967 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5968 new_lines = new_lines->next;
5970 text->line_start_cache = new_lines;
5972 find_cursor (text, TRUE);
5975 /**********************************************************************/
5977 /**********************************************************************/
5980 gtk_stext_set_selection (GtkEditable *editable,
5984 GtkSText *text = GTK_STEXT (editable);
5986 guint start1, end1, start2, end2;
5989 end = TEXT_LENGTH (text);
5991 start1 = MIN(start,end);
5992 end1 = MAX(start,end);
5993 start2 = MIN(editable->selection_start_pos, editable->selection_end_pos);
5994 end2 = MAX(editable->selection_start_pos, editable->selection_end_pos);
5996 if (start2 < start1)
6000 tmp = start1; start1 = start2; start2 = tmp;
6001 tmp = end1; end1 = end2; end2 = tmp;
6004 undraw_cursor (text, FALSE);
6005 editable->selection_start_pos = start;
6006 editable->selection_end_pos = end;
6007 draw_cursor (text, FALSE);
6009 /* Expose only what changed */
6011 if (start1 < start2)
6012 gtk_stext_update_text (editable, start1, MIN(end1, start2));
6015 gtk_stext_update_text (editable, MAX(end1, start2), end2);
6016 else if (end2 < end1)
6017 gtk_stext_update_text (editable, end2, end1);
6026 stext_blink_timer_proc (GtkSText *text)
6028 if (text->cursor_state_on)
6030 text->cursor_state_on = FALSE;
6031 undraw_cursor (text, TRUE);
6032 /* kill this timer... */
6033 gtk_timeout_remove (text->cursor_timer_id);
6034 text->cursor_timer_id = gtk_timeout_add (text->cursor_off_ms,
6035 (GtkFunction) stext_blink_timer_proc,
6040 text->cursor_state_on = TRUE;
6041 draw_cursor (text, TRUE);
6042 /* kill this timer... */
6043 gtk_timeout_remove (text->cursor_timer_id);
6044 text->cursor_timer_id = gtk_timeout_add (text->cursor_on_ms,
6045 (GtkFunction) stext_blink_timer_proc,
6053 stext_idle_timer_proc (GtkSText *text)
6055 /* make sure the cursor timer is off */
6056 if (text->cursor_timer_id)
6058 gtk_timeout_remove (text->cursor_timer_id);
6059 text->cursor_timer_id = 0;
6062 /* assuming it's always on when calling this function ... */
6063 text->cursor_state_on = TRUE;
6064 text->cursor_timer_id = gtk_timeout_add (text->cursor_on_ms,
6065 (GtkFunction) stext_blink_timer_proc,
6067 /* make sure we kill the timer (could perhaps make this function return
6068 FALSE (not documented in the current docs). should check the source. */
6069 gtk_idle_remove (text->cursor_idle_time_timer_id);
6070 text->cursor_idle_time_timer_id = 0;
6076 gtk_stext_enable_blink (GtkSText *text)
6078 if (text->cursor_timer_on)
6080 gtk_stext_disable_blink (text);
6081 text->cursor_idle_time_timer_id = gtk_idle_add ((GtkFunction) stext_idle_timer_proc,
6087 gtk_stext_disable_blink (GtkSText *text)
6089 if (text->cursor_timer_on)
6091 if (text->cursor_idle_time_timer_id)
6093 gtk_idle_remove (text->cursor_idle_time_timer_id);
6094 text->cursor_idle_time_timer_id = 0;
6096 if (text->cursor_timer_id)
6098 gtk_timeout_remove (text->cursor_timer_id);
6099 text->cursor_timer_id = 0;
6101 draw_cursor(text, TRUE);
6106 gtk_stext_set_blink (GtkSText *text, gboolean blinking_on)
6108 if (text->cursor_timer_on != blinking_on)
6110 if (text->cursor_timer_on)
6112 /* text widget already created? */
6113 if (text->cursor_visible)
6115 gtk_stext_disable_blink (text);
6117 text->cursor_timer_on = FALSE;
6121 if (text->cursor_visible)
6123 gtk_stext_enable_blink (text);
6125 text->cursor_timer_on = TRUE;
6131 void gtk_stext_set_wrap_rmargin (GtkSText *text, gint rmargin)
6133 /* not particularly impressive, but it does the job. */
6134 /* TODO: currently only allowed to set this after a
6135 * gtk_stext_new() */
6136 text->wrap_rmargin = rmargin >= 0 ? rmargin : 0;
6139 /**********************************************************************/
6141 /**********************************************************************/
6143 #ifdef DEBUG_GTK_STEXT
6145 gtk_stext_show_cache_line (GtkSText *text, GList *cache,
6146 const char* what, const char* func, gint line)
6148 LineParams *lp = &CACHE_DATA(cache);
6151 if (cache == text->line_start_cache)
6152 g_message ("Line Start Cache: ");
6154 if (cache == text->current_line)
6155 g_message("Current Line: ");
6157 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
6165 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
6166 g_message ("%c", GTK_STEXT_INDEX (text, i));
6172 gtk_stext_show_cache (GtkSText *text, const char* func, gint line)
6174 GList *l = text->line_start_cache;
6180 /* back up to the absolute beginning of the line cache */
6184 g_message ("*** line cache ***\n");
6185 for (; l; l = l->next)
6186 gtk_stext_show_cache_line (text, l, "all", func, line);
6190 gtk_stext_assert_mark (GtkSText *text,
6191 GtkSPropertyMark *mark,
6192 GtkSPropertyMark *before,
6193 GtkSPropertyMark *after,
6198 GtkSPropertyMark correct_mark = find_mark (text, mark->index);
6200 if (mark->offset != correct_mark.offset ||
6201 mark->property != correct_mark.property)
6202 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
6206 gtk_stext_assert (GtkSText *text,
6210 GList* cache = text->line_start_cache;
6211 GtkSPropertyMark* before_mark = NULL;
6212 GtkSPropertyMark* after_mark = NULL;
6214 gtk_stext_show_props (text, msg, line);
6216 for (; cache->prev; cache = cache->prev)
6219 g_message ("*** line markers ***\n");
6221 for (; cache; cache = cache->next)
6223 after_mark = &CACHE_DATA(cache).end;
6224 gtk_stext_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
6225 before_mark = &CACHE_DATA(cache).start;
6228 after_mark = &CACHE_DATA(cache->next).start;
6232 gtk_stext_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
6233 before_mark = &CACHE_DATA(cache).end;
6238 gtk_stext_show_adj (GtkSText *text,
6244 g_message ("*** adjustment ***\n");
6246 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
6253 adj->step_increment,
6254 adj->page_increment,
6259 gtk_stext_show_props (GtkSText *text,
6263 GList* props = text->text_properties;
6266 g_message ("%s:%d: ", msg, line);
6268 for (; props; props = props->next)
6270 TextProperty *p = (TextProperty*)props->data;
6272 proplen += p->length;
6274 g_message ("[%d,%p,", p->length, p);
6275 if (p->flags & PROPERTY_FONT)
6276 g_message ("%p,", p->font);
6279 if (p->flags & PROPERTY_FOREGROUND)
6280 g_message ("%ld, ", p->fore_color.pixel);
6283 if (p->flags & PROPERTY_BACKGROUND)
6284 g_message ("%ld] ", p->back_color.pixel);
6291 if (proplen - 1 != TEXT_LENGTH(text))
6292 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);