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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #undef GDK_DISABLE_DEPRECATED
28 #undef GTK_DISABLE_DEPRECATED
32 #include "gdk/gdkkeysyms.h"
33 #include "gdk/gdki18n.h"
35 #include "gtkmarshalers.h"
36 #include "gtkselection.h"
37 #include "gtksignal.h"
38 #define GTK_ENABLE_BROKEN
40 #include "line-wrap.xbm"
41 #include "line-arrow.xbm"
45 #define INITIAL_BUFFER_SIZE 1024
46 #define INITIAL_LINE_CACHE_SIZE 256
47 #define MIN_GAP_SIZE 256
48 #define LINE_DELIM '\n'
49 #define MIN_TEXT_WIDTH_LINES 20
50 #define MIN_TEXT_HEIGHT_LINES 10
51 #define TEXT_BORDER_ROOM 1
52 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
53 #define DEFAULT_TAB_STOP_WIDTH 4
54 #define SCROLL_PIXELS 5
55 #define KEY_SCROLL_PIXELS 10
56 #define SCROLL_TIME 100
57 #define FREEZE_LENGTH 1024
58 /* Freeze text when inserting or deleting more than this many characters */
60 #define SET_PROPERTY_MARK(m, p, o) do { \
61 (m)->property = (p); \
64 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
65 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
66 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
67 (mark)->property->prev->data \
69 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
70 #define MARK_LIST_PTR(mark) ((mark)->property)
71 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
72 #define MARK_OFFSET(mark) ((mark)->offset)
73 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
76 #define MARK_CURRENT_FONT(text, mark) \
77 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
78 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
79 gtk_style_get_font (GTK_WIDGET (text)->style))
80 #define MARK_CURRENT_FORE(text, mark) \
81 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
82 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
83 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
84 #define MARK_CURRENT_BACK(text, mark) \
85 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
86 &MARK_CURRENT_PROPERTY(mark)->back_color : \
87 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
88 #define MARK_CURRENT_TEXT_FONT(text, mark) \
89 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
90 MARK_CURRENT_PROPERTY(mark)->font : \
93 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
94 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
95 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
96 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
97 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
98 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
99 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
100 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
110 typedef struct _TextProperty TextProperty;
111 typedef struct _TabStopMark TabStopMark;
112 typedef struct _PrevTabCont PrevTabCont;
113 typedef struct _FetchLinesData FetchLinesData;
114 typedef struct _LineParams LineParams;
115 typedef struct _SetVerticalScrollData SetVerticalScrollData;
117 typedef gint (*LineIteratorFunction) (GtkText* text, LineParams* lp, void* data);
125 struct _SetVerticalScrollData {
127 gint last_didnt_wrap;
128 gint last_line_start;
129 GtkPropertyMark mark;
134 /* The actual font. */
138 gint16 char_widths[256];
142 PROPERTY_FONT = 1 << 0,
143 PROPERTY_FOREGROUND = 1 << 1,
144 PROPERTY_BACKGROUND = 1 << 2
152 /* Background Color. */
155 /* Foreground Color. */
158 /* Show which properties are set */
159 TextPropertyFlags flags;
161 /* Length of this property. */
167 GList* tab_stops; /* Index into list containing the next tab position. If
168 * NULL, using default widths. */
175 TabStopMark tab_start;
178 struct _FetchLinesData
191 guint displayable_chars;
194 PrevTabCont tab_cont;
195 PrevTabCont tab_cont_next;
197 GtkPropertyMark start;
202 static void gtk_text_class_init (GtkTextClass *klass);
203 static void gtk_text_set_property (GObject *object,
207 static void gtk_text_get_property (GObject *object,
211 static void gtk_text_editable_init (GtkEditableClass *iface);
212 static void gtk_text_init (GtkText *text);
213 static void gtk_text_destroy (GtkObject *object);
214 static void gtk_text_finalize (GObject *object);
215 static void gtk_text_realize (GtkWidget *widget);
216 static void gtk_text_unrealize (GtkWidget *widget);
217 static void gtk_text_style_set (GtkWidget *widget,
218 GtkStyle *previous_style);
219 static void gtk_text_state_changed (GtkWidget *widget,
220 GtkStateType previous_state);
221 static void gtk_text_draw_focus (GtkWidget *widget);
222 static void gtk_text_size_request (GtkWidget *widget,
223 GtkRequisition *requisition);
224 static void gtk_text_size_allocate (GtkWidget *widget,
225 GtkAllocation *allocation);
226 static void gtk_text_adjustment (GtkAdjustment *adjustment,
228 static void gtk_text_insert_text (GtkEditable *editable,
229 const gchar *new_text,
230 gint new_text_length,
232 static void gtk_text_delete_text (GtkEditable *editable,
235 static void gtk_text_update_text (GtkOldEditable *old_editable,
238 static gchar *gtk_text_get_chars (GtkOldEditable *old_editable,
241 static void gtk_text_set_selection (GtkOldEditable *old_editable,
244 static void gtk_text_real_set_editable (GtkOldEditable *old_editable,
245 gboolean is_editable);
247 static void gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
251 static gint gtk_text_expose (GtkWidget *widget,
252 GdkEventExpose *event);
253 static gint gtk_text_button_press (GtkWidget *widget,
254 GdkEventButton *event);
255 static gint gtk_text_button_release (GtkWidget *widget,
256 GdkEventButton *event);
257 static gint gtk_text_motion_notify (GtkWidget *widget,
258 GdkEventMotion *event);
259 static gint gtk_text_key_press (GtkWidget *widget,
262 static void move_gap (GtkText* text, guint index);
263 static void make_forward_space (GtkText* text, guint len);
265 /* Property management */
266 static GtkTextFont* get_text_font (GdkFont* gfont);
267 static void text_font_unref (GtkTextFont *text_font);
269 static void insert_text_property (GtkText* text, GdkFont* font,
270 const GdkColor *fore, const GdkColor* back, guint len);
271 static TextProperty* new_text_property (GtkText *text, GdkFont* font,
272 const GdkColor* fore, const GdkColor* back, guint length);
273 static void destroy_text_property (TextProperty *prop);
274 static void init_properties (GtkText *text);
275 static void realize_property (GtkText *text, TextProperty *prop);
276 static void realize_properties (GtkText *text);
277 static void unrealize_property (GtkText *text, TextProperty *prop);
278 static void unrealize_properties (GtkText *text);
280 static void delete_text_property (GtkText* text, guint len);
282 static guint pixel_height_of (GtkText* text, GList* cache_line);
284 /* Property Movement and Size Computations */
285 static void advance_mark (GtkPropertyMark* mark);
286 static void decrement_mark (GtkPropertyMark* mark);
287 static void advance_mark_n (GtkPropertyMark* mark, gint n);
288 static void decrement_mark_n (GtkPropertyMark* mark, gint n);
289 static void move_mark_n (GtkPropertyMark* mark, gint n);
290 static GtkPropertyMark find_mark (GtkText* text, guint mark_position);
291 static GtkPropertyMark find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near);
292 static void find_line_containing_point (GtkText* text, guint point,
296 static void compute_lines_pixels (GtkText* text, guint char_count,
297 guint *lines, guint *pixels);
299 static gint total_line_height (GtkText* text,
302 static LineParams find_line_params (GtkText* text,
303 const GtkPropertyMark *mark,
304 const PrevTabCont *tab_cont,
305 PrevTabCont *next_cont);
306 static void recompute_geometry (GtkText* text);
307 static void insert_expose (GtkText* text, guint old_pixels, gint nchars, guint new_line_count);
308 static void delete_expose (GtkText* text,
312 static GdkGC *create_bg_gc (GtkText *text);
313 static void clear_area (GtkText *text, GdkRectangle *area);
314 static void draw_line (GtkText* text,
317 static void draw_line_wrap (GtkText* text,
319 static void draw_cursor (GtkText* text, gint absolute);
320 static void undraw_cursor (GtkText* text, gint absolute);
321 static gint drawn_cursor_min (GtkText* text);
322 static gint drawn_cursor_max (GtkText* text);
323 static void expose_text (GtkText* text, GdkRectangle *area, gboolean cursor);
325 /* Search and Placement. */
326 static void find_cursor (GtkText* text,
328 static void find_cursor_at_line (GtkText* text,
329 const LineParams* start_line,
331 static void find_mouse_cursor (GtkText* text, gint x, gint y);
334 static void adjust_adj (GtkText* text, GtkAdjustment* adj);
335 static void scroll_up (GtkText* text, gint diff);
336 static void scroll_down (GtkText* text, gint diff);
337 static void scroll_int (GtkText* text, gint diff);
339 static void process_exposes (GtkText *text);
341 /* Cache Management. */
342 static void free_cache (GtkText* text);
343 static GList* remove_cache_line (GtkText* text, GList* list);
346 static void move_cursor_buffer_ver (GtkText *text, int dir);
347 static void move_cursor_page_ver (GtkText *text, int dir);
348 static void move_cursor_ver (GtkText *text, int count);
349 static void move_cursor_hor (GtkText *text, int count);
351 /* Binding actions */
352 static void gtk_text_move_cursor (GtkOldEditable *old_editable,
355 static void gtk_text_move_word (GtkOldEditable *old_editable,
357 static void gtk_text_move_page (GtkOldEditable *old_editable,
360 static void gtk_text_move_to_row (GtkOldEditable *old_editable,
362 static void gtk_text_move_to_column (GtkOldEditable *old_editable,
364 static void gtk_text_kill_char (GtkOldEditable *old_editable,
366 static void gtk_text_kill_word (GtkOldEditable *old_editable,
368 static void gtk_text_kill_line (GtkOldEditable *old_editable,
372 static void gtk_text_move_forward_character (GtkText *text);
373 static void gtk_text_move_backward_character (GtkText *text);
374 static void gtk_text_move_forward_word (GtkText *text);
375 static void gtk_text_move_backward_word (GtkText *text);
376 static void gtk_text_move_beginning_of_line (GtkText *text);
377 static void gtk_text_move_end_of_line (GtkText *text);
378 static void gtk_text_move_next_line (GtkText *text);
379 static void gtk_text_move_previous_line (GtkText *text);
381 static void gtk_text_delete_forward_character (GtkText *text);
382 static void gtk_text_delete_backward_character (GtkText *text);
383 static void gtk_text_delete_forward_word (GtkText *text);
384 static void gtk_text_delete_backward_word (GtkText *text);
385 static void gtk_text_delete_line (GtkText *text);
386 static void gtk_text_delete_to_line_end (GtkText *text);
387 static void gtk_text_select_word (GtkText *text,
389 static void gtk_text_select_line (GtkText *text,
392 static void gtk_text_set_position (GtkOldEditable *old_editable,
395 /* #define DEBUG_GTK_TEXT */
397 #if defined(DEBUG_GTK_TEXT) && defined(__GNUC__)
398 /* Debugging utilities. */
399 static void gtk_text_assert_mark (GtkText *text,
400 GtkPropertyMark *mark,
401 GtkPropertyMark *before,
402 GtkPropertyMark *after,
407 static void gtk_text_assert (GtkText *text,
410 static void gtk_text_show_cache_line (GtkText *text, GList *cache,
411 const char* what, const char* func, gint line);
412 static void gtk_text_show_cache (GtkText *text, const char* func, gint line);
413 static void gtk_text_show_adj (GtkText *text,
418 static void gtk_text_show_props (GtkText* test,
422 #define TDEBUG(args) g_message args
423 #define TEXT_ASSERT(text) gtk_text_assert (text,__PRETTY_FUNCTION__,__LINE__)
424 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_text_assert_mark (text,mark, \
425 __PRETTY_FUNCTION__,msg,__LINE__)
426 #define TEXT_SHOW(text) gtk_text_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
427 #define TEXT_SHOW_LINE(text,line,msg) gtk_text_show_cache_line (text,line,msg,\
428 __PRETTY_FUNCTION__,__LINE__)
429 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_text_show_adj (text,adj,msg, \
430 __PRETTY_FUNCTION__,__LINE__)
433 #define TEXT_ASSERT(text)
434 #define TEXT_ASSERT_MARK(text,mark,msg)
435 #define TEXT_SHOW(text)
436 #define TEXT_SHOW_LINE(text,line,msg)
437 #define TEXT_SHOW_ADJ(text,adj,msg)
440 /* Memory Management. */
441 static GMemChunk *params_mem_chunk = NULL;
442 static GMemChunk *text_property_chunk = NULL;
444 static GtkWidgetClass *parent_class = NULL;
447 static const GtkTextFunction control_keys[26] =
449 (GtkTextFunction)gtk_text_move_beginning_of_line, /* a */
450 (GtkTextFunction)gtk_text_move_backward_character, /* b */
451 (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
452 (GtkTextFunction)gtk_text_delete_forward_character, /* d */
453 (GtkTextFunction)gtk_text_move_end_of_line, /* e */
454 (GtkTextFunction)gtk_text_move_forward_character, /* f */
456 (GtkTextFunction)gtk_text_delete_backward_character, /* h */
459 (GtkTextFunction)gtk_text_delete_to_line_end, /* k */
462 (GtkTextFunction)gtk_text_move_next_line, /* n */
464 (GtkTextFunction)gtk_text_move_previous_line, /* p */
469 (GtkTextFunction)gtk_text_delete_line, /* u */
470 (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
471 (GtkTextFunction)gtk_text_delete_backward_word, /* w */
472 (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
477 static const GtkTextFunction alt_keys[26] =
480 (GtkTextFunction)gtk_text_move_backward_word, /* b */
482 (GtkTextFunction)gtk_text_delete_forward_word, /* d */
484 (GtkTextFunction)gtk_text_move_forward_word, /* f */
508 /**********************************************************************/
510 /**********************************************************************/
513 gtk_text_get_type (void)
515 static GtkType text_type = 0;
519 static const GtkTypeInfo text_info =
523 sizeof (GtkTextClass),
524 (GtkClassInitFunc) gtk_text_class_init,
525 (GtkObjectInitFunc) gtk_text_init,
526 /* reserved_1 */ NULL,
527 /* reserved_2 */ NULL,
528 (GtkClassInitFunc) NULL,
531 static const GInterfaceInfo editable_info =
533 (GInterfaceInitFunc) gtk_text_editable_init, /* interface_init */
534 NULL, /* interface_finalize */
535 NULL /* interface_data */
538 text_type = gtk_type_unique (GTK_TYPE_OLD_EDITABLE, &text_info);
539 g_type_add_interface_static (text_type,
548 gtk_text_class_init (GtkTextClass *class)
550 GObjectClass *gobject_class;
551 GtkObjectClass *object_class;
552 GtkWidgetClass *widget_class;
553 GtkOldEditableClass *old_editable_class;
555 gobject_class = G_OBJECT_CLASS (class);
556 object_class = (GtkObjectClass*) class;
557 widget_class = (GtkWidgetClass*) class;
558 old_editable_class = (GtkOldEditableClass*) class;
559 parent_class = gtk_type_class (GTK_TYPE_OLD_EDITABLE);
561 gobject_class->finalize = gtk_text_finalize;
562 gobject_class->set_property = gtk_text_set_property;
563 gobject_class->get_property = gtk_text_get_property;
565 object_class->destroy = gtk_text_destroy;
567 widget_class->realize = gtk_text_realize;
568 widget_class->unrealize = gtk_text_unrealize;
569 widget_class->style_set = gtk_text_style_set;
570 widget_class->state_changed = gtk_text_state_changed;
571 widget_class->size_request = gtk_text_size_request;
572 widget_class->size_allocate = gtk_text_size_allocate;
573 widget_class->expose_event = gtk_text_expose;
574 widget_class->button_press_event = gtk_text_button_press;
575 widget_class->button_release_event = gtk_text_button_release;
576 widget_class->motion_notify_event = gtk_text_motion_notify;
577 widget_class->key_press_event = gtk_text_key_press;
579 old_editable_class->set_editable = gtk_text_real_set_editable;
581 old_editable_class->move_cursor = gtk_text_move_cursor;
582 old_editable_class->move_word = gtk_text_move_word;
583 old_editable_class->move_page = gtk_text_move_page;
584 old_editable_class->move_to_row = gtk_text_move_to_row;
585 old_editable_class->move_to_column = gtk_text_move_to_column;
587 old_editable_class->kill_char = gtk_text_kill_char;
588 old_editable_class->kill_word = gtk_text_kill_word;
589 old_editable_class->kill_line = gtk_text_kill_line;
591 old_editable_class->update_text = gtk_text_update_text;
592 old_editable_class->get_chars = gtk_text_get_chars;
593 old_editable_class->set_selection = gtk_text_set_selection;
594 old_editable_class->set_position = gtk_text_set_position;
596 class->set_scroll_adjustments = gtk_text_set_adjustments;
598 g_object_class_install_property (gobject_class,
600 g_param_spec_object ("hadjustment",
601 P_("Horizontal Adjustment"),
602 P_("Horizontal adjustment for the text widget"),
606 g_object_class_install_property (gobject_class,
608 g_param_spec_object ("vadjustment",
609 P_("Vertical Adjustment"),
610 P_("Vertical adjustment for the text widget"),
614 g_object_class_install_property (gobject_class,
616 g_param_spec_boolean ("line_wrap",
618 P_("Whether lines are wrapped at widget edges"),
622 g_object_class_install_property (gobject_class,
624 g_param_spec_boolean ("word_wrap",
626 P_("Whether words are wrapped at widget edges"),
630 widget_class->set_scroll_adjustments_signal =
631 gtk_signal_new ("set_scroll_adjustments",
633 GTK_CLASS_TYPE (object_class),
634 GTK_SIGNAL_OFFSET (GtkTextClass, set_scroll_adjustments),
635 _gtk_marshal_VOID__OBJECT_OBJECT,
636 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
640 gtk_text_set_property (GObject *object,
647 text = GTK_TEXT (object);
651 case PROP_HADJUSTMENT:
652 gtk_text_set_adjustments (text,
653 g_value_get_object (value),
656 case PROP_VADJUSTMENT:
657 gtk_text_set_adjustments (text,
659 g_value_get_object (value));
662 gtk_text_set_line_wrap (text, g_value_get_boolean (value));
665 gtk_text_set_word_wrap (text, g_value_get_boolean (value));
668 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
674 gtk_text_get_property (GObject *object,
681 text = GTK_TEXT (object);
685 case PROP_HADJUSTMENT:
686 g_value_set_object (value, text->hadj);
688 case PROP_VADJUSTMENT:
689 g_value_set_object (value, text->vadj);
692 g_value_set_boolean (value, text->line_wrap);
695 g_value_set_boolean (value, text->word_wrap);
698 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
704 gtk_text_editable_init (GtkEditableClass *iface)
706 iface->insert_text = gtk_text_insert_text;
707 iface->delete_text = gtk_text_delete_text;
711 gtk_text_init (GtkText *text)
713 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
715 text->text_area = NULL;
720 text->line_wrap_bitmap = NULL;
721 text->line_arrow_bitmap = NULL;
723 text->use_wchar = FALSE;
724 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
725 text->text_len = INITIAL_BUFFER_SIZE;
727 text->scratch_buffer.ch = NULL;
728 text->scratch_buffer_len = 0;
730 text->freeze_count = 0;
732 if (!params_mem_chunk)
733 params_mem_chunk = g_mem_chunk_new ("LineParams",
735 256 * sizeof (LineParams),
738 text->default_tab_width = 4;
739 text->tab_stops = NULL;
741 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
742 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
744 text->line_start_cache = NULL;
745 text->first_cut_pixels = 0;
747 text->line_wrap = TRUE;
748 text->word_wrap = FALSE;
753 text->current_font = NULL;
755 init_properties (text);
757 GTK_OLD_EDITABLE (text)->editable = FALSE;
759 gtk_text_set_adjustments (text, NULL, NULL);
760 gtk_editable_set_position (GTK_EDITABLE (text), 0);
764 gtk_text_new (GtkAdjustment *hadj,
770 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
772 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
774 text = gtk_widget_new (GTK_TYPE_TEXT,
783 gtk_text_set_word_wrap (GtkText *text,
786 g_return_if_fail (GTK_IS_TEXT (text));
788 text->word_wrap = (word_wrap != FALSE);
790 if (GTK_WIDGET_REALIZED (text))
792 recompute_geometry (text);
793 gtk_widget_queue_draw (GTK_WIDGET (text));
796 g_object_notify (G_OBJECT (text), "word_wrap");
800 gtk_text_set_line_wrap (GtkText *text,
803 g_return_if_fail (GTK_IS_TEXT (text));
805 text->line_wrap = (line_wrap != FALSE);
807 if (GTK_WIDGET_REALIZED (text))
809 recompute_geometry (text);
810 gtk_widget_queue_draw (GTK_WIDGET (text));
813 g_object_notify (G_OBJECT (text), "line_wrap");
817 gtk_text_set_editable (GtkText *text,
818 gboolean is_editable)
820 g_return_if_fail (GTK_IS_TEXT (text));
822 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
826 gtk_text_real_set_editable (GtkOldEditable *old_editable,
827 gboolean is_editable)
831 g_return_if_fail (GTK_IS_TEXT (old_editable));
833 text = GTK_TEXT (old_editable);
835 old_editable->editable = (is_editable != FALSE);
838 draw_cursor (text, TRUE);
840 undraw_cursor (text, TRUE);
844 gtk_text_set_adjustments (GtkText *text,
848 g_return_if_fail (GTK_IS_TEXT (text));
850 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
852 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
854 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
856 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
858 if (text->hadj && (text->hadj != hadj))
860 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
861 gtk_object_unref (GTK_OBJECT (text->hadj));
864 if (text->vadj && (text->vadj != vadj))
866 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
867 gtk_object_unref (GTK_OBJECT (text->vadj));
870 g_object_freeze_notify (G_OBJECT (text));
871 if (text->hadj != hadj)
874 gtk_object_ref (GTK_OBJECT (text->hadj));
875 gtk_object_sink (GTK_OBJECT (text->hadj));
877 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
878 (GtkSignalFunc) gtk_text_adjustment,
880 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
881 (GtkSignalFunc) gtk_text_adjustment,
883 gtk_signal_connect (GTK_OBJECT (text->hadj), "destroy",
884 (GtkSignalFunc) gtk_text_adjustment_destroyed,
886 gtk_text_adjustment (hadj, text);
888 g_object_notify (G_OBJECT (text), "hadjustment");
891 if (text->vadj != vadj)
894 gtk_object_ref (GTK_OBJECT (text->vadj));
895 gtk_object_sink (GTK_OBJECT (text->vadj));
897 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
898 (GtkSignalFunc) gtk_text_adjustment,
900 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
901 (GtkSignalFunc) gtk_text_adjustment,
903 gtk_signal_connect (GTK_OBJECT (text->vadj), "destroy",
904 (GtkSignalFunc) gtk_text_adjustment_destroyed,
906 gtk_text_adjustment (vadj, text);
908 g_object_notify (G_OBJECT (text), "vadjustment");
910 g_object_thaw_notify (G_OBJECT (text));
914 gtk_text_set_point (GtkText *text,
917 g_return_if_fail (GTK_IS_TEXT (text));
918 g_return_if_fail (index <= TEXT_LENGTH (text));
920 text->point = find_mark (text, index);
924 gtk_text_get_point (GtkText *text)
926 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
928 return text->point.index;
932 gtk_text_get_length (GtkText *text)
934 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
936 return TEXT_LENGTH (text);
940 gtk_text_freeze (GtkText *text)
942 g_return_if_fail (GTK_IS_TEXT (text));
944 text->freeze_count++;
948 gtk_text_thaw (GtkText *text)
950 g_return_if_fail (GTK_IS_TEXT (text));
952 if (text->freeze_count)
953 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
955 recompute_geometry (text);
956 gtk_widget_queue_draw (GTK_WIDGET (text));
961 gtk_text_insert (GtkText *text,
963 const GdkColor *fore,
964 const GdkColor *back,
968 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
969 gboolean frozen = FALSE;
971 gint new_line_count = 1;
972 guint old_height = 0;
977 g_return_if_fail (GTK_IS_TEXT (text));
980 length = strlen (chars);
987 if (!text->freeze_count && (length > FREEZE_LENGTH))
989 gtk_text_freeze (text);
993 if (!text->freeze_count && (text->line_start_cache != NULL))
995 find_line_containing_point (text, text->point.index, TRUE);
996 old_height = total_line_height (text, text->current_line, 1);
999 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
1001 GtkWidget *widget = GTK_WIDGET (text);
1003 gtk_widget_ensure_style (widget);
1004 if ((widget->style) &&
1005 (gtk_style_get_font (widget->style)->type == GDK_FONT_FONTSET))
1007 text->use_wchar = TRUE;
1008 g_free (text->text.ch);
1009 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
1010 text->text_len = INITIAL_BUFFER_SIZE;
1011 if (text->scratch_buffer.ch)
1012 g_free (text->scratch_buffer.ch);
1013 text->scratch_buffer.wc = NULL;
1014 text->scratch_buffer_len = 0;
1018 move_gap (text, text->point.index);
1019 make_forward_space (text, length);
1021 if (text->use_wchar)
1023 char *chars_nt = (char *)chars;
1026 chars_nt = g_new (char, length+1);
1027 memcpy (chars_nt, chars, length);
1028 chars_nt[length] = 0;
1030 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
1032 if (chars_nt != chars)
1040 memcpy(text->text.ch + text->gap_position, chars, length);
1043 if (!text->freeze_count && (text->line_start_cache != NULL))
1045 if (text->use_wchar)
1047 for (i=0; i<numwcs; i++)
1048 if (text->text.wc[text->gap_position + i] == '\n')
1053 for (i=0; i<numwcs; i++)
1054 if (text->text.ch[text->gap_position + i] == '\n')
1061 insert_text_property (text, font, fore, back, numwcs);
1063 text->gap_size -= numwcs;
1064 text->gap_position += numwcs;
1066 if (text->point.index < text->first_line_start_index)
1067 text->first_line_start_index += numwcs;
1068 if (text->point.index < old_editable->selection_start_pos)
1069 old_editable->selection_start_pos += numwcs;
1070 if (text->point.index < old_editable->selection_end_pos)
1071 old_editable->selection_end_pos += numwcs;
1072 /* We'll reset the cursor later anyways if we aren't frozen */
1073 if (text->point.index < text->cursor_mark.index)
1074 text->cursor_mark.index += numwcs;
1076 advance_mark_n (&text->point, numwcs);
1078 if (!text->freeze_count && (text->line_start_cache != NULL))
1079 insert_expose (text, old_height, numwcs, new_line_count);
1083 gtk_text_thaw (text);
1087 gtk_text_backward_delete (GtkText *text,
1090 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1092 if (nchars > text->point.index || nchars <= 0)
1095 gtk_text_set_point (text, text->point.index - nchars);
1097 return gtk_text_forward_delete (text, nchars);
1101 gtk_text_forward_delete (GtkText *text,
1104 guint old_lines, old_height;
1105 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
1106 gboolean frozen = FALSE;
1108 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1110 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1113 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1115 gtk_text_freeze (text);
1119 if (!text->freeze_count && text->line_start_cache != NULL)
1121 /* We need to undraw the cursor here, since we may later
1122 * delete the cursor's property
1124 undraw_cursor (text, FALSE);
1125 find_line_containing_point (text, text->point.index, TRUE);
1126 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1129 /* FIXME, or resizing after deleting will be odd */
1130 if (text->point.index < text->first_line_start_index)
1132 if (text->point.index + nchars >= text->first_line_start_index)
1134 text->first_line_start_index = text->point.index;
1135 while ((text->first_line_start_index > 0) &&
1136 (GTK_TEXT_INDEX (text, text->first_line_start_index - 1)
1138 text->first_line_start_index -= 1;
1142 text->first_line_start_index -= nchars;
1145 if (text->point.index < old_editable->selection_start_pos)
1146 old_editable->selection_start_pos -=
1147 MIN(nchars, old_editable->selection_start_pos - text->point.index);
1148 if (text->point.index < old_editable->selection_end_pos)
1149 old_editable->selection_end_pos -=
1150 MIN(nchars, old_editable->selection_end_pos - text->point.index);
1151 /* We'll reset the cursor later anyways if we aren't frozen */
1152 if (text->point.index < text->cursor_mark.index)
1153 move_mark_n (&text->cursor_mark,
1154 -MIN(nchars, text->cursor_mark.index - text->point.index));
1156 move_gap (text, text->point.index);
1158 text->gap_size += nchars;
1160 delete_text_property (text, nchars);
1162 if (!text->freeze_count && (text->line_start_cache != NULL))
1164 delete_expose (text, nchars, old_lines, old_height);
1165 draw_cursor (text, FALSE);
1169 gtk_text_thaw (text);
1175 gtk_text_set_position (GtkOldEditable *old_editable,
1178 GtkText *text = (GtkText *) old_editable;
1181 position = gtk_text_get_length (text);
1183 undraw_cursor (text, FALSE);
1184 text->cursor_mark = find_mark (text, position);
1185 find_cursor (text, TRUE);
1186 draw_cursor (text, FALSE);
1187 gtk_editable_select_region (GTK_EDITABLE (old_editable), 0, 0);
1191 gtk_text_get_chars (GtkOldEditable *old_editable,
1199 g_return_val_if_fail (GTK_IS_TEXT (old_editable), NULL);
1200 text = GTK_TEXT (old_editable);
1203 end_pos = TEXT_LENGTH (text);
1205 if ((start_pos < 0) ||
1206 (end_pos > TEXT_LENGTH (text)) ||
1207 (end_pos < start_pos))
1210 move_gap (text, TEXT_LENGTH (text));
1211 make_forward_space (text, 1);
1213 if (text->use_wchar)
1216 ch = text->text.wc[end_pos];
1217 text->text.wc[end_pos] = 0;
1218 retval = gdk_wcstombs (text->text.wc + start_pos);
1219 text->text.wc[end_pos] = ch;
1224 ch = text->text.ch[end_pos];
1225 text->text.ch[end_pos] = 0;
1226 retval = g_strdup (text->text.ch + start_pos);
1227 text->text.ch[end_pos] = ch;
1235 gtk_text_destroy (GtkObject *object)
1239 g_return_if_fail (GTK_IS_TEXT (object));
1241 text = GTK_TEXT (object);
1245 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1246 gtk_object_unref (GTK_OBJECT (text->hadj));
1251 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1252 gtk_object_unref (GTK_OBJECT (text->vadj));
1258 g_source_remove (text->timer);
1262 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1266 gtk_text_finalize (GObject *object)
1271 g_return_if_fail (GTK_IS_TEXT (object));
1273 text = GTK_TEXT (object);
1275 /* Clean up the internal structures */
1276 if (text->use_wchar)
1277 g_free (text->text.wc);
1279 g_free (text->text.ch);
1281 tmp_list = text->text_properties;
1284 destroy_text_property (tmp_list->data);
1285 tmp_list = tmp_list->next;
1288 if (text->current_font)
1289 text_font_unref (text->current_font);
1291 g_list_free (text->text_properties);
1293 if (text->use_wchar)
1295 if (text->scratch_buffer.wc)
1296 g_free (text->scratch_buffer.wc);
1300 if (text->scratch_buffer.ch)
1301 g_free (text->scratch_buffer.ch);
1304 g_list_free (text->tab_stops);
1306 G_OBJECT_CLASS (parent_class)->finalize (object);
1310 gtk_text_realize (GtkWidget *widget)
1313 GtkOldEditable *old_editable;
1314 GdkWindowAttr attributes;
1315 gint attributes_mask;
1317 g_return_if_fail (GTK_IS_TEXT (widget));
1319 text = GTK_TEXT (widget);
1320 old_editable = GTK_OLD_EDITABLE (widget);
1321 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1323 attributes.window_type = GDK_WINDOW_CHILD;
1324 attributes.x = widget->allocation.x;
1325 attributes.y = widget->allocation.y;
1326 attributes.width = widget->allocation.width;
1327 attributes.height = widget->allocation.height;
1328 attributes.wclass = GDK_INPUT_OUTPUT;
1329 attributes.visual = gtk_widget_get_visual (widget);
1330 attributes.colormap = gtk_widget_get_colormap (widget);
1331 attributes.event_mask = gtk_widget_get_events (widget);
1332 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1333 GDK_BUTTON_PRESS_MASK |
1334 GDK_BUTTON_RELEASE_MASK |
1335 GDK_BUTTON_MOTION_MASK |
1336 GDK_ENTER_NOTIFY_MASK |
1337 GDK_LEAVE_NOTIFY_MASK |
1338 GDK_KEY_PRESS_MASK);
1339 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1341 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1342 gdk_window_set_user_data (widget->window, text);
1344 attributes.x = (widget->style->xthickness + TEXT_BORDER_ROOM);
1345 attributes.y = (widget->style->ythickness + TEXT_BORDER_ROOM);
1346 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1347 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1349 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1350 attributes_mask |= GDK_WA_CURSOR;
1352 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1353 gdk_window_set_user_data (text->text_area, text);
1355 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1357 widget->style = gtk_style_attach (widget->style, widget->window);
1359 /* Can't call gtk_style_set_background here because it's handled specially */
1360 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1361 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1363 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1364 text->bg_gc = create_bg_gc (text);
1366 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1367 (gchar*) line_wrap_bits,
1371 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1372 (gchar*) line_arrow_bits,
1376 text->gc = gdk_gc_new (text->text_area);
1377 gdk_gc_set_exposures (text->gc, TRUE);
1378 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1380 realize_properties (text);
1381 gdk_window_show (text->text_area);
1382 init_properties (text);
1384 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1385 gtk_old_editable_claim_selection (old_editable, TRUE, GDK_CURRENT_TIME);
1387 recompute_geometry (text);
1391 gtk_text_style_set (GtkWidget *widget,
1392 GtkStyle *previous_style)
1394 GtkText *text = GTK_TEXT (widget);
1396 if (GTK_WIDGET_REALIZED (widget))
1398 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1399 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1403 gdk_gc_destroy (text->bg_gc);
1407 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1408 text->bg_gc = create_bg_gc (text);
1410 recompute_geometry (text);
1413 if (text->current_font)
1414 text_font_unref (text->current_font);
1415 text->current_font = get_text_font (gtk_style_get_font (widget->style));
1419 gtk_text_state_changed (GtkWidget *widget,
1420 GtkStateType previous_state)
1422 GtkText *text = GTK_TEXT (widget);
1424 if (GTK_WIDGET_REALIZED (widget))
1426 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1427 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1432 gtk_text_unrealize (GtkWidget *widget)
1436 g_return_if_fail (GTK_IS_TEXT (widget));
1438 text = GTK_TEXT (widget);
1440 gdk_window_set_user_data (text->text_area, NULL);
1441 gdk_window_destroy (text->text_area);
1442 text->text_area = NULL;
1444 gdk_gc_destroy (text->gc);
1449 gdk_gc_destroy (text->bg_gc);
1453 gdk_pixmap_unref (text->line_wrap_bitmap);
1454 gdk_pixmap_unref (text->line_arrow_bitmap);
1456 unrealize_properties (text);
1460 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1461 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1465 clear_focus_area (GtkText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1467 GtkWidget *widget = GTK_WIDGET (text);
1470 gint ythick = TEXT_BORDER_ROOM + widget->style->ythickness;
1471 gint xthick = TEXT_BORDER_ROOM + widget->style->xthickness;
1475 if (area_width == 0 || area_height == 0)
1478 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1480 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1482 gdk_gc_set_ts_origin (text->bg_gc,
1483 (- text->first_onscreen_hor_pixel + xthick) % width,
1484 (- text->first_onscreen_ver_pixel + ythick) % height);
1489 gc = widget->style->base_gc[widget->state];
1491 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1492 area_x, area_y, area_width, area_height);
1496 gtk_text_draw_focus (GtkWidget *widget)
1502 g_return_if_fail (GTK_IS_TEXT (widget));
1504 text = GTK_TEXT (widget);
1506 if (GTK_WIDGET_DRAWABLE (widget))
1508 gint ythick = widget->style->ythickness;
1509 gint xthick = widget->style->xthickness;
1510 gint xextra = TEXT_BORDER_ROOM;
1511 gint yextra = TEXT_BORDER_ROOM;
1513 TDEBUG (("in gtk_text_draw_focus\n"));
1517 width = widget->allocation.width;
1518 height = widget->allocation.height;
1520 if (GTK_WIDGET_HAS_FOCUS (widget))
1529 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1530 NULL, widget, "text",
1532 widget->allocation.width,
1533 widget->allocation.height);
1536 gtk_paint_shadow (widget->style, widget->window,
1537 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1538 NULL, widget, "text",
1539 x, y, width, height);
1543 width -= 2 * xthick;
1544 height -= 2 * ythick;
1547 clear_focus_area (text, x, y, width, yextra);
1549 clear_focus_area (text, x, y + yextra,
1550 xextra, y + height - 2 * yextra);
1552 clear_focus_area (text, x + width - xextra, y + yextra,
1553 xextra, height - 2 * ythick);
1555 clear_focus_area (text, x, x + height - yextra, width, yextra);
1559 TDEBUG (("in gtk_text_draw_focus (undrawable !!!)\n"));
1564 gtk_text_size_request (GtkWidget *widget,
1565 GtkRequisition *requisition)
1573 g_return_if_fail (GTK_IS_TEXT (widget));
1574 g_return_if_fail (requisition != NULL);
1576 xthickness = widget->style->xthickness + TEXT_BORDER_ROOM;
1577 ythickness = widget->style->ythickness + TEXT_BORDER_ROOM;
1579 font = gtk_style_get_font (widget->style);
1581 char_height = MIN_TEXT_HEIGHT_LINES * (font->ascent +
1584 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (font,
1585 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1589 requisition->width = char_width + xthickness * 2;
1590 requisition->height = char_height + ythickness * 2;
1594 gtk_text_size_allocate (GtkWidget *widget,
1595 GtkAllocation *allocation)
1598 GtkOldEditable *old_editable;
1600 g_return_if_fail (GTK_IS_TEXT (widget));
1601 g_return_if_fail (allocation != NULL);
1603 text = GTK_TEXT (widget);
1604 old_editable = GTK_OLD_EDITABLE (widget);
1606 widget->allocation = *allocation;
1607 if (GTK_WIDGET_REALIZED (widget))
1609 gdk_window_move_resize (widget->window,
1610 allocation->x, allocation->y,
1611 allocation->width, allocation->height);
1613 gdk_window_move_resize (text->text_area,
1614 widget->style->xthickness + TEXT_BORDER_ROOM,
1615 widget->style->ythickness + TEXT_BORDER_ROOM,
1616 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->xthickness +
1617 (gint)TEXT_BORDER_ROOM) * 2),
1618 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->ythickness +
1619 (gint)TEXT_BORDER_ROOM) * 2));
1621 recompute_geometry (text);
1626 gtk_text_expose (GtkWidget *widget,
1627 GdkEventExpose *event)
1629 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1630 g_return_val_if_fail (event != NULL, FALSE);
1632 if (event->window == GTK_TEXT (widget)->text_area)
1634 TDEBUG (("in gtk_text_expose (expose)\n"));
1635 expose_text (GTK_TEXT (widget), &event->area, TRUE);
1637 else if (event->count == 0)
1639 TDEBUG (("in gtk_text_expose (focus)\n"));
1640 gtk_text_draw_focus (widget);
1647 gtk_text_scroll_timeout (gpointer data)
1651 GdkModifierType mask;
1653 GDK_THREADS_ENTER ();
1655 text = GTK_TEXT (data);
1658 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1660 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1662 GdkEvent *event = gdk_event_new (GDK_MOTION_NOTIFY);
1664 event->motion.is_hint = 0;
1665 event->motion.x = x;
1666 event->motion.y = y;
1667 event->motion.state = mask;
1669 gtk_text_motion_notify (GTK_WIDGET (text), (GdkEventMotion *)event);
1671 gdk_event_free (event);
1674 GDK_THREADS_LEAVE ();
1680 gtk_text_button_press (GtkWidget *widget,
1681 GdkEventButton *event)
1684 GtkOldEditable *old_editable;
1686 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1687 g_return_val_if_fail (event != NULL, FALSE);
1689 text = GTK_TEXT (widget);
1690 old_editable = GTK_OLD_EDITABLE (widget);
1692 if (text->button && (event->button != text->button))
1695 text->button = event->button;
1697 if (!GTK_WIDGET_HAS_FOCUS (widget))
1698 gtk_widget_grab_focus (widget);
1700 if (event->button == 1)
1702 switch (event->type)
1704 case GDK_BUTTON_PRESS:
1705 gtk_grab_add (widget);
1707 undraw_cursor (text, FALSE);
1708 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1709 draw_cursor (text, FALSE);
1711 /* Set it now, so we display things right. We'll unset it
1712 * later if things don't work out */
1713 old_editable->has_selection = TRUE;
1714 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1715 text->cursor_mark.index,
1716 text->cursor_mark.index);
1720 case GDK_2BUTTON_PRESS:
1721 gtk_text_select_word (text, event->time);
1724 case GDK_3BUTTON_PRESS:
1725 gtk_text_select_line (text, event->time);
1732 else if (event->type == GDK_BUTTON_PRESS)
1734 if ((event->button == 2) && old_editable->editable)
1736 if (old_editable->selection_start_pos == old_editable->selection_end_pos ||
1737 old_editable->has_selection)
1739 undraw_cursor (text, FALSE);
1740 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1741 draw_cursor (text, FALSE);
1745 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1746 gdk_atom_intern ("UTF8_STRING", FALSE),
1751 GdkDisplay *display = gtk_widget_get_display (widget);
1753 gtk_grab_add (widget);
1755 undraw_cursor (text, FALSE);
1756 find_mouse_cursor (text, event->x, event->y);
1757 draw_cursor (text, FALSE);
1759 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1760 text->cursor_mark.index,
1761 text->cursor_mark.index);
1763 old_editable->has_selection = FALSE;
1764 if (gdk_selection_owner_get_for_display (display,
1765 GDK_SELECTION_PRIMARY) == widget->window)
1766 gtk_selection_owner_set_for_display (display,
1768 GDK_SELECTION_PRIMARY,
1777 gtk_text_button_release (GtkWidget *widget,
1778 GdkEventButton *event)
1781 GtkOldEditable *old_editable;
1782 GdkDisplay *display;
1784 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1785 g_return_val_if_fail (event != NULL, FALSE);
1787 text = GTK_TEXT (widget);
1789 gtk_grab_remove (widget);
1791 if (text->button != event->button)
1798 g_source_remove (text->timer);
1802 if (event->button == 1)
1804 text = GTK_TEXT (widget);
1805 old_editable = GTK_OLD_EDITABLE (widget);
1806 display = gtk_widget_get_display (widget);
1808 gtk_grab_remove (widget);
1810 old_editable->has_selection = FALSE;
1811 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1813 if (gtk_selection_owner_set_for_display (display,
1815 GDK_SELECTION_PRIMARY,
1817 old_editable->has_selection = TRUE;
1819 gtk_text_update_text (old_editable, old_editable->selection_start_pos,
1820 old_editable->selection_end_pos);
1824 if (gdk_selection_owner_get_for_display (display,
1825 GDK_SELECTION_PRIMARY) == widget->window)
1826 gtk_selection_owner_set_for_display (display,
1828 GDK_SELECTION_PRIMARY,
1832 else if (event->button == 3)
1834 gtk_grab_remove (widget);
1837 undraw_cursor (text, FALSE);
1838 find_cursor (text, TRUE);
1839 draw_cursor (text, FALSE);
1845 gtk_text_motion_notify (GtkWidget *widget,
1846 GdkEventMotion *event)
1851 GdkModifierType mask;
1853 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1854 g_return_val_if_fail (event != NULL, FALSE);
1856 text = GTK_TEXT (widget);
1860 mask = event->state;
1861 if (event->is_hint || (text->text_area != event->window))
1863 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1866 if ((text->button == 0) ||
1867 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1870 gdk_window_get_size (text->text_area, NULL, &height);
1872 if ((y < 0) || (y > height))
1874 if (text->timer == 0)
1876 text->timer = g_timeout_add (SCROLL_TIME,
1877 gtk_text_scroll_timeout,
1881 scroll_int (text, y/2);
1883 scroll_int (text, (y - height)/2);
1889 undraw_cursor (GTK_TEXT (widget), FALSE);
1890 find_mouse_cursor (GTK_TEXT (widget), x, y);
1891 draw_cursor (GTK_TEXT (widget), FALSE);
1893 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1894 GTK_OLD_EDITABLE (text)->selection_start_pos,
1895 text->cursor_mark.index);
1901 gtk_text_insert_text (GtkEditable *editable,
1902 const gchar *new_text,
1903 gint new_text_length,
1906 GtkText *text = GTK_TEXT (editable);
1908 GdkColor *fore, *back;
1910 TextProperty *property;
1912 gtk_text_set_point (text, *position);
1914 property = MARK_CURRENT_PROPERTY (&text->point);
1915 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
1916 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
1917 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
1919 gtk_text_insert (text, font, fore, back, new_text, new_text_length);
1921 *position = text->point.index;
1925 gtk_text_delete_text (GtkEditable *editable,
1931 g_return_if_fail (start_pos >= 0);
1933 text = GTK_TEXT (editable);
1935 gtk_text_set_point (text, start_pos);
1937 end_pos = TEXT_LENGTH (text);
1939 if (end_pos > start_pos)
1940 gtk_text_forward_delete (text, end_pos - start_pos);
1944 gtk_text_key_press (GtkWidget *widget,
1948 GtkOldEditable *old_editable;
1953 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1954 g_return_val_if_fail (event != NULL, FALSE);
1956 text = GTK_TEXT (widget);
1957 old_editable = GTK_OLD_EDITABLE (widget);
1959 key = event->keyval;
1962 if ((GTK_OLD_EDITABLE(text)->editable == FALSE))
1964 switch (event->keyval)
1968 if (event->state & GDK_CONTROL_MASK)
1969 scroll_int (text, -text->vadj->value);
1975 if (event->state & GDK_CONTROL_MASK)
1976 scroll_int (text, +text->vadj->upper);
1980 case GDK_KP_Page_Up:
1981 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
1982 case GDK_KP_Page_Down:
1983 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
1985 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
1987 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
1990 if (event->state & GDK_CONTROL_MASK)
1991 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2002 gint extend_selection;
2004 guint initial_pos = old_editable->current_pos;
2006 text->point = find_mark (text, text->cursor_mark.index);
2008 extend_selection = event->state & GDK_SHIFT_MASK;
2009 extend_start = FALSE;
2011 if (extend_selection)
2013 old_editable->has_selection = TRUE;
2015 if (old_editable->selection_start_pos == old_editable->selection_end_pos)
2017 old_editable->selection_start_pos = text->point.index;
2018 old_editable->selection_end_pos = text->point.index;
2021 extend_start = (text->point.index == old_editable->selection_start_pos);
2024 switch (event->keyval)
2028 if (event->state & GDK_CONTROL_MASK)
2029 move_cursor_buffer_ver (text, -1);
2031 gtk_text_move_beginning_of_line (text);
2035 if (event->state & GDK_CONTROL_MASK)
2036 move_cursor_buffer_ver (text, +1);
2038 gtk_text_move_end_of_line (text);
2040 case GDK_KP_Page_Up:
2041 case GDK_Page_Up: move_cursor_page_ver (text, -1); break;
2042 case GDK_KP_Page_Down:
2043 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
2044 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
2046 case GDK_Up: move_cursor_ver (text, -1); break;
2048 case GDK_Down: move_cursor_ver (text, +1); break;
2051 if (event->state & GDK_CONTROL_MASK)
2052 gtk_text_move_backward_word (text);
2054 move_cursor_hor (text, -1);
2058 if (event->state & GDK_CONTROL_MASK)
2059 gtk_text_move_forward_word (text);
2061 move_cursor_hor (text, +1);
2065 if (event->state & GDK_CONTROL_MASK)
2066 gtk_text_delete_backward_word (text);
2068 gtk_text_delete_backward_character (text);
2071 gtk_text_delete_line (text);
2075 if (event->state & GDK_SHIFT_MASK)
2077 extend_selection = FALSE;
2078 gtk_editable_paste_clipboard (GTK_EDITABLE (old_editable));
2080 else if (event->state & GDK_CONTROL_MASK)
2082 gtk_editable_copy_clipboard (GTK_EDITABLE (old_editable));
2086 /* gtk_toggle_insert(text) -- IMPLEMENT */
2091 if (event->state & GDK_CONTROL_MASK)
2092 gtk_text_delete_forward_word (text);
2093 else if (event->state & GDK_SHIFT_MASK)
2095 extend_selection = FALSE;
2096 gtk_editable_cut_clipboard (GTK_EDITABLE (old_editable));
2099 gtk_text_delete_forward_character (text);
2102 case GDK_ISO_Left_Tab:
2104 position = text->point.index;
2105 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\t", 1, &position);
2109 if (event->state & GDK_CONTROL_MASK)
2110 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2113 position = text->point.index;
2114 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\n", 1, &position);
2118 /* Don't insert literally */
2125 if (event->state & GDK_CONTROL_MASK)
2127 if ((key >= 'A') && (key <= 'Z'))
2130 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2132 (* control_keys[(int) (key - 'a')]) (old_editable, event->time);
2138 else if (event->state & GDK_MOD1_MASK)
2140 if ((key >= 'A') && (key <= 'Z'))
2143 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2145 (* alt_keys[(int) (key - 'a')]) (old_editable, event->time);
2151 else if (event->length > 0)
2153 extend_selection = FALSE;
2155 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
2156 position = text->point.index;
2157 gtk_editable_insert_text (GTK_EDITABLE (old_editable), event->string, event->length, &position);
2163 if (return_val && (old_editable->current_pos != initial_pos))
2165 if (extend_selection)
2167 if (old_editable->current_pos < old_editable->selection_start_pos)
2168 gtk_text_set_selection (old_editable, old_editable->current_pos,
2169 old_editable->selection_end_pos);
2170 else if (old_editable->current_pos > old_editable->selection_end_pos)
2171 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2172 old_editable->current_pos);
2176 gtk_text_set_selection (old_editable, old_editable->current_pos,
2177 old_editable->selection_end_pos);
2179 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2180 old_editable->current_pos);
2184 gtk_text_set_selection (old_editable, 0, 0);
2186 gtk_old_editable_claim_selection (old_editable,
2187 old_editable->selection_start_pos != old_editable->selection_end_pos,
2196 gtk_text_adjustment (GtkAdjustment *adjustment,
2199 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2200 g_return_if_fail (GTK_IS_TEXT (text));
2202 /* Just ignore it if we haven't been size-allocated and realized yet */
2203 if (text->line_start_cache == NULL)
2206 if (adjustment == text->hadj)
2208 g_warning ("horizontal scrolling not implemented");
2212 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2216 undraw_cursor (text, FALSE);
2219 scroll_down (text, diff);
2220 else /* if (diff < 0) */
2221 scroll_up (text, diff);
2223 draw_cursor (text, FALSE);
2225 text->last_ver_value = adjustment->value;
2231 gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
2234 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2235 g_return_if_fail (GTK_IS_TEXT (text));
2237 if (adjustment == text->hadj)
2238 gtk_text_set_adjustments (text, NULL, text->vadj);
2239 if (adjustment == text->vadj)
2240 gtk_text_set_adjustments (text, text->hadj, NULL);
2244 static GtkPropertyMark
2245 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2247 GtkPropertyMark mark;
2249 mark = find_mark_near (text, point_position, near);
2251 while (mark.index > 0 &&
2252 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2253 decrement_mark (&mark);
2259 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2261 tab_cont->pixel_offset = 0;
2262 tab_cont->tab_start.tab_stops = text->tab_stops;
2263 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2265 if (!tab_cont->tab_start.to_next_tab)
2266 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2270 line_params_iterate (GtkText* text,
2271 const GtkPropertyMark* mark0,
2272 const PrevTabCont* tab_mark0,
2275 LineIteratorFunction iter)
2276 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2277 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2278 * for each line following MARK, iteration continues unless ITER_CALL
2281 GtkPropertyMark mark = *mark0;
2282 PrevTabCont tab_conts[2];
2283 LineParams *lp, lpbuf;
2284 gint tab_cont_index = 0;
2287 tab_conts[0] = *tab_mark0;
2289 init_tab_cont (text, tab_conts);
2294 lp = g_chunk_new (LineParams, params_mem_chunk);
2298 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2299 tab_conts + (tab_cont_index + 1) % 2);
2301 if ((*iter) (text, lp, data))
2304 if (LAST_INDEX (text, lp->end))
2308 advance_mark (&mark);
2309 tab_cont_index = (tab_cont_index + 1) % 2;
2314 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2316 FetchLinesData *fldata = (FetchLinesData*) data;
2318 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2320 switch (fldata->fl_type)
2322 case FetchLinesCount:
2323 if (!text->line_wrap || !lp->wraps)
2326 if (fldata->data >= fldata->data_max)
2330 case FetchLinesPixels:
2332 fldata->data += LINE_HEIGHT(*lp);
2334 if (fldata->data >= fldata->data_max)
2344 fetch_lines (GtkText* text,
2345 const GtkPropertyMark* mark0,
2346 const PrevTabCont* tab_cont0,
2350 FetchLinesData fl_data;
2352 fl_data.new_lines = NULL;
2354 fl_data.data_max = data;
2355 fl_data.fl_type = fl_type;
2357 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2359 return g_list_reverse (fl_data.new_lines);
2363 fetch_lines_backward (GtkText* text)
2365 GList* new_lines = NULL, *new_line_start;
2366 GtkPropertyMark mark;
2368 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2371 mark = find_this_line_start_mark (text,
2372 CACHE_DATA(text->line_start_cache).start.index - 1,
2373 &CACHE_DATA(text->line_start_cache).start);
2375 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2377 while (new_line_start->next)
2378 new_line_start = new_line_start->next;
2380 new_line_start->next = text->line_start_cache;
2381 text->line_start_cache->prev = new_line_start;
2385 fetch_lines_forward (GtkText* text, gint line_count)
2387 GtkPropertyMark mark;
2388 GList* line = text->line_start_cache;
2393 mark = CACHE_DATA(line).end;
2395 if (LAST_INDEX (text, mark))
2398 advance_mark(&mark);
2400 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2403 line->next->prev = line;
2406 /* Compute the number of lines, and vertical pixels for n characters
2407 * starting from the point
2410 compute_lines_pixels (GtkText* text, guint char_count,
2411 guint *lines, guint *pixels)
2413 GList *line = text->current_line;
2414 gint chars_left = char_count;
2419 /* If chars_left == 0, that means we're joining two lines in a
2420 * deletion, so add in the values for the next line as well
2422 for (; line && chars_left >= 0; line = line->next)
2424 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2426 if (line == text->current_line)
2427 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2429 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2431 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2435 chars_left = 0; /* force another loop */
2438 fetch_lines_forward (text, 1);
2443 total_line_height (GtkText* text, GList* line, gint line_count)
2447 for (; line && line_count > 0; line = line->next)
2449 height += LINE_HEIGHT(CACHE_DATA(line));
2451 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2455 fetch_lines_forward (text, line_count);
2462 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2464 if (old == text->line_start_cache)
2468 for (; old_line_count > 0; old_line_count -= 1)
2470 while (text->line_start_cache &&
2472 CACHE_DATA(text->line_start_cache).wraps)
2473 remove_cache_line(text, text->line_start_cache);
2475 remove_cache_line(text, text->line_start_cache);
2478 last = g_list_last (new);
2480 last->next = text->line_start_cache;
2482 if (text->line_start_cache)
2483 text->line_start_cache->prev = last;
2485 text->line_start_cache = new;
2491 g_assert (old->prev);
2495 for (; old_line_count > 0; old_line_count -= 1)
2497 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2498 old = remove_cache_line (text, old);
2500 old = remove_cache_line (text, old);
2506 last = g_list_last (new);
2516 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2518 GList* cache = text->current_line;
2521 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2524 for (; cache; cache = cache->next)
2526 GtkPropertyMark *start = &CACHE_DATA(cache).start;
2527 GtkPropertyMark *end = &CACHE_DATA(cache).end;
2529 start->index -= nchars;
2530 end->index -= nchars;
2532 if (LAST_INDEX (text, text->point) &&
2533 start->index == text->point.index)
2534 *start = text->point;
2535 else if (start->property == text->point.property)
2536 start->offset = start->index - (text->point.index - text->point.offset);
2538 if (LAST_INDEX (text, text->point) &&
2539 end->index == text->point.index)
2541 if (end->property == text->point.property)
2542 end->offset = end->index - (text->point.index - text->point.offset);
2544 /*TEXT_ASSERT_MARK(text, start, "start");*/
2545 /*TEXT_ASSERT_MARK(text, end, "end");*/
2550 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2552 GtkWidget *widget = GTK_WIDGET (text);
2555 guint new_pixels = 0;
2557 GList* new_line = NULL;
2560 text->cursor_virtual_x = 0;
2562 correct_cache_delete (text, nchars, old_lines);
2564 pixel_height = pixel_height_of(text, text->current_line) -
2565 LINE_HEIGHT(CACHE_DATA(text->current_line));
2567 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2568 CACHE_DATA(text->current_line).start = text->point;
2570 new_line = fetch_lines (text,
2571 &CACHE_DATA(text->current_line).start,
2572 &CACHE_DATA(text->current_line).tab_cont,
2576 swap_lines (text, text->current_line, new_line, old_lines);
2578 text->current_line = new_line;
2580 new_pixels = total_line_height (text, new_line, 1);
2582 gdk_window_get_size (text->text_area, &width, &height);
2584 if (old_pixels != new_pixels)
2586 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2588 gdk_draw_pixmap (text->text_area,
2592 pixel_height + old_pixels,
2594 pixel_height + new_pixels,
2598 text->vadj->upper += new_pixels;
2599 text->vadj->upper -= old_pixels;
2600 adjust_adj (text, text->vadj);
2604 rect.y = pixel_height;
2606 rect.height = new_pixels;
2608 expose_text (text, &rect, FALSE);
2609 gtk_text_draw_focus ( (GtkWidget *) text);
2611 text->cursor_mark = text->point;
2613 find_cursor (text, TRUE);
2615 if (old_pixels != new_pixels)
2617 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2620 rect.y = pixel_height + new_pixels;
2622 rect.height = height - rect.y;
2624 expose_text (text, &rect, FALSE);
2627 process_exposes (text);
2634 /* note, the point has already been moved forward */
2636 correct_cache_insert (GtkText* text, gint nchars)
2639 GtkPropertyMark *start;
2640 GtkPropertyMark *end;
2641 gboolean was_split = FALSE;
2643 /* We need to distinguish whether the property was split in the
2644 * insert or not, so we check if the point (which points after
2645 * the insertion here), points to the same character as the
2646 * point before. Ugh.
2650 GtkPropertyMark tmp_mark = text->point;
2651 move_mark_n (&tmp_mark, -1);
2653 if (tmp_mark.property != text->point.property)
2657 /* If we inserted a property exactly at the beginning of the
2658 * line, we have to correct here, or fetch_lines will
2661 start = &CACHE_DATA(text->current_line).start;
2663 /* Check if if we split exactly at the beginning of the line:
2664 * (was_split won't be set if we are inserting at the end of the text,
2665 * so we don't check)
2667 if (start->offset == MARK_CURRENT_PROPERTY (start)->length)
2668 SET_PROPERTY_MARK (start, start->property->next, 0);
2669 /* Check if we inserted a property at the beginning of the text: */
2670 else if (was_split &&
2671 (start->property == text->point.property) &&
2672 (start->index == text->point.index - nchars))
2673 SET_PROPERTY_MARK (start, start->property->prev, 0);
2675 /* Now correct the offsets, and check for start or end marks that
2676 * are after the point, yet point to a property before the point's
2677 * property. This indicates that they are meant to point to the
2678 * second half of a property we split in insert_text_property(), so
2679 * we fix them up that way.
2681 cache = text->current_line->next;
2683 for (; cache; cache = cache->next)
2685 start = &CACHE_DATA(cache).start;
2686 end = &CACHE_DATA(cache).end;
2688 if (LAST_INDEX (text, text->point) &&
2689 start->index == text->point.index)
2690 *start = text->point;
2691 else if (start->index >= text->point.index - nchars)
2693 if (!was_split && start->property == text->point.property)
2694 move_mark_n(start, nchars);
2697 if (start->property->next &&
2698 (start->property->next->next == text->point.property))
2700 g_assert (start->offset >= MARK_CURRENT_PROPERTY (start)->length);
2701 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2702 start->property = text->point.property;
2704 start->index += nchars;
2708 if (LAST_INDEX (text, text->point) &&
2709 end->index == text->point.index)
2711 if (end->index >= text->point.index - nchars)
2713 if (!was_split && end->property == text->point.property)
2714 move_mark_n(end, nchars);
2717 if (end->property->next &&
2718 (end->property->next->next == text->point.property))
2720 g_assert (end->offset >= MARK_CURRENT_PROPERTY (end)->length);
2721 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2722 end->property = text->point.property;
2724 end->index += nchars;
2728 /*TEXT_ASSERT_MARK(text, start, "start");*/
2729 /*TEXT_ASSERT_MARK(text, end, "end");*/
2735 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2736 guint new_line_count)
2738 GtkWidget *widget = GTK_WIDGET (text);
2741 guint new_pixels = 0;
2743 GList* new_lines = NULL;
2746 text->cursor_virtual_x = 0;
2748 undraw_cursor (text, FALSE);
2750 correct_cache_insert (text, nchars);
2752 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2754 pixel_height = pixel_height_of(text, text->current_line) -
2755 LINE_HEIGHT(CACHE_DATA(text->current_line));
2757 new_lines = fetch_lines (text,
2758 &CACHE_DATA(text->current_line).start,
2759 &CACHE_DATA(text->current_line).tab_cont,
2763 swap_lines (text, text->current_line, new_lines, 1);
2765 text->current_line = new_lines;
2767 new_pixels = total_line_height (text, new_lines, new_line_count);
2769 gdk_window_get_size (text->text_area, &width, &height);
2771 if (old_pixels != new_pixels)
2773 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2775 gdk_draw_pixmap (text->text_area,
2779 pixel_height + old_pixels,
2781 pixel_height + new_pixels,
2783 height + (old_pixels - new_pixels) - pixel_height);
2786 text->vadj->upper += new_pixels;
2787 text->vadj->upper -= old_pixels;
2788 adjust_adj (text, text->vadj);
2792 rect.y = pixel_height;
2794 rect.height = new_pixels;
2796 expose_text (text, &rect, FALSE);
2797 gtk_text_draw_focus ( (GtkWidget *) text);
2799 text->cursor_mark = text->point;
2801 find_cursor (text, TRUE);
2803 draw_cursor (text, FALSE);
2805 if (old_pixels != new_pixels)
2807 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2810 rect.y = pixel_height + new_pixels;
2812 rect.height = height - rect.y;
2814 expose_text (text, &rect, FALSE);
2817 process_exposes (text);
2820 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2825 /* Text property functions */
2828 font_hash (gconstpointer font)
2830 return gdk_font_id ((const GdkFont*) font);
2833 static GHashTable *font_cache_table = NULL;
2836 get_text_font (GdkFont* gfont)
2841 if (!font_cache_table)
2842 font_cache_table = g_hash_table_new (font_hash, (GEqualFunc) gdk_font_equal);
2844 tf = g_hash_table_lookup (font_cache_table, gfont);
2852 tf = g_new (GtkTextFont, 1);
2855 tf->gdk_font = gfont;
2856 gdk_font_ref (gfont);
2858 for(i = 0; i < 256; i += 1)
2859 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2861 g_hash_table_insert (font_cache_table, gfont, tf);
2867 text_font_unref (GtkTextFont *text_font)
2869 text_font->ref_count--;
2870 if (text_font->ref_count == 0)
2872 g_hash_table_remove (font_cache_table, text_font->gdk_font);
2873 gdk_font_unref (text_font->gdk_font);
2879 text_properties_equal (TextProperty* prop, GdkFont* font, const GdkColor *fore, const GdkColor *back)
2881 if (prop->flags & PROPERTY_FONT)
2884 GtkTextFont *text_font;
2889 text_font = get_text_font (font);
2891 retval = (prop->font == text_font);
2892 text_font_unref (text_font);
2901 if (prop->flags & PROPERTY_FOREGROUND)
2903 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
2910 if (prop->flags & PROPERTY_BACKGROUND)
2912 if (!back || !gdk_color_equal (&prop->back_color, back))
2923 realize_property (GtkText *text, TextProperty *prop)
2925 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2927 if (prop->flags & PROPERTY_FOREGROUND)
2928 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
2930 if (prop->flags & PROPERTY_BACKGROUND)
2931 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
2935 realize_properties (GtkText *text)
2937 GList *tmp_list = text->text_properties;
2941 realize_property (text, tmp_list->data);
2943 tmp_list = tmp_list->next;
2948 unrealize_property (GtkText *text, TextProperty *prop)
2950 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2952 if (prop->flags & PROPERTY_FOREGROUND)
2953 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
2955 if (prop->flags & PROPERTY_BACKGROUND)
2956 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
2960 unrealize_properties (GtkText *text)
2962 GList *tmp_list = text->text_properties;
2966 unrealize_property (text, tmp_list->data);
2968 tmp_list = tmp_list->next;
2972 static TextProperty*
2973 new_text_property (GtkText *text, GdkFont *font, const GdkColor* fore,
2974 const GdkColor* back, guint length)
2978 if (text_property_chunk == NULL)
2980 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
2981 sizeof(TextProperty),
2982 1024*sizeof(TextProperty),
2986 prop = g_chunk_new(TextProperty, text_property_chunk);
2991 prop->flags |= PROPERTY_FONT;
2992 prop->font = get_text_font (font);
2999 prop->flags |= PROPERTY_FOREGROUND;
3000 prop->fore_color = *fore;
3005 prop->flags |= PROPERTY_BACKGROUND;
3006 prop->back_color = *back;
3009 prop->length = length;
3011 if (GTK_WIDGET_REALIZED (text))
3012 realize_property (text, prop);
3018 destroy_text_property (TextProperty *prop)
3021 text_font_unref (prop->font);
3023 g_mem_chunk_free (text_property_chunk, prop);
3026 /* Flop the memory between the point and the gap around like a
3029 move_gap (GtkText* text, guint index)
3031 if (text->gap_position < index)
3033 gint diff = index - text->gap_position;
3035 if (text->use_wchar)
3036 g_memmove (text->text.wc + text->gap_position,
3037 text->text.wc + text->gap_position + text->gap_size,
3038 diff*sizeof (GdkWChar));
3040 g_memmove (text->text.ch + text->gap_position,
3041 text->text.ch + text->gap_position + text->gap_size,
3044 text->gap_position = index;
3046 else if (text->gap_position > index)
3048 gint diff = text->gap_position - index;
3050 if (text->use_wchar)
3051 g_memmove (text->text.wc + index + text->gap_size,
3052 text->text.wc + index,
3053 diff*sizeof (GdkWChar));
3055 g_memmove (text->text.ch + index + text->gap_size,
3056 text->text.ch + index,
3059 text->gap_position = index;
3063 /* Increase the gap size. */
3065 make_forward_space (GtkText* text, guint len)
3067 if (text->gap_size < len)
3069 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3071 if (sum >= text->text_len)
3075 while (i <= sum) i <<= 1;
3077 if (text->use_wchar)
3078 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3079 i*sizeof(GdkWChar));
3081 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3085 if (text->use_wchar)
3086 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3087 text->text.wc + text->gap_position + text->gap_size,
3088 (text->text_end - (text->gap_position + text->gap_size))
3091 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3092 text->text.ch + text->gap_position + text->gap_size,
3093 text->text_end - (text->gap_position + text->gap_size));
3095 text->text_end += len*2;
3096 text->gap_size += len*2;
3100 /* Inserts into the text property list a list element that guarantees
3101 * that for len characters following the point, text has the correct
3102 * property. does not move point. adjusts text_properties_point and
3103 * text_properties_point_offset relative to the current value of
3106 insert_text_property (GtkText* text, GdkFont* font,
3107 const GdkColor *fore, const GdkColor* back, guint len)
3109 GtkPropertyMark *mark = &text->point;
3110 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3111 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3113 if (MARK_OFFSET(mark) == 0)
3115 /* Point is on the boundary of two properties.
3116 * If it is the same as either, grow, else insert
3119 if (text_properties_equal(forward_prop, font, fore, back))
3121 /* Grow the property in front of us. */
3123 MARK_PROPERTY_LENGTH(mark) += len;
3125 else if (backward_prop &&
3126 text_properties_equal(backward_prop, font, fore, back))
3128 /* Grow property behind us, point property and offset
3131 SET_PROPERTY_MARK (&text->point,
3132 MARK_PREV_LIST_PTR (mark),
3133 backward_prop->length);
3135 backward_prop->length += len;
3137 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3138 (forward_prop->length == 1))
3140 /* Next property just has last position, take it over */
3142 if (GTK_WIDGET_REALIZED (text))
3143 unrealize_property (text, forward_prop);
3145 forward_prop->flags = 0;
3148 forward_prop->flags |= PROPERTY_FONT;
3149 forward_prop->font = get_text_font (font);
3152 forward_prop->font = NULL;
3156 forward_prop->flags |= PROPERTY_FOREGROUND;
3157 forward_prop->fore_color = *fore;
3161 forward_prop->flags |= PROPERTY_BACKGROUND;
3162 forward_prop->back_color = *back;
3164 forward_prop->length += len;
3166 if (GTK_WIDGET_REALIZED (text))
3167 realize_property (text, forward_prop);
3171 /* Splice a new property into the list. */
3173 GList* new_prop = g_list_alloc();
3175 new_prop->next = MARK_LIST_PTR(mark);
3176 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3177 new_prop->next->prev = new_prop;
3180 new_prop->prev->next = new_prop;
3182 new_prop->data = new_text_property (text, font, fore, back, len);
3184 SET_PROPERTY_MARK (mark, new_prop, 0);
3189 /* The following will screw up the line_start cache,
3190 * we'll fix it up in correct_cache_insert
3193 /* In the middle of forward_prop, if properties are equal,
3194 * just add to its length, else split it into two and splice
3196 if (text_properties_equal (forward_prop, font, fore, back))
3198 forward_prop->length += len;
3200 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3201 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3203 /* Inserting before only the last position in the text */
3206 forward_prop->length -= 1;
3208 new_prop = g_list_alloc();
3209 new_prop->data = new_text_property (text, font, fore, back, len+1);
3210 new_prop->prev = MARK_LIST_PTR(mark);
3211 new_prop->next = NULL;
3212 MARK_NEXT_LIST_PTR(mark) = new_prop;
3214 SET_PROPERTY_MARK (mark, new_prop, 0);
3218 GList* new_prop = g_list_alloc();
3219 GList* new_prop_forward = g_list_alloc();
3220 gint old_length = forward_prop->length;
3221 GList* next = MARK_NEXT_LIST_PTR(mark);
3223 /* Set the new lengths according to where they are split. Construct
3224 * two new properties. */
3225 forward_prop->length = MARK_OFFSET(mark);
3227 new_prop_forward->data =
3228 new_text_property(text,
3229 forward_prop->flags & PROPERTY_FONT ?
3230 forward_prop->font->gdk_font : NULL,
3231 forward_prop->flags & PROPERTY_FOREGROUND ?
3232 &forward_prop->fore_color : NULL,
3233 forward_prop->flags & PROPERTY_BACKGROUND ?
3234 &forward_prop->back_color : NULL,
3235 old_length - forward_prop->length);
3237 new_prop->data = new_text_property(text, font, fore, back, len);
3239 /* Now splice things in. */
3240 MARK_NEXT_LIST_PTR(mark) = new_prop;
3241 new_prop->prev = MARK_LIST_PTR(mark);
3243 new_prop->next = new_prop_forward;
3244 new_prop_forward->prev = new_prop;
3246 new_prop_forward->next = next;
3249 next->prev = new_prop_forward;
3251 SET_PROPERTY_MARK (mark, new_prop, 0);
3255 while (text->text_properties_end->next)
3256 text->text_properties_end = text->text_properties_end->next;
3258 while (text->text_properties->prev)
3259 text->text_properties = text->text_properties->prev;
3263 delete_text_property (GtkText* text, guint nchars)
3265 /* Delete nchars forward from point. */
3267 /* Deleting text properties is problematical, because we
3268 * might be storing around marks pointing to a property.
3270 * The marks in question and how we handle them are:
3272 * point: We know the new value, since it will be at the
3273 * end of the deleted text, and we move it there
3275 * cursor: We just remove the mark and set it equal to the
3276 * point after the operation.
3277 * line-start cache: We replace most affected lines.
3278 * The current line gets used to fetch the new
3279 * lines so, if necessary, (delete at the beginning
3280 * of a line) we fix it up by setting it equal to the
3288 for(; nchars; nchars -= 1)
3290 prop = MARK_CURRENT_PROPERTY(&text->point);
3294 if (prop->length == 0)
3296 tmp = MARK_LIST_PTR (&text->point);
3298 is_first = tmp == text->text_properties;
3300 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3301 text->point.offset = 0;
3303 if (GTK_WIDGET_REALIZED (text))
3304 unrealize_property (text, prop);
3306 destroy_text_property (prop);
3307 g_list_free_1 (tmp);
3309 prop = MARK_CURRENT_PROPERTY (&text->point);
3312 text->text_properties = MARK_LIST_PTR (&text->point);
3314 g_assert (prop->length != 0);
3316 else if (prop->length == text->point.offset)
3318 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3319 text->point.offset = 0;
3323 /* Check to see if we have just the single final position remaining
3324 * along in a property; if so, combine it with the previous property
3326 if (LAST_INDEX (text, text->point) &&
3327 (MARK_OFFSET (&text->point) == 0) &&
3328 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3330 tmp = MARK_LIST_PTR (&text->point);
3331 prop = MARK_CURRENT_PROPERTY(&text->point);
3333 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3334 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3335 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3337 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3339 if (GTK_WIDGET_REALIZED (text))
3340 unrealize_property (text, prop);
3342 destroy_text_property (prop);
3343 g_list_free_1 (tmp);
3348 init_properties (GtkText *text)
3350 if (!text->text_properties)
3352 text->text_properties = g_list_alloc();
3353 text->text_properties->next = NULL;
3354 text->text_properties->prev = NULL;
3355 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3356 text->text_properties_end = text->text_properties;
3358 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3360 text->point.index = 0;
3365 /**********************************************************************/
3366 /* Property Movement */
3367 /**********************************************************************/
3370 move_mark_n (GtkPropertyMark* mark, gint n)
3373 advance_mark_n(mark, n);
3375 decrement_mark_n(mark, -n);
3379 advance_mark (GtkPropertyMark* mark)
3381 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3385 if (prop->length > mark->offset + 1)
3389 mark->property = MARK_NEXT_LIST_PTR (mark);
3395 advance_mark_n (GtkPropertyMark* mark, gint n)
3402 i = 0; /* otherwise it migth not be init. */
3403 prop = MARK_CURRENT_PROPERTY(mark);
3405 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3406 /* to make it easier */
3407 n += (mark->offset);
3408 mark->index -= mark->offset;
3410 /* first we take seven-mile-leaps to get to the right text
3412 while ((n-i) > prop->length - 1) {
3414 mark->index += prop->length;
3415 mark->property = MARK_NEXT_LIST_PTR (mark);
3416 prop = MARK_CURRENT_PROPERTY (mark);
3420 /* and then the rest */
3421 mark->index += n - i;
3422 mark->offset += n - i;
3426 decrement_mark (GtkPropertyMark* mark)
3430 if (mark->offset > 0)
3434 mark->property = MARK_PREV_LIST_PTR (mark);
3435 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3440 decrement_mark_n (GtkPropertyMark* mark, gint n)
3444 while (mark->offset < n) {
3445 /* jump to end of prev */
3446 n -= mark->offset + 1;
3447 mark->index -= mark->offset + 1;
3448 mark->property = MARK_PREV_LIST_PTR (mark);
3449 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3457 static GtkPropertyMark
3458 find_mark (GtkText* text, guint mark_position)
3460 return find_mark_near (text, mark_position, &text->point);
3464 * You can also start from the end, what a drag.
3466 static GtkPropertyMark
3467 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3472 GtkPropertyMark mark;
3475 diffa = mark_position + 1;
3477 diffa = mark_position - near->index;
3479 diffb = mark_position;
3491 mark.property = text->text_properties;
3495 move_mark_n (&mark, mark_position - mark.index);
3500 /* This routine must be called with scroll == FALSE, only when
3501 * point is at least partially on screen
3505 find_line_containing_point (GtkText* text, guint point,
3511 text->current_line = NULL;
3515 /* Scroll backwards until the point is on screen
3517 while (CACHE_DATA(text->line_start_cache).start.index > point)
3518 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3520 /* Now additionally try to make sure that the point is fully on screen
3524 while (text->first_cut_pixels != 0 &&
3525 text->line_start_cache->next &&
3526 CACHE_DATA(text->line_start_cache->next).start.index > point)
3527 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3530 gdk_window_get_size (text->text_area, NULL, &height);
3532 for (cache = text->line_start_cache; cache; cache = cache->next)
3536 if (CACHE_DATA(cache).end.index >= point ||
3537 LAST_INDEX(text, CACHE_DATA(cache).end))
3539 text->current_line = cache; /* LOOK HERE, this proc has an
3540 * important side effect. */
3544 TEXT_SHOW_LINE (text, cache, "cache");
3546 if (cache->next == NULL)
3547 fetch_lines_forward (text, 1);
3551 lph = pixel_height_of (text, cache->next);
3553 /* Scroll the bottom of the line is on screen, or until
3554 * the line is the first onscreen line.
3556 while (cache->next != text->line_start_cache && lph > height)
3558 TEXT_SHOW_LINE (text, cache, "cache");
3559 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3560 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3561 lph = pixel_height_of (text, cache->next);
3566 g_assert_not_reached (); /* Must set text->current_line here */
3570 pixel_height_of (GtkText* text, GList* cache_line)
3572 gint pixels = - text->first_cut_pixels;
3573 GList *cache = text->line_start_cache;
3576 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3578 if (cache->data == cache_line->data)
3581 cache = cache->next;
3587 /**********************************************************************/
3588 /* Search and Placement */
3589 /**********************************************************************/
3592 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3595 gint16* char_widths;
3597 if (LAST_INDEX (text, *mark))
3600 ch = GTK_TEXT_INDEX (text, mark->index);
3601 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3605 return tab_mark->to_next_tab * char_widths[' '];
3609 return char_widths[ch];
3613 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3618 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, GdkWChar ch)
3620 if (tab_mark->to_next_tab == 1 || ch == '\t')
3622 if (tab_mark->tab_stops->next)
3624 tab_mark->tab_stops = tab_mark->tab_stops->next;
3625 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3629 tab_mark->to_next_tab = text->default_tab_width;
3634 tab_mark->to_next_tab -= 1;
3639 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3643 advance_tab_mark (text, tab_mark, 0);
3647 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3651 GtkPropertyMark mark = start_line->start;
3652 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3653 gint pixel_width = LINE_START_PIXEL (*start_line);
3655 while (mark.index < text->cursor_mark.index)
3657 pixel_width += find_char_width (text, &mark, &tab_mark);
3659 advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3660 advance_mark (&mark);
3663 text->cursor_pos_x = pixel_width;
3664 text->cursor_pos_y = pixel_height;
3665 text->cursor_char_offset = start_line->font_descent;
3666 text->cursor_mark = mark;
3668 ch = LAST_INDEX (text, mark) ?
3669 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3671 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3672 text->cursor_char = 0;
3674 text->cursor_char = ch;
3678 find_cursor (GtkText* text, gboolean scroll)
3680 if (GTK_WIDGET_REALIZED (text))
3682 find_line_containing_point (text, text->cursor_mark.index, scroll);
3684 if (text->current_line)
3685 find_cursor_at_line (text,
3686 &CACHE_DATA(text->current_line),
3687 pixel_height_of(text, text->current_line));
3690 GTK_OLD_EDITABLE (text)->current_pos = text->cursor_mark.index;
3694 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3695 guint line_pixel_height,
3698 GtkPropertyMark mark = lp->start;
3699 TabStopMark tab_mark = lp->tab_cont.tab_start;
3701 gint char_width = find_char_width(text, &mark, &tab_mark);
3702 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3704 text->cursor_pos_y = line_pixel_height;
3708 GdkWChar ch = LAST_INDEX (text, mark) ?
3709 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3711 if (button_x < pixel_width || mark.index == lp->end.index)
3713 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3714 text->cursor_mark = mark;
3715 text->cursor_char_offset = lp->font_descent;
3717 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3718 text->cursor_char = 0;
3720 text->cursor_char = ch;
3725 advance_tab_mark (text, &tab_mark, ch);
3726 advance_mark (&mark);
3728 pixel_width += char_width/2;
3730 char_width = find_char_width (text, &mark, &tab_mark);
3732 pixel_width += (char_width+1)/2;
3737 find_mouse_cursor (GtkText* text, gint x, gint y)
3740 GList* cache = text->line_start_cache;
3744 pixel_height = - text->first_cut_pixels;
3746 for (; cache; cache = cache->next)
3748 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3750 if (y < pixel_height || !cache->next)
3752 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3754 find_cursor (text, FALSE);
3761 /**********************************************************************/
3763 /**********************************************************************/
3766 free_cache (GtkText* text)
3768 GList* cache = text->line_start_cache;
3773 cache = cache->prev;
3775 text->line_start_cache = cache;
3778 for (; cache; cache = cache->next)
3779 g_mem_chunk_free (params_mem_chunk, cache->data);
3781 g_list_free (text->line_start_cache);
3783 text->line_start_cache = NULL;
3787 remove_cache_line (GtkText* text, GList* member)
3794 if (member == text->line_start_cache)
3795 text->line_start_cache = text->line_start_cache->next;
3798 member->prev->next = member->next;
3801 member->next->prev = member->prev;
3803 list = member->next;
3805 g_mem_chunk_free (params_mem_chunk, member->data);
3806 g_list_free_1 (member);
3811 /**********************************************************************/
3813 /**********************************************************************/
3816 move_cursor_buffer_ver (GtkText *text, int dir)
3818 undraw_cursor (text, FALSE);
3822 scroll_int (text, text->vadj->upper);
3823 text->cursor_mark = find_this_line_start_mark (text,
3825 &text->cursor_mark);
3829 scroll_int (text, - text->vadj->value);
3830 text->cursor_mark = find_this_line_start_mark (text,
3832 &text->cursor_mark);
3835 find_cursor (text, TRUE);
3836 draw_cursor (text, FALSE);
3840 move_cursor_page_ver (GtkText *text, int dir)
3842 scroll_int (text, dir * text->vadj->page_increment);
3846 move_cursor_ver (GtkText *text, int count)
3849 GtkPropertyMark mark;
3852 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3853 offset = text->cursor_mark.index - mark.index;
3855 if (offset > text->cursor_virtual_x)
3856 text->cursor_virtual_x = offset;
3860 if (mark.index == 0)
3863 decrement_mark (&mark);
3864 mark = find_this_line_start_mark (text, mark.index, &mark);
3868 mark = text->cursor_mark;
3870 while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3871 advance_mark (&mark);
3873 if (LAST_INDEX(text, mark))
3876 advance_mark (&mark);
3879 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3880 if (LAST_INDEX(text, mark) ||
3881 GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3884 undraw_cursor (text, FALSE);
3886 text->cursor_mark = mark;
3888 find_cursor (text, TRUE);
3890 draw_cursor (text, FALSE);
3894 move_cursor_hor (GtkText *text, int count)
3896 /* count should be +-1. */
3897 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3898 (count < 0 && text->cursor_mark.index < (- count)) ||
3902 text->cursor_virtual_x = 0;
3904 undraw_cursor (text, FALSE);
3906 move_mark_n (&text->cursor_mark, count);
3908 find_cursor (text, TRUE);
3910 draw_cursor (text, FALSE);
3914 gtk_text_move_cursor (GtkOldEditable *old_editable,
3921 move_cursor_hor (GTK_TEXT (old_editable), 1);
3926 move_cursor_hor (GTK_TEXT (old_editable), -1);
3932 move_cursor_ver (GTK_TEXT (old_editable), 1);
3937 move_cursor_ver (GTK_TEXT (old_editable), -1);
3942 gtk_text_move_forward_character (GtkText *text)
3944 move_cursor_hor (text, 1);
3948 gtk_text_move_backward_character (GtkText *text)
3950 move_cursor_hor (text, -1);
3954 gtk_text_move_next_line (GtkText *text)
3956 move_cursor_ver (text, 1);
3960 gtk_text_move_previous_line (GtkText *text)
3962 move_cursor_ver (text, -1);
3966 gtk_text_move_word (GtkOldEditable *old_editable,
3972 gtk_text_move_forward_word (GTK_TEXT (old_editable));
3977 gtk_text_move_backward_word (GTK_TEXT (old_editable));
3982 gtk_text_move_forward_word (GtkText *text)
3984 text->cursor_virtual_x = 0;
3986 undraw_cursor (text, FALSE);
3988 if (text->use_wchar)
3990 while (!LAST_INDEX (text, text->cursor_mark) &&
3991 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3992 advance_mark (&text->cursor_mark);
3994 while (!LAST_INDEX (text, text->cursor_mark) &&
3995 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3996 advance_mark (&text->cursor_mark);
4000 while (!LAST_INDEX (text, text->cursor_mark) &&
4001 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4002 advance_mark (&text->cursor_mark);
4004 while (!LAST_INDEX (text, text->cursor_mark) &&
4005 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4006 advance_mark (&text->cursor_mark);
4009 find_cursor (text, TRUE);
4010 draw_cursor (text, FALSE);
4014 gtk_text_move_backward_word (GtkText *text)
4016 text->cursor_virtual_x = 0;
4018 undraw_cursor (text, FALSE);
4020 if (text->use_wchar)
4022 while ((text->cursor_mark.index > 0) &&
4023 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4024 decrement_mark (&text->cursor_mark);
4026 while ((text->cursor_mark.index > 0) &&
4027 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4028 decrement_mark (&text->cursor_mark);
4032 while ((text->cursor_mark.index > 0) &&
4033 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4034 decrement_mark (&text->cursor_mark);
4036 while ((text->cursor_mark.index > 0) &&
4037 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4038 decrement_mark (&text->cursor_mark);
4041 find_cursor (text, TRUE);
4042 draw_cursor (text, FALSE);
4046 gtk_text_move_page (GtkOldEditable *old_editable,
4051 scroll_int (GTK_TEXT (old_editable),
4052 y * GTK_TEXT(old_editable)->vadj->page_increment);
4056 gtk_text_move_to_row (GtkOldEditable *old_editable,
4062 gtk_text_move_to_column (GtkOldEditable *old_editable,
4067 text = GTK_TEXT (old_editable);
4069 text->cursor_virtual_x = 0; /* FIXME */
4071 undraw_cursor (text, FALSE);
4073 /* Move to the beginning of the line */
4074 while ((text->cursor_mark.index > 0) &&
4075 (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4076 decrement_mark (&text->cursor_mark);
4078 while (!LAST_INDEX (text, text->cursor_mark) &&
4079 (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4083 else if (column == 0)
4086 advance_mark (&text->cursor_mark);
4089 find_cursor (text, TRUE);
4090 draw_cursor (text, FALSE);
4094 gtk_text_move_beginning_of_line (GtkText *text)
4096 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4101 gtk_text_move_end_of_line (GtkText *text)
4103 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), -1);
4107 gtk_text_kill_char (GtkOldEditable *old_editable,
4112 text = GTK_TEXT (old_editable);
4114 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4115 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4120 if (text->point.index + 1 <= TEXT_LENGTH (text))
4121 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index, text->point.index + 1);
4125 if (text->point.index > 0)
4126 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index - 1, text->point.index);
4132 gtk_text_delete_forward_character (GtkText *text)
4134 gtk_text_kill_char (GTK_OLD_EDITABLE (text), 1);
4138 gtk_text_delete_backward_character (GtkText *text)
4140 gtk_text_kill_char (GTK_OLD_EDITABLE (text), -1);
4144 gtk_text_kill_word (GtkOldEditable *old_editable,
4147 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4148 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4151 gint old_pos = old_editable->current_pos;
4154 gtk_text_move_word (old_editable, 1);
4155 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4159 gtk_text_move_word (old_editable, -1);
4160 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4166 gtk_text_delete_forward_word (GtkText *text)
4168 gtk_text_kill_word (GTK_OLD_EDITABLE (text), 1);
4172 gtk_text_delete_backward_word (GtkText *text)
4174 gtk_text_kill_word (GTK_OLD_EDITABLE (text), -1);
4178 gtk_text_kill_line (GtkOldEditable *old_editable,
4181 gint old_pos = old_editable->current_pos;
4184 gtk_text_move_to_column (old_editable, -1);
4185 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4189 gtk_text_move_to_column (old_editable, 0);
4190 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4195 gtk_text_delete_line (GtkText *text)
4197 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4198 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4202 gtk_text_delete_to_line_end (GtkText *text)
4204 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4208 gtk_text_select_word (GtkText *text, guint32 time)
4213 GtkOldEditable *old_editable;
4214 old_editable = GTK_OLD_EDITABLE (text);
4216 gtk_text_move_backward_word (text);
4217 start_pos = text->cursor_mark.index;
4219 gtk_text_move_forward_word (text);
4220 end_pos = text->cursor_mark.index;
4222 old_editable->has_selection = TRUE;
4223 gtk_text_set_selection (old_editable, start_pos, end_pos);
4224 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4228 gtk_text_select_line (GtkText *text, guint32 time)
4233 GtkOldEditable *old_editable;
4234 old_editable = GTK_OLD_EDITABLE (text);
4236 gtk_text_move_beginning_of_line (text);
4237 start_pos = text->cursor_mark.index;
4239 gtk_text_move_end_of_line (text);
4240 gtk_text_move_forward_character (text);
4241 end_pos = text->cursor_mark.index;
4243 old_editable->has_selection = TRUE;
4244 gtk_text_set_selection (old_editable, start_pos, end_pos);
4245 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4248 /**********************************************************************/
4250 /**********************************************************************/
4253 adjust_adj (GtkText* text, GtkAdjustment* adj)
4257 gdk_window_get_size (text->text_area, NULL, &height);
4259 adj->step_increment = MIN (adj->upper, SCROLL_PIXELS);
4260 adj->page_increment = MIN (adj->upper, height - KEY_SCROLL_PIXELS);
4261 adj->page_size = MIN (adj->upper, height);
4262 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4263 adj->value = MAX (adj->value, 0.0);
4265 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4269 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
4271 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4273 if ((text->first_line_start_index >= lp->start.index) &&
4274 (text->first_line_start_index <= lp->end.index))
4276 svdata->mark = lp->start;
4278 if (text->first_line_start_index == lp->start.index)
4280 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4284 text->first_onscreen_ver_pixel = svdata->pixel_height;
4285 text->first_cut_pixels = 0;
4288 text->vadj->value = text->first_onscreen_ver_pixel;
4291 svdata->pixel_height += LINE_HEIGHT (*lp);
4297 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
4299 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4302 if (svdata->pixel_height <= (gint) text->vadj->value &&
4303 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4305 svdata->mark = lp->start;
4307 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4308 text->first_onscreen_ver_pixel = svdata->pixel_height;
4309 text->first_line_start_index = lp->start.index;
4315 svdata->pixel_height += LINE_HEIGHT (*lp);
4323 static GtkPropertyMark
4324 set_vertical_scroll (GtkText* text)
4326 GtkPropertyMark mark = find_mark (text, 0);
4327 SetVerticalScrollData data;
4331 data.pixel_height = 0;
4332 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4334 text->vadj->upper = data.pixel_height;
4335 orig_value = (gint) text->vadj->value;
4337 gdk_window_get_size (text->text_area, NULL, &height);
4339 text->vadj->step_increment = MIN (text->vadj->upper, SCROLL_PIXELS);
4340 text->vadj->page_increment = MIN (text->vadj->upper, height - KEY_SCROLL_PIXELS);
4341 text->vadj->page_size = MIN (text->vadj->upper, height);
4342 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4343 text->vadj->value = MAX (text->vadj->value, 0.0);
4345 text->last_ver_value = (gint)text->vadj->value;
4347 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4349 if (text->vadj->value != orig_value)
4351 /* We got clipped, and don't really know which line to put first. */
4352 data.pixel_height = 0;
4353 data.last_didnt_wrap = TRUE;
4355 line_params_iterate (text, &mark, NULL,
4357 set_vertical_scroll_find_iterator);
4364 scroll_int (GtkText* text, gint diff)
4368 text->vadj->value += diff;
4370 upper = text->vadj->upper - text->vadj->page_size;
4371 text->vadj->value = MIN (text->vadj->value, upper);
4372 text->vadj->value = MAX (text->vadj->value, 0.0);
4374 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4378 process_exposes (GtkText *text)
4382 /* Make sure graphics expose events are processed before scrolling
4385 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4387 gtk_widget_send_expose (GTK_WIDGET (text), event);
4388 if (event->expose.count == 0)
4390 gdk_event_free (event);
4393 gdk_event_free (event);
4397 static gint last_visible_line_height (GtkText* text)
4399 GList *cache = text->line_start_cache;
4402 gdk_window_get_size (text->text_area, NULL, &height);
4404 for (; cache->next; cache = cache->next)
4405 if (pixel_height_of(text, cache->next) > height)
4409 return pixel_height_of(text, cache) - 1;
4414 static gint first_visible_line_height (GtkText* text)
4416 if (text->first_cut_pixels)
4417 return pixel_height_of(text, text->line_start_cache) + 1;
4423 scroll_down (GtkText* text, gint diff0)
4429 text->first_onscreen_ver_pixel += diff0;
4433 g_assert (text->line_start_cache);
4435 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4437 text->first_cut_pixels += 1;
4441 text->first_cut_pixels = 0;
4443 text->line_start_cache = text->line_start_cache->next;
4445 text->first_line_start_index =
4446 CACHE_DATA(text->line_start_cache).start.index;
4448 if (!text->line_start_cache->next)
4449 fetch_lines_forward (text, 1);
4455 gdk_window_get_size (text->text_area, &width, &height);
4456 if (height > real_diff)
4457 gdk_draw_pixmap (text->text_area,
4465 height - real_diff);
4468 rect.y = MAX (0, height - real_diff);
4470 rect.height = MIN (height, real_diff);
4472 expose_text (text, &rect, FALSE);
4473 gtk_text_draw_focus ( (GtkWidget *) text);
4475 if (text->current_line)
4479 text->cursor_pos_y -= real_diff;
4480 cursor_min = drawn_cursor_min(text);
4483 find_mouse_cursor (text, text->cursor_pos_x,
4484 first_visible_line_height (text));
4487 if (height > real_diff)
4488 process_exposes (text);
4492 scroll_up (GtkText* text, gint diff0)
4498 text->first_onscreen_ver_pixel += diff0;
4502 g_assert (text->line_start_cache);
4504 if (text->first_cut_pixels > 0)
4506 text->first_cut_pixels -= 1;
4510 if (!text->line_start_cache->prev)
4511 fetch_lines_backward (text);
4513 text->line_start_cache = text->line_start_cache->prev;
4515 text->first_line_start_index =
4516 CACHE_DATA(text->line_start_cache).start.index;
4518 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4524 gdk_window_get_size (text->text_area, &width, &height);
4525 if (height > real_diff)
4526 gdk_draw_pixmap (text->text_area,
4534 height - real_diff);
4539 rect.height = MIN (height, real_diff);
4541 expose_text (text, &rect, FALSE);
4542 gtk_text_draw_focus ( (GtkWidget *) text);
4544 if (text->current_line)
4549 text->cursor_pos_y += real_diff;
4550 cursor_max = drawn_cursor_max(text);
4551 gdk_window_get_size (text->text_area, NULL, &height);
4553 if (cursor_max >= height)
4554 find_mouse_cursor (text, text->cursor_pos_x,
4555 last_visible_line_height (text));
4558 if (height > real_diff)
4559 process_exposes (text);
4562 /**********************************************************************/
4564 /**********************************************************************/
4566 /* Assumes mark starts a line. Calculates the height, width, and
4567 * displayable character count of a single DISPLAYABLE line. That
4568 * means that in line-wrap mode, this does may not compute the
4569 * properties of an entire line. */
4571 find_line_params (GtkText* text,
4572 const GtkPropertyMark* mark,
4573 const PrevTabCont *tab_cont,
4574 PrevTabCont *next_cont)
4577 TabStopMark tab_mark = tab_cont->tab_start;
4578 guint max_display_pixels;
4583 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4584 max_display_pixels -= LINE_WRAP_ROOM;
4587 lp.tab_cont = *tab_cont;
4590 lp.pixel_width = tab_cont->pixel_offset;
4591 lp.displayable_chars = 0;
4593 lp.font_descent = 0;
4595 init_tab_cont (text, next_cont);
4597 while (!LAST_INDEX(text, lp.end))
4599 g_assert (lp.end.property);
4601 ch = GTK_TEXT_INDEX (text, lp.end.index);
4602 font = MARK_CURRENT_FONT (text, &lp.end);
4604 if (ch == LINE_DELIM)
4606 /* Newline doesn't count in computation of line height, even
4607 * if its in a bigger font than the rest of the line. Unless,
4608 * of course, there are no other characters. */
4610 if (!lp.font_ascent && !lp.font_descent)
4612 lp.font_ascent = font->ascent;
4613 lp.font_descent = font->descent;
4616 lp.tab_cont_next = *next_cont;
4621 ch_width = find_char_width (text, &lp.end, &tab_mark);
4623 if ((ch_width + lp.pixel_width > max_display_pixels) &&
4624 (lp.end.index > lp.start.index))
4628 if (text->line_wrap)
4630 next_cont->tab_start = tab_mark;
4631 next_cont->pixel_offset = 0;
4635 /* Here's the tough case, a tab is wrapping. */
4636 gint pixels_avail = max_display_pixels - lp.pixel_width;
4637 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
4638 gint spaces_avail = pixels_avail / space_width;
4640 if (spaces_avail == 0)
4642 decrement_mark (&lp.end);
4646 advance_tab_mark (text, &next_cont->tab_start, '\t');
4647 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4649 lp.displayable_chars += 1;
4654 if (text->word_wrap)
4656 GtkPropertyMark saved_mark = lp.end;
4657 guint saved_characters = lp.displayable_chars;
4659 lp.displayable_chars += 1;
4661 if (text->use_wchar)
4663 while (!gdk_iswspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4664 (lp.end.index > lp.start.index))
4666 decrement_mark (&lp.end);
4667 lp.displayable_chars -= 1;
4672 while (!isspace(GTK_TEXT_INDEX (text, lp.end.index)) &&
4673 (lp.end.index > lp.start.index))
4675 decrement_mark (&lp.end);
4676 lp.displayable_chars -= 1;
4680 /* If whole line is one word, revert to char wrapping */
4681 if (lp.end.index == lp.start.index)
4683 lp.end = saved_mark;
4684 lp.displayable_chars = saved_characters;
4685 decrement_mark (&lp.end);
4690 /* Don't include this character, it will wrap. */
4691 decrement_mark (&lp.end);
4695 lp.tab_cont_next = *next_cont;
4702 lp.displayable_chars += 1;
4705 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4706 lp.font_descent = MAX (font->descent, lp.font_descent);
4707 lp.pixel_width += ch_width;
4709 advance_mark(&lp.end);
4710 advance_tab_mark (text, &tab_mark, ch);
4713 if (LAST_INDEX(text, lp.start))
4715 /* Special case, empty last line. */
4716 font = MARK_CURRENT_FONT (text, &lp.end);
4718 lp.font_ascent = font->ascent;
4719 lp.font_descent = font->descent;
4722 lp.tab_cont_next = *next_cont;
4728 expand_scratch_buffer (GtkText* text, guint len)
4730 if (len >= text->scratch_buffer_len)
4734 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4736 if (text->use_wchar)
4738 if (text->scratch_buffer.wc)
4739 text->scratch_buffer.wc = g_new (GdkWChar, i);
4741 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
4742 i*sizeof (GdkWChar));
4746 if (text->scratch_buffer.ch)
4747 text->scratch_buffer.ch = g_new (guchar, i);
4749 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
4752 text->scratch_buffer_len = i;
4756 /* Side effect: modifies text->gc
4759 draw_bg_rect (GtkText* text, GtkPropertyMark *mark,
4760 gint x, gint y, gint width, gint height,
4761 gboolean already_cleared)
4763 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4765 if ((mark->index >= MIN(old_editable->selection_start_pos, old_editable->selection_end_pos) &&
4766 mark->index < MAX(old_editable->selection_start_pos, old_editable->selection_end_pos)))
4768 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
4769 old_editable->has_selection ?
4770 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
4772 NULL, GTK_WIDGET(text), "text",
4773 x, y, width, height);
4775 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
4776 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
4778 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
4780 gdk_draw_rectangle (text->text_area,
4782 TRUE, x, y, width, height);
4784 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4791 rect.height = height;
4793 clear_area (text, &rect);
4795 else if (!already_cleared)
4796 gdk_window_clear_area (text->text_area, x, y, width, height);
4800 draw_line (GtkText* text,
4801 gint pixel_start_height,
4804 GdkGCValues gc_values;
4807 guint running_offset = lp->tab_cont.pixel_offset;
4808 union { GdkWChar *wc; guchar *ch; } buffer;
4811 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4813 guint selection_start_pos = MIN (old_editable->selection_start_pos, old_editable->selection_end_pos);
4814 guint selection_end_pos = MAX (old_editable->selection_start_pos, old_editable->selection_end_pos);
4816 GtkPropertyMark mark = lp->start;
4817 TabStopMark tab_mark = lp->tab_cont.tab_start;
4818 gint pixel_height = pixel_start_height + lp->font_ascent;
4819 guint chars = lp->displayable_chars;
4821 /* First provide a contiguous segment of memory. This makes reading
4822 * the code below *much* easier, and only incurs the cost of copying
4823 * when the line being displayed spans the gap. */
4824 if (mark.index <= text->gap_position &&
4825 mark.index + chars > text->gap_position)
4827 expand_scratch_buffer (text, chars);
4829 if (text->use_wchar)
4831 for (i = 0; i < chars; i += 1)
4832 text->scratch_buffer.wc[i] = GTK_TEXT_INDEX(text, mark.index + i);
4833 buffer.wc = text->scratch_buffer.wc;
4837 for (i = 0; i < chars; i += 1)
4838 text->scratch_buffer.ch[i] = GTK_TEXT_INDEX(text, mark.index + i);
4839 buffer.ch = text->scratch_buffer.ch;
4844 if (text->use_wchar)
4846 if (mark.index >= text->gap_position)
4847 buffer.wc = text->text.wc + mark.index + text->gap_size;
4849 buffer.wc = text->text.wc + mark.index;
4853 if (mark.index >= text->gap_position)
4854 buffer.ch = text->text.ch + mark.index + text->gap_size;
4856 buffer.ch = text->text.ch + mark.index;
4861 if (running_offset > 0)
4863 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
4864 LINE_HEIGHT (*lp), TRUE);
4870 if ((text->use_wchar && buffer.wc[0] != '\t') ||
4871 (!text->use_wchar && buffer.ch[0] != '\t'))
4873 union { GdkWChar *wc; guchar *ch; } next_tab;
4878 if (text->use_wchar)
4879 for (i=0; i<chars; i++)
4881 if (buffer.wc[i] == '\t')
4883 next_tab.wc = buffer.wc + i;
4888 next_tab.ch = memchr (buffer.ch, '\t', chars);
4890 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4892 if (text->use_wchar)
4895 len = MIN (len, next_tab.wc - buffer.wc);
4900 len = MIN (len, next_tab.ch - buffer.ch);
4903 if (mark.index < selection_start_pos)
4904 len = MIN (len, selection_start_pos - mark.index);
4905 else if (mark.index < selection_end_pos)
4906 len = MIN (len, selection_end_pos - mark.index);
4908 font = MARK_CURRENT_FONT (text, &mark);
4909 if (font->type == GDK_FONT_FONT)
4911 gdk_gc_set_font (text->gc, font);
4912 gdk_gc_get_values (text->gc, &gc_values);
4913 if (text->use_wchar)
4914 pixel_width = gdk_text_width_wc (gc_values.font,
4917 pixel_width = gdk_text_width (gc_values.font,
4922 if (text->use_wchar)
4923 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
4925 pixel_width = gdk_text_width (font, buffer.ch, len);
4928 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4929 pixel_width, LINE_HEIGHT (*lp), TRUE);
4931 if ((mark.index >= selection_start_pos) &&
4932 (mark.index < selection_end_pos))
4934 if (old_editable->has_selection)
4935 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_SELECTED];
4937 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_ACTIVE];
4941 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
4945 if (text->use_wchar)
4946 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
4953 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
4960 running_offset += pixel_width;
4962 advance_tab_mark_n (text, &tab_mark, len);
4966 gint pixels_remaining;
4972 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
4973 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
4975 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4977 spaces_avail = pixels_remaining / space_width;
4978 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
4980 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4981 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
4983 running_offset += tab_mark.to_next_tab *
4984 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4986 advance_tab_mark (text, &tab_mark, '\t');
4989 advance_mark_n (&mark, len);
4990 if (text->use_wchar)
4999 draw_line_wrap (GtkText* text, guint height /* baseline height */)
5006 if (text->line_wrap)
5008 bitmap = text->line_wrap_bitmap;
5009 bitmap_width = line_wrap_width;
5010 bitmap_height = line_wrap_height;
5014 bitmap = text->line_arrow_bitmap;
5015 bitmap_width = line_arrow_width;
5016 bitmap_height = line_arrow_height;
5019 gdk_window_get_size (text->text_area, &width, NULL);
5020 width -= LINE_WRAP_ROOM;
5022 gdk_gc_set_stipple (text->gc,
5025 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
5027 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5029 gdk_gc_set_ts_origin (text->gc,
5031 height - bitmap_height - 1);
5033 gdk_draw_rectangle (text->text_area,
5037 height - bitmap_height - 1 /* one pixel above the baseline. */,
5041 gdk_gc_set_ts_origin (text->gc, 0, 0);
5043 gdk_gc_set_fill (text->gc, GDK_SOLID);
5047 undraw_cursor (GtkText* text, gint absolute)
5049 GtkOldEditable *old_editable = (GtkOldEditable *) text;
5051 TDEBUG (("in undraw_cursor\n"));
5054 text->cursor_drawn_level = 0;
5056 if ((text->cursor_drawn_level ++ == 0) &&
5057 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5058 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5062 g_assert(text->cursor_mark.property);
5064 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5066 draw_bg_rect (text, &text->cursor_mark,
5068 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5069 1, font->ascent + 1, FALSE);
5071 if (text->cursor_char)
5073 if (font->type == GDK_FONT_FONT)
5074 gdk_gc_set_font (text->gc, font);
5076 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5078 gdk_draw_text_wc (text->text_area, font,
5081 text->cursor_pos_y - text->cursor_char_offset,
5089 drawn_cursor_min (GtkText* text)
5093 g_assert(text->cursor_mark.property);
5095 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5097 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5101 drawn_cursor_max (GtkText* text)
5105 g_assert(text->cursor_mark.property);
5107 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5109 return text->cursor_pos_y - text->cursor_char_offset;
5113 draw_cursor (GtkText* text, gint absolute)
5115 GtkOldEditable *old_editable = (GtkOldEditable *)text;
5117 TDEBUG (("in draw_cursor\n"));
5120 text->cursor_drawn_level = 1;
5122 if ((--text->cursor_drawn_level == 0) &&
5123 old_editable->editable &&
5124 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5125 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5129 g_assert (text->cursor_mark.property);
5131 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5133 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5135 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
5136 text->cursor_pos_y - text->cursor_char_offset,
5138 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5143 create_bg_gc (GtkText *text)
5147 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5148 values.fill = GDK_TILED;
5150 return gdk_gc_new_with_values (text->text_area, &values,
5151 GDK_GC_FILL | GDK_GC_TILE);
5155 clear_area (GtkText *text, GdkRectangle *area)
5157 GtkWidget *widget = GTK_WIDGET (text);
5163 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5165 gdk_gc_set_ts_origin (text->bg_gc,
5166 (- text->first_onscreen_hor_pixel) % width,
5167 (- text->first_onscreen_ver_pixel) % height);
5169 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5170 area->x, area->y, area->width, area->height);
5173 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5177 expose_text (GtkText* text, GdkRectangle *area, gboolean cursor)
5179 GList *cache = text->line_start_cache;
5180 gint pixels = - text->first_cut_pixels;
5181 gint min_y = MAX (0, area->y);
5182 gint max_y = MAX (0, area->y + area->height);
5185 gdk_window_get_size (text->text_area, NULL, &height);
5186 max_y = MIN (max_y, height);
5188 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5190 clear_area (text, area);
5192 for (; pixels < height; cache = cache->next)
5194 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5196 draw_line (text, pixels, &CACHE_DATA(cache));
5198 if (CACHE_DATA(cache).wraps)
5199 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5202 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5204 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5205 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5207 /* We undraw and draw the cursor here to get the drawn
5208 * level right ... FIXME - maybe the second parameter
5209 * of draw_cursor should work differently
5211 undraw_cursor (text, FALSE);
5212 draw_cursor (text, FALSE);
5216 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5220 fetch_lines_forward (text, 1);
5229 gtk_text_update_text (GtkOldEditable *old_editable,
5233 GtkText *text = GTK_TEXT (old_editable);
5235 GList *cache = text->line_start_cache;
5236 gint pixels = - text->first_cut_pixels;
5242 end_pos = TEXT_LENGTH (text);
5244 if (end_pos < start_pos)
5247 gdk_window_get_size (text->text_area, &width, &height);
5253 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5255 for (; pixels < height; cache = cache->next)
5257 if (CACHE_DATA(cache).start.index < end_pos)
5259 if (CACHE_DATA(cache).end.index >= start_pos)
5262 area.y = MAX(0,pixels);
5263 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5269 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5273 fetch_lines_forward (text, 1);
5281 expose_text (text, &area, TRUE);
5285 recompute_geometry (GtkText* text)
5287 GtkPropertyMark mark, start_mark;
5294 mark = start_mark = set_vertical_scroll (text);
5296 /* We need a real start of a line when calling fetch_lines().
5297 * not the start of a wrapped line.
5299 while (mark.index > 0 &&
5300 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5301 decrement_mark (&mark);
5303 gdk_window_get_size (text->text_area, &width, &height);
5305 /* Fetch an entire line, to make sure that we get all the text
5306 * we backed over above, in addition to enough text to fill up
5307 * the space vertically
5310 new_lines = fetch_lines (text,
5316 mark = CACHE_DATA (g_list_last (new_lines)).end;
5317 if (!LAST_INDEX (text, mark))
5319 advance_mark (&mark);
5321 new_lines = g_list_concat (new_lines,
5326 height + text->first_cut_pixels));
5329 /* Now work forward to the actual first onscreen line */
5331 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5332 new_lines = new_lines->next;
5334 text->line_start_cache = new_lines;
5336 find_cursor (text, TRUE);
5339 /**********************************************************************/
5341 /**********************************************************************/
5344 gtk_text_set_selection (GtkOldEditable *old_editable,
5348 GtkText *text = GTK_TEXT (old_editable);
5350 guint start1, end1, start2, end2;
5353 end = TEXT_LENGTH (text);
5355 start1 = MIN(start,end);
5356 end1 = MAX(start,end);
5357 start2 = MIN(old_editable->selection_start_pos, old_editable->selection_end_pos);
5358 end2 = MAX(old_editable->selection_start_pos, old_editable->selection_end_pos);
5360 if (start2 < start1)
5364 tmp = start1; start1 = start2; start2 = tmp;
5365 tmp = end1; end1 = end2; end2 = tmp;
5368 undraw_cursor (text, FALSE);
5369 old_editable->selection_start_pos = start;
5370 old_editable->selection_end_pos = end;
5371 draw_cursor (text, FALSE);
5373 /* Expose only what changed */
5375 if (start1 < start2)
5376 gtk_text_update_text (old_editable, start1, MIN(end1, start2));
5379 gtk_text_update_text (old_editable, MAX(end1, start2), end2);
5380 else if (end2 < end1)
5381 gtk_text_update_text (old_editable, end2, end1);
5385 /**********************************************************************/
5387 /**********************************************************************/
5389 #ifdef DEBUG_GTK_TEXT
5391 gtk_text_show_cache_line (GtkText *text, GList *cache,
5392 const char* what, const char* func, gint line)
5394 LineParams *lp = &CACHE_DATA(cache);
5397 if (cache == text->line_start_cache)
5398 g_message ("Line Start Cache: ");
5400 if (cache == text->current_line)
5401 g_message("Current Line: ");
5403 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
5411 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
5412 g_message ("%c", GTK_TEXT_INDEX (text, i));
5418 gtk_text_show_cache (GtkText *text, const char* func, gint line)
5420 GList *l = text->line_start_cache;
5426 /* back up to the absolute beginning of the line cache */
5430 g_message ("*** line cache ***\n");
5431 for (; l; l = l->next)
5432 gtk_text_show_cache_line (text, l, "all", func, line);
5436 gtk_text_assert_mark (GtkText *text,
5437 GtkPropertyMark *mark,
5438 GtkPropertyMark *before,
5439 GtkPropertyMark *after,
5444 GtkPropertyMark correct_mark = find_mark (text, mark->index);
5446 if (mark->offset != correct_mark.offset ||
5447 mark->property != correct_mark.property)
5448 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
5452 gtk_text_assert (GtkText *text,
5456 GList* cache = text->line_start_cache;
5457 GtkPropertyMark* before_mark = NULL;
5458 GtkPropertyMark* after_mark = NULL;
5460 gtk_text_show_props (text, msg, line);
5462 for (; cache->prev; cache = cache->prev)
5465 g_message ("*** line markers ***\n");
5467 for (; cache; cache = cache->next)
5469 after_mark = &CACHE_DATA(cache).end;
5470 gtk_text_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
5471 before_mark = &CACHE_DATA(cache).start;
5474 after_mark = &CACHE_DATA(cache->next).start;
5478 gtk_text_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
5479 before_mark = &CACHE_DATA(cache).end;
5484 gtk_text_show_adj (GtkText *text,
5490 g_message ("*** adjustment ***\n");
5492 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
5499 adj->step_increment,
5500 adj->page_increment,
5505 gtk_text_show_props (GtkText *text,
5509 GList* props = text->text_properties;
5512 g_message ("%s:%d: ", msg, line);
5514 for (; props; props = props->next)
5516 TextProperty *p = (TextProperty*)props->data;
5518 proplen += p->length;
5520 g_message ("[%d,%p,", p->length, p);
5521 if (p->flags & PROPERTY_FONT)
5522 g_message ("%p,", p->font);
5525 if (p->flags & PROPERTY_FOREGROUND)
5526 g_message ("%ld, ", p->fore_color.pixel);
5529 if (p->flags & PROPERTY_BACKGROUND)
5530 g_message ("%ld] ", p->back_color.pixel);
5537 if (proplen - 1 != TEXT_LENGTH(text))
5538 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);