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 #include "gdk/gdkkeysyms.h"
26 #include "gtkselection.h"
27 #include "gtksignal.h"
29 #include "line-wrap.xbm"
30 #include "line-arrow.xbm"
33 #define INITIAL_BUFFER_SIZE 1024
34 #define INITIAL_LINE_CACHE_SIZE 256
35 #define MIN_GAP_SIZE 256
36 #define LINE_DELIM '\n'
37 #define MIN_TEXT_WIDTH_LINES 20
38 #define MIN_TEXT_HEIGHT_LINES 10
39 #define TEXT_BORDER_ROOM 1
40 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
41 #define DEFAULT_TAB_STOP_WIDTH 4
42 #define SCROLL_PIXELS 5
43 #define KEY_SCROLL_PIXELS 10
44 #define SCROLL_TIME 100
45 #define FREEZE_LENGTH 1024
46 /* Freeze text when inserting or deleting more than this many characters */
48 #define SET_PROPERTY_MARK(m, p, o) do { \
49 (m)->property = (p); \
52 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
53 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
54 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
55 (mark)->property->prev->data \
57 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
58 #define MARK_LIST_PTR(mark) ((mark)->property)
59 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
60 #define MARK_OFFSET(mark) ((mark)->offset)
61 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
62 #define MARK_CURRENT_FONT(mark) (((TextProperty*)(mark)->property->data)->font->gdk_font)
63 #define MARK_CURRENT_FORE(mark) (&((TextProperty*)(mark)->property->data)->fore_color)
64 #define MARK_CURRENT_BACK(mark) (&((TextProperty*)(mark)->property->data)->back_color)
66 #define MARK_CURRENT_TEXT_FONT(m) (((TextProperty*)(m)->property->data)->font)
67 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
68 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
69 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
70 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
71 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
72 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
73 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
74 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
76 typedef struct _TextFont TextFont;
77 typedef struct _TextProperty TextProperty;
78 typedef struct _TabStopMark TabStopMark;
79 typedef struct _PrevTabCont PrevTabCont;
80 typedef struct _FetchLinesData FetchLinesData;
81 typedef struct _LineParams LineParams;
82 typedef struct _SetVerticalScrollData SetVerticalScrollData;
84 typedef gint (*LineIteratorFunction) (GtkText* text, LineParams* lp, void* data);
92 struct _SetVerticalScrollData {
101 /* The actual font. */
104 gint16 char_widths[256];
112 /* Background Color. */
115 /* Foreground Color. */
118 /* Length of this property. */
124 GList* tab_stops; /* Index into list containing the next tab position. If
125 * NULL, using default widths. */
132 TabStopMark tab_start;
135 struct _FetchLinesData
148 guint displayable_chars;
151 PrevTabCont tab_cont;
152 PrevTabCont tab_cont_next;
154 GtkPropertyMark start;
159 static void gtk_text_class_init (GtkTextClass *klass);
160 static void gtk_text_init (GtkText *text);
161 static void gtk_text_destroy (GtkObject *object);
162 static void gtk_text_finalize (GtkObject *object);
163 static void gtk_text_realize (GtkWidget *widget);
164 static void gtk_text_unrealize (GtkWidget *widget);
165 static void gtk_text_style_set (GtkWidget *widget,
166 GtkStyle *previous_style);
167 static void gtk_text_draw_focus (GtkWidget *widget);
168 static void gtk_text_size_request (GtkWidget *widget,
169 GtkRequisition *requisition);
170 static void gtk_text_size_allocate (GtkWidget *widget,
171 GtkAllocation *allocation);
172 static void gtk_text_adjustment (GtkAdjustment *adjustment,
174 static void gtk_text_disconnect (GtkAdjustment *adjustment,
177 static void gtk_text_insert_text (GtkEditable *editable,
178 const gchar *new_text,
179 gint new_text_length,
181 static void gtk_text_delete_text (GtkEditable *editable,
184 static void gtk_text_update_text (GtkEditable *editable,
187 static gchar *gtk_text_get_chars (GtkEditable *editable,
190 static void gtk_text_set_selection (GtkEditable *editable,
193 static void gtk_text_real_set_editable (GtkEditable *editable,
194 gboolean is_editable);
197 static void gtk_text_draw (GtkWidget *widget,
199 static gint gtk_text_expose (GtkWidget *widget,
200 GdkEventExpose *event);
201 static gint gtk_text_button_press (GtkWidget *widget,
202 GdkEventButton *event);
203 static gint gtk_text_button_release (GtkWidget *widget,
204 GdkEventButton *event);
205 static gint gtk_text_motion_notify (GtkWidget *widget,
206 GdkEventMotion *event);
207 static gint gtk_text_key_press (GtkWidget *widget,
209 static gint gtk_text_focus_in (GtkWidget *widget,
210 GdkEventFocus *event);
211 static gint gtk_text_focus_out (GtkWidget *widget,
212 GdkEventFocus *event);
214 static void move_gap_to_point (GtkText* text);
215 static void make_forward_space (GtkText* text, guint len);
216 static void insert_text_property (GtkText* text, GdkFont* font,
217 GdkColor *fore, GdkColor* back, guint len);
218 static void delete_text_property (GtkText* text, guint len);
219 static void init_properties (GtkText *text);
220 static guint pixel_height_of (GtkText* text, GList* cache_line);
222 /* Property Movement and Size Computations */
223 static void advance_mark (GtkPropertyMark* mark);
224 static void decrement_mark (GtkPropertyMark* mark);
225 static void advance_mark_n (GtkPropertyMark* mark, gint n);
226 static void decrement_mark_n (GtkPropertyMark* mark, gint n);
227 static void move_mark_n (GtkPropertyMark* mark, gint n);
228 static GtkPropertyMark find_mark (GtkText* text, guint mark_position);
229 static GtkPropertyMark find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near);
230 static void find_line_containing_point (GtkText* text, guint point,
232 static TextProperty* new_text_property (GdkFont* font, GdkColor* fore, GdkColor* back, guint length);
235 static void compute_lines_pixels (GtkText* text, guint char_count,
236 guint *lines, guint *pixels);
238 static gint total_line_height (GtkText* text,
241 static LineParams find_line_params (GtkText* text,
242 const GtkPropertyMark *mark,
243 const PrevTabCont *tab_cont,
244 PrevTabCont *next_cont);
245 static void recompute_geometry (GtkText* text);
246 static void insert_expose (GtkText* text, guint old_pixels, gint nchars, guint new_line_count);
247 static void delete_expose (GtkText* text,
251 static void clear_area (GtkText *text, GdkRectangle *area);
252 static void draw_line (GtkText* text,
255 static void draw_line_wrap (GtkText* text,
257 static void draw_cursor (GtkText* text, gint absolute);
258 static void undraw_cursor (GtkText* text, gint absolute);
259 static gint drawn_cursor_min (GtkText* text);
260 static gint drawn_cursor_max (GtkText* text);
261 static void expose_text (GtkText* text, GdkRectangle *area, gboolean cursor);
263 /* Search and Placement. */
264 static void find_cursor (GtkText* text,
266 static void find_cursor_at_line (GtkText* text,
267 const LineParams* start_line,
269 static void find_mouse_cursor (GtkText* text, gint x, gint y);
272 static void adjust_adj (GtkText* text, GtkAdjustment* adj);
273 static void scroll_up (GtkText* text, gint diff);
274 static void scroll_down (GtkText* text, gint diff);
275 static void scroll_int (GtkText* text, gint diff);
277 static void process_exposes (GtkText *text);
279 /* Cache Management. */
280 static void free_cache (GtkText* text);
281 static GList* remove_cache_line (GtkText* text, GList* list);
284 static void move_cursor_buffer_ver (GtkText *text, int dir);
285 static void move_cursor_page_ver (GtkText *text, int dir);
286 static void move_cursor_ver (GtkText *text, int count);
287 static void move_cursor_hor (GtkText *text, int count);
289 /* Binding actions */
290 static void gtk_text_move_cursor (GtkEditable *editable,
293 static void gtk_text_move_word (GtkEditable *editable,
295 static void gtk_text_move_page (GtkEditable *editable,
298 static void gtk_text_move_to_row (GtkEditable *editable,
300 static void gtk_text_move_to_column (GtkEditable *editable,
302 static void gtk_text_kill_char (GtkEditable *editable,
304 static void gtk_text_kill_word (GtkEditable *editable,
306 static void gtk_text_kill_line (GtkEditable *editable,
310 static void gtk_text_move_forward_character (GtkText *text);
311 static void gtk_text_move_backward_character (GtkText *text);
312 static void gtk_text_move_forward_word (GtkText *text);
313 static void gtk_text_move_backward_word (GtkText *text);
314 static void gtk_text_move_beginning_of_line (GtkText *text);
315 static void gtk_text_move_end_of_line (GtkText *text);
316 static void gtk_text_move_next_line (GtkText *text);
317 static void gtk_text_move_previous_line (GtkText *text);
319 static void gtk_text_delete_forward_character (GtkText *text);
320 static void gtk_text_delete_backward_character (GtkText *text);
321 static void gtk_text_delete_forward_word (GtkText *text);
322 static void gtk_text_delete_backward_word (GtkText *text);
323 static void gtk_text_delete_line (GtkText *text);
324 static void gtk_text_delete_to_line_end (GtkText *text);
325 static void gtk_text_select_word (GtkText *text,
327 static void gtk_text_select_line (GtkText *text,
330 static void gtk_text_set_position (GtkEditable *editable,
333 /* #define DEBUG_GTK_TEXT */
335 #if defined(DEBUG_GTK_TEXT) && defined(__GNUC__)
336 /* Debugging utilities. */
337 static void gtk_text_assert_mark (GtkText *text,
338 GtkPropertyMark *mark,
339 GtkPropertyMark *before,
340 GtkPropertyMark *after,
345 static void gtk_text_assert (GtkText *text,
348 static void gtk_text_show_cache_line (GtkText *text, GList *cache,
349 const char* what, const char* func, gint line);
350 static void gtk_text_show_cache (GtkText *text, const char* func, gint line);
351 static void gtk_text_show_adj (GtkText *text,
356 static void gtk_text_show_props (GtkText* test,
360 #define TDEBUG(args) g_print args
361 #define TEXT_ASSERT(text) gtk_text_assert (text,__PRETTY_FUNCTION__,__LINE__)
362 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_text_assert_mark (text,mark, \
363 __PRETTY_FUNCTION__,msg,__LINE__)
364 #define TEXT_SHOW(text) gtk_text_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
365 #define TEXT_SHOW_LINE(text,line,msg) gtk_text_show_cache_line (text,line,msg,\
366 __PRETTY_FUNCTION__,__LINE__)
367 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_text_show_adj (text,adj,msg, \
368 __PRETTY_FUNCTION__,__LINE__)
371 #define TEXT_ASSERT(text)
372 #define TEXT_ASSERT_MARK(text,mark,msg)
373 #define TEXT_SHOW(text)
374 #define TEXT_SHOW_LINE(text,line,msg)
375 #define TEXT_SHOW_ADJ(text,adj,msg)
378 /* Memory Management. */
379 static GMemChunk *params_mem_chunk = NULL;
380 static GMemChunk *text_property_chunk = NULL;
382 static GtkWidgetClass *parent_class = NULL;
385 static GtkTextFunction control_keys[26] =
387 (GtkTextFunction)gtk_text_move_beginning_of_line, /* a */
388 (GtkTextFunction)gtk_text_move_backward_character, /* b */
389 (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
390 (GtkTextFunction)gtk_text_delete_forward_character, /* d */
391 (GtkTextFunction)gtk_text_move_end_of_line, /* e */
392 (GtkTextFunction)gtk_text_move_forward_character, /* f */
394 (GtkTextFunction)gtk_text_delete_backward_character, /* h */
397 (GtkTextFunction)gtk_text_delete_to_line_end, /* k */
400 (GtkTextFunction)gtk_text_move_next_line, /* n */
402 (GtkTextFunction)gtk_text_move_previous_line, /* p */
407 (GtkTextFunction)gtk_text_delete_line, /* u */
408 (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
409 (GtkTextFunction)gtk_text_delete_backward_word, /* w */
410 (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
415 static GtkTextFunction alt_keys[26] =
418 (GtkTextFunction)gtk_text_move_backward_word, /* b */
420 (GtkTextFunction)gtk_text_delete_forward_word, /* d */
422 (GtkTextFunction)gtk_text_move_forward_word, /* f */
446 /**********************************************************************/
448 /**********************************************************************/
451 gtk_text_get_type (void)
453 static GtkType text_type = 0;
457 GtkTypeInfo text_info =
461 sizeof (GtkTextClass),
462 (GtkClassInitFunc) gtk_text_class_init,
463 (GtkObjectInitFunc) gtk_text_init,
464 /* reserved_1 */ NULL,
465 /* reserved_2 */ NULL,
466 (GtkClassInitFunc) NULL,
469 text_type = gtk_type_unique (GTK_TYPE_EDITABLE, &text_info);
476 gtk_text_class_init (GtkTextClass *class)
478 GtkObjectClass *object_class;
479 GtkWidgetClass *widget_class;
480 GtkEditableClass *editable_class;
482 object_class = (GtkObjectClass*) class;
483 widget_class = (GtkWidgetClass*) class;
484 editable_class = (GtkEditableClass*) class;
486 parent_class = gtk_type_class (GTK_TYPE_EDITABLE);
488 object_class->destroy = gtk_text_destroy;
489 object_class->finalize = gtk_text_finalize;
491 widget_class->realize = gtk_text_realize;
492 widget_class->unrealize = gtk_text_unrealize;
493 widget_class->style_set = gtk_text_style_set;
494 widget_class->draw_focus = gtk_text_draw_focus;
495 widget_class->size_request = gtk_text_size_request;
496 widget_class->size_allocate = gtk_text_size_allocate;
497 widget_class->draw = gtk_text_draw;
498 widget_class->expose_event = gtk_text_expose;
499 widget_class->button_press_event = gtk_text_button_press;
500 widget_class->button_release_event = gtk_text_button_release;
501 widget_class->motion_notify_event = gtk_text_motion_notify;
502 widget_class->key_press_event = gtk_text_key_press;
503 widget_class->focus_in_event = gtk_text_focus_in;
504 widget_class->focus_out_event = gtk_text_focus_out;
506 editable_class->set_editable = gtk_text_real_set_editable;
507 editable_class->insert_text = gtk_text_insert_text;
508 editable_class->delete_text = gtk_text_delete_text;
510 editable_class->move_cursor = gtk_text_move_cursor;
511 editable_class->move_word = gtk_text_move_word;
512 editable_class->move_page = gtk_text_move_page;
513 editable_class->move_to_row = gtk_text_move_to_row;
514 editable_class->move_to_column = gtk_text_move_to_column;
516 editable_class->kill_char = gtk_text_kill_char;
517 editable_class->kill_word = gtk_text_kill_word;
518 editable_class->kill_line = gtk_text_kill_line;
520 editable_class->update_text = gtk_text_update_text;
521 editable_class->get_chars = gtk_text_get_chars;
522 editable_class->set_selection = gtk_text_set_selection;
523 editable_class->set_position = gtk_text_set_position;
527 gtk_text_init (GtkText *text)
529 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
531 text->text = g_new (guchar, INITIAL_BUFFER_SIZE);
532 text->text_len = INITIAL_BUFFER_SIZE;
534 if (!params_mem_chunk)
535 params_mem_chunk = g_mem_chunk_new ("LineParams",
537 256 * sizeof (LineParams),
540 text->default_tab_width = 4;
541 text->tab_stops = NULL;
543 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
544 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
546 text->line_start_cache = NULL;
548 text->line_wrap = TRUE;
549 text->word_wrap = FALSE;
554 GTK_EDITABLE(text)->editable = FALSE;
558 gtk_text_new (GtkAdjustment *hadj,
563 text = gtk_type_new (GTK_TYPE_TEXT);
565 gtk_text_set_adjustments (text, hadj, vadj);
566 gtk_editable_set_position (GTK_EDITABLE (text), 0);
568 return GTK_WIDGET (text);
572 gtk_text_set_word_wrap (GtkText *text,
575 g_return_if_fail (text != NULL);
576 g_return_if_fail (GTK_IS_TEXT (text));
578 text->word_wrap = (word_wrap != FALSE);
580 if (GTK_WIDGET_REALIZED (text))
582 recompute_geometry (text);
583 gtk_widget_queue_draw (GTK_WIDGET (text));
588 gtk_text_set_editable (GtkText *text,
589 gboolean is_editable)
591 g_return_if_fail (text != NULL);
592 g_return_if_fail (GTK_IS_TEXT (text));
594 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
598 gtk_text_real_set_editable (GtkEditable *editable,
599 gboolean is_editable)
603 g_return_if_fail (editable != NULL);
604 g_return_if_fail (GTK_IS_TEXT (editable));
606 text = GTK_TEXT (editable);
608 editable->editable = (is_editable != FALSE);
611 draw_cursor (text, TRUE);
613 undraw_cursor (text, TRUE);
617 gtk_text_set_adjustments (GtkText *text,
621 g_return_if_fail (text != NULL);
622 g_return_if_fail (GTK_IS_TEXT (text));
624 if (text->hadj && (text->hadj != hadj))
626 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
627 gtk_object_unref (GTK_OBJECT (text->hadj));
630 if (text->vadj && (text->vadj != vadj))
632 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
633 gtk_object_unref (GTK_OBJECT (text->vadj));
637 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
640 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
642 if (text->hadj != hadj)
645 gtk_object_ref (GTK_OBJECT (text->hadj));
646 gtk_object_sink (GTK_OBJECT (text->hadj));
648 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
649 (GtkSignalFunc) gtk_text_adjustment,
651 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
652 (GtkSignalFunc) gtk_text_adjustment,
654 gtk_signal_connect (GTK_OBJECT (text->hadj), "disconnect",
655 (GtkSignalFunc) gtk_text_disconnect,
659 if (text->vadj != vadj)
662 gtk_object_ref (GTK_OBJECT (text->vadj));
663 gtk_object_sink (GTK_OBJECT (text->vadj));
665 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
666 (GtkSignalFunc) gtk_text_adjustment,
668 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
669 (GtkSignalFunc) gtk_text_adjustment,
671 gtk_signal_connect (GTK_OBJECT (text->vadj), "disconnect",
672 (GtkSignalFunc) gtk_text_disconnect,
678 gtk_text_set_point (GtkText *text,
681 g_return_if_fail (text != NULL);
682 g_return_if_fail (GTK_IS_TEXT (text));
683 g_return_if_fail (index <= TEXT_LENGTH (text));
685 text->point = find_mark (text, index);
689 gtk_text_get_point (GtkText *text)
691 g_return_val_if_fail (text != NULL, 0);
692 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
694 return text->point.index;
698 gtk_text_get_length (GtkText *text)
700 g_return_val_if_fail (text != NULL, 0);
701 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
703 return TEXT_LENGTH (text);
707 gtk_text_freeze (GtkText *text)
709 g_return_if_fail (text != NULL);
710 g_return_if_fail (GTK_IS_TEXT (text));
716 gtk_text_thaw (GtkText *text)
718 g_return_if_fail (text != NULL);
719 g_return_if_fail (GTK_IS_TEXT (text));
721 text->freeze = FALSE;
723 if (GTK_WIDGET_REALIZED (text))
725 recompute_geometry (text);
726 gtk_widget_queue_draw (GTK_WIDGET (text));
731 gtk_text_insert (GtkText *text,
738 GtkEditable *editable = GTK_EDITABLE (text);
739 gboolean frozen = FALSE;
741 gint new_line_count = 1;
742 guint old_height = 0;
746 g_return_if_fail (text != NULL);
747 g_return_if_fail (GTK_IS_TEXT (text));
749 /* This must be because we need to have the style set up. */
750 g_assert (GTK_WIDGET_REALIZED(text));
753 length = strlen (chars);
760 if (!text->freeze && (length > FREEZE_LENGTH))
762 gtk_text_freeze (text);
767 fore = >K_WIDGET (text)->style->text[GTK_STATE_NORMAL];
769 back = >K_WIDGET (text)->style->base[GTK_STATE_NORMAL];
771 if (!text->freeze && (text->line_start_cache != NULL))
773 find_line_containing_point (text, text->point.index, TRUE);
774 old_height = total_line_height (text, text->current_line, 1);
775 for (i=0; i<length; i++)
776 if (chars[i] == '\n')
780 if (text->point.index < text->first_line_start_index)
781 text->first_line_start_index += length;
783 if (text->point.index < editable->selection_start_pos)
784 editable->selection_start_pos += length;
785 if (text->point.index < editable->selection_end_pos)
786 editable->selection_end_pos += length;
787 /* We'll reset the cursor later anyways if we aren't frozen */
788 if (text->point.index < text->cursor_mark.index)
789 text->cursor_mark.index += length;
791 move_gap_to_point (text);
794 font = GTK_WIDGET (text)->style->font;
796 make_forward_space (text, length);
798 memcpy (text->text + text->gap_position, chars, length);
800 insert_text_property (text, font, fore, back, length);
802 text->gap_size -= length;
803 text->gap_position += length;
805 advance_mark_n (&text->point, length);
807 if (!text->freeze && (text->line_start_cache != NULL))
808 insert_expose (text, old_height, length, new_line_count);
811 gtk_text_thaw (text);
815 gtk_text_backward_delete (GtkText *text,
818 g_return_val_if_fail (text != NULL, 0);
819 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
821 if (nchars > text->point.index || nchars <= 0)
824 gtk_text_set_point (text, text->point.index - nchars);
826 return gtk_text_forward_delete (text, nchars);
830 gtk_text_forward_delete (GtkText *text,
833 guint old_lines, old_height;
834 GtkEditable *editable = GTK_EDITABLE (text);
835 gboolean frozen = FALSE;
837 g_return_val_if_fail (text != NULL, 0);
838 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
840 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
843 if (!text->freeze && (nchars > FREEZE_LENGTH))
845 gtk_text_freeze (text);
849 if (!text->freeze && (text->line_start_cache != NULL))
851 /* We need to undraw the cursor here, since we may later
852 * delete the cursor's property
854 undraw_cursor (text, FALSE);
855 find_line_containing_point (text, text->point.index, TRUE);
856 compute_lines_pixels (text, nchars, &old_lines, &old_height);
859 if (text->point.index < text->first_line_start_index)
861 if (text->point.index + nchars >= text->first_line_start_index)
863 text->first_line_start_index = text->point.index;
864 while ((text->first_line_start_index > 0) &&
865 (GTK_TEXT_INDEX (text, text->first_line_start_index - 1) != LINE_DELIM))
866 text->first_line_start_index -= 1;
870 text->first_line_start_index -= nchars;
873 if (text->point.index < editable->selection_start_pos)
874 editable->selection_start_pos -=
875 MIN(nchars, editable->selection_start_pos - text->point.index);
876 if (text->point.index < editable->selection_end_pos)
877 editable->selection_end_pos -=
878 MIN(nchars, editable->selection_end_pos - text->point.index);
879 /* We'll reset the cursor later anyways if we aren't frozen */
880 if (text->point.index < text->cursor_mark.index)
881 move_mark_n (&text->cursor_mark,
882 -MIN(nchars, text->cursor_mark.index - text->point.index));
884 move_gap_to_point (text);
886 text->gap_size += nchars;
888 delete_text_property (text, nchars);
890 if (!text->freeze && (text->line_start_cache != NULL))
892 delete_expose (text, nchars, old_lines, old_height);
893 draw_cursor (text, FALSE);
897 gtk_text_thaw (text);
903 gtk_text_set_position (GtkEditable *editable,
906 GtkText *text = (GtkText *) editable;
908 undraw_cursor (text, FALSE);
909 text->cursor_mark = find_mark (text, position);
910 find_cursor (text, TRUE);
911 draw_cursor (text, FALSE);
912 gtk_editable_select_region (editable, 0, 0);
916 gtk_text_get_chars (GtkEditable *editable,
926 g_return_val_if_fail (editable != NULL, NULL);
927 g_return_val_if_fail (GTK_IS_TEXT (editable), NULL);
928 text = GTK_TEXT (editable);
931 end_pos = TEXT_LENGTH (text);
933 if ((start_pos < 0) ||
934 (end_pos > TEXT_LENGTH (text)) ||
935 (end_pos < start_pos))
938 nchars = end_pos - start_pos;
940 retval = g_new (gchar, nchars+1);
943 if (start_pos < text->gap_position)
945 n = MIN (text->gap_position - start_pos, nchars);
946 memcpy (p, &text->text[start_pos], n);
952 if (start_pos+nchars >= text->gap_position)
955 text->text + MAX (text->gap_position + text->gap_size,
956 start_pos + text->gap_size),
968 gtk_text_destroy (GtkObject *object)
972 g_return_if_fail (object != NULL);
973 g_return_if_fail (GTK_IS_TEXT (object));
975 text = (GtkText *)object;
978 gtk_object_unref (GTK_OBJECT (text->hadj));
983 gtk_object_unref (GTK_OBJECT (text->vadj));
988 gtk_timeout_remove (text->timer);
992 GTK_OBJECT_CLASS(parent_class)->destroy (object);
996 gtk_text_finalize (GtkObject *object)
1001 g_return_if_fail (object != NULL);
1002 g_return_if_fail (GTK_IS_TEXT (object));
1004 text = (GtkText *)object;
1006 /* Clean up the internal structures */
1007 g_free (text->text);
1010 tmp_list = text->text_properties;
1013 g_mem_chunk_free (text_property_chunk, tmp_list->data);
1014 tmp_list = tmp_list->next;
1017 g_list_free (text->text_properties);
1019 if (text->scratch_buffer)
1020 g_free (text->scratch_buffer);
1022 g_list_free (text->tab_stops);
1024 GTK_OBJECT_CLASS(parent_class)->finalize (object);
1028 gtk_text_realize (GtkWidget *widget)
1031 GtkEditable *editable;
1032 GdkWindowAttr attributes;
1033 gint attributes_mask;
1035 g_return_if_fail (widget != NULL);
1036 g_return_if_fail (GTK_IS_TEXT (widget));
1038 text = GTK_TEXT (widget);
1039 editable = GTK_EDITABLE (widget);
1040 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1042 attributes.window_type = GDK_WINDOW_CHILD;
1043 attributes.x = widget->allocation.x;
1044 attributes.y = widget->allocation.y;
1045 attributes.width = widget->allocation.width;
1046 attributes.height = widget->allocation.height;
1047 attributes.wclass = GDK_INPUT_OUTPUT;
1048 attributes.visual = gtk_widget_get_visual (widget);
1049 attributes.colormap = gtk_widget_get_colormap (widget);
1050 attributes.event_mask = gtk_widget_get_events (widget);
1051 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1052 GDK_BUTTON_PRESS_MASK |
1053 GDK_BUTTON_RELEASE_MASK |
1054 GDK_BUTTON_MOTION_MASK |
1055 GDK_ENTER_NOTIFY_MASK |
1056 GDK_LEAVE_NOTIFY_MASK |
1057 GDK_KEY_PRESS_MASK);
1058 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1060 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1061 gdk_window_set_user_data (widget->window, text);
1063 attributes.x = (widget->style->klass->xthickness + TEXT_BORDER_ROOM);
1064 attributes.y = (widget->style->klass->ythickness + TEXT_BORDER_ROOM);
1065 attributes.width = widget->allocation.width - attributes.x * 2;
1066 attributes.height = widget->allocation.height - attributes.y * 2;
1068 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1069 gdk_window_set_user_data (text->text_area, text);
1071 widget->style = gtk_style_attach (widget->style, widget->window);
1073 /* Can't call gtk_style_set_background here because it's handled specially */
1074 gdk_window_set_background (widget->window, &widget->style->base[GTK_STATE_NORMAL]);
1075 gdk_window_set_background (text->text_area, &widget->style->base[GTK_STATE_NORMAL]);
1077 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1078 (gchar*) line_wrap_bits,
1082 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1083 (gchar*) line_arrow_bits,
1087 text->gc = gdk_gc_new (text->text_area);
1088 gdk_gc_set_exposures (text->gc, TRUE);
1089 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1092 if (gdk_im_ready ())
1099 GdkIMStyle supported_style = GdkIMPreeditNone | GdkIMPreeditNothing |
1100 GdkIMPreeditPosition |
1101 GdkIMStatusNone | GdkIMStatusNothing;
1103 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1104 supported_style &= ~GdkIMPreeditPosition;
1106 style = gdk_im_decide_style (supported_style);
1107 switch (style & GdkIMPreeditMask)
1109 case GdkIMPreeditPosition:
1110 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1112 g_warning ("over-the-spot style requires fontset");
1115 gdk_window_get_size (text->text_area, &width, &height);
1119 rect.height = height;
1123 editable->ic = gdk_ic_new (text->text_area, text->text_area,
1125 "spotLocation", &spot,
1127 "fontSet", GDK_FONT_XFONT (widget->style->font),
1131 editable->ic = gdk_ic_new (text->text_area, text->text_area,
1135 if (editable->ic == NULL)
1136 g_warning ("Can't create input context.");
1139 GdkColormap *colormap;
1141 mask = gdk_window_get_events (text->text_area);
1142 mask |= gdk_ic_get_events (editable->ic);
1143 gdk_window_set_events (text->text_area, mask);
1145 if ((colormap = gtk_widget_get_colormap (widget)) !=
1146 gtk_widget_get_default_colormap ())
1148 gdk_ic_set_attr (editable->ic, "preeditAttributes",
1149 "colorMap", GDK_COLORMAP_XCOLORMAP (colormap),
1156 init_properties (text);
1158 gdk_window_show (text->text_area);
1160 if (editable->selection_start_pos != editable->selection_end_pos)
1161 gtk_editable_claim_selection (editable, TRUE, GDK_CURRENT_TIME);
1163 if ((widget->allocation.width > 1) || (widget->allocation.height > 1))
1164 recompute_geometry (text);
1168 gtk_text_style_set (GtkWidget *widget,
1169 GtkStyle *previous_style)
1173 g_return_if_fail (widget != NULL);
1174 g_return_if_fail (GTK_IS_TEXT (widget));
1176 text = GTK_TEXT (widget);
1177 if (GTK_WIDGET_REALIZED (widget))
1179 gdk_window_set_background (widget->window, &widget->style->base[GTK_STATE_NORMAL]);
1180 gdk_window_set_background (text->text_area, &widget->style->base[GTK_STATE_NORMAL]);
1182 if ((widget->allocation.width > 1) || (widget->allocation.height > 1))
1183 recompute_geometry (text);
1186 if (GTK_WIDGET_DRAWABLE (widget))
1187 gdk_window_clear (widget->window);
1191 gtk_text_unrealize (GtkWidget *widget)
1195 g_return_if_fail (widget != NULL);
1196 g_return_if_fail (GTK_IS_TEXT (widget));
1198 text = GTK_TEXT (widget);
1200 gdk_window_set_user_data (text->text_area, NULL);
1201 gdk_window_destroy (text->text_area);
1202 text->text_area = NULL;
1204 gdk_gc_destroy (text->gc);
1207 gdk_pixmap_unref (text->line_wrap_bitmap);
1208 gdk_pixmap_unref (text->line_arrow_bitmap);
1210 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1211 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1215 clear_focus_area (GtkText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1217 GtkWidget *widget = GTK_WIDGET (text);
1219 gint ythick = TEXT_BORDER_ROOM + widget->style->klass->ythickness;
1220 gint xthick = TEXT_BORDER_ROOM + widget->style->klass->xthickness;
1226 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1228 yorig = - text->first_onscreen_ver_pixel + ythick;
1229 xorig = - text->first_onscreen_hor_pixel + xthick;
1237 for (y = area_y; y < area_y + area_height; )
1239 gint yoff = (y - yorig) % height;
1240 gint yw = MIN(height - yoff, (area_y + area_height) - y);
1242 for (x = area_x; x < area_x + area_width; )
1244 gint xoff = (x - xorig) % width;
1245 gint xw = MIN(width - xoff, (area_x + area_width) - x);
1247 gdk_draw_pixmap (widget->window,
1249 widget->style->bg_pixmap[GTK_STATE_NORMAL],
1266 gtk_text_draw_focus (GtkWidget *widget)
1272 g_return_if_fail (widget != NULL);
1273 g_return_if_fail (GTK_IS_TEXT (widget));
1275 text = GTK_TEXT (widget);
1277 if (GTK_WIDGET_DRAWABLE (widget))
1279 gint ythick = widget->style->klass->ythickness;
1280 gint xthick = widget->style->klass->xthickness;
1281 gint xextra = TEXT_BORDER_ROOM;
1282 gint yextra = TEXT_BORDER_ROOM;
1284 TDEBUG (("in gtk_text_draw_focus\n"));
1288 width = widget->allocation.width;
1289 height = widget->allocation.height;
1291 if (GTK_WIDGET_HAS_FOCUS (widget))
1300 gdk_draw_rectangle (widget->window,
1301 widget->style->fg_gc[GTK_STATE_NORMAL],
1303 widget->allocation.width - 1,
1304 widget->allocation.height - 1);
1307 gtk_draw_shadow (widget->style, widget->window,
1308 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1309 x, y, width, height);
1313 width -= 2 * xthick;
1314 height -= 2 * ythick;
1316 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1319 clear_focus_area (text, x, y, width, yextra);
1321 clear_focus_area (text, x, y + yextra,
1322 xextra, y + height - 2 * yextra);
1324 clear_focus_area (text, x + width - xextra, y + yextra,
1325 xextra, height - 2 * ythick);
1327 clear_focus_area (text, x, x + height - yextra, width, yextra);
1329 else if (!GTK_WIDGET_HAS_FOCUS (widget))
1331 gdk_draw_rectangle (widget->window,
1332 widget->style->base_gc[GTK_STATE_NORMAL], FALSE,
1340 TDEBUG (("in gtk_text_draw_focus (undrawable !!!)\n"));
1345 gtk_text_size_request (GtkWidget *widget,
1346 GtkRequisition *requisition)
1353 g_return_if_fail (widget != NULL);
1354 g_return_if_fail (GTK_IS_TEXT (widget));
1355 g_return_if_fail (requisition != NULL);
1357 xthickness = widget->style->klass->xthickness + TEXT_BORDER_ROOM;
1358 ythickness = widget->style->klass->ythickness + TEXT_BORDER_ROOM;
1360 char_height = MIN_TEXT_HEIGHT_LINES * (widget->style->font->ascent +
1361 widget->style->font->descent);
1363 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (widget->style->font,
1364 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1368 requisition->width = char_width + xthickness * 2;
1369 requisition->height = char_height + ythickness * 2;
1373 gtk_text_size_allocate (GtkWidget *widget,
1374 GtkAllocation *allocation)
1377 GtkEditable *editable;
1379 g_return_if_fail (widget != NULL);
1380 g_return_if_fail (GTK_IS_TEXT (widget));
1381 g_return_if_fail (allocation != NULL);
1383 text = GTK_TEXT (widget);
1384 editable = GTK_EDITABLE (widget);
1386 widget->allocation = *allocation;
1387 if (GTK_WIDGET_REALIZED (widget))
1389 gdk_window_move_resize (widget->window,
1390 allocation->x, allocation->y,
1391 allocation->width, allocation->height);
1393 gdk_window_move_resize (text->text_area,
1394 widget->style->klass->xthickness + TEXT_BORDER_ROOM,
1395 widget->style->klass->ythickness + TEXT_BORDER_ROOM,
1396 widget->allocation.width - (widget->style->klass->xthickness +
1397 TEXT_BORDER_ROOM) * 2,
1398 widget->allocation.height - (widget->style->klass->ythickness +
1399 TEXT_BORDER_ROOM) * 2);
1402 if (editable->ic && (gdk_ic_get_style (editable->ic) & GdkIMPreeditPosition))
1407 gdk_window_get_size (text->text_area, &width, &height);
1411 rect.height = height;
1412 gdk_ic_set_attr (editable->ic, "preeditAttributes", "area", &rect, NULL);
1416 recompute_geometry (text);
1421 gtk_text_draw (GtkWidget *widget,
1424 g_return_if_fail (widget != NULL);
1425 g_return_if_fail (GTK_IS_TEXT (widget));
1426 g_return_if_fail (area != NULL);
1428 if (GTK_WIDGET_DRAWABLE (widget))
1430 expose_text (GTK_TEXT (widget), area, TRUE);
1431 gtk_widget_draw_focus (widget);
1436 gtk_text_expose (GtkWidget *widget,
1437 GdkEventExpose *event)
1439 g_return_val_if_fail (widget != NULL, FALSE);
1440 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1441 g_return_val_if_fail (event != NULL, FALSE);
1443 if (event->window == GTK_TEXT (widget)->text_area)
1445 TDEBUG (("in gtk_text_expose (expose)\n"));
1446 expose_text (GTK_TEXT (widget), &event->area, TRUE);
1448 else if (event->count == 0)
1450 TDEBUG (("in gtk_text_expose (focus)\n"));
1451 gtk_widget_draw_focus (widget);
1458 gtk_text_scroll_timeout (gpointer data)
1461 GdkEventMotion event;
1464 GdkModifierType mask;
1466 g_return_val_if_fail (GTK_IS_TEXT (data), FALSE);
1468 text = GTK_TEXT (data);
1471 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1473 if (!(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1481 gtk_text_motion_notify (GTK_WIDGET (text), &event);
1487 gtk_text_button_press (GtkWidget *widget,
1488 GdkEventButton *event)
1491 GtkEditable *editable;
1492 static GdkAtom ctext_atom = GDK_NONE;
1494 g_return_val_if_fail (widget != NULL, FALSE);
1495 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1496 g_return_val_if_fail (event != NULL, FALSE);
1498 if (ctext_atom == GDK_NONE)
1499 ctext_atom = gdk_atom_intern ("COMPOUND_TEXT", FALSE);
1501 text = GTK_TEXT (widget);
1502 editable = GTK_EDITABLE (widget);
1504 if (text->button && (event->button != text->button))
1507 text->button = event->button;
1509 if (!GTK_WIDGET_HAS_FOCUS (widget))
1510 gtk_widget_grab_focus (widget);
1512 if (event->button == 1)
1514 switch (event->type)
1516 case GDK_BUTTON_PRESS:
1517 gtk_grab_add (widget);
1519 undraw_cursor (text, FALSE);
1520 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1521 draw_cursor (text, FALSE);
1523 /* Set it now, so we display things right. We'll unset it
1524 * later if things don't work out */
1525 editable->has_selection = TRUE;
1526 gtk_text_set_selection (GTK_EDITABLE(text),
1527 text->cursor_mark.index,
1528 text->cursor_mark.index);
1532 case GDK_2BUTTON_PRESS:
1533 gtk_text_select_word (text, event->time);
1536 case GDK_3BUTTON_PRESS:
1537 gtk_text_select_line (text, event->time);
1544 else if (event->type == GDK_BUTTON_PRESS)
1546 if ((event->button == 2) && editable->editable)
1548 if (editable->selection_start_pos == editable->selection_end_pos ||
1549 editable->has_selection)
1551 undraw_cursor (text, FALSE);
1552 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1553 draw_cursor (text, FALSE);
1557 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1558 ctext_atom, event->time);
1562 gtk_grab_add (widget);
1564 undraw_cursor (text, FALSE);
1565 find_mouse_cursor (text, event->x, event->y);
1566 draw_cursor (text, FALSE);
1568 gtk_text_set_selection (GTK_EDITABLE(text),
1569 text->cursor_mark.index,
1570 text->cursor_mark.index);
1572 editable->has_selection = FALSE;
1573 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1574 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1582 gtk_text_button_release (GtkWidget *widget,
1583 GdkEventButton *event)
1586 GtkEditable *editable;
1587 g_return_val_if_fail (widget != NULL, FALSE);
1588 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1589 g_return_val_if_fail (event != NULL, FALSE);
1591 text = GTK_TEXT (widget);
1593 gtk_grab_remove (widget);
1595 if (text->button != event->button)
1602 gtk_timeout_remove (text->timer);
1606 if (event->button == 1)
1608 text = GTK_TEXT (widget);
1609 editable = GTK_EDITABLE (widget);
1611 gtk_grab_remove (widget);
1613 editable->has_selection = FALSE;
1614 if (editable->selection_start_pos != editable->selection_end_pos)
1616 if (gtk_selection_owner_set (widget,
1617 GDK_SELECTION_PRIMARY,
1619 editable->has_selection = TRUE;
1621 gtk_text_update_text (editable, editable->selection_start_pos,
1622 editable->selection_end_pos);
1626 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1627 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1630 else if (event->button == 3)
1632 gtk_grab_remove (widget);
1635 undraw_cursor (text, FALSE);
1636 find_cursor (text, TRUE);
1637 draw_cursor (text, FALSE);
1643 gtk_text_motion_notify (GtkWidget *widget,
1644 GdkEventMotion *event)
1649 GdkModifierType mask;
1651 g_return_val_if_fail (widget != NULL, FALSE);
1652 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1653 g_return_val_if_fail (event != NULL, FALSE);
1655 text = GTK_TEXT (widget);
1659 mask = event->state;
1660 if (event->is_hint || (text->text_area != event->window))
1662 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1665 if ((text->button == 0) ||
1666 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1669 gdk_window_get_size (text->text_area, NULL, &height);
1671 if ((y < 0) || (y > height))
1673 if (text->timer == 0)
1675 text->timer = gtk_timeout_add (SCROLL_TIME,
1676 gtk_text_scroll_timeout,
1680 scroll_int (text, y/2);
1682 scroll_int (text, (y - height)/2);
1688 undraw_cursor (GTK_TEXT (widget), FALSE);
1689 find_mouse_cursor (GTK_TEXT (widget), x, y);
1690 draw_cursor (GTK_TEXT (widget), FALSE);
1692 gtk_text_set_selection (GTK_EDITABLE(text),
1693 GTK_EDITABLE(text)->selection_start_pos,
1694 text->cursor_mark.index);
1700 gtk_text_insert_text (GtkEditable *editable,
1701 const gchar *new_text,
1702 gint new_text_length,
1705 GtkText *text = GTK_TEXT (editable);
1707 gtk_text_set_point (text, *position);
1708 gtk_text_insert (text,
1709 MARK_CURRENT_FONT (&text->point),
1710 MARK_CURRENT_FORE (&text->point),
1711 MARK_CURRENT_BACK (&text->point),
1712 new_text, new_text_length);
1714 *position = text->point.index;
1718 gtk_text_delete_text (GtkEditable *editable,
1724 g_return_if_fail (start_pos >= 0);
1726 text = GTK_TEXT (editable);
1728 gtk_text_set_point (text, start_pos);
1730 end_pos = TEXT_LENGTH (text);
1732 if (end_pos > start_pos)
1733 gtk_text_forward_delete (text, end_pos - start_pos);
1737 gtk_text_key_press (GtkWidget *widget,
1741 GtkEditable *editable;
1746 g_return_val_if_fail (widget != NULL, FALSE);
1747 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1748 g_return_val_if_fail (event != NULL, FALSE);
1752 text = GTK_TEXT (widget);
1753 editable = GTK_EDITABLE (widget);
1755 key = event->keyval;
1758 if ((GTK_EDITABLE(text)->editable == FALSE))
1760 switch (event->keyval)
1763 if (event->state & GDK_CONTROL_MASK)
1764 scroll_int (text, -text->vadj->value);
1767 if (event->state & GDK_CONTROL_MASK)
1768 scroll_int (text, +text->vadj->upper);
1770 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
1771 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
1772 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
1773 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
1775 if (event->state & GDK_CONTROL_MASK)
1776 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
1785 gint extend_selection;
1787 guint initial_pos = editable->current_pos;
1789 text->point = find_mark (text, text->cursor_mark.index);
1791 extend_selection = event->state & GDK_SHIFT_MASK;
1792 extend_start = FALSE;
1794 if (extend_selection)
1796 editable->has_selection = TRUE;
1798 if (editable->selection_start_pos == editable->selection_end_pos)
1800 editable->selection_start_pos = text->point.index;
1801 editable->selection_end_pos = text->point.index;
1804 extend_start = (text->point.index == editable->selection_start_pos);
1807 switch (event->keyval)
1810 if (event->state & GDK_CONTROL_MASK)
1811 move_cursor_buffer_ver (text, -1);
1813 gtk_text_move_beginning_of_line (text);
1816 if (event->state & GDK_CONTROL_MASK)
1817 move_cursor_buffer_ver (text, +1);
1819 gtk_text_move_end_of_line (text);
1821 case GDK_Page_Up: move_cursor_page_ver (text, -1); break;
1822 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
1823 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
1824 case GDK_Up: move_cursor_ver (text, -1); break;
1825 case GDK_Down: move_cursor_ver (text, +1); break;
1827 if (event->state & GDK_CONTROL_MASK)
1828 gtk_text_move_backward_word (text);
1830 move_cursor_hor (text, -1);
1833 if (event->state & GDK_CONTROL_MASK)
1834 gtk_text_move_forward_word (text);
1836 move_cursor_hor (text, +1);
1840 if (event->state & GDK_CONTROL_MASK)
1841 gtk_text_delete_backward_word (text);
1843 gtk_text_delete_backward_character (text);
1846 gtk_text_delete_line (text);
1849 if (event->state & GDK_SHIFT_MASK)
1851 extend_selection = FALSE;
1852 gtk_editable_paste_clipboard (editable);
1854 else if (event->state & GDK_CONTROL_MASK)
1856 gtk_editable_copy_clipboard (editable);
1860 /* gtk_toggle_insert(text) -- IMPLEMENT */
1864 if (event->state & GDK_CONTROL_MASK)
1865 gtk_text_delete_forward_word (text);
1866 else if (event->state & GDK_SHIFT_MASK)
1868 extend_selection = FALSE;
1869 gtk_editable_cut_clipboard (editable);
1872 gtk_text_delete_forward_character (text);
1875 position = text->point.index;
1876 gtk_editable_insert_text (editable, "\t", 1, &position);
1879 if (event->state & GDK_CONTROL_MASK)
1880 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
1883 position = text->point.index;
1884 gtk_editable_insert_text (editable, "\n", 1, &position);
1888 /* Don't insert literally */
1895 if (event->state & GDK_CONTROL_MASK)
1897 if ((key >= 'A') && (key <= 'Z'))
1900 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
1902 (* control_keys[(int) (key - 'a')]) (editable, event->time);
1908 else if (event->state & GDK_MOD1_MASK)
1910 if ((key >= 'A') && (key <= 'Z'))
1913 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
1915 (* alt_keys[(int) (key - 'a')]) (editable, event->time);
1921 else if (event->length > 0)
1923 extend_selection = FALSE;
1925 if (event->length == 1)
1927 gtk_editable_delete_selection (editable);
1928 position = text->point.index;
1929 gtk_editable_insert_text (editable, &(event->string[0]), 1, &position);
1938 if (return_val && (editable->current_pos != initial_pos))
1940 if (extend_selection)
1942 if (editable->current_pos < editable->selection_start_pos)
1943 gtk_text_set_selection (editable, editable->current_pos,
1944 editable->selection_end_pos);
1945 else if (editable->current_pos > editable->selection_end_pos)
1946 gtk_text_set_selection (editable, editable->selection_start_pos,
1947 editable->current_pos);
1951 gtk_text_set_selection (editable, editable->current_pos,
1952 editable->selection_end_pos);
1954 gtk_text_set_selection (editable, editable->selection_start_pos,
1955 editable->current_pos);
1959 gtk_text_set_selection (editable, 0, 0);
1961 gtk_editable_claim_selection (editable,
1962 editable->selection_start_pos != editable->selection_end_pos,
1971 gtk_text_focus_in (GtkWidget *widget,
1972 GdkEventFocus *event)
1974 g_return_val_if_fail (widget != NULL, FALSE);
1975 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1976 g_return_val_if_fail (event != NULL, FALSE);
1978 TDEBUG (("in gtk_text_focus_in\n"));
1980 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
1981 gtk_widget_draw_focus (widget);
1984 if (GTK_EDITABLE(widget)->ic)
1985 gdk_im_begin (GTK_EDITABLE(widget)->ic, GTK_TEXT(widget)->text_area);
1988 draw_cursor (GTK_TEXT(widget), TRUE);
1994 gtk_text_focus_out (GtkWidget *widget,
1995 GdkEventFocus *event)
1997 g_return_val_if_fail (widget != NULL, FALSE);
1998 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1999 g_return_val_if_fail (event != NULL, FALSE);
2001 TDEBUG (("in gtk_text_focus_out\n"));
2003 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
2004 gtk_widget_draw_focus (widget);
2006 undraw_cursor (GTK_TEXT(widget), TRUE);
2016 gtk_text_adjustment (GtkAdjustment *adjustment,
2019 g_return_if_fail (adjustment != NULL);
2020 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2021 g_return_if_fail (text != NULL);
2022 g_return_if_fail (GTK_IS_TEXT (text));
2024 /* Just ignore it if we haven't been size-allocated yet, or
2025 * if something weird has happened */
2026 if ((text->line_start_cache == NULL) ||
2027 (GTK_WIDGET (text)->allocation.height <= 1) ||
2028 (GTK_WIDGET (text)->allocation.width <= 1))
2031 if (adjustment == text->hadj)
2033 g_warning ("horizontal scrolling not implemented");
2037 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2041 undraw_cursor (text, FALSE);
2044 scroll_down (text, diff);
2045 else /* if (diff < 0) */
2046 scroll_up (text, diff);
2048 draw_cursor (text, FALSE);
2050 text->last_ver_value = adjustment->value;
2056 gtk_text_disconnect (GtkAdjustment *adjustment,
2059 g_return_if_fail (adjustment != NULL);
2060 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2061 g_return_if_fail (text != NULL);
2062 g_return_if_fail (GTK_IS_TEXT (text));
2064 if (adjustment == text->hadj)
2066 if (adjustment == text->vadj)
2071 static GtkPropertyMark
2072 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2074 GtkPropertyMark mark;
2076 mark = find_mark_near (text, point_position, near);
2078 while (mark.index > 0 &&
2079 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2080 decrement_mark (&mark);
2086 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2088 tab_cont->pixel_offset = 0;
2089 tab_cont->tab_start.tab_stops = text->tab_stops;
2090 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2092 if (!tab_cont->tab_start.to_next_tab)
2093 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2097 line_params_iterate (GtkText* text,
2098 const GtkPropertyMark* mark0,
2099 const PrevTabCont* tab_mark0,
2102 LineIteratorFunction iter)
2103 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2104 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2105 * for each line following MARK, iteration continues unless ITER_CALL
2108 GtkPropertyMark mark = *mark0;
2109 PrevTabCont tab_conts[2];
2110 LineParams *lp, lpbuf;
2111 gint tab_cont_index = 0;
2114 tab_conts[0] = *tab_mark0;
2116 init_tab_cont (text, tab_conts);
2121 lp = g_chunk_new (LineParams, params_mem_chunk);
2125 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2126 tab_conts + (tab_cont_index + 1) % 2);
2128 if ((*iter) (text, lp, data))
2131 if (LAST_INDEX (text, lp->end))
2135 advance_mark (&mark);
2136 tab_cont_index = (tab_cont_index + 1) % 2;
2141 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2143 FetchLinesData *fldata = (FetchLinesData*) data;
2145 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2147 switch (fldata->fl_type)
2149 case FetchLinesCount:
2150 if (!text->line_wrap || !lp->wraps)
2153 if (fldata->data >= fldata->data_max)
2157 case FetchLinesPixels:
2159 fldata->data += LINE_HEIGHT(*lp);
2161 if (fldata->data >= fldata->data_max)
2171 fetch_lines (GtkText* text,
2172 const GtkPropertyMark* mark0,
2173 const PrevTabCont* tab_cont0,
2177 FetchLinesData fl_data;
2179 fl_data.new_lines = NULL;
2181 fl_data.data_max = data;
2182 fl_data.fl_type = fl_type;
2184 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2186 return g_list_reverse (fl_data.new_lines);
2190 fetch_lines_backward (GtkText* text)
2192 GList* new_lines = NULL, *new_line_start;
2193 GtkPropertyMark mark;
2195 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2198 mark = find_this_line_start_mark (text,
2199 CACHE_DATA(text->line_start_cache).start.index - 1,
2200 &CACHE_DATA(text->line_start_cache).start);
2202 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2204 while (new_line_start->next)
2205 new_line_start = new_line_start->next;
2207 new_line_start->next = text->line_start_cache;
2208 text->line_start_cache->prev = new_line_start;
2212 fetch_lines_forward (GtkText* text, gint line_count)
2214 GtkPropertyMark mark;
2215 GList* line = text->line_start_cache;
2220 mark = CACHE_DATA(line).end;
2222 if (LAST_INDEX (text, mark))
2225 advance_mark(&mark);
2227 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2230 line->next->prev = line;
2233 /* Compute the number of lines, and vertical pixels for n characters
2234 * starting from the point
2237 compute_lines_pixels (GtkText* text, guint char_count,
2238 guint *lines, guint *pixels)
2240 GList *line = text->current_line;
2241 gint chars_left = char_count;
2246 /* If chars_left == 0, that means we're joining two lines in a
2247 * deletion, so add in the values for the next line as well
2249 for (; line && chars_left >= 0; line = line->next)
2251 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2253 if (line == text->current_line)
2254 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2256 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2258 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2262 chars_left = 0; /* force another loop */
2265 fetch_lines_forward (text, 1);
2270 total_line_height (GtkText* text, GList* line, gint line_count)
2274 for (; line && line_count > 0; line = line->next)
2276 height += LINE_HEIGHT(CACHE_DATA(line));
2278 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2282 fetch_lines_forward (text, line_count);
2289 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2291 if (old == text->line_start_cache)
2295 for (; old_line_count > 0; old_line_count -= 1)
2297 while (text->line_start_cache &&
2299 CACHE_DATA(text->line_start_cache).wraps)
2300 remove_cache_line(text, text->line_start_cache);
2302 remove_cache_line(text, text->line_start_cache);
2305 last = g_list_last (new);
2307 last->next = text->line_start_cache;
2309 if (text->line_start_cache)
2310 text->line_start_cache->prev = last;
2312 text->line_start_cache = new;
2318 g_assert (old->prev);
2322 for (; old_line_count > 0; old_line_count -= 1)
2324 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2325 old = remove_cache_line (text, old);
2327 old = remove_cache_line (text, old);
2333 last = g_list_last (new);
2343 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2345 GList* cache = text->current_line;
2348 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2351 for (; cache; cache = cache->next)
2353 GtkPropertyMark *start = &CACHE_DATA(cache).start;
2354 GtkPropertyMark *end = &CACHE_DATA(cache).end;
2356 start->index -= nchars;
2357 end->index -= nchars;
2359 if (LAST_INDEX (text, text->point) &&
2360 start->index == text->point.index)
2361 *start = text->point;
2362 else if (start->property == text->point.property)
2363 start->offset = start->index - (text->point.index - text->point.offset);
2365 if (LAST_INDEX (text, text->point) &&
2366 end->index == text->point.index)
2368 if (end->property == text->point.property)
2369 end->offset = end->index - (text->point.index - text->point.offset);
2371 /*TEXT_ASSERT_MARK(text, start, "start");*/
2372 /*TEXT_ASSERT_MARK(text, end, "end");*/
2377 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2379 GtkWidget *widget = GTK_WIDGET (text);
2382 guint new_pixels = 0;
2384 GList* new_line = NULL;
2387 text->cursor_virtual_x = 0;
2389 correct_cache_delete (text, nchars, old_lines);
2391 pixel_height = pixel_height_of(text, text->current_line) -
2392 LINE_HEIGHT(CACHE_DATA(text->current_line));
2394 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2395 CACHE_DATA(text->current_line).start = text->point;
2397 new_line = fetch_lines (text,
2398 &CACHE_DATA(text->current_line).start,
2399 &CACHE_DATA(text->current_line).tab_cont,
2403 swap_lines (text, text->current_line, new_line, old_lines);
2405 text->current_line = new_line;
2407 new_pixels = total_line_height (text, new_line, 1);
2409 gdk_window_get_size (text->text_area, &width, &height);
2411 if (old_pixels != new_pixels)
2413 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2415 gdk_draw_pixmap (text->text_area,
2419 pixel_height + old_pixels,
2421 pixel_height + new_pixels,
2425 text->vadj->upper += new_pixels;
2426 text->vadj->upper -= old_pixels;
2427 adjust_adj (text, text->vadj);
2431 rect.y = pixel_height;
2433 rect.height = new_pixels;
2435 expose_text (text, &rect, FALSE);
2436 gtk_text_draw_focus ( (GtkWidget *) text);
2438 text->cursor_mark = text->point;
2440 find_cursor (text, TRUE);
2442 if (old_pixels != new_pixels)
2444 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2447 rect.y = pixel_height + new_pixels;
2449 rect.height = height - rect.y;
2451 expose_text (text, &rect, FALSE);
2454 process_exposes (text);
2461 /* note, the point has already been moved forward */
2463 correct_cache_insert (GtkText* text, gint nchars)
2466 GtkPropertyMark *start;
2467 GtkPropertyMark *end;
2468 gboolean was_split = FALSE;
2470 /* We need to distinguish whether the property was split in the
2471 * insert or not, so we check if the point (which points after
2472 * the insertion here), points to the same character as the
2473 * point before. Ugh.
2477 GtkPropertyMark tmp_mark = text->point;
2478 move_mark_n (&tmp_mark, -1);
2480 if (tmp_mark.property != text->point.property)
2484 /* If we inserted a property exactly at the beginning of the
2485 * line, we have to correct here, or fetch_lines will
2488 * This also handles the case when we split exactly
2489 * at the beginning and start->offset is now invalid
2492 start = &CACHE_DATA(text->current_line).start;
2495 /* If we split exactly at the beginning... */
2496 if (start->offset == MARK_CURRENT_PROPERTY (start)->length)
2497 SET_PROPERTY_MARK (start, start->property->next, 0);
2498 /* If we inserted a property at the beginning of the text... */
2499 else if ((start->property == text->point.property) &&
2500 (start->index == text->point.index - nchars))
2501 SET_PROPERTY_MARK (start, start->property->prev, 0);
2504 /* Now correct the offsets, and check for start or end marks that
2505 * are after the point, yet point to a property before the point's
2506 * property. This indicates that they are meant to point to the
2507 * second half of a property we split in insert_text_property(), so
2508 * we fix them up that way.
2510 cache = text->current_line->next;
2512 for (; cache; cache = cache->next)
2514 start = &CACHE_DATA(cache).start;
2515 end = &CACHE_DATA(cache).end;
2517 if (LAST_INDEX (text, text->point) &&
2518 start->index == text->point.index)
2519 *start = text->point;
2520 else if (start->index >= text->point.index - nchars)
2522 if (!was_split && start->property == text->point.property)
2523 move_mark_n(start, nchars);
2526 if (start->property->next &&
2527 (start->property->next->next == text->point.property))
2529 g_assert (start->offset >= MARK_CURRENT_PROPERTY (start)->length);
2530 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2531 start->property = text->point.property;
2533 start->index += nchars;
2537 if (LAST_INDEX (text, text->point) &&
2538 end->index == text->point.index)
2540 if (end->index >= text->point.index - nchars)
2542 if (!was_split && end->property == text->point.property)
2543 move_mark_n(end, nchars);
2546 if (end->property->next &&
2547 (end->property->next->next == text->point.property))
2549 g_assert (end->offset >= MARK_CURRENT_PROPERTY (end)->length);
2550 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2551 end->property = text->point.property;
2553 end->index += nchars;
2557 /*TEXT_ASSERT_MARK(text, start, "start");*/
2558 /*TEXT_ASSERT_MARK(text, end, "end");*/
2564 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2565 guint new_line_count)
2567 GtkWidget *widget = GTK_WIDGET (text);
2570 guint new_pixels = 0;
2572 GList* new_lines = NULL;
2575 text->cursor_virtual_x = 0;
2577 undraw_cursor (text, FALSE);
2579 correct_cache_insert (text, nchars);
2581 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2583 pixel_height = pixel_height_of(text, text->current_line) -
2584 LINE_HEIGHT(CACHE_DATA(text->current_line));
2586 new_lines = fetch_lines (text,
2587 &CACHE_DATA(text->current_line).start,
2588 &CACHE_DATA(text->current_line).tab_cont,
2592 swap_lines (text, text->current_line, new_lines, 1);
2594 text->current_line = new_lines;
2596 new_pixels = total_line_height (text, new_lines, new_line_count);
2598 gdk_window_get_size (text->text_area, &width, &height);
2600 if (old_pixels != new_pixels)
2602 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2604 gdk_draw_pixmap (text->text_area,
2608 pixel_height + old_pixels,
2610 pixel_height + new_pixels,
2612 height + (old_pixels - new_pixels) - pixel_height);
2615 text->vadj->upper += new_pixels;
2616 text->vadj->upper -= old_pixels;
2617 adjust_adj (text, text->vadj);
2621 rect.y = pixel_height;
2623 rect.height = new_pixels;
2625 expose_text (text, &rect, FALSE);
2626 gtk_text_draw_focus ( (GtkWidget *) text);
2628 text->cursor_mark = text->point;
2630 find_cursor (text, TRUE);
2632 draw_cursor (text, FALSE);
2634 if (old_pixels != new_pixels)
2636 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2639 rect.y = pixel_height + new_pixels;
2641 rect.height = height - rect.y;
2643 expose_text (text, &rect, FALSE);
2646 process_exposes (text);
2649 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2655 font_hash (gconstpointer font)
2657 return gdk_font_id ((const GdkFont*) font);
2661 get_text_font (GdkFont* gfont)
2663 static GHashTable *font_cache_table = NULL;
2668 if (!font_cache_table)
2669 font_cache_table = g_hash_table_new (font_hash, (GCompareFunc) gdk_font_equal);
2671 lu = g_hash_table_lookup (font_cache_table, gfont);
2674 return (TextFont*)lu;
2676 tf = g_new (TextFont, 1);
2678 tf->gdk_font = gfont;
2680 for(i = 0; i < 256; i += 1)
2681 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2683 g_hash_table_insert (font_cache_table, gfont, tf);
2689 text_properties_equal (TextProperty* prop, GdkFont* font, GdkColor *fore, GdkColor *back)
2691 return prop->font == get_text_font(font) &&
2692 gdk_color_equal(&prop->fore_color, fore) &&
2693 gdk_color_equal(&prop->back_color, back);
2696 static TextProperty*
2697 new_text_property (GdkFont* font, GdkColor* fore, GdkColor* back, guint length)
2701 if (text_property_chunk == NULL)
2703 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
2704 sizeof(TextProperty),
2705 1024*sizeof(TextProperty),
2709 prop = g_chunk_new(TextProperty, text_property_chunk);
2711 prop->font = get_text_font (font);
2712 prop->fore_color = *fore;
2714 prop->back_color = *back;
2715 prop->length = length;
2720 /* Flop the memory between the point and the gap around like a
2723 move_gap_to_point (GtkText* text)
2725 if (text->gap_position < text->point.index)
2727 gint diff = text->point.index - text->gap_position;
2729 g_memmove (text->text + text->gap_position,
2730 text->text + text->gap_position + text->gap_size,
2733 text->gap_position = text->point.index;
2735 else if (text->gap_position > text->point.index)
2737 gint diff = text->gap_position - text->point.index;
2739 g_memmove (text->text + text->point.index + text->gap_size,
2740 text->text + text->point.index,
2743 text->gap_position = text->point.index;
2747 /* Increase the gap size. */
2749 make_forward_space (GtkText* text, guint len)
2751 if (text->gap_size < len)
2753 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
2755 if (sum >= text->text_len)
2759 while (i <= sum) i <<= 1;
2761 text->text = (guchar*)g_realloc(text->text, i);
2764 g_memmove (text->text + text->gap_position + text->gap_size + 2*len,
2765 text->text + text->gap_position + text->gap_size,
2766 text->text_end - (text->gap_position + text->gap_size));
2768 text->text_end += len*2;
2769 text->gap_size += len*2;
2773 /* Inserts into the text property list a list element that guarantees
2774 * that for len characters following the point, text has the correct
2775 * property. does not move point. adjusts text_properties_point and
2776 * text_properties_point_offset relative to the current value of
2779 insert_text_property (GtkText* text, GdkFont* font,
2780 GdkColor *fore, GdkColor* back, guint len)
2782 GtkPropertyMark *mark = &text->point;
2783 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
2784 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
2786 if (MARK_OFFSET(mark) == 0)
2788 /* Point is on the boundary of two properties.
2789 * If it is the same as either, grow, else insert
2792 if (text_properties_equal(forward_prop, font, fore, back))
2794 /* Grow the property in front of us. */
2796 MARK_PROPERTY_LENGTH(mark) += len;
2798 else if (backward_prop &&
2799 text_properties_equal(backward_prop, font, fore, back))
2801 /* Grow property behind us, point property and offset
2804 SET_PROPERTY_MARK (&text->point,
2805 MARK_PREV_LIST_PTR (mark),
2806 backward_prop->length);
2808 backward_prop->length += len;
2810 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
2811 (forward_prop->length == 1))
2813 /* Next property just has last position, take it over */
2814 forward_prop->font = get_text_font (font);
2815 forward_prop->fore_color = *fore;
2816 forward_prop->back_color = *back;
2817 forward_prop->length += len;
2821 /* Splice a new property into the list. */
2823 GList* new_prop = g_list_alloc();
2825 new_prop->next = MARK_LIST_PTR(mark);
2826 new_prop->prev = MARK_PREV_LIST_PTR(mark);
2827 new_prop->next->prev = new_prop;
2830 new_prop->prev->next = new_prop;
2832 new_prop->data = new_text_property (font, fore, back, len);
2834 SET_PROPERTY_MARK (mark, new_prop, 0);
2839 /* The following will screw up the line_start cache,
2840 * we'll fix it up in correct_cache_insert
2843 /* In the middle of forward_prop, if properties are equal,
2844 * just add to its length, else split it into two and splice
2846 if (text_properties_equal (forward_prop, font, fore, back))
2848 forward_prop->length += len;
2850 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
2851 (MARK_OFFSET(mark) + 1 == forward_prop->length))
2853 /* Inserting before only the last position in the text */
2856 forward_prop->length -= 1;
2858 new_prop = g_list_alloc();
2859 new_prop->data = new_text_property (font, fore, back, len+1);
2860 new_prop->prev = MARK_LIST_PTR(mark);
2861 new_prop->next = NULL;
2862 MARK_NEXT_LIST_PTR(mark) = new_prop;
2864 SET_PROPERTY_MARK (mark, new_prop, 0);
2868 GList* new_prop = g_list_alloc();
2869 GList* new_prop_forward = g_list_alloc();
2870 gint old_length = forward_prop->length;
2871 GList* next = MARK_NEXT_LIST_PTR(mark);
2873 /* Set the new lengths according to where they are split. Construct
2874 * two new properties. */
2875 forward_prop->length = MARK_OFFSET(mark);
2877 new_prop_forward->data = new_text_property(forward_prop->font->gdk_font,
2878 &forward_prop->fore_color,
2879 &forward_prop->back_color,
2880 old_length - forward_prop->length);
2882 new_prop->data = new_text_property(font, fore, back, len);
2884 /* Now splice things in. */
2885 MARK_NEXT_LIST_PTR(mark) = new_prop;
2886 new_prop->prev = MARK_LIST_PTR(mark);
2888 new_prop->next = new_prop_forward;
2889 new_prop_forward->prev = new_prop;
2891 new_prop_forward->next = next;
2894 next->prev = new_prop_forward;
2896 SET_PROPERTY_MARK (mark, new_prop, 0);
2900 while (text->text_properties_end->next)
2901 text->text_properties_end = text->text_properties_end->next;
2903 while (text->text_properties->prev)
2904 text->text_properties = text->text_properties->prev;
2908 delete_text_property (GtkText* text, guint nchars)
2910 /* Delete nchars forward from point. */
2912 /* Deleting text properties is problematical, because we
2913 * might be storing around marks pointing to a property.
2915 * The marks in question and how we handle them are:
2917 * point: We know the new value, since it will be at the
2918 * end of the deleted text, and we move it there
2920 * cursor: We just remove the mark and set it equal to the
2921 * point after the operation.
2922 * line-start cache: We replace most affected lines.
2923 * The current line gets used to fetch the new
2924 * lines so, if necessary, (delete at the beginning
2925 * of a line) we fix it up by setting it equal to the
2933 for(; nchars; nchars -= 1)
2935 prop = MARK_CURRENT_PROPERTY(&text->point);
2939 if (prop->length == 0)
2941 tmp = MARK_LIST_PTR (&text->point);
2943 is_first = tmp == text->text_properties;
2945 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
2946 text->point.offset = 0;
2948 g_mem_chunk_free (text_property_chunk, prop);
2949 g_list_free_1 (tmp);
2951 prop = MARK_CURRENT_PROPERTY (&text->point);
2954 text->text_properties = MARK_LIST_PTR (&text->point);
2956 g_assert (prop->length != 0);
2958 else if (prop->length == text->point.offset)
2960 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
2961 text->point.offset = 0;
2965 /* Check to see if we have just the single final position remaining
2966 * along in a property; if so, combine it with the previous property
2968 if (LAST_INDEX (text, text->point) &&
2969 (MARK_OFFSET (&text->point) == 0) &&
2970 (MARK_PREV_LIST_PTR(&text->point) != NULL))
2972 tmp = MARK_LIST_PTR (&text->point);
2973 prop = MARK_CURRENT_PROPERTY(&text->point);
2975 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
2976 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
2977 MARK_NEXT_LIST_PTR(&text->point) = NULL;
2979 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
2981 g_mem_chunk_free (text_property_chunk, prop);
2982 g_list_free_1 (tmp);
2987 init_properties (GtkText *text)
2989 GtkWidget *widget = (GtkWidget *)text;
2991 if (!text->text_properties)
2993 text->text_properties = g_list_alloc();
2994 text->text_properties->next = NULL;
2995 text->text_properties->prev = NULL;
2996 text->text_properties->data = new_text_property (widget->style->font,
2997 &widget->style->text[GTK_STATE_NORMAL],
2998 &widget->style->base[GTK_STATE_NORMAL],
3000 text->text_properties_end = text->text_properties;
3002 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3004 text->point.index = 0;
3009 /**********************************************************************/
3010 /* Property Movement */
3011 /**********************************************************************/
3014 move_mark_n (GtkPropertyMark* mark, gint n)
3017 advance_mark_n(mark, n);
3019 decrement_mark_n(mark, -n);
3023 advance_mark_n (GtkPropertyMark* mark, gint n)
3029 for (i = 0; i < n; i += 1)
3030 advance_mark (mark);
3034 advance_mark (GtkPropertyMark* mark)
3036 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3040 if (prop->length > mark->offset + 1)
3044 mark->property = MARK_NEXT_LIST_PTR (mark);
3050 decrement_mark (GtkPropertyMark* mark)
3054 if (mark->offset > 0)
3058 mark->property = MARK_PREV_LIST_PTR (mark);
3059 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3064 decrement_mark_n (GtkPropertyMark* mark, gint n)
3070 for (i = 0; i < n; i += 1)
3071 decrement_mark (mark);
3074 static GtkPropertyMark
3075 find_mark (GtkText* text, guint mark_position)
3077 return find_mark_near (text, mark_position, &text->point);
3080 /* This can be optimized in two ways.
3081 * First, advances can be made in units of the current TextProperty
3082 * length, when possible. This will reduce computation and function
3085 * You can also start from the end, what a drag.
3087 static GtkPropertyMark
3088 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3093 GtkPropertyMark mark;
3096 diffa = mark_position + 1;
3098 diffa = mark_position - near->index;
3100 diffb = mark_position;
3112 mark.property = text->text_properties;
3116 if (mark.index > mark_position)
3118 while (mark.index > mark_position)
3119 decrement_mark (&mark);
3123 while (mark_position > mark.index)
3124 advance_mark (&mark);
3130 /* This routine must be called with scroll == FALSE, only when
3131 * point is at least partially on screen
3135 find_line_containing_point (GtkText* text, guint point,
3141 text->current_line = NULL;
3143 if (!text->line_start_cache->next)
3145 /* @@@ Its visible, right? */
3146 text->current_line = text->line_start_cache;
3150 while ( ( scroll && (text->first_cut_pixels != 0) &&
3151 (CACHE_DATA(text->line_start_cache->next).start.index > point) ) ||
3152 ( (text->first_cut_pixels == 0) &&
3153 (CACHE_DATA(text->line_start_cache).start.index > point) ) )
3155 scroll_int (text, - SCROLL_PIXELS);
3156 g_assert (text->line_start_cache->next);
3160 gdk_window_get_size (text->text_area, NULL, &height);
3162 for (cache = text->line_start_cache; cache; cache = cache->next)
3166 if (CACHE_DATA(cache).end.index >= point ||
3167 LAST_INDEX(text, CACHE_DATA(cache).end))
3169 text->current_line = cache; /* LOOK HERE, this proc has an
3170 * important side effect. */
3174 TEXT_SHOW_LINE (text, cache, "cache");
3176 if (cache->next == NULL)
3177 fetch_lines_forward (text, 1);
3181 lph = pixel_height_of (text, cache->next);
3183 /* Scroll the bottom of the line is on screen, or until
3184 * the line is the first onscreen line.
3186 while (cache->next != text->line_start_cache && lph > height)
3188 TEXT_SHOW_LINE (text, cache, "cache");
3189 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3190 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3191 lph = pixel_height_of (text, cache->next);
3196 g_assert_not_reached (); /* Must set text->current_line here */
3200 pixel_height_of (GtkText* text, GList* cache_line)
3202 gint pixels = - text->first_cut_pixels;
3203 GList *cache = text->line_start_cache;
3206 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3208 if (cache->data == cache_line->data)
3211 cache = cache->next;
3217 /**********************************************************************/
3218 /* Search and Placement */
3219 /**********************************************************************/
3222 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3225 gint16* char_widths;
3227 if (LAST_INDEX (text, *mark))
3230 ch = GTK_TEXT_INDEX (text, mark->index);
3231 char_widths = MARK_CURRENT_TEXT_FONT (mark)->char_widths;
3235 return tab_mark->to_next_tab * char_widths[' '];
3239 return char_widths[ch & 0xff];
3244 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, gchar ch)
3246 if (tab_mark->to_next_tab == 1 || ch == '\t')
3248 if (tab_mark->tab_stops->next)
3250 tab_mark->tab_stops = tab_mark->tab_stops->next;
3251 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3255 tab_mark->to_next_tab = text->default_tab_width;
3260 tab_mark->to_next_tab -= 1;
3265 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3269 advance_tab_mark (text, tab_mark, 0);
3273 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3276 GtkEditable *editable = (GtkEditable *)text;
3278 GtkPropertyMark mark = start_line->start;
3279 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3280 gint pixel_width = LINE_START_PIXEL (*start_line);
3282 while (mark.index < text->cursor_mark.index)
3284 pixel_width += find_char_width (text, &mark, &tab_mark);
3286 advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3287 advance_mark (&mark);
3290 text->cursor_pos_x = pixel_width;
3291 text->cursor_pos_y = pixel_height;
3292 text->cursor_char_offset = start_line->font_descent;
3293 text->cursor_mark = mark;
3295 ch = LAST_INDEX (text, mark) ?
3296 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3299 text->cursor_char = ch;
3301 text->cursor_char = 0;
3304 if (gdk_im_ready() && editable->ic &&
3305 gdk_ic_get_style (editable->ic) & GdkIMPreeditPosition)
3309 spot.x = text->cursor_pos_x;
3310 spot.y = text->cursor_pos_y - text->cursor_char_offset;
3311 if (MARK_CURRENT_FONT (&mark)->type == GDK_FONT_FONTSET)
3312 gdk_ic_set_attr (editable->ic, "preeditAttributes",
3313 "fontSet", GDK_FONT_XFONT (MARK_CURRENT_FONT (&mark)),
3316 gdk_ic_set_attr (editable->ic, "preeditAttributes",
3317 "spotLocation", &spot,
3318 "lineSpace", LINE_HEIGHT (*start_line),
3319 "foreground", MARK_CURRENT_FORE (&mark)->pixel,
3320 "background", MARK_CURRENT_BACK (&mark)->pixel,
3327 find_cursor (GtkText* text, gboolean scroll)
3329 if (GTK_WIDGET_REALIZED (text))
3331 find_line_containing_point (text, text->cursor_mark.index, scroll);
3333 g_assert (text->cursor_mark.index >= text->first_line_start_index);
3335 if (text->current_line)
3336 find_cursor_at_line (text,
3337 &CACHE_DATA(text->current_line),
3338 pixel_height_of(text, text->current_line));
3341 GTK_EDITABLE (text)->current_pos = text->cursor_mark.index;
3345 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3346 guint line_pixel_height,
3349 GtkPropertyMark mark = lp->start;
3350 TabStopMark tab_mark = lp->tab_cont.tab_start;
3352 gint char_width = find_char_width(text, &mark, &tab_mark);
3353 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3355 text->cursor_pos_y = line_pixel_height;
3359 gchar ch = LAST_INDEX (text, mark) ?
3360 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3362 if (button_x < pixel_width || mark.index == lp->end.index)
3364 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3365 text->cursor_mark = mark;
3366 text->cursor_char_offset = lp->font_descent;
3369 text->cursor_char = ch;
3371 text->cursor_char = 0;
3376 advance_tab_mark (text, &tab_mark, ch);
3377 advance_mark (&mark);
3379 pixel_width += char_width/2;
3381 char_width = find_char_width (text, &mark, &tab_mark);
3383 pixel_width += (char_width+1)/2;
3388 find_mouse_cursor (GtkText* text, gint x, gint y)
3391 GList* cache = text->line_start_cache;
3395 pixel_height = - text->first_cut_pixels;
3397 for (; cache; cache = cache->next)
3399 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3401 if (y < pixel_height || !cache->next)
3403 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3405 find_cursor (text, FALSE);
3412 /**********************************************************************/
3414 /**********************************************************************/
3417 free_cache (GtkText* text)
3419 GList* cache = text->line_start_cache;
3424 cache = cache->prev;
3426 text->line_start_cache = cache;
3429 for (; cache; cache = cache->next)
3430 g_mem_chunk_free (params_mem_chunk, cache->data);
3432 g_list_free (text->line_start_cache);
3434 text->line_start_cache = NULL;
3438 remove_cache_line (GtkText* text, GList* member)
3445 if (member == text->line_start_cache)
3446 text->line_start_cache = text->line_start_cache->next;
3449 member->prev->next = member->next;
3452 member->next->prev = member->prev;
3454 list = member->next;
3456 g_mem_chunk_free (params_mem_chunk, member->data);
3457 g_list_free_1 (member);
3462 /**********************************************************************/
3464 /**********************************************************************/
3467 move_cursor_buffer_ver (GtkText *text, int dir)
3469 undraw_cursor (text, FALSE);
3473 scroll_int (text, text->vadj->upper);
3474 text->cursor_mark = find_this_line_start_mark (text,
3476 &text->cursor_mark);
3480 scroll_int (text, - text->vadj->value);
3481 text->cursor_mark = find_this_line_start_mark (text,
3483 &text->cursor_mark);
3486 find_cursor (text, TRUE);
3487 draw_cursor (text, FALSE);
3491 move_cursor_page_ver (GtkText *text, int dir)
3493 scroll_int (text, dir * text->vadj->page_increment);
3497 move_cursor_ver (GtkText *text, int count)
3500 GtkPropertyMark mark;
3503 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3504 offset = text->cursor_mark.index - mark.index;
3506 if (offset > text->cursor_virtual_x)
3507 text->cursor_virtual_x = offset;
3511 if (mark.index == 0)
3514 decrement_mark (&mark);
3515 mark = find_this_line_start_mark (text, mark.index, &mark);
3519 mark = text->cursor_mark;
3521 while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3522 advance_mark (&mark);
3524 if (LAST_INDEX(text, mark))
3527 advance_mark (&mark);
3530 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3531 if (LAST_INDEX(text, mark) || GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3534 undraw_cursor (text, FALSE);
3536 text->cursor_mark = mark;
3538 find_cursor (text, TRUE);
3540 draw_cursor (text, FALSE);
3544 move_cursor_hor (GtkText *text, int count)
3546 /* count should be +-1. */
3547 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3548 (count < 0 && text->cursor_mark.index < (- count)) ||
3552 text->cursor_virtual_x = 0;
3554 undraw_cursor (text, FALSE);
3556 move_mark_n (&text->cursor_mark, count);
3558 find_cursor (text, TRUE);
3560 draw_cursor (text, FALSE);
3564 gtk_text_move_cursor (GtkEditable *editable,
3571 move_cursor_hor (GTK_TEXT (editable), 1);
3576 move_cursor_hor (GTK_TEXT (editable), -1);
3582 move_cursor_ver (GTK_TEXT (editable), 1);
3587 move_cursor_ver (GTK_TEXT (editable), -1);
3592 gtk_text_move_forward_character (GtkText *text)
3594 move_cursor_hor (text, 1);
3598 gtk_text_move_backward_character (GtkText *text)
3600 move_cursor_hor (text, -1);
3604 gtk_text_move_next_line (GtkText *text)
3606 move_cursor_ver (text, 1);
3610 gtk_text_move_previous_line (GtkText *text)
3612 move_cursor_ver (text, -1);
3616 gtk_text_move_word (GtkEditable *editable,
3622 gtk_text_move_forward_word (GTK_TEXT (editable));
3627 gtk_text_move_backward_word (GTK_TEXT (editable));
3632 gtk_text_move_forward_word (GtkText *text)
3634 text->cursor_virtual_x = 0;
3636 undraw_cursor (text, FALSE);
3638 while (!LAST_INDEX (text, text->cursor_mark) &&
3639 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3640 advance_mark (&text->cursor_mark);
3642 while (!LAST_INDEX (text, text->cursor_mark) &&
3643 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3644 advance_mark (&text->cursor_mark);
3646 find_cursor (text, TRUE);
3647 draw_cursor (text, FALSE);
3651 gtk_text_move_backward_word (GtkText *text)
3653 text->cursor_virtual_x = 0;
3655 undraw_cursor (text, FALSE);
3657 while ((text->cursor_mark.index > 0) &&
3658 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3659 decrement_mark (&text->cursor_mark);
3661 while ((text->cursor_mark.index > 0) &&
3662 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3663 decrement_mark (&text->cursor_mark);
3665 find_cursor (text, TRUE);
3666 draw_cursor (text, FALSE);
3670 gtk_text_move_page (GtkEditable *editable,
3675 scroll_int (GTK_TEXT (editable),
3676 y * GTK_TEXT(editable)->vadj->page_increment);
3680 gtk_text_move_to_row (GtkEditable *editable,
3686 gtk_text_move_to_column (GtkEditable *editable,
3691 text = GTK_TEXT (editable);
3693 text->cursor_virtual_x = 0; /* FIXME */
3695 undraw_cursor (text, FALSE);
3697 /* Move to the beginning of the line */
3698 while ((text->cursor_mark.index > 0) &&
3699 (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
3700 decrement_mark (&text->cursor_mark);
3702 while (!LAST_INDEX (text, text->cursor_mark) &&
3703 (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
3707 else if (column == 0)
3710 advance_mark (&text->cursor_mark);
3713 find_cursor (text, TRUE);
3714 draw_cursor (text, FALSE);
3718 gtk_text_move_beginning_of_line (GtkText *text)
3720 gtk_text_move_to_column (GTK_EDITABLE (text), 0);
3725 gtk_text_move_end_of_line (GtkText *text)
3727 gtk_text_move_to_column (GTK_EDITABLE (text), -1);
3731 gtk_text_kill_char (GtkEditable *editable,
3736 text = GTK_TEXT (editable);
3738 if (editable->selection_start_pos != editable->selection_end_pos)
3739 gtk_editable_delete_selection (editable);
3744 if (text->point.index + 1 <= TEXT_LENGTH (text))
3745 gtk_editable_delete_text (editable, text->point.index, text->point.index + 1);
3749 if (text->point.index > 0)
3750 gtk_editable_delete_text (editable, text->point.index - 1, text->point.index);
3756 gtk_text_delete_forward_character (GtkText *text)
3758 gtk_text_kill_char (GTK_EDITABLE (text), 1);
3762 gtk_text_delete_backward_character (GtkText *text)
3764 gtk_text_kill_char (GTK_EDITABLE (text), -1);
3768 gtk_text_kill_word (GtkEditable *editable,
3771 if (editable->selection_start_pos != editable->selection_end_pos)
3772 gtk_editable_delete_selection (editable);
3775 gint old_pos = editable->current_pos;
3778 gtk_text_move_word (editable, 1);
3779 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
3783 gtk_text_move_word (editable, -1);
3784 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
3790 gtk_text_delete_forward_word (GtkText *text)
3792 gtk_text_kill_word (GTK_EDITABLE (text), 1);
3796 gtk_text_delete_backward_word (GtkText *text)
3798 gtk_text_kill_word (GTK_EDITABLE (text), -1);
3802 gtk_text_kill_line (GtkEditable *editable,
3805 gint old_pos = editable->current_pos;
3808 gtk_text_move_to_column (editable, -1);
3809 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
3813 gtk_text_move_to_column (editable, 0);
3814 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
3819 gtk_text_delete_line (GtkText *text)
3821 gtk_text_move_to_column (GTK_EDITABLE (text), 0);
3822 gtk_text_kill_line (GTK_EDITABLE (text), 1);
3826 gtk_text_delete_to_line_end (GtkText *text)
3828 gtk_text_kill_line (GTK_EDITABLE (text), 1);
3832 gtk_text_select_word (GtkText *text, guint32 time)
3837 GtkEditable *editable;
3838 editable = GTK_EDITABLE (text);
3840 gtk_text_move_backward_word (text);
3841 start_pos = text->cursor_mark.index;
3843 gtk_text_move_forward_word (text);
3844 end_pos = text->cursor_mark.index;
3846 editable->has_selection = TRUE;
3847 gtk_text_set_selection (editable, start_pos, end_pos);
3848 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
3852 gtk_text_select_line (GtkText *text, guint32 time)
3857 GtkEditable *editable;
3858 editable = GTK_EDITABLE (text);
3860 gtk_text_move_beginning_of_line (text);
3861 start_pos = text->cursor_mark.index;
3863 gtk_text_move_end_of_line (text);
3864 gtk_text_move_forward_character (text);
3865 end_pos = text->cursor_mark.index;
3867 editable->has_selection = TRUE;
3868 gtk_text_set_selection (editable, start_pos, end_pos);
3869 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
3872 /**********************************************************************/
3874 /**********************************************************************/
3877 adjust_adj (GtkText* text, GtkAdjustment* adj)
3881 gdk_window_get_size (text->text_area, NULL, &height);
3883 adj->step_increment = MIN (adj->upper, (float) SCROLL_PIXELS);
3884 adj->page_increment = MIN (adj->upper, height - (float) KEY_SCROLL_PIXELS);
3885 adj->page_size = MIN (adj->upper, height);
3886 adj->value = MIN (adj->value, adj->upper - adj->page_size);
3887 adj->value = MAX (adj->value, 0.0);
3889 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
3893 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
3895 gint *pixel_count = (gint*) data;
3897 if (text->first_line_start_index == lp->start.index)
3898 text->vadj->value = (float) *pixel_count;
3900 *pixel_count += LINE_HEIGHT (*lp);
3906 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
3908 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
3911 if (svdata->last_didnt_wrap)
3912 svdata->last_line_start = lp->start.index;
3914 if (svdata->pixel_height <= (gint) text->vadj->value &&
3915 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
3917 svdata->mark = lp->start;
3919 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
3920 text->first_onscreen_ver_pixel = svdata->pixel_height;
3921 text->first_line_start_index = svdata->last_line_start;
3927 svdata->pixel_height += LINE_HEIGHT (*lp);
3933 svdata->last_didnt_wrap = TRUE;
3935 svdata->last_didnt_wrap = FALSE;
3940 static GtkPropertyMark
3941 set_vertical_scroll (GtkText* text)
3943 GtkPropertyMark mark = find_mark (text, 0);
3944 SetVerticalScrollData data;
3946 gint pixel_count = 0;
3949 line_params_iterate (text, &mark, NULL, FALSE, &pixel_count, set_vertical_scroll_iterator);
3951 text->vadj->upper = (float) pixel_count;
3952 orig_value = (gint) text->vadj->value;
3954 gdk_window_get_size (text->text_area, NULL, &height);
3956 text->vadj->step_increment = MIN (text->vadj->upper, (float) SCROLL_PIXELS);
3957 text->vadj->page_increment = MIN (text->vadj->upper, height - (float) KEY_SCROLL_PIXELS);
3958 text->vadj->page_size = MIN (text->vadj->upper, height);
3959 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
3960 text->vadj->value = MAX (text->vadj->value, 0.0);
3962 text->last_ver_value = (gint)text->vadj->value;
3963 text->first_cut_pixels = 0;
3965 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
3967 if (text->vadj->value != orig_value)
3969 /* We got clipped, and don't really know which line to put first. */
3970 data.pixel_height = 0;
3971 data.last_didnt_wrap = TRUE;
3973 line_params_iterate (text, &mark, NULL,
3975 set_vertical_scroll_find_iterator);
3981 return find_mark (text, text->first_line_start_index);
3986 scroll_int (GtkText* text, gint diff)
3990 text->vadj->value += diff;
3992 upper = text->vadj->upper - text->vadj->page_size;
3993 text->vadj->value = MIN (text->vadj->value, upper);
3994 text->vadj->value = MAX (text->vadj->value, 0.0);
3996 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4000 process_exposes (GtkText *text)
4004 /* Make sure graphics expose events are processed before scrolling
4007 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4009 gtk_widget_event (GTK_WIDGET (text), event);
4010 if (event->expose.count == 0)
4012 gdk_event_free (event);
4015 gdk_event_free (event);
4019 static gint last_visible_line_height (GtkText* text)
4021 GList *cache = text->line_start_cache;
4024 gdk_window_get_size (text->text_area, NULL, &height);
4026 for (; cache->next; cache = cache->next)
4027 if (pixel_height_of(text, cache->next) > height)
4031 return pixel_height_of(text, cache) - 1;
4036 static gint first_visible_line_height (GtkText* text)
4038 if (text->first_cut_pixels)
4039 return pixel_height_of(text, text->line_start_cache) + 1;
4045 scroll_down (GtkText* text, gint diff0)
4051 text->first_onscreen_ver_pixel += diff0;
4055 g_assert (text->line_start_cache);
4057 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4059 text->first_cut_pixels += 1;
4063 text->first_cut_pixels = 0;
4065 text->line_start_cache = text->line_start_cache->next;
4067 text->first_line_start_index =
4068 CACHE_DATA(text->line_start_cache).start.index;
4070 if (!text->line_start_cache->next)
4071 fetch_lines_forward (text, 1);
4077 gdk_window_get_size (text->text_area, &width, &height);
4078 if (height > real_diff)
4079 gdk_draw_pixmap (text->text_area,
4087 height - real_diff);
4090 rect.y = MAX (0, height - real_diff);
4092 rect.height = MIN (height, real_diff);
4094 expose_text (text, &rect, FALSE);
4095 gtk_text_draw_focus ( (GtkWidget *) text);
4097 if (text->current_line)
4101 text->cursor_pos_y -= real_diff;
4102 cursor_min = drawn_cursor_min(text);
4105 find_mouse_cursor (text, text->cursor_pos_x,
4106 first_visible_line_height (text));
4109 if (height > real_diff)
4110 process_exposes (text);
4114 scroll_up (GtkText* text, gint diff0)
4120 text->first_onscreen_ver_pixel += diff0;
4124 g_assert (text->line_start_cache);
4126 if (text->first_cut_pixels > 0)
4128 text->first_cut_pixels -= 1;
4132 if (!text->line_start_cache->prev)
4133 fetch_lines_backward (text);
4135 text->line_start_cache = text->line_start_cache->prev;
4137 text->first_line_start_index =
4138 CACHE_DATA(text->line_start_cache).start.index;
4140 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4146 gdk_window_get_size (text->text_area, &width, &height);
4147 if (height > real_diff)
4148 gdk_draw_pixmap (text->text_area,
4156 height - real_diff);
4161 rect.height = MIN (height, real_diff);
4163 expose_text (text, &rect, FALSE);
4164 gtk_text_draw_focus ( (GtkWidget *) text);
4166 if (text->current_line)
4171 text->cursor_pos_y += real_diff;
4172 cursor_max = drawn_cursor_max(text);
4173 gdk_window_get_size (text->text_area, NULL, &height);
4175 if (cursor_max >= height)
4176 find_mouse_cursor (text, text->cursor_pos_x,
4177 last_visible_line_height (text));
4180 if (height > real_diff)
4181 process_exposes (text);
4184 /**********************************************************************/
4186 /**********************************************************************/
4188 /* Assumes mark starts a line. Calculates the height, width, and
4189 * displayable character count of a single DISPLAYABLE line. That
4190 * means that in line-wrap mode, this does may not compute the
4191 * properties of an entire line. */
4193 find_line_params (GtkText* text,
4194 const GtkPropertyMark* mark,
4195 const PrevTabCont *tab_cont,
4196 PrevTabCont *next_cont)
4199 TabStopMark tab_mark = tab_cont->tab_start;
4200 guint max_display_pixels;
4205 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4206 max_display_pixels -= LINE_WRAP_ROOM;
4209 lp.tab_cont = *tab_cont;
4212 lp.pixel_width = tab_cont->pixel_offset;
4213 lp.displayable_chars = 0;
4215 lp.font_descent = 0;
4217 init_tab_cont (text, next_cont);
4219 while (!LAST_INDEX(text, lp.end))
4221 g_assert (lp.end.property);
4223 ch = GTK_TEXT_INDEX (text, lp.end.index);
4224 font = MARK_CURRENT_FONT (&lp.end);
4226 if (ch == LINE_DELIM)
4228 /* Newline doesn't count in computation of line height, even
4229 * if its in a bigger font than the rest of the line. Unless,
4230 * of course, there are no other characters. */
4232 if (!lp.font_ascent && !lp.font_descent)
4234 lp.font_ascent = font->ascent;
4235 lp.font_descent = font->descent;
4238 lp.tab_cont_next = *next_cont;
4243 ch_width = find_char_width (text, &lp.end, &tab_mark);
4245 if (ch_width + lp.pixel_width > max_display_pixels)
4249 if (text->line_wrap)
4251 next_cont->tab_start = tab_mark;
4252 next_cont->pixel_offset = 0;
4256 /* Here's the tough case, a tab is wrapping. */
4257 gint pixels_avail = max_display_pixels - lp.pixel_width;
4258 gint space_width = MARK_CURRENT_TEXT_FONT(&lp.end)->char_widths[' '];
4259 gint spaces_avail = pixels_avail / space_width;
4261 if (spaces_avail == 0)
4263 decrement_mark (&lp.end);
4267 advance_tab_mark (text, &next_cont->tab_start, '\t');
4268 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4270 lp.displayable_chars += 1;
4275 if (text->word_wrap)
4277 GtkPropertyMark saved_mark = lp.end;
4278 guint saved_characters = lp.displayable_chars;
4280 lp.displayable_chars += 1;
4282 while (!isspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4283 (lp.end.index > lp.start.index))
4285 decrement_mark (&lp.end);
4286 lp.displayable_chars -= 1;
4289 /* If whole line is one word, revert to char wrapping */
4290 if (lp.end.index == lp.start.index)
4292 lp.end = saved_mark;
4293 lp.displayable_chars = saved_characters;
4294 decrement_mark (&lp.end);
4299 /* Don't include this character, it will wrap. */
4300 decrement_mark (&lp.end);
4304 lp.tab_cont_next = *next_cont;
4311 lp.displayable_chars += 1;
4314 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4315 lp.font_descent = MAX (font->descent, lp.font_descent);
4316 lp.pixel_width += ch_width;
4318 advance_mark(&lp.end);
4319 advance_tab_mark (text, &tab_mark, ch);
4322 if (LAST_INDEX(text, lp.start))
4324 /* Special case, empty last line. */
4325 font = MARK_CURRENT_FONT (&lp.end);
4327 lp.font_ascent = font->ascent;
4328 lp.font_descent = font->descent;
4331 lp.tab_cont_next = *next_cont;
4337 expand_scratch_buffer (GtkText* text, guint len)
4339 if (len >= text->scratch_buffer_len)
4343 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4345 if (text->scratch_buffer)
4346 text->scratch_buffer = g_new (guchar, i);
4348 text->scratch_buffer = g_realloc (text->scratch_buffer, i);
4350 text->scratch_buffer_len = i;
4354 /* Returns a GC to draw a background for the text at a mark,
4355 * or NULL, if the mark's background is NULL
4357 * Side effect: modifies text->gc
4360 mark_bg_gc (GtkText* text, const GtkPropertyMark *mark)
4362 GtkEditable *editable = GTK_EDITABLE(text);
4364 if ((mark->index >= MIN(editable->selection_start_pos, editable->selection_end_pos) &&
4365 mark->index < MAX(editable->selection_start_pos, editable->selection_end_pos)))
4367 if (editable->has_selection)
4368 return GTK_WIDGET(text)->style->bg_gc[GTK_STATE_SELECTED];
4370 return GTK_WIDGET(text)->style->bg_gc[GTK_STATE_ACTIVE];
4372 else if (!gdk_color_equal(MARK_CURRENT_BACK (mark),
4373 >K_WIDGET(text)->style->base[GTK_STATE_NORMAL]))
4376 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (mark));
4383 draw_line (GtkText* text,
4384 gint pixel_start_height,
4387 GdkGCValues gc_values;
4390 guint running_offset = lp->tab_cont.pixel_offset;
4392 GdkGC *fg_gc, *bg_gc;
4394 GtkEditable *editable = GTK_EDITABLE(text);
4396 guint selection_start_pos = MIN (editable->selection_start_pos, editable->selection_end_pos);
4397 guint selection_end_pos = MAX (editable->selection_start_pos, editable->selection_end_pos);
4399 GtkPropertyMark mark = lp->start;
4400 TabStopMark tab_mark = lp->tab_cont.tab_start;
4401 gint pixel_height = pixel_start_height + lp->font_ascent;
4402 guint chars = lp->displayable_chars;
4404 /* First provide a contiguous segment of memory. This makes reading
4405 * the code below *much* easier, and only incurs the cost of copying
4406 * when the line being displayed spans the gap. */
4407 if (mark.index <= text->gap_position &&
4408 mark.index + chars > text->gap_position)
4410 expand_scratch_buffer (text, chars);
4412 for (i = 0; i < chars; i += 1)
4413 text->scratch_buffer[i] = GTK_TEXT_INDEX(text, mark.index + i);
4415 buffer = text->scratch_buffer;
4419 if (mark.index >= text->gap_position)
4420 buffer = text->text + mark.index + text->gap_size;
4422 buffer = text->text + mark.index;
4426 if (running_offset > 0)
4428 bg_gc = mark_bg_gc (text, &mark);
4431 gdk_draw_rectangle (text->text_area,
4438 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4443 rect.y = pixel_start_height;
4444 rect.width = running_offset;
4445 rect.height = LINE_HEIGHT (*lp);
4447 clear_area (text, &rect);
4451 for (; chars > 0; chars -= len, buffer += len, len = 0)
4453 if (buffer[0] != '\t')
4455 guchar* next_tab = memchr (buffer, '\t', chars);
4459 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4462 len = MIN (len, next_tab - buffer);
4464 if (mark.index < selection_start_pos)
4465 len = MIN (len, selection_start_pos - mark.index);
4466 else if (mark.index < selection_end_pos)
4467 len = MIN (len, selection_end_pos - mark.index);
4469 font = MARK_CURRENT_PROPERTY (&mark)->font->gdk_font;
4470 if (font->type == GDK_FONT_FONT)
4472 gdk_gc_set_font (text->gc, font);
4473 gdk_gc_get_values (text->gc, &gc_values);
4474 pixel_width = gdk_text_width (gc_values.font,
4475 (gchar*) buffer, len);
4478 pixel_width = gdk_text_width (font, (gchar*) buffer, len);
4480 bg_gc = mark_bg_gc (text, &mark);
4482 gdk_draw_rectangle (text->text_area,
4489 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4493 rect.x = running_offset;
4494 rect.y = pixel_start_height;
4495 rect.width = pixel_width;
4496 rect.height = LINE_HEIGHT (*lp);
4498 clear_area (text, &rect);
4501 if ((mark.index >= selection_start_pos) &&
4502 (mark.index < selection_end_pos))
4504 if (editable->has_selection)
4505 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_SELECTED];
4507 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_ACTIVE];
4511 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (&mark));
4515 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (&mark),
4522 running_offset += pixel_width;
4524 advance_tab_mark_n (text, &tab_mark, len);
4530 bg_gc = mark_bg_gc (text, &mark);
4533 gint pixels_remaining;
4537 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
4538 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
4540 space_width = MARK_CURRENT_TEXT_FONT(&mark)->char_widths[' '];
4542 spaces_avail = pixels_remaining / space_width;
4543 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
4545 gdk_draw_rectangle (text->text_area,
4550 spaces_avail * space_width,
4554 running_offset += tab_mark.to_next_tab *
4555 MARK_CURRENT_TEXT_FONT(&mark)->char_widths[' '];
4557 advance_tab_mark (text, &tab_mark, '\t');
4560 advance_mark_n (&mark, len);
4565 draw_line_wrap (GtkText* text, guint height /* baseline height */)
4572 if (text->line_wrap)
4574 bitmap = text->line_wrap_bitmap;
4575 bitmap_width = line_wrap_width;
4576 bitmap_height = line_wrap_height;
4580 bitmap = text->line_arrow_bitmap;
4581 bitmap_width = line_arrow_width;
4582 bitmap_height = line_arrow_height;
4585 gdk_window_get_size (text->text_area, &width, NULL);
4586 width -= LINE_WRAP_ROOM;
4588 gdk_gc_set_stipple (text->gc,
4591 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
4593 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
4595 gdk_gc_set_ts_origin (text->gc,
4597 height - bitmap_height - 1);
4599 gdk_draw_rectangle (text->text_area,
4603 height - bitmap_height - 1 /* one pixel above the baseline. */,
4607 gdk_gc_set_ts_origin (text->gc, 0, 0);
4609 gdk_gc_set_fill (text->gc, GDK_SOLID);
4613 undraw_cursor (GtkText* text, gint absolute)
4615 GtkEditable *editable = (GtkEditable *)text;
4617 TDEBUG (("in undraw_cursor\n"));
4620 text->cursor_drawn_level = 0;
4622 if ((text->cursor_drawn_level ++ == 0) &&
4623 (editable->selection_start_pos == editable->selection_end_pos) &&
4624 GTK_WIDGET_DRAWABLE (text))
4628 g_assert(text->cursor_mark.property);
4630 font = MARK_CURRENT_FONT(&text->cursor_mark);
4632 if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4636 rect.x = text->cursor_pos_x;
4637 rect.y = text->cursor_pos_y - text->cursor_char_offset - font->ascent;
4639 rect.height = font->ascent + 1; /* @@@ I add one here because draw_line is inclusive, right? */
4641 clear_area (text, &rect);
4645 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (&text->cursor_mark));
4646 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
4647 text->cursor_pos_y - text->cursor_char_offset, text->cursor_pos_x,
4648 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
4651 if (text->cursor_char)
4653 if (font->type == GDK_FONT_FONT)
4654 gdk_gc_set_font (text->gc, font);
4656 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (&text->cursor_mark));
4658 gdk_draw_text (text->text_area, font,
4661 text->cursor_pos_y - text->cursor_char_offset,
4669 drawn_cursor_min (GtkText* text)
4673 g_assert(text->cursor_mark.property);
4675 font = MARK_CURRENT_FONT(&text->cursor_mark);
4677 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
4681 drawn_cursor_max (GtkText* text)
4685 g_assert(text->cursor_mark.property);
4687 font = MARK_CURRENT_FONT(&text->cursor_mark);
4689 return text->cursor_pos_y - text->cursor_char_offset;
4693 draw_cursor (GtkText* text, gint absolute)
4695 GtkEditable *editable = (GtkEditable *)text;
4697 TDEBUG (("in draw_cursor\n"));
4700 text->cursor_drawn_level = 1;
4702 if ((--text->cursor_drawn_level == 0) &&
4703 editable->editable &&
4704 (editable->selection_start_pos == editable->selection_end_pos) &&
4705 GTK_WIDGET_DRAWABLE (text))
4709 g_assert (text->cursor_mark.property);
4711 font = MARK_CURRENT_FONT (&text->cursor_mark);
4713 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
4715 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
4716 text->cursor_pos_y - text->cursor_char_offset,
4718 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
4723 clear_area (GtkText *text, GdkRectangle *area)
4725 GtkWidget *widget = GTK_WIDGET (text);
4727 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
4730 gint x = area->x, y = area->y;
4733 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
4735 yorig = - text->first_onscreen_ver_pixel;
4736 xorig = - text->first_onscreen_hor_pixel;
4738 for (y = area->y; y < area->y + area->height; )
4740 gint yoff = (y - yorig) % height;
4741 gint yw = MIN(height - yoff, (area->y + area->height) - y);
4743 for (x = area->x; x < area->x + area->width; )
4745 gint xoff = (x - xorig) % width;
4746 gint xw = MIN(width - xoff, (area->x + area->width) - x);
4748 gdk_draw_pixmap (text->text_area,
4750 widget->style->bg_pixmap[GTK_STATE_NORMAL],
4764 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
4768 expose_text (GtkText* text, GdkRectangle *area, gboolean cursor)
4770 GList *cache = text->line_start_cache;
4771 gint pixels = - text->first_cut_pixels;
4772 gint min_y = MAX (0, area->y);
4773 gint max_y = MAX (0, area->y + area->height);
4776 gdk_window_get_size (text->text_area, NULL, &height);
4777 max_y = MIN (max_y, height);
4779 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
4781 clear_area (text, area);
4783 for (; pixels < height; cache = cache->next)
4785 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
4787 draw_line (text, pixels, &CACHE_DATA(cache));
4789 if (CACHE_DATA(cache).wraps)
4790 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
4793 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
4795 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
4796 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
4798 /* We undraw and draw the cursor here to get the drawn
4799 * level right ... FIXME - maybe the second parameter
4800 * of draw_cursor should work differently
4802 undraw_cursor (text, FALSE);
4803 draw_cursor (text, FALSE);
4807 pixels += LINE_HEIGHT(CACHE_DATA(cache));
4811 fetch_lines_forward (text, 1);
4820 gtk_text_update_text (GtkEditable *editable,
4824 GtkText *text = GTK_TEXT (editable);
4826 GList *cache = text->line_start_cache;
4827 gint pixels = - text->first_cut_pixels;
4833 end_pos = TEXT_LENGTH (text);
4835 if (end_pos < start_pos)
4838 gdk_window_get_size (text->text_area, &width, &height);
4844 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
4846 for (; pixels < height; cache = cache->next)
4848 if (CACHE_DATA(cache).start.index < end_pos)
4850 if (CACHE_DATA(cache).end.index >= start_pos)
4853 area.y = MAX(0,pixels);
4854 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
4860 pixels += LINE_HEIGHT(CACHE_DATA(cache));
4864 fetch_lines_forward (text, 1);
4872 expose_text (text, &area, TRUE);
4876 recompute_geometry (GtkText* text)
4878 GtkPropertyMark start_mark;
4884 start_mark = set_vertical_scroll (text);
4886 gdk_window_get_size (text->text_area, &width, &height);
4888 text->line_start_cache = fetch_lines (text,
4892 height + text->first_cut_pixels);
4894 find_cursor (text, TRUE);
4897 /**********************************************************************/
4899 /**********************************************************************/
4902 gtk_text_set_selection (GtkEditable *editable,
4906 GtkText *text = GTK_TEXT (editable);
4908 guint start1, end1, start2, end2;
4911 end = TEXT_LENGTH (text);
4913 start1 = MIN(start,end);
4914 end1 = MAX(start,end);
4915 start2 = MIN(editable->selection_start_pos, editable->selection_end_pos);
4916 end2 = MAX(editable->selection_start_pos, editable->selection_end_pos);
4918 if (start2 < start1)
4922 tmp = start1; start1 = start2; start2 = tmp;
4923 tmp = end1; end1 = end2; end2 = tmp;
4926 undraw_cursor (text, FALSE);
4927 editable->selection_start_pos = start;
4928 editable->selection_end_pos = end;
4929 draw_cursor (text, FALSE);
4931 /* Expose only what changed */
4933 if (start1 < start2)
4934 gtk_text_update_text (editable, start1, MIN(end1, start2));
4937 gtk_text_update_text (editable, MAX(end1, start2), end2);
4938 else if (end2 < end1)
4939 gtk_text_update_text (editable, end2, end1);
4943 /**********************************************************************/
4945 /**********************************************************************/
4947 #ifdef DEBUG_GTK_TEXT
4949 gtk_text_show_cache_line (GtkText *text, GList *cache,
4950 const char* what, const char* func, gint line)
4952 LineParams *lp = &CACHE_DATA(cache);
4955 if (cache == text->line_start_cache)
4956 g_print ("Line Start Cache: ");
4958 if (cache == text->current_line)
4959 g_print("Current Line: ");
4961 g_print ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
4969 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
4970 g_print ("%c", GTK_TEXT_INDEX (text, i));
4976 gtk_text_show_cache (GtkText *text, const char* func, gint line)
4978 GList *l = text->line_start_cache;
4984 /* back up to the absolute beginning of the line cache */
4988 g_print ("*** line cache ***\n");
4989 for (; l; l = l->next)
4990 gtk_text_show_cache_line (text, l, "all", func, line);
4994 gtk_text_assert_mark (GtkText *text,
4995 GtkPropertyMark *mark,
4996 GtkPropertyMark *before,
4997 GtkPropertyMark *after,
5002 GtkPropertyMark correct_mark = find_mark (text, mark->index);
5004 if (mark->offset != correct_mark.offset ||
5005 mark->property != correct_mark.property)
5006 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
5010 gtk_text_assert (GtkText *text,
5014 GList* cache = text->line_start_cache;
5015 GtkPropertyMark* before_mark = NULL;
5016 GtkPropertyMark* after_mark = NULL;
5018 gtk_text_show_props (text, msg, line);
5020 for (; cache->prev; cache = cache->prev)
5023 g_print ("*** line markers ***\n");
5025 for (; cache; cache = cache->next)
5027 after_mark = &CACHE_DATA(cache).end;
5028 gtk_text_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
5029 before_mark = &CACHE_DATA(cache).start;
5032 after_mark = &CACHE_DATA(cache->next).start;
5036 gtk_text_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
5037 before_mark = &CACHE_DATA(cache).end;
5042 gtk_text_show_adj (GtkText *text,
5048 g_print ("*** adjustment ***\n");
5050 g_print ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
5057 adj->step_increment,
5058 adj->page_increment,
5063 gtk_text_show_props (GtkText *text,
5067 GList* props = text->text_properties;
5070 g_print ("%s:%d: ", msg, line);
5072 for (; props; props = props->next)
5074 TextProperty *p = (TextProperty*)props->data;
5076 proplen += p->length;
5078 g_print ("[%d,%p,%p,%ld,%ld] ", p->length, p, p->font, p->fore_color.pixel, p->back_color.pixel);
5083 if (proplen - 1 != TEXT_LENGTH(text))
5084 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);