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
33 #include "gdk/gdkkeysyms.h"
34 #include "gdk/gdki18n.h"
36 #include "gtkmarshalers.h"
37 #include "gtkselection.h"
38 #include "gtksignal.h"
40 #include "gtkobject.h"
41 #define GTK_ENABLE_BROKEN
43 #include "line-wrap.xbm"
44 #include "line-arrow.xbm"
45 #include "gtkprivate.h"
50 #define INITIAL_BUFFER_SIZE 1024
51 #define INITIAL_LINE_CACHE_SIZE 256
52 #define MIN_GAP_SIZE 256
53 #define LINE_DELIM '\n'
54 #define MIN_TEXT_WIDTH_LINES 20
55 #define MIN_TEXT_HEIGHT_LINES 10
56 #define TEXT_BORDER_ROOM 1
57 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
58 #define DEFAULT_TAB_STOP_WIDTH 4
59 #define SCROLL_PIXELS 5
60 #define KEY_SCROLL_PIXELS 10
61 #define SCROLL_TIME 100
62 #define FREEZE_LENGTH 1024
63 /* Freeze text when inserting or deleting more than this many characters */
65 #define SET_PROPERTY_MARK(m, p, o) do { \
66 (m)->property = (p); \
69 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
70 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
71 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
72 (mark)->property->prev->data \
74 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
75 #define MARK_LIST_PTR(mark) ((mark)->property)
76 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
77 #define MARK_OFFSET(mark) ((mark)->offset)
78 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
81 #define MARK_CURRENT_FONT(text, mark) \
82 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
83 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
84 gtk_style_get_font (GTK_WIDGET (text)->style))
85 #define MARK_CURRENT_FORE(text, mark) \
86 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
87 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
88 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
89 #define MARK_CURRENT_BACK(text, mark) \
90 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
91 &MARK_CURRENT_PROPERTY(mark)->back_color : \
92 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
93 #define MARK_CURRENT_TEXT_FONT(text, mark) \
94 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
95 MARK_CURRENT_PROPERTY(mark)->font : \
98 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
99 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
100 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
101 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
102 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
103 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
104 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
105 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
115 typedef struct _TextProperty TextProperty;
116 typedef struct _TabStopMark TabStopMark;
117 typedef struct _PrevTabCont PrevTabCont;
118 typedef struct _FetchLinesData FetchLinesData;
119 typedef struct _LineParams LineParams;
120 typedef struct _SetVerticalScrollData SetVerticalScrollData;
122 typedef gint (*LineIteratorFunction) (GtkText* text, LineParams* lp, void* data);
130 struct _SetVerticalScrollData {
132 gint last_didnt_wrap;
133 gint last_line_start;
134 GtkPropertyMark mark;
139 /* The actual font. */
143 gint16 char_widths[256];
147 PROPERTY_FONT = 1 << 0,
148 PROPERTY_FOREGROUND = 1 << 1,
149 PROPERTY_BACKGROUND = 1 << 2
157 /* Background Color. */
160 /* Foreground Color. */
163 /* Show which properties are set */
164 TextPropertyFlags flags;
166 /* Length of this property. */
172 GList* tab_stops; /* Index into list containing the next tab position. If
173 * NULL, using default widths. */
180 TabStopMark tab_start;
183 struct _FetchLinesData
196 guint displayable_chars;
199 PrevTabCont tab_cont;
200 PrevTabCont tab_cont_next;
202 GtkPropertyMark start;
207 static void gtk_text_class_init (GtkTextClass *klass);
208 static void gtk_text_set_property (GObject *object,
212 static void gtk_text_get_property (GObject *object,
216 static void gtk_text_editable_init (GtkEditableClass *iface);
217 static void gtk_text_init (GtkText *text);
218 static void gtk_text_destroy (GtkObject *object);
219 static void gtk_text_finalize (GObject *object);
220 static void gtk_text_realize (GtkWidget *widget);
221 static void gtk_text_unrealize (GtkWidget *widget);
222 static void gtk_text_style_set (GtkWidget *widget,
223 GtkStyle *previous_style);
224 static void gtk_text_state_changed (GtkWidget *widget,
225 GtkStateType previous_state);
226 static void gtk_text_draw_focus (GtkWidget *widget);
227 static void gtk_text_size_request (GtkWidget *widget,
228 GtkRequisition *requisition);
229 static void gtk_text_size_allocate (GtkWidget *widget,
230 GtkAllocation *allocation);
231 static void gtk_text_adjustment (GtkAdjustment *adjustment,
233 static void gtk_text_insert_text (GtkEditable *editable,
234 const gchar *new_text,
235 gint new_text_length,
237 static void gtk_text_delete_text (GtkEditable *editable,
240 static void gtk_text_update_text (GtkOldEditable *old_editable,
243 static gchar *gtk_text_get_chars (GtkOldEditable *old_editable,
246 static void gtk_text_set_selection (GtkOldEditable *old_editable,
249 static void gtk_text_real_set_editable (GtkOldEditable *old_editable,
250 gboolean is_editable);
252 static void gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
256 static gint gtk_text_expose (GtkWidget *widget,
257 GdkEventExpose *event);
258 static gint gtk_text_button_press (GtkWidget *widget,
259 GdkEventButton *event);
260 static gint gtk_text_button_release (GtkWidget *widget,
261 GdkEventButton *event);
262 static gint gtk_text_motion_notify (GtkWidget *widget,
263 GdkEventMotion *event);
264 static gint gtk_text_key_press (GtkWidget *widget,
267 static void move_gap (GtkText* text, guint index);
268 static void make_forward_space (GtkText* text, guint len);
270 /* Property management */
271 static GtkTextFont* get_text_font (GdkFont* gfont);
272 static void text_font_unref (GtkTextFont *text_font);
274 static void insert_text_property (GtkText* text, GdkFont* font,
275 const GdkColor *fore, const GdkColor* back, guint len);
276 static TextProperty* new_text_property (GtkText *text, GdkFont* font,
277 const GdkColor* fore, const GdkColor* back, guint length);
278 static void destroy_text_property (TextProperty *prop);
279 static void init_properties (GtkText *text);
280 static void realize_property (GtkText *text, TextProperty *prop);
281 static void realize_properties (GtkText *text);
282 static void unrealize_property (GtkText *text, TextProperty *prop);
283 static void unrealize_properties (GtkText *text);
285 static void delete_text_property (GtkText* text, guint len);
287 static guint pixel_height_of (GtkText* text, GList* cache_line);
289 /* Property Movement and Size Computations */
290 static void advance_mark (GtkPropertyMark* mark);
291 static void decrement_mark (GtkPropertyMark* mark);
292 static void advance_mark_n (GtkPropertyMark* mark, gint n);
293 static void decrement_mark_n (GtkPropertyMark* mark, gint n);
294 static void move_mark_n (GtkPropertyMark* mark, gint n);
295 static GtkPropertyMark find_mark (GtkText* text, guint mark_position);
296 static GtkPropertyMark find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near);
297 static void find_line_containing_point (GtkText* text, guint point,
301 static void compute_lines_pixels (GtkText* text, guint char_count,
302 guint *lines, guint *pixels);
304 static gint total_line_height (GtkText* text,
307 static LineParams find_line_params (GtkText* text,
308 const GtkPropertyMark *mark,
309 const PrevTabCont *tab_cont,
310 PrevTabCont *next_cont);
311 static void recompute_geometry (GtkText* text);
312 static void insert_expose (GtkText* text, guint old_pixels, gint nchars, guint new_line_count);
313 static void delete_expose (GtkText* text,
317 static GdkGC *create_bg_gc (GtkText *text);
318 static void clear_area (GtkText *text, GdkRectangle *area);
319 static void draw_line (GtkText* text,
322 static void draw_line_wrap (GtkText* text,
324 static void draw_cursor (GtkText* text, gint absolute);
325 static void undraw_cursor (GtkText* text, gint absolute);
326 static gint drawn_cursor_min (GtkText* text);
327 static gint drawn_cursor_max (GtkText* text);
328 static void expose_text (GtkText* text, GdkRectangle *area, gboolean cursor);
330 /* Search and Placement. */
331 static void find_cursor (GtkText* text,
333 static void find_cursor_at_line (GtkText* text,
334 const LineParams* start_line,
336 static void find_mouse_cursor (GtkText* text, gint x, gint y);
339 static void adjust_adj (GtkText* text, GtkAdjustment* adj);
340 static void scroll_up (GtkText* text, gint diff);
341 static void scroll_down (GtkText* text, gint diff);
342 static void scroll_int (GtkText* text, gint diff);
344 static void process_exposes (GtkText *text);
346 /* Cache Management. */
347 static void free_cache (GtkText* text);
348 static GList* remove_cache_line (GtkText* text, GList* list);
351 static void move_cursor_buffer_ver (GtkText *text, int dir);
352 static void move_cursor_page_ver (GtkText *text, int dir);
353 static void move_cursor_ver (GtkText *text, int count);
354 static void move_cursor_hor (GtkText *text, int count);
356 /* Binding actions */
357 static void gtk_text_move_cursor (GtkOldEditable *old_editable,
360 static void gtk_text_move_word (GtkOldEditable *old_editable,
362 static void gtk_text_move_page (GtkOldEditable *old_editable,
365 static void gtk_text_move_to_row (GtkOldEditable *old_editable,
367 static void gtk_text_move_to_column (GtkOldEditable *old_editable,
369 static void gtk_text_kill_char (GtkOldEditable *old_editable,
371 static void gtk_text_kill_word (GtkOldEditable *old_editable,
373 static void gtk_text_kill_line (GtkOldEditable *old_editable,
377 static void gtk_text_move_forward_character (GtkText *text);
378 static void gtk_text_move_backward_character (GtkText *text);
379 static void gtk_text_move_forward_word (GtkText *text);
380 static void gtk_text_move_backward_word (GtkText *text);
381 static void gtk_text_move_beginning_of_line (GtkText *text);
382 static void gtk_text_move_end_of_line (GtkText *text);
383 static void gtk_text_move_next_line (GtkText *text);
384 static void gtk_text_move_previous_line (GtkText *text);
386 static void gtk_text_delete_forward_character (GtkText *text);
387 static void gtk_text_delete_backward_character (GtkText *text);
388 static void gtk_text_delete_forward_word (GtkText *text);
389 static void gtk_text_delete_backward_word (GtkText *text);
390 static void gtk_text_delete_line (GtkText *text);
391 static void gtk_text_delete_to_line_end (GtkText *text);
392 static void gtk_text_select_word (GtkText *text,
394 static void gtk_text_select_line (GtkText *text,
397 static void gtk_text_set_position (GtkOldEditable *old_editable,
400 /* #define DEBUG_GTK_TEXT */
402 #if defined(DEBUG_GTK_TEXT) && defined(__GNUC__)
403 /* Debugging utilities. */
404 static void gtk_text_assert_mark (GtkText *text,
405 GtkPropertyMark *mark,
406 GtkPropertyMark *before,
407 GtkPropertyMark *after,
412 static void gtk_text_assert (GtkText *text,
415 static void gtk_text_show_cache_line (GtkText *text, GList *cache,
416 const char* what, const char* func, gint line);
417 static void gtk_text_show_cache (GtkText *text, const char* func, gint line);
418 static void gtk_text_show_adj (GtkText *text,
423 static void gtk_text_show_props (GtkText* test,
427 #define TDEBUG(args) g_message args
428 #define TEXT_ASSERT(text) gtk_text_assert (text,__PRETTY_FUNCTION__,__LINE__)
429 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_text_assert_mark (text,mark, \
430 __PRETTY_FUNCTION__,msg,__LINE__)
431 #define TEXT_SHOW(text) gtk_text_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
432 #define TEXT_SHOW_LINE(text,line,msg) gtk_text_show_cache_line (text,line,msg,\
433 __PRETTY_FUNCTION__,__LINE__)
434 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_text_show_adj (text,adj,msg, \
435 __PRETTY_FUNCTION__,__LINE__)
438 #define TEXT_ASSERT(text)
439 #define TEXT_ASSERT_MARK(text,mark,msg)
440 #define TEXT_SHOW(text)
441 #define TEXT_SHOW_LINE(text,line,msg)
442 #define TEXT_SHOW_ADJ(text,adj,msg)
445 /* Memory Management. */
446 static GMemChunk *params_mem_chunk = NULL;
447 static GMemChunk *text_property_chunk = NULL;
449 static GtkWidgetClass *parent_class = NULL;
452 static const GtkTextFunction control_keys[26] =
454 (GtkTextFunction)gtk_text_move_beginning_of_line, /* a */
455 (GtkTextFunction)gtk_text_move_backward_character, /* b */
456 (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
457 (GtkTextFunction)gtk_text_delete_forward_character, /* d */
458 (GtkTextFunction)gtk_text_move_end_of_line, /* e */
459 (GtkTextFunction)gtk_text_move_forward_character, /* f */
461 (GtkTextFunction)gtk_text_delete_backward_character, /* h */
464 (GtkTextFunction)gtk_text_delete_to_line_end, /* k */
467 (GtkTextFunction)gtk_text_move_next_line, /* n */
469 (GtkTextFunction)gtk_text_move_previous_line, /* p */
474 (GtkTextFunction)gtk_text_delete_line, /* u */
475 (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
476 (GtkTextFunction)gtk_text_delete_backward_word, /* w */
477 (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
482 static const GtkTextFunction alt_keys[26] =
485 (GtkTextFunction)gtk_text_move_backward_word, /* b */
487 (GtkTextFunction)gtk_text_delete_forward_word, /* d */
489 (GtkTextFunction)gtk_text_move_forward_word, /* f */
513 /**********************************************************************/
515 /**********************************************************************/
518 gtk_text_get_type (void)
520 static GtkType text_type = 0;
524 static const GtkTypeInfo text_info =
528 sizeof (GtkTextClass),
529 (GtkClassInitFunc) gtk_text_class_init,
530 (GtkObjectInitFunc) gtk_text_init,
531 /* reserved_1 */ NULL,
532 /* reserved_2 */ NULL,
533 (GtkClassInitFunc) NULL,
536 static const GInterfaceInfo editable_info =
538 (GInterfaceInitFunc) gtk_text_editable_init, /* interface_init */
539 NULL, /* interface_finalize */
540 NULL /* interface_data */
544 text_type = gtk_type_unique (GTK_TYPE_OLD_EDITABLE, &text_info);
545 g_type_add_interface_static (text_type,
554 gtk_text_class_init (GtkTextClass *class)
556 GObjectClass *gobject_class;
557 GtkObjectClass *object_class;
558 GtkWidgetClass *widget_class;
559 GtkOldEditableClass *old_editable_class;
561 gobject_class = G_OBJECT_CLASS (class);
562 object_class = (GtkObjectClass*) class;
563 widget_class = (GtkWidgetClass*) class;
564 old_editable_class = (GtkOldEditableClass*) class;
565 parent_class = gtk_type_class (GTK_TYPE_OLD_EDITABLE);
567 gobject_class->finalize = gtk_text_finalize;
568 gobject_class->set_property = gtk_text_set_property;
569 gobject_class->get_property = gtk_text_get_property;
571 object_class->destroy = gtk_text_destroy;
573 widget_class->realize = gtk_text_realize;
574 widget_class->unrealize = gtk_text_unrealize;
575 widget_class->style_set = gtk_text_style_set;
576 widget_class->state_changed = gtk_text_state_changed;
577 widget_class->size_request = gtk_text_size_request;
578 widget_class->size_allocate = gtk_text_size_allocate;
579 widget_class->expose_event = gtk_text_expose;
580 widget_class->button_press_event = gtk_text_button_press;
581 widget_class->button_release_event = gtk_text_button_release;
582 widget_class->motion_notify_event = gtk_text_motion_notify;
583 widget_class->key_press_event = gtk_text_key_press;
585 old_editable_class->set_editable = gtk_text_real_set_editable;
587 old_editable_class->move_cursor = gtk_text_move_cursor;
588 old_editable_class->move_word = gtk_text_move_word;
589 old_editable_class->move_page = gtk_text_move_page;
590 old_editable_class->move_to_row = gtk_text_move_to_row;
591 old_editable_class->move_to_column = gtk_text_move_to_column;
593 old_editable_class->kill_char = gtk_text_kill_char;
594 old_editable_class->kill_word = gtk_text_kill_word;
595 old_editable_class->kill_line = gtk_text_kill_line;
597 old_editable_class->update_text = gtk_text_update_text;
598 old_editable_class->get_chars = gtk_text_get_chars;
599 old_editable_class->set_selection = gtk_text_set_selection;
600 old_editable_class->set_position = gtk_text_set_position;
602 class->set_scroll_adjustments = gtk_text_set_adjustments;
604 g_object_class_install_property (gobject_class,
606 g_param_spec_object ("hadjustment",
607 P_("Horizontal Adjustment"),
608 P_("Horizontal adjustment for the text widget"),
610 GTK_PARAM_READWRITE));
612 g_object_class_install_property (gobject_class,
614 g_param_spec_object ("vadjustment",
615 P_("Vertical Adjustment"),
616 P_("Vertical adjustment for the text widget"),
618 GTK_PARAM_READWRITE));
620 g_object_class_install_property (gobject_class,
622 g_param_spec_boolean ("line-wrap",
624 P_("Whether lines are wrapped at widget edges"),
626 GTK_PARAM_READWRITE));
628 g_object_class_install_property (gobject_class,
630 g_param_spec_boolean ("word-wrap",
632 P_("Whether words are wrapped at widget edges"),
634 GTK_PARAM_READWRITE));
636 widget_class->set_scroll_adjustments_signal =
637 gtk_signal_new (I_("set_scroll_adjustments"),
639 GTK_CLASS_TYPE (object_class),
640 GTK_SIGNAL_OFFSET (GtkTextClass, set_scroll_adjustments),
641 _gtk_marshal_VOID__OBJECT_OBJECT,
642 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
646 gtk_text_set_property (GObject *object,
653 text = GTK_TEXT (object);
657 case PROP_HADJUSTMENT:
658 gtk_text_set_adjustments (text,
659 g_value_get_object (value),
662 case PROP_VADJUSTMENT:
663 gtk_text_set_adjustments (text,
665 g_value_get_object (value));
668 gtk_text_set_line_wrap (text, g_value_get_boolean (value));
671 gtk_text_set_word_wrap (text, g_value_get_boolean (value));
674 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
680 gtk_text_get_property (GObject *object,
687 text = GTK_TEXT (object);
691 case PROP_HADJUSTMENT:
692 g_value_set_object (value, text->hadj);
694 case PROP_VADJUSTMENT:
695 g_value_set_object (value, text->vadj);
698 g_value_set_boolean (value, text->line_wrap);
701 g_value_set_boolean (value, text->word_wrap);
704 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
710 gtk_text_editable_init (GtkEditableClass *iface)
712 iface->insert_text = gtk_text_insert_text;
713 iface->delete_text = gtk_text_delete_text;
717 gtk_text_init (GtkText *text)
719 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
721 text->text_area = NULL;
726 text->line_wrap_bitmap = NULL;
727 text->line_arrow_bitmap = NULL;
729 text->use_wchar = FALSE;
730 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
731 text->text_len = INITIAL_BUFFER_SIZE;
733 text->scratch_buffer.ch = NULL;
734 text->scratch_buffer_len = 0;
736 text->freeze_count = 0;
738 if (!params_mem_chunk)
739 params_mem_chunk = g_mem_chunk_new ("LineParams",
741 256 * sizeof (LineParams),
744 text->default_tab_width = 4;
745 text->tab_stops = NULL;
747 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
748 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
750 text->line_start_cache = NULL;
751 text->first_cut_pixels = 0;
753 text->line_wrap = TRUE;
754 text->word_wrap = FALSE;
759 text->current_font = NULL;
761 init_properties (text);
763 GTK_OLD_EDITABLE (text)->editable = FALSE;
765 gtk_text_set_adjustments (text, NULL, NULL);
766 gtk_editable_set_position (GTK_EDITABLE (text), 0);
770 gtk_text_new (GtkAdjustment *hadj,
776 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
778 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
780 text = gtk_widget_new (GTK_TYPE_TEXT,
789 gtk_text_set_word_wrap (GtkText *text,
792 g_return_if_fail (GTK_IS_TEXT (text));
794 text->word_wrap = (word_wrap != FALSE);
796 if (GTK_WIDGET_REALIZED (text))
798 recompute_geometry (text);
799 gtk_widget_queue_draw (GTK_WIDGET (text));
802 g_object_notify (G_OBJECT (text), "word_wrap");
806 gtk_text_set_line_wrap (GtkText *text,
809 g_return_if_fail (GTK_IS_TEXT (text));
811 text->line_wrap = (line_wrap != FALSE);
813 if (GTK_WIDGET_REALIZED (text))
815 recompute_geometry (text);
816 gtk_widget_queue_draw (GTK_WIDGET (text));
819 g_object_notify (G_OBJECT (text), "line_wrap");
823 gtk_text_set_editable (GtkText *text,
824 gboolean is_editable)
826 g_return_if_fail (GTK_IS_TEXT (text));
828 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
832 gtk_text_real_set_editable (GtkOldEditable *old_editable,
833 gboolean is_editable)
837 g_return_if_fail (GTK_IS_TEXT (old_editable));
839 text = GTK_TEXT (old_editable);
841 old_editable->editable = (is_editable != FALSE);
844 draw_cursor (text, TRUE);
846 undraw_cursor (text, TRUE);
850 gtk_text_set_adjustments (GtkText *text,
854 g_return_if_fail (GTK_IS_TEXT (text));
856 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
858 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
860 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
862 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
864 if (text->hadj && (text->hadj != hadj))
866 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
867 gtk_object_unref (GTK_OBJECT (text->hadj));
870 if (text->vadj && (text->vadj != vadj))
872 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
873 gtk_object_unref (GTK_OBJECT (text->vadj));
876 g_object_freeze_notify (G_OBJECT (text));
877 if (text->hadj != hadj)
880 gtk_object_ref (GTK_OBJECT (text->hadj));
881 gtk_object_sink (GTK_OBJECT (text->hadj));
883 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
884 (GtkSignalFunc) gtk_text_adjustment,
886 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
887 (GtkSignalFunc) gtk_text_adjustment,
889 gtk_signal_connect (GTK_OBJECT (text->hadj), "destroy",
890 (GtkSignalFunc) gtk_text_adjustment_destroyed,
892 gtk_text_adjustment (hadj, text);
894 g_object_notify (G_OBJECT (text), "hadjustment");
897 if (text->vadj != vadj)
900 gtk_object_ref (GTK_OBJECT (text->vadj));
901 gtk_object_sink (GTK_OBJECT (text->vadj));
903 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
904 (GtkSignalFunc) gtk_text_adjustment,
906 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
907 (GtkSignalFunc) gtk_text_adjustment,
909 gtk_signal_connect (GTK_OBJECT (text->vadj), "destroy",
910 (GtkSignalFunc) gtk_text_adjustment_destroyed,
912 gtk_text_adjustment (vadj, text);
914 g_object_notify (G_OBJECT (text), "vadjustment");
916 g_object_thaw_notify (G_OBJECT (text));
920 gtk_text_set_point (GtkText *text,
923 g_return_if_fail (GTK_IS_TEXT (text));
924 g_return_if_fail (index <= TEXT_LENGTH (text));
926 text->point = find_mark (text, index);
930 gtk_text_get_point (GtkText *text)
932 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
934 return text->point.index;
938 gtk_text_get_length (GtkText *text)
940 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
942 return TEXT_LENGTH (text);
946 gtk_text_freeze (GtkText *text)
948 g_return_if_fail (GTK_IS_TEXT (text));
950 text->freeze_count++;
954 gtk_text_thaw (GtkText *text)
956 g_return_if_fail (GTK_IS_TEXT (text));
958 if (text->freeze_count)
959 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
961 recompute_geometry (text);
962 gtk_widget_queue_draw (GTK_WIDGET (text));
967 gtk_text_insert (GtkText *text,
969 const GdkColor *fore,
970 const GdkColor *back,
974 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
975 gboolean frozen = FALSE;
977 gint new_line_count = 1;
978 guint old_height = 0;
983 g_return_if_fail (GTK_IS_TEXT (text));
986 length = strlen (chars);
993 if (!text->freeze_count && (length > FREEZE_LENGTH))
995 gtk_text_freeze (text);
999 if (!text->freeze_count && (text->line_start_cache != NULL))
1001 find_line_containing_point (text, text->point.index, TRUE);
1002 old_height = total_line_height (text, text->current_line, 1);
1005 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
1007 GtkWidget *widget = GTK_WIDGET (text);
1009 gtk_widget_ensure_style (widget);
1010 if ((widget->style) &&
1011 (gtk_style_get_font (widget->style)->type == GDK_FONT_FONTSET))
1013 text->use_wchar = TRUE;
1014 g_free (text->text.ch);
1015 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
1016 text->text_len = INITIAL_BUFFER_SIZE;
1017 if (text->scratch_buffer.ch)
1018 g_free (text->scratch_buffer.ch);
1019 text->scratch_buffer.wc = NULL;
1020 text->scratch_buffer_len = 0;
1024 move_gap (text, text->point.index);
1025 make_forward_space (text, length);
1027 if (text->use_wchar)
1029 char *chars_nt = (char *)chars;
1032 chars_nt = g_new (char, length+1);
1033 memcpy (chars_nt, chars, length);
1034 chars_nt[length] = 0;
1036 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
1038 if (chars_nt != chars)
1046 memcpy(text->text.ch + text->gap_position, chars, length);
1049 if (!text->freeze_count && (text->line_start_cache != NULL))
1051 if (text->use_wchar)
1053 for (i=0; i<numwcs; i++)
1054 if (text->text.wc[text->gap_position + i] == '\n')
1059 for (i=0; i<numwcs; i++)
1060 if (text->text.ch[text->gap_position + i] == '\n')
1067 insert_text_property (text, font, fore, back, numwcs);
1069 text->gap_size -= numwcs;
1070 text->gap_position += numwcs;
1072 if (text->point.index < text->first_line_start_index)
1073 text->first_line_start_index += numwcs;
1074 if (text->point.index < old_editable->selection_start_pos)
1075 old_editable->selection_start_pos += numwcs;
1076 if (text->point.index < old_editable->selection_end_pos)
1077 old_editable->selection_end_pos += numwcs;
1078 /* We'll reset the cursor later anyways if we aren't frozen */
1079 if (text->point.index < text->cursor_mark.index)
1080 text->cursor_mark.index += numwcs;
1082 advance_mark_n (&text->point, numwcs);
1084 if (!text->freeze_count && (text->line_start_cache != NULL))
1085 insert_expose (text, old_height, numwcs, new_line_count);
1089 gtk_text_thaw (text);
1093 gtk_text_backward_delete (GtkText *text,
1096 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1098 if (nchars > text->point.index || nchars <= 0)
1101 gtk_text_set_point (text, text->point.index - nchars);
1103 return gtk_text_forward_delete (text, nchars);
1107 gtk_text_forward_delete (GtkText *text,
1110 guint old_lines = 0, old_height = 0;
1111 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
1112 gboolean frozen = FALSE;
1114 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1116 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1119 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1121 gtk_text_freeze (text);
1125 if (!text->freeze_count && text->line_start_cache != NULL)
1127 /* We need to undraw the cursor here, since we may later
1128 * delete the cursor's property
1130 undraw_cursor (text, FALSE);
1131 find_line_containing_point (text, text->point.index, TRUE);
1132 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1135 /* FIXME, or resizing after deleting will be odd */
1136 if (text->point.index < text->first_line_start_index)
1138 if (text->point.index + nchars >= text->first_line_start_index)
1140 text->first_line_start_index = text->point.index;
1141 while ((text->first_line_start_index > 0) &&
1142 (GTK_TEXT_INDEX (text, text->first_line_start_index - 1)
1144 text->first_line_start_index -= 1;
1148 text->first_line_start_index -= nchars;
1151 if (text->point.index < old_editable->selection_start_pos)
1152 old_editable->selection_start_pos -=
1153 MIN(nchars, old_editable->selection_start_pos - text->point.index);
1154 if (text->point.index < old_editable->selection_end_pos)
1155 old_editable->selection_end_pos -=
1156 MIN(nchars, old_editable->selection_end_pos - text->point.index);
1157 /* We'll reset the cursor later anyways if we aren't frozen */
1158 if (text->point.index < text->cursor_mark.index)
1159 move_mark_n (&text->cursor_mark,
1160 -MIN(nchars, text->cursor_mark.index - text->point.index));
1162 move_gap (text, text->point.index);
1164 text->gap_size += nchars;
1166 delete_text_property (text, nchars);
1168 if (!text->freeze_count && (text->line_start_cache != NULL))
1170 delete_expose (text, nchars, old_lines, old_height);
1171 draw_cursor (text, FALSE);
1175 gtk_text_thaw (text);
1181 gtk_text_set_position (GtkOldEditable *old_editable,
1184 GtkText *text = (GtkText *) old_editable;
1187 position = gtk_text_get_length (text);
1189 undraw_cursor (text, FALSE);
1190 text->cursor_mark = find_mark (text, position);
1191 find_cursor (text, TRUE);
1192 draw_cursor (text, FALSE);
1193 gtk_editable_select_region (GTK_EDITABLE (old_editable), 0, 0);
1197 gtk_text_get_chars (GtkOldEditable *old_editable,
1205 g_return_val_if_fail (GTK_IS_TEXT (old_editable), NULL);
1206 text = GTK_TEXT (old_editable);
1209 end_pos = TEXT_LENGTH (text);
1211 if ((start_pos < 0) ||
1212 (end_pos > TEXT_LENGTH (text)) ||
1213 (end_pos < start_pos))
1216 move_gap (text, TEXT_LENGTH (text));
1217 make_forward_space (text, 1);
1219 if (text->use_wchar)
1222 ch = text->text.wc[end_pos];
1223 text->text.wc[end_pos] = 0;
1224 retval = gdk_wcstombs (text->text.wc + start_pos);
1225 text->text.wc[end_pos] = ch;
1230 ch = text->text.ch[end_pos];
1231 text->text.ch[end_pos] = 0;
1232 retval = g_strdup (text->text.ch + start_pos);
1233 text->text.ch[end_pos] = ch;
1241 gtk_text_destroy (GtkObject *object)
1245 g_return_if_fail (GTK_IS_TEXT (object));
1247 text = GTK_TEXT (object);
1251 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1252 gtk_object_unref (GTK_OBJECT (text->hadj));
1257 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1258 gtk_object_unref (GTK_OBJECT (text->vadj));
1264 g_source_remove (text->timer);
1268 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1272 gtk_text_finalize (GObject *object)
1277 g_return_if_fail (GTK_IS_TEXT (object));
1279 text = GTK_TEXT (object);
1281 /* Clean up the internal structures */
1282 if (text->use_wchar)
1283 g_free (text->text.wc);
1285 g_free (text->text.ch);
1287 tmp_list = text->text_properties;
1290 destroy_text_property (tmp_list->data);
1291 tmp_list = tmp_list->next;
1294 if (text->current_font)
1295 text_font_unref (text->current_font);
1297 g_list_free (text->text_properties);
1299 if (text->use_wchar)
1301 if (text->scratch_buffer.wc)
1302 g_free (text->scratch_buffer.wc);
1306 if (text->scratch_buffer.ch)
1307 g_free (text->scratch_buffer.ch);
1310 g_list_free (text->tab_stops);
1312 G_OBJECT_CLASS (parent_class)->finalize (object);
1316 gtk_text_realize (GtkWidget *widget)
1319 GtkOldEditable *old_editable;
1320 GdkWindowAttr attributes;
1321 gint attributes_mask;
1323 g_return_if_fail (GTK_IS_TEXT (widget));
1325 text = GTK_TEXT (widget);
1326 old_editable = GTK_OLD_EDITABLE (widget);
1327 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1329 attributes.window_type = GDK_WINDOW_CHILD;
1330 attributes.x = widget->allocation.x;
1331 attributes.y = widget->allocation.y;
1332 attributes.width = widget->allocation.width;
1333 attributes.height = widget->allocation.height;
1334 attributes.wclass = GDK_INPUT_OUTPUT;
1335 attributes.visual = gtk_widget_get_visual (widget);
1336 attributes.colormap = gtk_widget_get_colormap (widget);
1337 attributes.event_mask = gtk_widget_get_events (widget);
1338 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1339 GDK_BUTTON_PRESS_MASK |
1340 GDK_BUTTON_RELEASE_MASK |
1341 GDK_BUTTON_MOTION_MASK |
1342 GDK_ENTER_NOTIFY_MASK |
1343 GDK_LEAVE_NOTIFY_MASK |
1344 GDK_KEY_PRESS_MASK);
1345 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1347 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1348 gdk_window_set_user_data (widget->window, text);
1350 attributes.x = (widget->style->xthickness + TEXT_BORDER_ROOM);
1351 attributes.y = (widget->style->ythickness + TEXT_BORDER_ROOM);
1352 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1353 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1355 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1356 attributes_mask |= GDK_WA_CURSOR;
1358 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1359 gdk_window_set_user_data (text->text_area, text);
1361 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1363 widget->style = gtk_style_attach (widget->style, widget->window);
1365 /* Can't call gtk_style_set_background here because it's handled specially */
1366 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1367 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1369 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1370 text->bg_gc = create_bg_gc (text);
1372 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1373 (gchar*) line_wrap_bits,
1377 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1378 (gchar*) line_arrow_bits,
1382 text->gc = gdk_gc_new (text->text_area);
1383 gdk_gc_set_exposures (text->gc, TRUE);
1384 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1386 realize_properties (text);
1387 gdk_window_show (text->text_area);
1388 init_properties (text);
1390 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1391 gtk_old_editable_claim_selection (old_editable, TRUE, GDK_CURRENT_TIME);
1393 recompute_geometry (text);
1397 gtk_text_style_set (GtkWidget *widget,
1398 GtkStyle *previous_style)
1400 GtkText *text = GTK_TEXT (widget);
1402 if (GTK_WIDGET_REALIZED (widget))
1404 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1405 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1409 gdk_gc_destroy (text->bg_gc);
1413 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1414 text->bg_gc = create_bg_gc (text);
1416 recompute_geometry (text);
1419 if (text->current_font)
1420 text_font_unref (text->current_font);
1421 text->current_font = get_text_font (gtk_style_get_font (widget->style));
1425 gtk_text_state_changed (GtkWidget *widget,
1426 GtkStateType previous_state)
1428 GtkText *text = GTK_TEXT (widget);
1430 if (GTK_WIDGET_REALIZED (widget))
1432 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1433 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1438 gtk_text_unrealize (GtkWidget *widget)
1442 g_return_if_fail (GTK_IS_TEXT (widget));
1444 text = GTK_TEXT (widget);
1446 gdk_window_set_user_data (text->text_area, NULL);
1447 gdk_window_destroy (text->text_area);
1448 text->text_area = NULL;
1450 gdk_gc_destroy (text->gc);
1455 gdk_gc_destroy (text->bg_gc);
1459 gdk_pixmap_unref (text->line_wrap_bitmap);
1460 gdk_pixmap_unref (text->line_arrow_bitmap);
1462 unrealize_properties (text);
1466 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1467 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1471 clear_focus_area (GtkText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1473 GtkWidget *widget = GTK_WIDGET (text);
1476 gint ythick = TEXT_BORDER_ROOM + widget->style->ythickness;
1477 gint xthick = TEXT_BORDER_ROOM + widget->style->xthickness;
1481 if (area_width == 0 || area_height == 0)
1484 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1486 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1488 gdk_gc_set_ts_origin (text->bg_gc,
1489 (- text->first_onscreen_hor_pixel + xthick) % width,
1490 (- text->first_onscreen_ver_pixel + ythick) % height);
1495 gc = widget->style->base_gc[widget->state];
1497 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1498 area_x, area_y, area_width, area_height);
1502 gtk_text_draw_focus (GtkWidget *widget)
1508 g_return_if_fail (GTK_IS_TEXT (widget));
1510 text = GTK_TEXT (widget);
1512 if (GTK_WIDGET_DRAWABLE (widget))
1514 gint ythick = widget->style->ythickness;
1515 gint xthick = widget->style->xthickness;
1516 gint xextra = TEXT_BORDER_ROOM;
1517 gint yextra = TEXT_BORDER_ROOM;
1519 TDEBUG (("in gtk_text_draw_focus\n"));
1523 width = widget->allocation.width;
1524 height = widget->allocation.height;
1526 if (GTK_WIDGET_HAS_FOCUS (widget))
1535 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1536 NULL, widget, "text",
1538 widget->allocation.width,
1539 widget->allocation.height);
1542 gtk_paint_shadow (widget->style, widget->window,
1543 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1544 NULL, widget, "text",
1545 x, y, width, height);
1549 width -= 2 * xthick;
1550 height -= 2 * ythick;
1553 clear_focus_area (text, x, y, width, yextra);
1555 clear_focus_area (text, x, y + yextra,
1556 xextra, y + height - 2 * yextra);
1558 clear_focus_area (text, x + width - xextra, y + yextra,
1559 xextra, height - 2 * ythick);
1561 clear_focus_area (text, x, x + height - yextra, width, yextra);
1565 TDEBUG (("in gtk_text_draw_focus (undrawable !!!)\n"));
1570 gtk_text_size_request (GtkWidget *widget,
1571 GtkRequisition *requisition)
1579 g_return_if_fail (GTK_IS_TEXT (widget));
1580 g_return_if_fail (requisition != NULL);
1582 xthickness = widget->style->xthickness + TEXT_BORDER_ROOM;
1583 ythickness = widget->style->ythickness + TEXT_BORDER_ROOM;
1585 font = gtk_style_get_font (widget->style);
1587 char_height = MIN_TEXT_HEIGHT_LINES * (font->ascent +
1590 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (font,
1591 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1595 requisition->width = char_width + xthickness * 2;
1596 requisition->height = char_height + ythickness * 2;
1600 gtk_text_size_allocate (GtkWidget *widget,
1601 GtkAllocation *allocation)
1605 g_return_if_fail (GTK_IS_TEXT (widget));
1606 g_return_if_fail (allocation != NULL);
1608 text = GTK_TEXT (widget);
1610 widget->allocation = *allocation;
1611 if (GTK_WIDGET_REALIZED (widget))
1613 gdk_window_move_resize (widget->window,
1614 allocation->x, allocation->y,
1615 allocation->width, allocation->height);
1617 gdk_window_move_resize (text->text_area,
1618 widget->style->xthickness + TEXT_BORDER_ROOM,
1619 widget->style->ythickness + TEXT_BORDER_ROOM,
1620 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->xthickness +
1621 (gint)TEXT_BORDER_ROOM) * 2),
1622 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->ythickness +
1623 (gint)TEXT_BORDER_ROOM) * 2));
1625 recompute_geometry (text);
1630 gtk_text_expose (GtkWidget *widget,
1631 GdkEventExpose *event)
1633 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1634 g_return_val_if_fail (event != NULL, FALSE);
1636 if (event->window == GTK_TEXT (widget)->text_area)
1638 TDEBUG (("in gtk_text_expose (expose)\n"));
1639 expose_text (GTK_TEXT (widget), &event->area, TRUE);
1641 else if (event->count == 0)
1643 TDEBUG (("in gtk_text_expose (focus)\n"));
1644 gtk_text_draw_focus (widget);
1651 gtk_text_scroll_timeout (gpointer data)
1655 GdkModifierType mask;
1657 GDK_THREADS_ENTER ();
1659 text = GTK_TEXT (data);
1662 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1664 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1666 GdkEvent *event = gdk_event_new (GDK_MOTION_NOTIFY);
1668 event->motion.is_hint = 0;
1669 event->motion.x = x;
1670 event->motion.y = y;
1671 event->motion.state = mask;
1673 gtk_text_motion_notify (GTK_WIDGET (text), (GdkEventMotion *)event);
1675 gdk_event_free (event);
1678 GDK_THREADS_LEAVE ();
1684 gtk_text_button_press (GtkWidget *widget,
1685 GdkEventButton *event)
1688 GtkOldEditable *old_editable;
1690 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1691 g_return_val_if_fail (event != NULL, FALSE);
1693 text = GTK_TEXT (widget);
1694 old_editable = GTK_OLD_EDITABLE (widget);
1696 if (text->button && (event->button != text->button))
1699 text->button = event->button;
1701 if (!GTK_WIDGET_HAS_FOCUS (widget))
1702 gtk_widget_grab_focus (widget);
1704 if (event->button == 1)
1706 switch (event->type)
1708 case GDK_BUTTON_PRESS:
1709 gtk_grab_add (widget);
1711 undraw_cursor (text, FALSE);
1712 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1713 draw_cursor (text, FALSE);
1715 /* Set it now, so we display things right. We'll unset it
1716 * later if things don't work out */
1717 old_editable->has_selection = TRUE;
1718 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1719 text->cursor_mark.index,
1720 text->cursor_mark.index);
1724 case GDK_2BUTTON_PRESS:
1725 gtk_text_select_word (text, event->time);
1728 case GDK_3BUTTON_PRESS:
1729 gtk_text_select_line (text, event->time);
1736 else if (event->type == GDK_BUTTON_PRESS)
1738 if ((event->button == 2) && old_editable->editable)
1740 if (old_editable->selection_start_pos == old_editable->selection_end_pos ||
1741 old_editable->has_selection)
1743 undraw_cursor (text, FALSE);
1744 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1745 draw_cursor (text, FALSE);
1749 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1750 gdk_atom_intern_static_string ("UTF8_STRING"),
1755 GdkDisplay *display = gtk_widget_get_display (widget);
1757 gtk_grab_add (widget);
1759 undraw_cursor (text, FALSE);
1760 find_mouse_cursor (text, event->x, event->y);
1761 draw_cursor (text, FALSE);
1763 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1764 text->cursor_mark.index,
1765 text->cursor_mark.index);
1767 old_editable->has_selection = FALSE;
1768 if (gdk_selection_owner_get_for_display (display,
1769 GDK_SELECTION_PRIMARY) == widget->window)
1770 gtk_selection_owner_set_for_display (display,
1772 GDK_SELECTION_PRIMARY,
1781 gtk_text_button_release (GtkWidget *widget,
1782 GdkEventButton *event)
1785 GtkOldEditable *old_editable;
1786 GdkDisplay *display;
1788 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1789 g_return_val_if_fail (event != NULL, FALSE);
1791 text = GTK_TEXT (widget);
1793 gtk_grab_remove (widget);
1795 if (text->button != event->button)
1802 g_source_remove (text->timer);
1806 if (event->button == 1)
1808 text = GTK_TEXT (widget);
1809 old_editable = GTK_OLD_EDITABLE (widget);
1810 display = gtk_widget_get_display (widget);
1812 gtk_grab_remove (widget);
1814 old_editable->has_selection = FALSE;
1815 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1817 if (gtk_selection_owner_set_for_display (display,
1819 GDK_SELECTION_PRIMARY,
1821 old_editable->has_selection = TRUE;
1823 gtk_text_update_text (old_editable, old_editable->selection_start_pos,
1824 old_editable->selection_end_pos);
1828 if (gdk_selection_owner_get_for_display (display,
1829 GDK_SELECTION_PRIMARY) == widget->window)
1830 gtk_selection_owner_set_for_display (display,
1832 GDK_SELECTION_PRIMARY,
1836 else if (event->button == 3)
1838 gtk_grab_remove (widget);
1841 undraw_cursor (text, FALSE);
1842 find_cursor (text, TRUE);
1843 draw_cursor (text, FALSE);
1849 gtk_text_motion_notify (GtkWidget *widget,
1850 GdkEventMotion *event)
1855 GdkModifierType mask;
1857 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1858 g_return_val_if_fail (event != NULL, FALSE);
1860 text = GTK_TEXT (widget);
1864 mask = event->state;
1865 if (event->is_hint || (text->text_area != event->window))
1867 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1870 if ((text->button == 0) ||
1871 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1874 gdk_window_get_size (text->text_area, NULL, &height);
1876 if ((y < 0) || (y > height))
1878 if (text->timer == 0)
1880 text->timer = g_timeout_add (SCROLL_TIME,
1881 gtk_text_scroll_timeout,
1885 scroll_int (text, y/2);
1887 scroll_int (text, (y - height)/2);
1893 undraw_cursor (GTK_TEXT (widget), FALSE);
1894 find_mouse_cursor (GTK_TEXT (widget), x, y);
1895 draw_cursor (GTK_TEXT (widget), FALSE);
1897 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1898 GTK_OLD_EDITABLE (text)->selection_start_pos,
1899 text->cursor_mark.index);
1905 gtk_text_insert_text (GtkEditable *editable,
1906 const gchar *new_text,
1907 gint new_text_length,
1910 GtkText *text = GTK_TEXT (editable);
1912 GdkColor *fore, *back;
1914 TextProperty *property;
1916 gtk_text_set_point (text, *position);
1918 property = MARK_CURRENT_PROPERTY (&text->point);
1919 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
1920 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
1921 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
1923 gtk_text_insert (text, font, fore, back, new_text, new_text_length);
1925 *position = text->point.index;
1929 gtk_text_delete_text (GtkEditable *editable,
1935 g_return_if_fail (start_pos >= 0);
1937 text = GTK_TEXT (editable);
1939 gtk_text_set_point (text, start_pos);
1941 end_pos = TEXT_LENGTH (text);
1943 if (end_pos > start_pos)
1944 gtk_text_forward_delete (text, end_pos - start_pos);
1948 gtk_text_key_press (GtkWidget *widget,
1952 GtkOldEditable *old_editable;
1957 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1958 g_return_val_if_fail (event != NULL, FALSE);
1960 text = GTK_TEXT (widget);
1961 old_editable = GTK_OLD_EDITABLE (widget);
1963 key = event->keyval;
1966 if ((GTK_OLD_EDITABLE(text)->editable == FALSE))
1968 switch (event->keyval)
1972 if (event->state & GDK_CONTROL_MASK)
1973 scroll_int (text, -text->vadj->value);
1979 if (event->state & GDK_CONTROL_MASK)
1980 scroll_int (text, +text->vadj->upper);
1984 case GDK_KP_Page_Up:
1985 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
1986 case GDK_KP_Page_Down:
1987 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
1989 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
1991 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
1994 if (event->state & GDK_CONTROL_MASK)
1995 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2006 gint extend_selection;
2008 guint initial_pos = old_editable->current_pos;
2010 text->point = find_mark (text, text->cursor_mark.index);
2012 extend_selection = event->state & GDK_SHIFT_MASK;
2013 extend_start = FALSE;
2015 if (extend_selection)
2017 old_editable->has_selection = TRUE;
2019 if (old_editable->selection_start_pos == old_editable->selection_end_pos)
2021 old_editable->selection_start_pos = text->point.index;
2022 old_editable->selection_end_pos = text->point.index;
2025 extend_start = (text->point.index == old_editable->selection_start_pos);
2028 switch (event->keyval)
2032 if (event->state & GDK_CONTROL_MASK)
2033 move_cursor_buffer_ver (text, -1);
2035 gtk_text_move_beginning_of_line (text);
2039 if (event->state & GDK_CONTROL_MASK)
2040 move_cursor_buffer_ver (text, +1);
2042 gtk_text_move_end_of_line (text);
2044 case GDK_KP_Page_Up:
2045 case GDK_Page_Up: move_cursor_page_ver (text, -1); break;
2046 case GDK_KP_Page_Down:
2047 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
2048 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
2050 case GDK_Up: move_cursor_ver (text, -1); break;
2052 case GDK_Down: move_cursor_ver (text, +1); break;
2055 if (event->state & GDK_CONTROL_MASK)
2056 gtk_text_move_backward_word (text);
2058 move_cursor_hor (text, -1);
2062 if (event->state & GDK_CONTROL_MASK)
2063 gtk_text_move_forward_word (text);
2065 move_cursor_hor (text, +1);
2069 if (event->state & GDK_CONTROL_MASK)
2070 gtk_text_delete_backward_word (text);
2072 gtk_text_delete_backward_character (text);
2075 gtk_text_delete_line (text);
2079 if (event->state & GDK_SHIFT_MASK)
2081 extend_selection = FALSE;
2082 gtk_editable_paste_clipboard (GTK_EDITABLE (old_editable));
2084 else if (event->state & GDK_CONTROL_MASK)
2086 gtk_editable_copy_clipboard (GTK_EDITABLE (old_editable));
2090 /* gtk_toggle_insert(text) -- IMPLEMENT */
2095 if (event->state & GDK_CONTROL_MASK)
2096 gtk_text_delete_forward_word (text);
2097 else if (event->state & GDK_SHIFT_MASK)
2099 extend_selection = FALSE;
2100 gtk_editable_cut_clipboard (GTK_EDITABLE (old_editable));
2103 gtk_text_delete_forward_character (text);
2106 case GDK_ISO_Left_Tab:
2108 position = text->point.index;
2109 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\t", 1, &position);
2113 if (event->state & GDK_CONTROL_MASK)
2114 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2117 position = text->point.index;
2118 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\n", 1, &position);
2122 /* Don't insert literally */
2129 if (event->state & GDK_CONTROL_MASK)
2131 if ((key >= 'A') && (key <= 'Z'))
2134 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2136 (* control_keys[(int) (key - 'a')]) (old_editable, event->time);
2142 else if (event->state & GDK_MOD1_MASK)
2144 if ((key >= 'A') && (key <= 'Z'))
2147 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2149 (* alt_keys[(int) (key - 'a')]) (old_editable, event->time);
2155 else if (event->length > 0)
2157 extend_selection = FALSE;
2159 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
2160 position = text->point.index;
2161 gtk_editable_insert_text (GTK_EDITABLE (old_editable), event->string, event->length, &position);
2167 if (return_val && (old_editable->current_pos != initial_pos))
2169 if (extend_selection)
2171 if (old_editable->current_pos < old_editable->selection_start_pos)
2172 gtk_text_set_selection (old_editable, old_editable->current_pos,
2173 old_editable->selection_end_pos);
2174 else if (old_editable->current_pos > old_editable->selection_end_pos)
2175 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2176 old_editable->current_pos);
2180 gtk_text_set_selection (old_editable, old_editable->current_pos,
2181 old_editable->selection_end_pos);
2183 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2184 old_editable->current_pos);
2188 gtk_text_set_selection (old_editable, 0, 0);
2190 gtk_old_editable_claim_selection (old_editable,
2191 old_editable->selection_start_pos != old_editable->selection_end_pos,
2200 gtk_text_adjustment (GtkAdjustment *adjustment,
2203 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2204 g_return_if_fail (GTK_IS_TEXT (text));
2206 /* Just ignore it if we haven't been size-allocated and realized yet */
2207 if (text->line_start_cache == NULL)
2210 if (adjustment == text->hadj)
2212 g_warning ("horizontal scrolling not implemented");
2216 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2220 undraw_cursor (text, FALSE);
2223 scroll_down (text, diff);
2224 else /* if (diff < 0) */
2225 scroll_up (text, diff);
2227 draw_cursor (text, FALSE);
2229 text->last_ver_value = adjustment->value;
2235 gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
2238 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2239 g_return_if_fail (GTK_IS_TEXT (text));
2241 if (adjustment == text->hadj)
2242 gtk_text_set_adjustments (text, NULL, text->vadj);
2243 if (adjustment == text->vadj)
2244 gtk_text_set_adjustments (text, text->hadj, NULL);
2248 static GtkPropertyMark
2249 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2251 GtkPropertyMark mark;
2253 mark = find_mark_near (text, point_position, near);
2255 while (mark.index > 0 &&
2256 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2257 decrement_mark (&mark);
2263 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2265 tab_cont->pixel_offset = 0;
2266 tab_cont->tab_start.tab_stops = text->tab_stops;
2267 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2269 if (!tab_cont->tab_start.to_next_tab)
2270 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2274 line_params_iterate (GtkText* text,
2275 const GtkPropertyMark* mark0,
2276 const PrevTabCont* tab_mark0,
2279 LineIteratorFunction iter)
2280 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2281 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2282 * for each line following MARK, iteration continues unless ITER_CALL
2285 GtkPropertyMark mark = *mark0;
2286 PrevTabCont tab_conts[2];
2287 LineParams *lp, lpbuf;
2288 gint tab_cont_index = 0;
2291 tab_conts[0] = *tab_mark0;
2293 init_tab_cont (text, tab_conts);
2298 lp = g_chunk_new (LineParams, params_mem_chunk);
2302 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2303 tab_conts + (tab_cont_index + 1) % 2);
2305 if ((*iter) (text, lp, data))
2308 if (LAST_INDEX (text, lp->end))
2312 advance_mark (&mark);
2313 tab_cont_index = (tab_cont_index + 1) % 2;
2318 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2320 FetchLinesData *fldata = (FetchLinesData*) data;
2322 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2324 switch (fldata->fl_type)
2326 case FetchLinesCount:
2327 if (!text->line_wrap || !lp->wraps)
2330 if (fldata->data >= fldata->data_max)
2334 case FetchLinesPixels:
2336 fldata->data += LINE_HEIGHT(*lp);
2338 if (fldata->data >= fldata->data_max)
2348 fetch_lines (GtkText* text,
2349 const GtkPropertyMark* mark0,
2350 const PrevTabCont* tab_cont0,
2354 FetchLinesData fl_data;
2356 fl_data.new_lines = NULL;
2358 fl_data.data_max = data;
2359 fl_data.fl_type = fl_type;
2361 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2363 return g_list_reverse (fl_data.new_lines);
2367 fetch_lines_backward (GtkText* text)
2369 GList *new_line_start;
2370 GtkPropertyMark mark;
2372 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2375 mark = find_this_line_start_mark (text,
2376 CACHE_DATA(text->line_start_cache).start.index - 1,
2377 &CACHE_DATA(text->line_start_cache).start);
2379 new_line_start = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2381 while (new_line_start->next)
2382 new_line_start = new_line_start->next;
2384 new_line_start->next = text->line_start_cache;
2385 text->line_start_cache->prev = new_line_start;
2389 fetch_lines_forward (GtkText* text, gint line_count)
2391 GtkPropertyMark mark;
2392 GList* line = text->line_start_cache;
2397 mark = CACHE_DATA(line).end;
2399 if (LAST_INDEX (text, mark))
2402 advance_mark(&mark);
2404 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2407 line->next->prev = line;
2410 /* Compute the number of lines, and vertical pixels for n characters
2411 * starting from the point
2414 compute_lines_pixels (GtkText* text, guint char_count,
2415 guint *lines, guint *pixels)
2417 GList *line = text->current_line;
2418 gint chars_left = char_count;
2423 /* If chars_left == 0, that means we're joining two lines in a
2424 * deletion, so add in the values for the next line as well
2426 for (; line && chars_left >= 0; line = line->next)
2428 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2430 if (line == text->current_line)
2431 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2433 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2435 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2439 chars_left = 0; /* force another loop */
2442 fetch_lines_forward (text, 1);
2447 total_line_height (GtkText* text, GList* line, gint line_count)
2451 for (; line && line_count > 0; line = line->next)
2453 height += LINE_HEIGHT(CACHE_DATA(line));
2455 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2459 fetch_lines_forward (text, line_count);
2466 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2468 if (old == text->line_start_cache)
2472 for (; old_line_count > 0; old_line_count -= 1)
2474 while (text->line_start_cache &&
2476 CACHE_DATA(text->line_start_cache).wraps)
2477 remove_cache_line(text, text->line_start_cache);
2479 remove_cache_line(text, text->line_start_cache);
2482 last = g_list_last (new);
2484 last->next = text->line_start_cache;
2486 if (text->line_start_cache)
2487 text->line_start_cache->prev = last;
2489 text->line_start_cache = new;
2495 g_assert (old->prev);
2499 for (; old_line_count > 0; old_line_count -= 1)
2501 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2502 old = remove_cache_line (text, old);
2504 old = remove_cache_line (text, old);
2510 last = g_list_last (new);
2520 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2522 GList* cache = text->current_line;
2525 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2528 for (; cache; cache = cache->next)
2530 GtkPropertyMark *start = &CACHE_DATA(cache).start;
2531 GtkPropertyMark *end = &CACHE_DATA(cache).end;
2533 start->index -= nchars;
2534 end->index -= nchars;
2536 if (LAST_INDEX (text, text->point) &&
2537 start->index == text->point.index)
2538 *start = text->point;
2539 else if (start->property == text->point.property)
2540 start->offset = start->index - (text->point.index - text->point.offset);
2542 if (LAST_INDEX (text, text->point) &&
2543 end->index == text->point.index)
2545 if (end->property == text->point.property)
2546 end->offset = end->index - (text->point.index - text->point.offset);
2548 /*TEXT_ASSERT_MARK(text, start, "start");*/
2549 /*TEXT_ASSERT_MARK(text, end, "end");*/
2554 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2556 GtkWidget *widget = GTK_WIDGET (text);
2559 guint new_pixels = 0;
2561 GList* new_line = NULL;
2564 text->cursor_virtual_x = 0;
2566 correct_cache_delete (text, nchars, old_lines);
2568 pixel_height = pixel_height_of(text, text->current_line) -
2569 LINE_HEIGHT(CACHE_DATA(text->current_line));
2571 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2572 CACHE_DATA(text->current_line).start = text->point;
2574 new_line = fetch_lines (text,
2575 &CACHE_DATA(text->current_line).start,
2576 &CACHE_DATA(text->current_line).tab_cont,
2580 swap_lines (text, text->current_line, new_line, old_lines);
2582 text->current_line = new_line;
2584 new_pixels = total_line_height (text, new_line, 1);
2586 gdk_window_get_size (text->text_area, &width, &height);
2588 if (old_pixels != new_pixels)
2590 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2592 gdk_draw_pixmap (text->text_area,
2596 pixel_height + old_pixels,
2598 pixel_height + new_pixels,
2602 text->vadj->upper += new_pixels;
2603 text->vadj->upper -= old_pixels;
2604 adjust_adj (text, text->vadj);
2608 rect.y = pixel_height;
2610 rect.height = new_pixels;
2612 expose_text (text, &rect, FALSE);
2613 gtk_text_draw_focus ( (GtkWidget *) text);
2615 text->cursor_mark = text->point;
2617 find_cursor (text, TRUE);
2619 if (old_pixels != new_pixels)
2621 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2624 rect.y = pixel_height + new_pixels;
2626 rect.height = height - rect.y;
2628 expose_text (text, &rect, FALSE);
2631 process_exposes (text);
2638 /* note, the point has already been moved forward */
2640 correct_cache_insert (GtkText* text, gint nchars)
2643 GtkPropertyMark *start;
2644 GtkPropertyMark *end;
2645 gboolean was_split = FALSE;
2647 /* We need to distinguish whether the property was split in the
2648 * insert or not, so we check if the point (which points after
2649 * the insertion here), points to the same character as the
2650 * point before. Ugh.
2654 GtkPropertyMark tmp_mark = text->point;
2655 move_mark_n (&tmp_mark, -1);
2657 if (tmp_mark.property != text->point.property)
2661 /* If we inserted a property exactly at the beginning of the
2662 * line, we have to correct here, or fetch_lines will
2665 start = &CACHE_DATA(text->current_line).start;
2667 /* Check if if we split exactly at the beginning of the line:
2668 * (was_split won't be set if we are inserting at the end of the text,
2669 * so we don't check)
2671 if (start->offset == MARK_CURRENT_PROPERTY (start)->length)
2672 SET_PROPERTY_MARK (start, start->property->next, 0);
2673 /* Check if we inserted a property at the beginning of the text: */
2674 else if (was_split &&
2675 (start->property == text->point.property) &&
2676 (start->index == text->point.index - nchars))
2677 SET_PROPERTY_MARK (start, start->property->prev, 0);
2679 /* Now correct the offsets, and check for start or end marks that
2680 * are after the point, yet point to a property before the point's
2681 * property. This indicates that they are meant to point to the
2682 * second half of a property we split in insert_text_property(), so
2683 * we fix them up that way.
2685 cache = text->current_line->next;
2687 for (; cache; cache = cache->next)
2689 start = &CACHE_DATA(cache).start;
2690 end = &CACHE_DATA(cache).end;
2692 if (LAST_INDEX (text, text->point) &&
2693 start->index == text->point.index)
2694 *start = text->point;
2695 else if (start->index >= text->point.index - nchars)
2697 if (!was_split && start->property == text->point.property)
2698 move_mark_n(start, nchars);
2701 if (start->property->next &&
2702 (start->property->next->next == text->point.property))
2704 g_assert (start->offset >= MARK_CURRENT_PROPERTY (start)->length);
2705 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2706 start->property = text->point.property;
2708 start->index += nchars;
2712 if (LAST_INDEX (text, text->point) &&
2713 end->index == text->point.index)
2715 if (end->index >= text->point.index - nchars)
2717 if (!was_split && end->property == text->point.property)
2718 move_mark_n(end, nchars);
2721 if (end->property->next &&
2722 (end->property->next->next == text->point.property))
2724 g_assert (end->offset >= MARK_CURRENT_PROPERTY (end)->length);
2725 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2726 end->property = text->point.property;
2728 end->index += nchars;
2732 /*TEXT_ASSERT_MARK(text, start, "start");*/
2733 /*TEXT_ASSERT_MARK(text, end, "end");*/
2739 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2740 guint new_line_count)
2742 GtkWidget *widget = GTK_WIDGET (text);
2745 guint new_pixels = 0;
2747 GList* new_lines = NULL;
2750 text->cursor_virtual_x = 0;
2752 undraw_cursor (text, FALSE);
2754 correct_cache_insert (text, nchars);
2756 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2758 pixel_height = pixel_height_of(text, text->current_line) -
2759 LINE_HEIGHT(CACHE_DATA(text->current_line));
2761 new_lines = fetch_lines (text,
2762 &CACHE_DATA(text->current_line).start,
2763 &CACHE_DATA(text->current_line).tab_cont,
2767 swap_lines (text, text->current_line, new_lines, 1);
2769 text->current_line = new_lines;
2771 new_pixels = total_line_height (text, new_lines, new_line_count);
2773 gdk_window_get_size (text->text_area, &width, &height);
2775 if (old_pixels != new_pixels)
2777 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2779 gdk_draw_pixmap (text->text_area,
2783 pixel_height + old_pixels,
2785 pixel_height + new_pixels,
2787 height + (old_pixels - new_pixels) - pixel_height);
2790 text->vadj->upper += new_pixels;
2791 text->vadj->upper -= old_pixels;
2792 adjust_adj (text, text->vadj);
2796 rect.y = pixel_height;
2798 rect.height = new_pixels;
2800 expose_text (text, &rect, FALSE);
2801 gtk_text_draw_focus ( (GtkWidget *) text);
2803 text->cursor_mark = text->point;
2805 find_cursor (text, TRUE);
2807 draw_cursor (text, FALSE);
2809 if (old_pixels != new_pixels)
2811 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2814 rect.y = pixel_height + new_pixels;
2816 rect.height = height - rect.y;
2818 expose_text (text, &rect, FALSE);
2821 process_exposes (text);
2824 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2829 /* Text property functions */
2832 font_hash (gconstpointer font)
2834 return gdk_font_id ((const GdkFont*) font);
2837 static GHashTable *font_cache_table = NULL;
2840 get_text_font (GdkFont* gfont)
2845 if (!font_cache_table)
2846 font_cache_table = g_hash_table_new (font_hash, (GEqualFunc) gdk_font_equal);
2848 tf = g_hash_table_lookup (font_cache_table, gfont);
2856 tf = g_new (GtkTextFont, 1);
2859 tf->gdk_font = gfont;
2860 gdk_font_ref (gfont);
2862 for(i = 0; i < 256; i += 1)
2863 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2865 g_hash_table_insert (font_cache_table, gfont, tf);
2871 text_font_unref (GtkTextFont *text_font)
2873 text_font->ref_count--;
2874 if (text_font->ref_count == 0)
2876 g_hash_table_remove (font_cache_table, text_font->gdk_font);
2877 gdk_font_unref (text_font->gdk_font);
2883 text_properties_equal (TextProperty* prop, GdkFont* font, const GdkColor *fore, const GdkColor *back)
2885 if (prop->flags & PROPERTY_FONT)
2888 GtkTextFont *text_font;
2893 text_font = get_text_font (font);
2895 retval = (prop->font == text_font);
2896 text_font_unref (text_font);
2905 if (prop->flags & PROPERTY_FOREGROUND)
2907 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
2914 if (prop->flags & PROPERTY_BACKGROUND)
2916 if (!back || !gdk_color_equal (&prop->back_color, back))
2927 realize_property (GtkText *text, TextProperty *prop)
2929 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2931 if (prop->flags & PROPERTY_FOREGROUND)
2932 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
2934 if (prop->flags & PROPERTY_BACKGROUND)
2935 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
2939 realize_properties (GtkText *text)
2941 GList *tmp_list = text->text_properties;
2945 realize_property (text, tmp_list->data);
2947 tmp_list = tmp_list->next;
2952 unrealize_property (GtkText *text, TextProperty *prop)
2954 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2956 if (prop->flags & PROPERTY_FOREGROUND)
2957 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
2959 if (prop->flags & PROPERTY_BACKGROUND)
2960 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
2964 unrealize_properties (GtkText *text)
2966 GList *tmp_list = text->text_properties;
2970 unrealize_property (text, tmp_list->data);
2972 tmp_list = tmp_list->next;
2976 static TextProperty*
2977 new_text_property (GtkText *text, GdkFont *font, const GdkColor* fore,
2978 const GdkColor* back, guint length)
2982 if (text_property_chunk == NULL)
2984 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
2985 sizeof(TextProperty),
2986 1024*sizeof(TextProperty),
2990 prop = g_chunk_new(TextProperty, text_property_chunk);
2995 prop->flags |= PROPERTY_FONT;
2996 prop->font = get_text_font (font);
3003 prop->flags |= PROPERTY_FOREGROUND;
3004 prop->fore_color = *fore;
3009 prop->flags |= PROPERTY_BACKGROUND;
3010 prop->back_color = *back;
3013 prop->length = length;
3015 if (GTK_WIDGET_REALIZED (text))
3016 realize_property (text, prop);
3022 destroy_text_property (TextProperty *prop)
3025 text_font_unref (prop->font);
3027 g_mem_chunk_free (text_property_chunk, prop);
3030 /* Flop the memory between the point and the gap around like a
3033 move_gap (GtkText* text, guint index)
3035 if (text->gap_position < index)
3037 gint diff = index - text->gap_position;
3039 if (text->use_wchar)
3040 g_memmove (text->text.wc + text->gap_position,
3041 text->text.wc + text->gap_position + text->gap_size,
3042 diff*sizeof (GdkWChar));
3044 g_memmove (text->text.ch + text->gap_position,
3045 text->text.ch + text->gap_position + text->gap_size,
3048 text->gap_position = index;
3050 else if (text->gap_position > index)
3052 gint diff = text->gap_position - index;
3054 if (text->use_wchar)
3055 g_memmove (text->text.wc + index + text->gap_size,
3056 text->text.wc + index,
3057 diff*sizeof (GdkWChar));
3059 g_memmove (text->text.ch + index + text->gap_size,
3060 text->text.ch + index,
3063 text->gap_position = index;
3067 /* Increase the gap size. */
3069 make_forward_space (GtkText* text, guint len)
3071 if (text->gap_size < len)
3073 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3075 if (sum >= text->text_len)
3079 while (i <= sum) i <<= 1;
3081 if (text->use_wchar)
3082 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3083 i*sizeof(GdkWChar));
3085 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3089 if (text->use_wchar)
3090 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3091 text->text.wc + text->gap_position + text->gap_size,
3092 (text->text_end - (text->gap_position + text->gap_size))
3095 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3096 text->text.ch + text->gap_position + text->gap_size,
3097 text->text_end - (text->gap_position + text->gap_size));
3099 text->text_end += len*2;
3100 text->gap_size += len*2;
3104 /* Inserts into the text property list a list element that guarantees
3105 * that for len characters following the point, text has the correct
3106 * property. does not move point. adjusts text_properties_point and
3107 * text_properties_point_offset relative to the current value of
3110 insert_text_property (GtkText* text, GdkFont* font,
3111 const GdkColor *fore, const GdkColor* back, guint len)
3113 GtkPropertyMark *mark = &text->point;
3114 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3115 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3117 if (MARK_OFFSET(mark) == 0)
3119 /* Point is on the boundary of two properties.
3120 * If it is the same as either, grow, else insert
3123 if (text_properties_equal(forward_prop, font, fore, back))
3125 /* Grow the property in front of us. */
3127 MARK_PROPERTY_LENGTH(mark) += len;
3129 else if (backward_prop &&
3130 text_properties_equal(backward_prop, font, fore, back))
3132 /* Grow property behind us, point property and offset
3135 SET_PROPERTY_MARK (&text->point,
3136 MARK_PREV_LIST_PTR (mark),
3137 backward_prop->length);
3139 backward_prop->length += len;
3141 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3142 (forward_prop->length == 1))
3144 /* Next property just has last position, take it over */
3146 if (GTK_WIDGET_REALIZED (text))
3147 unrealize_property (text, forward_prop);
3149 forward_prop->flags = 0;
3152 forward_prop->flags |= PROPERTY_FONT;
3153 forward_prop->font = get_text_font (font);
3156 forward_prop->font = NULL;
3160 forward_prop->flags |= PROPERTY_FOREGROUND;
3161 forward_prop->fore_color = *fore;
3165 forward_prop->flags |= PROPERTY_BACKGROUND;
3166 forward_prop->back_color = *back;
3168 forward_prop->length += len;
3170 if (GTK_WIDGET_REALIZED (text))
3171 realize_property (text, forward_prop);
3175 /* Splice a new property into the list. */
3177 GList* new_prop = g_list_alloc();
3179 new_prop->next = MARK_LIST_PTR(mark);
3180 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3181 new_prop->next->prev = new_prop;
3184 new_prop->prev->next = new_prop;
3186 new_prop->data = new_text_property (text, font, fore, back, len);
3188 SET_PROPERTY_MARK (mark, new_prop, 0);
3193 /* The following will screw up the line_start cache,
3194 * we'll fix it up in correct_cache_insert
3197 /* In the middle of forward_prop, if properties are equal,
3198 * just add to its length, else split it into two and splice
3200 if (text_properties_equal (forward_prop, font, fore, back))
3202 forward_prop->length += len;
3204 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3205 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3207 /* Inserting before only the last position in the text */
3210 forward_prop->length -= 1;
3212 new_prop = g_list_alloc();
3213 new_prop->data = new_text_property (text, font, fore, back, len+1);
3214 new_prop->prev = MARK_LIST_PTR(mark);
3215 new_prop->next = NULL;
3216 MARK_NEXT_LIST_PTR(mark) = new_prop;
3218 SET_PROPERTY_MARK (mark, new_prop, 0);
3222 GList* new_prop = g_list_alloc();
3223 GList* new_prop_forward = g_list_alloc();
3224 gint old_length = forward_prop->length;
3225 GList* next = MARK_NEXT_LIST_PTR(mark);
3227 /* Set the new lengths according to where they are split. Construct
3228 * two new properties. */
3229 forward_prop->length = MARK_OFFSET(mark);
3231 new_prop_forward->data =
3232 new_text_property(text,
3233 forward_prop->flags & PROPERTY_FONT ?
3234 forward_prop->font->gdk_font : NULL,
3235 forward_prop->flags & PROPERTY_FOREGROUND ?
3236 &forward_prop->fore_color : NULL,
3237 forward_prop->flags & PROPERTY_BACKGROUND ?
3238 &forward_prop->back_color : NULL,
3239 old_length - forward_prop->length);
3241 new_prop->data = new_text_property(text, font, fore, back, len);
3243 /* Now splice things in. */
3244 MARK_NEXT_LIST_PTR(mark) = new_prop;
3245 new_prop->prev = MARK_LIST_PTR(mark);
3247 new_prop->next = new_prop_forward;
3248 new_prop_forward->prev = new_prop;
3250 new_prop_forward->next = next;
3253 next->prev = new_prop_forward;
3255 SET_PROPERTY_MARK (mark, new_prop, 0);
3259 while (text->text_properties_end->next)
3260 text->text_properties_end = text->text_properties_end->next;
3262 while (text->text_properties->prev)
3263 text->text_properties = text->text_properties->prev;
3267 delete_text_property (GtkText* text, guint nchars)
3269 /* Delete nchars forward from point. */
3271 /* Deleting text properties is problematical, because we
3272 * might be storing around marks pointing to a property.
3274 * The marks in question and how we handle them are:
3276 * point: We know the new value, since it will be at the
3277 * end of the deleted text, and we move it there
3279 * cursor: We just remove the mark and set it equal to the
3280 * point after the operation.
3281 * line-start cache: We replace most affected lines.
3282 * The current line gets used to fetch the new
3283 * lines so, if necessary, (delete at the beginning
3284 * of a line) we fix it up by setting it equal to the
3292 for(; nchars; nchars -= 1)
3294 prop = MARK_CURRENT_PROPERTY(&text->point);
3298 if (prop->length == 0)
3300 tmp = MARK_LIST_PTR (&text->point);
3302 is_first = tmp == text->text_properties;
3304 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3305 text->point.offset = 0;
3307 if (GTK_WIDGET_REALIZED (text))
3308 unrealize_property (text, prop);
3310 destroy_text_property (prop);
3311 g_list_free_1 (tmp);
3313 prop = MARK_CURRENT_PROPERTY (&text->point);
3316 text->text_properties = MARK_LIST_PTR (&text->point);
3318 g_assert (prop->length != 0);
3320 else if (prop->length == text->point.offset)
3322 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3323 text->point.offset = 0;
3327 /* Check to see if we have just the single final position remaining
3328 * along in a property; if so, combine it with the previous property
3330 if (LAST_INDEX (text, text->point) &&
3331 (MARK_OFFSET (&text->point) == 0) &&
3332 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3334 tmp = MARK_LIST_PTR (&text->point);
3335 prop = MARK_CURRENT_PROPERTY(&text->point);
3337 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3338 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3339 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3341 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3343 if (GTK_WIDGET_REALIZED (text))
3344 unrealize_property (text, prop);
3346 destroy_text_property (prop);
3347 g_list_free_1 (tmp);
3352 init_properties (GtkText *text)
3354 if (!text->text_properties)
3356 text->text_properties = g_list_alloc();
3357 text->text_properties->next = NULL;
3358 text->text_properties->prev = NULL;
3359 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3360 text->text_properties_end = text->text_properties;
3362 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3364 text->point.index = 0;
3369 /**********************************************************************/
3370 /* Property Movement */
3371 /**********************************************************************/
3374 move_mark_n (GtkPropertyMark* mark, gint n)
3377 advance_mark_n(mark, n);
3379 decrement_mark_n(mark, -n);
3383 advance_mark (GtkPropertyMark* mark)
3385 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3389 if (prop->length > mark->offset + 1)
3393 mark->property = MARK_NEXT_LIST_PTR (mark);
3399 advance_mark_n (GtkPropertyMark* mark, gint n)
3406 i = 0; /* otherwise it migth not be init. */
3407 prop = MARK_CURRENT_PROPERTY(mark);
3409 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3410 /* to make it easier */
3411 n += (mark->offset);
3412 mark->index -= mark->offset;
3414 /* first we take seven-mile-leaps to get to the right text
3416 while ((n-i) > prop->length - 1) {
3418 mark->index += prop->length;
3419 mark->property = MARK_NEXT_LIST_PTR (mark);
3420 prop = MARK_CURRENT_PROPERTY (mark);
3424 /* and then the rest */
3425 mark->index += n - i;
3426 mark->offset += n - i;
3430 decrement_mark (GtkPropertyMark* mark)
3434 if (mark->offset > 0)
3438 mark->property = MARK_PREV_LIST_PTR (mark);
3439 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3444 decrement_mark_n (GtkPropertyMark* mark, gint n)
3448 while (mark->offset < n) {
3449 /* jump to end of prev */
3450 n -= mark->offset + 1;
3451 mark->index -= mark->offset + 1;
3452 mark->property = MARK_PREV_LIST_PTR (mark);
3453 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3461 static GtkPropertyMark
3462 find_mark (GtkText* text, guint mark_position)
3464 return find_mark_near (text, mark_position, &text->point);
3468 * You can also start from the end, what a drag.
3470 static GtkPropertyMark
3471 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3476 GtkPropertyMark mark;
3479 diffa = mark_position + 1;
3481 diffa = mark_position - near->index;
3483 diffb = mark_position;
3495 mark.property = text->text_properties;
3499 move_mark_n (&mark, mark_position - mark.index);
3504 /* This routine must be called with scroll == FALSE, only when
3505 * point is at least partially on screen
3509 find_line_containing_point (GtkText* text, guint point,
3515 text->current_line = NULL;
3519 /* Scroll backwards until the point is on screen
3521 while (CACHE_DATA(text->line_start_cache).start.index > point)
3522 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3524 /* Now additionally try to make sure that the point is fully on screen
3528 while (text->first_cut_pixels != 0 &&
3529 text->line_start_cache->next &&
3530 CACHE_DATA(text->line_start_cache->next).start.index > point)
3531 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3534 gdk_window_get_size (text->text_area, NULL, &height);
3536 for (cache = text->line_start_cache; cache; cache = cache->next)
3540 if (CACHE_DATA(cache).end.index >= point ||
3541 LAST_INDEX(text, CACHE_DATA(cache).end))
3543 text->current_line = cache; /* LOOK HERE, this proc has an
3544 * important side effect. */
3548 TEXT_SHOW_LINE (text, cache, "cache");
3550 if (cache->next == NULL)
3551 fetch_lines_forward (text, 1);
3555 lph = pixel_height_of (text, cache->next);
3557 /* Scroll the bottom of the line is on screen, or until
3558 * the line is the first onscreen line.
3560 while (cache->next != text->line_start_cache && lph > height)
3562 TEXT_SHOW_LINE (text, cache, "cache");
3563 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3564 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3565 lph = pixel_height_of (text, cache->next);
3570 g_assert_not_reached (); /* Must set text->current_line here */
3574 pixel_height_of (GtkText* text, GList* cache_line)
3576 gint pixels = - text->first_cut_pixels;
3577 GList *cache = text->line_start_cache;
3580 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3582 if (cache->data == cache_line->data)
3585 cache = cache->next;
3591 /**********************************************************************/
3592 /* Search and Placement */
3593 /**********************************************************************/
3596 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3599 gint16* char_widths;
3601 if (LAST_INDEX (text, *mark))
3604 ch = GTK_TEXT_INDEX (text, mark->index);
3605 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3609 return tab_mark->to_next_tab * char_widths[' '];
3613 return char_widths[ch];
3617 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3622 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, GdkWChar ch)
3624 if (tab_mark->to_next_tab == 1 || ch == '\t')
3626 if (tab_mark->tab_stops->next)
3628 tab_mark->tab_stops = tab_mark->tab_stops->next;
3629 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3633 tab_mark->to_next_tab = text->default_tab_width;
3638 tab_mark->to_next_tab -= 1;
3643 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3647 advance_tab_mark (text, tab_mark, 0);
3651 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3655 GtkPropertyMark mark = start_line->start;
3656 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3657 gint pixel_width = LINE_START_PIXEL (*start_line);
3659 while (mark.index < text->cursor_mark.index)
3661 pixel_width += find_char_width (text, &mark, &tab_mark);
3663 advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3664 advance_mark (&mark);
3667 text->cursor_pos_x = pixel_width;
3668 text->cursor_pos_y = pixel_height;
3669 text->cursor_char_offset = start_line->font_descent;
3670 text->cursor_mark = mark;
3672 ch = LAST_INDEX (text, mark) ?
3673 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3675 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3676 text->cursor_char = 0;
3678 text->cursor_char = ch;
3682 find_cursor (GtkText* text, gboolean scroll)
3684 if (GTK_WIDGET_REALIZED (text))
3686 find_line_containing_point (text, text->cursor_mark.index, scroll);
3688 if (text->current_line)
3689 find_cursor_at_line (text,
3690 &CACHE_DATA(text->current_line),
3691 pixel_height_of(text, text->current_line));
3694 GTK_OLD_EDITABLE (text)->current_pos = text->cursor_mark.index;
3698 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3699 guint line_pixel_height,
3702 GtkPropertyMark mark = lp->start;
3703 TabStopMark tab_mark = lp->tab_cont.tab_start;
3705 gint char_width = find_char_width(text, &mark, &tab_mark);
3706 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3708 text->cursor_pos_y = line_pixel_height;
3712 GdkWChar ch = LAST_INDEX (text, mark) ?
3713 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3715 if (button_x < pixel_width || mark.index == lp->end.index)
3717 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3718 text->cursor_mark = mark;
3719 text->cursor_char_offset = lp->font_descent;
3721 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3722 text->cursor_char = 0;
3724 text->cursor_char = ch;
3729 advance_tab_mark (text, &tab_mark, ch);
3730 advance_mark (&mark);
3732 pixel_width += char_width/2;
3734 char_width = find_char_width (text, &mark, &tab_mark);
3736 pixel_width += (char_width+1)/2;
3741 find_mouse_cursor (GtkText* text, gint x, gint y)
3744 GList* cache = text->line_start_cache;
3748 pixel_height = - text->first_cut_pixels;
3750 for (; cache; cache = cache->next)
3752 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3754 if (y < pixel_height || !cache->next)
3756 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3758 find_cursor (text, FALSE);
3765 /**********************************************************************/
3767 /**********************************************************************/
3770 free_cache (GtkText* text)
3772 GList* cache = text->line_start_cache;
3777 cache = cache->prev;
3779 text->line_start_cache = cache;
3782 for (; cache; cache = cache->next)
3783 g_mem_chunk_free (params_mem_chunk, cache->data);
3785 g_list_free (text->line_start_cache);
3787 text->line_start_cache = NULL;
3791 remove_cache_line (GtkText* text, GList* member)
3798 if (member == text->line_start_cache)
3799 text->line_start_cache = text->line_start_cache->next;
3802 member->prev->next = member->next;
3805 member->next->prev = member->prev;
3807 list = member->next;
3809 g_mem_chunk_free (params_mem_chunk, member->data);
3810 g_list_free_1 (member);
3815 /**********************************************************************/
3817 /**********************************************************************/
3820 move_cursor_buffer_ver (GtkText *text, int dir)
3822 undraw_cursor (text, FALSE);
3826 scroll_int (text, text->vadj->upper);
3827 text->cursor_mark = find_this_line_start_mark (text,
3829 &text->cursor_mark);
3833 scroll_int (text, - text->vadj->value);
3834 text->cursor_mark = find_this_line_start_mark (text,
3836 &text->cursor_mark);
3839 find_cursor (text, TRUE);
3840 draw_cursor (text, FALSE);
3844 move_cursor_page_ver (GtkText *text, int dir)
3846 scroll_int (text, dir * text->vadj->page_increment);
3850 move_cursor_ver (GtkText *text, int count)
3853 GtkPropertyMark mark;
3856 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3857 offset = text->cursor_mark.index - mark.index;
3859 if (offset > text->cursor_virtual_x)
3860 text->cursor_virtual_x = offset;
3864 if (mark.index == 0)
3867 decrement_mark (&mark);
3868 mark = find_this_line_start_mark (text, mark.index, &mark);
3872 mark = text->cursor_mark;
3874 while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3875 advance_mark (&mark);
3877 if (LAST_INDEX(text, mark))
3880 advance_mark (&mark);
3883 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3884 if (LAST_INDEX(text, mark) ||
3885 GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3888 undraw_cursor (text, FALSE);
3890 text->cursor_mark = mark;
3892 find_cursor (text, TRUE);
3894 draw_cursor (text, FALSE);
3898 move_cursor_hor (GtkText *text, int count)
3900 /* count should be +-1. */
3901 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3902 (count < 0 && text->cursor_mark.index < (- count)) ||
3906 text->cursor_virtual_x = 0;
3908 undraw_cursor (text, FALSE);
3910 move_mark_n (&text->cursor_mark, count);
3912 find_cursor (text, TRUE);
3914 draw_cursor (text, FALSE);
3918 gtk_text_move_cursor (GtkOldEditable *old_editable,
3925 move_cursor_hor (GTK_TEXT (old_editable), 1);
3930 move_cursor_hor (GTK_TEXT (old_editable), -1);
3936 move_cursor_ver (GTK_TEXT (old_editable), 1);
3941 move_cursor_ver (GTK_TEXT (old_editable), -1);
3946 gtk_text_move_forward_character (GtkText *text)
3948 move_cursor_hor (text, 1);
3952 gtk_text_move_backward_character (GtkText *text)
3954 move_cursor_hor (text, -1);
3958 gtk_text_move_next_line (GtkText *text)
3960 move_cursor_ver (text, 1);
3964 gtk_text_move_previous_line (GtkText *text)
3966 move_cursor_ver (text, -1);
3970 gtk_text_move_word (GtkOldEditable *old_editable,
3976 gtk_text_move_forward_word (GTK_TEXT (old_editable));
3981 gtk_text_move_backward_word (GTK_TEXT (old_editable));
3986 gtk_text_move_forward_word (GtkText *text)
3988 text->cursor_virtual_x = 0;
3990 undraw_cursor (text, FALSE);
3992 if (text->use_wchar)
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);
3998 while (!LAST_INDEX (text, text->cursor_mark) &&
3999 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4000 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);
4008 while (!LAST_INDEX (text, text->cursor_mark) &&
4009 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4010 advance_mark (&text->cursor_mark);
4013 find_cursor (text, TRUE);
4014 draw_cursor (text, FALSE);
4018 gtk_text_move_backward_word (GtkText *text)
4020 text->cursor_virtual_x = 0;
4022 undraw_cursor (text, FALSE);
4024 if (text->use_wchar)
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);
4030 while ((text->cursor_mark.index > 0) &&
4031 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4032 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);
4040 while ((text->cursor_mark.index > 0) &&
4041 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4042 decrement_mark (&text->cursor_mark);
4045 find_cursor (text, TRUE);
4046 draw_cursor (text, FALSE);
4050 gtk_text_move_page (GtkOldEditable *old_editable,
4055 scroll_int (GTK_TEXT (old_editable),
4056 y * GTK_TEXT(old_editable)->vadj->page_increment);
4060 gtk_text_move_to_row (GtkOldEditable *old_editable,
4066 gtk_text_move_to_column (GtkOldEditable *old_editable,
4071 text = GTK_TEXT (old_editable);
4073 text->cursor_virtual_x = 0; /* FIXME */
4075 undraw_cursor (text, FALSE);
4077 /* Move to the beginning of the line */
4078 while ((text->cursor_mark.index > 0) &&
4079 (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4080 decrement_mark (&text->cursor_mark);
4082 while (!LAST_INDEX (text, text->cursor_mark) &&
4083 (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4087 else if (column == 0)
4090 advance_mark (&text->cursor_mark);
4093 find_cursor (text, TRUE);
4094 draw_cursor (text, FALSE);
4098 gtk_text_move_beginning_of_line (GtkText *text)
4100 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4105 gtk_text_move_end_of_line (GtkText *text)
4107 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), -1);
4111 gtk_text_kill_char (GtkOldEditable *old_editable,
4116 text = GTK_TEXT (old_editable);
4118 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4119 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4124 if (text->point.index + 1 <= TEXT_LENGTH (text))
4125 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index, text->point.index + 1);
4129 if (text->point.index > 0)
4130 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index - 1, text->point.index);
4136 gtk_text_delete_forward_character (GtkText *text)
4138 gtk_text_kill_char (GTK_OLD_EDITABLE (text), 1);
4142 gtk_text_delete_backward_character (GtkText *text)
4144 gtk_text_kill_char (GTK_OLD_EDITABLE (text), -1);
4148 gtk_text_kill_word (GtkOldEditable *old_editable,
4151 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4152 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4155 gint old_pos = old_editable->current_pos;
4158 gtk_text_move_word (old_editable, 1);
4159 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4163 gtk_text_move_word (old_editable, -1);
4164 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4170 gtk_text_delete_forward_word (GtkText *text)
4172 gtk_text_kill_word (GTK_OLD_EDITABLE (text), 1);
4176 gtk_text_delete_backward_word (GtkText *text)
4178 gtk_text_kill_word (GTK_OLD_EDITABLE (text), -1);
4182 gtk_text_kill_line (GtkOldEditable *old_editable,
4185 gint old_pos = old_editable->current_pos;
4188 gtk_text_move_to_column (old_editable, -1);
4189 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4193 gtk_text_move_to_column (old_editable, 0);
4194 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4199 gtk_text_delete_line (GtkText *text)
4201 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4202 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4206 gtk_text_delete_to_line_end (GtkText *text)
4208 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4212 gtk_text_select_word (GtkText *text, guint32 time)
4217 GtkOldEditable *old_editable;
4218 old_editable = GTK_OLD_EDITABLE (text);
4220 gtk_text_move_backward_word (text);
4221 start_pos = text->cursor_mark.index;
4223 gtk_text_move_forward_word (text);
4224 end_pos = text->cursor_mark.index;
4226 old_editable->has_selection = TRUE;
4227 gtk_text_set_selection (old_editable, start_pos, end_pos);
4228 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4232 gtk_text_select_line (GtkText *text, guint32 time)
4237 GtkOldEditable *old_editable;
4238 old_editable = GTK_OLD_EDITABLE (text);
4240 gtk_text_move_beginning_of_line (text);
4241 start_pos = text->cursor_mark.index;
4243 gtk_text_move_end_of_line (text);
4244 gtk_text_move_forward_character (text);
4245 end_pos = text->cursor_mark.index;
4247 old_editable->has_selection = TRUE;
4248 gtk_text_set_selection (old_editable, start_pos, end_pos);
4249 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4252 /**********************************************************************/
4254 /**********************************************************************/
4257 adjust_adj (GtkText* text, GtkAdjustment* adj)
4261 gdk_window_get_size (text->text_area, NULL, &height);
4263 adj->step_increment = MIN (adj->upper, SCROLL_PIXELS);
4264 adj->page_increment = MIN (adj->upper, height - KEY_SCROLL_PIXELS);
4265 adj->page_size = MIN (adj->upper, height);
4266 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4267 adj->value = MAX (adj->value, 0.0);
4269 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4273 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
4275 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4277 if ((text->first_line_start_index >= lp->start.index) &&
4278 (text->first_line_start_index <= lp->end.index))
4280 svdata->mark = lp->start;
4282 if (text->first_line_start_index == lp->start.index)
4284 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4288 text->first_onscreen_ver_pixel = svdata->pixel_height;
4289 text->first_cut_pixels = 0;
4292 text->vadj->value = text->first_onscreen_ver_pixel;
4295 svdata->pixel_height += LINE_HEIGHT (*lp);
4301 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
4303 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4306 if (svdata->pixel_height <= (gint) text->vadj->value &&
4307 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4309 svdata->mark = lp->start;
4311 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4312 text->first_onscreen_ver_pixel = svdata->pixel_height;
4313 text->first_line_start_index = lp->start.index;
4319 svdata->pixel_height += LINE_HEIGHT (*lp);
4327 static GtkPropertyMark
4328 set_vertical_scroll (GtkText* text)
4330 GtkPropertyMark mark = find_mark (text, 0);
4331 SetVerticalScrollData data;
4335 data.pixel_height = 0;
4336 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4338 text->vadj->upper = data.pixel_height;
4339 orig_value = (gint) text->vadj->value;
4341 gdk_window_get_size (text->text_area, NULL, &height);
4343 text->vadj->step_increment = MIN (text->vadj->upper, SCROLL_PIXELS);
4344 text->vadj->page_increment = MIN (text->vadj->upper, height - KEY_SCROLL_PIXELS);
4345 text->vadj->page_size = MIN (text->vadj->upper, height);
4346 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4347 text->vadj->value = MAX (text->vadj->value, 0.0);
4349 text->last_ver_value = (gint)text->vadj->value;
4351 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4353 if (text->vadj->value != orig_value)
4355 /* We got clipped, and don't really know which line to put first. */
4356 data.pixel_height = 0;
4357 data.last_didnt_wrap = TRUE;
4359 line_params_iterate (text, &mark, NULL,
4361 set_vertical_scroll_find_iterator);
4368 scroll_int (GtkText* text, gint diff)
4372 text->vadj->value += diff;
4374 upper = text->vadj->upper - text->vadj->page_size;
4375 text->vadj->value = MIN (text->vadj->value, upper);
4376 text->vadj->value = MAX (text->vadj->value, 0.0);
4378 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4382 process_exposes (GtkText *text)
4386 /* Make sure graphics expose events are processed before scrolling
4389 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4391 gtk_widget_send_expose (GTK_WIDGET (text), event);
4392 if (event->expose.count == 0)
4394 gdk_event_free (event);
4397 gdk_event_free (event);
4401 static gint last_visible_line_height (GtkText* text)
4403 GList *cache = text->line_start_cache;
4406 gdk_window_get_size (text->text_area, NULL, &height);
4408 for (; cache->next; cache = cache->next)
4409 if (pixel_height_of(text, cache->next) > height)
4413 return pixel_height_of(text, cache) - 1;
4418 static gint first_visible_line_height (GtkText* text)
4420 if (text->first_cut_pixels)
4421 return pixel_height_of(text, text->line_start_cache) + 1;
4427 scroll_down (GtkText* text, gint diff0)
4433 text->first_onscreen_ver_pixel += diff0;
4437 g_assert (text->line_start_cache);
4439 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4441 text->first_cut_pixels += 1;
4445 text->first_cut_pixels = 0;
4447 text->line_start_cache = text->line_start_cache->next;
4449 text->first_line_start_index =
4450 CACHE_DATA(text->line_start_cache).start.index;
4452 if (!text->line_start_cache->next)
4453 fetch_lines_forward (text, 1);
4459 gdk_window_get_size (text->text_area, &width, &height);
4460 if (height > real_diff)
4461 gdk_draw_pixmap (text->text_area,
4469 height - real_diff);
4472 rect.y = MAX (0, height - real_diff);
4474 rect.height = MIN (height, real_diff);
4476 expose_text (text, &rect, FALSE);
4477 gtk_text_draw_focus ( (GtkWidget *) text);
4479 if (text->current_line)
4483 text->cursor_pos_y -= real_diff;
4484 cursor_min = drawn_cursor_min(text);
4487 find_mouse_cursor (text, text->cursor_pos_x,
4488 first_visible_line_height (text));
4491 if (height > real_diff)
4492 process_exposes (text);
4496 scroll_up (GtkText* text, gint diff0)
4502 text->first_onscreen_ver_pixel += diff0;
4506 g_assert (text->line_start_cache);
4508 if (text->first_cut_pixels > 0)
4510 text->first_cut_pixels -= 1;
4514 if (!text->line_start_cache->prev)
4515 fetch_lines_backward (text);
4517 text->line_start_cache = text->line_start_cache->prev;
4519 text->first_line_start_index =
4520 CACHE_DATA(text->line_start_cache).start.index;
4522 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4528 gdk_window_get_size (text->text_area, &width, &height);
4529 if (height > real_diff)
4530 gdk_draw_pixmap (text->text_area,
4538 height - real_diff);
4543 rect.height = MIN (height, real_diff);
4545 expose_text (text, &rect, FALSE);
4546 gtk_text_draw_focus ( (GtkWidget *) text);
4548 if (text->current_line)
4553 text->cursor_pos_y += real_diff;
4554 cursor_max = drawn_cursor_max(text);
4555 gdk_window_get_size (text->text_area, NULL, &height);
4557 if (cursor_max >= height)
4558 find_mouse_cursor (text, text->cursor_pos_x,
4559 last_visible_line_height (text));
4562 if (height > real_diff)
4563 process_exposes (text);
4566 /**********************************************************************/
4568 /**********************************************************************/
4570 /* Assumes mark starts a line. Calculates the height, width, and
4571 * displayable character count of a single DISPLAYABLE line. That
4572 * means that in line-wrap mode, this does may not compute the
4573 * properties of an entire line. */
4575 find_line_params (GtkText* text,
4576 const GtkPropertyMark* mark,
4577 const PrevTabCont *tab_cont,
4578 PrevTabCont *next_cont)
4581 TabStopMark tab_mark = tab_cont->tab_start;
4582 guint max_display_pixels;
4587 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4588 max_display_pixels -= LINE_WRAP_ROOM;
4591 lp.tab_cont = *tab_cont;
4594 lp.pixel_width = tab_cont->pixel_offset;
4595 lp.displayable_chars = 0;
4597 lp.font_descent = 0;
4599 init_tab_cont (text, next_cont);
4601 while (!LAST_INDEX(text, lp.end))
4603 g_assert (lp.end.property);
4605 ch = GTK_TEXT_INDEX (text, lp.end.index);
4606 font = MARK_CURRENT_FONT (text, &lp.end);
4608 if (ch == LINE_DELIM)
4610 /* Newline doesn't count in computation of line height, even
4611 * if its in a bigger font than the rest of the line. Unless,
4612 * of course, there are no other characters. */
4614 if (!lp.font_ascent && !lp.font_descent)
4616 lp.font_ascent = font->ascent;
4617 lp.font_descent = font->descent;
4620 lp.tab_cont_next = *next_cont;
4625 ch_width = find_char_width (text, &lp.end, &tab_mark);
4627 if ((ch_width + lp.pixel_width > max_display_pixels) &&
4628 (lp.end.index > lp.start.index))
4632 if (text->line_wrap)
4634 next_cont->tab_start = tab_mark;
4635 next_cont->pixel_offset = 0;
4639 /* Here's the tough case, a tab is wrapping. */
4640 gint pixels_avail = max_display_pixels - lp.pixel_width;
4641 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
4642 gint spaces_avail = pixels_avail / space_width;
4644 if (spaces_avail == 0)
4646 decrement_mark (&lp.end);
4650 advance_tab_mark (text, &next_cont->tab_start, '\t');
4651 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4653 lp.displayable_chars += 1;
4658 if (text->word_wrap)
4660 GtkPropertyMark saved_mark = lp.end;
4661 guint saved_characters = lp.displayable_chars;
4663 lp.displayable_chars += 1;
4665 if (text->use_wchar)
4667 while (!gdk_iswspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4668 (lp.end.index > lp.start.index))
4670 decrement_mark (&lp.end);
4671 lp.displayable_chars -= 1;
4676 while (!isspace(GTK_TEXT_INDEX (text, lp.end.index)) &&
4677 (lp.end.index > lp.start.index))
4679 decrement_mark (&lp.end);
4680 lp.displayable_chars -= 1;
4684 /* If whole line is one word, revert to char wrapping */
4685 if (lp.end.index == lp.start.index)
4687 lp.end = saved_mark;
4688 lp.displayable_chars = saved_characters;
4689 decrement_mark (&lp.end);
4694 /* Don't include this character, it will wrap. */
4695 decrement_mark (&lp.end);
4699 lp.tab_cont_next = *next_cont;
4706 lp.displayable_chars += 1;
4709 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4710 lp.font_descent = MAX (font->descent, lp.font_descent);
4711 lp.pixel_width += ch_width;
4713 advance_mark(&lp.end);
4714 advance_tab_mark (text, &tab_mark, ch);
4717 if (LAST_INDEX(text, lp.start))
4719 /* Special case, empty last line. */
4720 font = MARK_CURRENT_FONT (text, &lp.end);
4722 lp.font_ascent = font->ascent;
4723 lp.font_descent = font->descent;
4726 lp.tab_cont_next = *next_cont;
4732 expand_scratch_buffer (GtkText* text, guint len)
4734 if (len >= text->scratch_buffer_len)
4738 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4740 if (text->use_wchar)
4742 if (text->scratch_buffer.wc)
4743 text->scratch_buffer.wc = g_new (GdkWChar, i);
4745 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
4746 i*sizeof (GdkWChar));
4750 if (text->scratch_buffer.ch)
4751 text->scratch_buffer.ch = g_new (guchar, i);
4753 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
4756 text->scratch_buffer_len = i;
4760 /* Side effect: modifies text->gc
4763 draw_bg_rect (GtkText* text, GtkPropertyMark *mark,
4764 gint x, gint y, gint width, gint height,
4765 gboolean already_cleared)
4767 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4769 if ((mark->index >= MIN(old_editable->selection_start_pos, old_editable->selection_end_pos) &&
4770 mark->index < MAX(old_editable->selection_start_pos, old_editable->selection_end_pos)))
4772 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
4773 old_editable->has_selection ?
4774 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
4776 NULL, GTK_WIDGET(text), "text",
4777 x, y, width, height);
4779 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
4780 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
4782 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
4784 gdk_draw_rectangle (text->text_area,
4786 TRUE, x, y, width, height);
4788 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4795 rect.height = height;
4797 clear_area (text, &rect);
4799 else if (!already_cleared)
4800 gdk_window_clear_area (text->text_area, x, y, width, height);
4804 draw_line (GtkText* text,
4805 gint pixel_start_height,
4808 GdkGCValues gc_values;
4811 guint running_offset = lp->tab_cont.pixel_offset;
4812 union { GdkWChar *wc; guchar *ch; } buffer;
4815 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4817 guint selection_start_pos = MIN (old_editable->selection_start_pos, old_editable->selection_end_pos);
4818 guint selection_end_pos = MAX (old_editable->selection_start_pos, old_editable->selection_end_pos);
4820 GtkPropertyMark mark = lp->start;
4821 TabStopMark tab_mark = lp->tab_cont.tab_start;
4822 gint pixel_height = pixel_start_height + lp->font_ascent;
4823 guint chars = lp->displayable_chars;
4825 /* First provide a contiguous segment of memory. This makes reading
4826 * the code below *much* easier, and only incurs the cost of copying
4827 * when the line being displayed spans the gap. */
4828 if (mark.index <= text->gap_position &&
4829 mark.index + chars > text->gap_position)
4831 expand_scratch_buffer (text, chars);
4833 if (text->use_wchar)
4835 for (i = 0; i < chars; i += 1)
4836 text->scratch_buffer.wc[i] = GTK_TEXT_INDEX(text, mark.index + i);
4837 buffer.wc = text->scratch_buffer.wc;
4841 for (i = 0; i < chars; i += 1)
4842 text->scratch_buffer.ch[i] = GTK_TEXT_INDEX(text, mark.index + i);
4843 buffer.ch = text->scratch_buffer.ch;
4848 if (text->use_wchar)
4850 if (mark.index >= text->gap_position)
4851 buffer.wc = text->text.wc + mark.index + text->gap_size;
4853 buffer.wc = text->text.wc + mark.index;
4857 if (mark.index >= text->gap_position)
4858 buffer.ch = text->text.ch + mark.index + text->gap_size;
4860 buffer.ch = text->text.ch + mark.index;
4865 if (running_offset > 0)
4867 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
4868 LINE_HEIGHT (*lp), TRUE);
4874 if ((text->use_wchar && buffer.wc[0] != '\t') ||
4875 (!text->use_wchar && buffer.ch[0] != '\t'))
4877 union { GdkWChar *wc; guchar *ch; } next_tab;
4882 if (text->use_wchar)
4883 for (i=0; i<chars; i++)
4885 if (buffer.wc[i] == '\t')
4887 next_tab.wc = buffer.wc + i;
4892 next_tab.ch = memchr (buffer.ch, '\t', chars);
4894 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4896 if (text->use_wchar)
4899 len = MIN (len, next_tab.wc - buffer.wc);
4904 len = MIN (len, next_tab.ch - buffer.ch);
4907 if (mark.index < selection_start_pos)
4908 len = MIN (len, selection_start_pos - mark.index);
4909 else if (mark.index < selection_end_pos)
4910 len = MIN (len, selection_end_pos - mark.index);
4912 font = MARK_CURRENT_FONT (text, &mark);
4913 if (font->type == GDK_FONT_FONT)
4915 gdk_gc_set_font (text->gc, font);
4916 gdk_gc_get_values (text->gc, &gc_values);
4917 if (text->use_wchar)
4918 pixel_width = gdk_text_width_wc (gc_values.font,
4921 pixel_width = gdk_text_width (gc_values.font,
4926 if (text->use_wchar)
4927 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
4929 pixel_width = gdk_text_width (font, buffer.ch, len);
4932 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4933 pixel_width, LINE_HEIGHT (*lp), TRUE);
4935 if ((mark.index >= selection_start_pos) &&
4936 (mark.index < selection_end_pos))
4938 if (old_editable->has_selection)
4939 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_SELECTED];
4941 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_ACTIVE];
4945 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
4949 if (text->use_wchar)
4950 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
4957 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
4964 running_offset += pixel_width;
4966 advance_tab_mark_n (text, &tab_mark, len);
4970 gint pixels_remaining;
4976 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
4977 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
4979 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4981 spaces_avail = pixels_remaining / space_width;
4982 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
4984 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4985 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
4987 running_offset += tab_mark.to_next_tab *
4988 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4990 advance_tab_mark (text, &tab_mark, '\t');
4993 advance_mark_n (&mark, len);
4994 if (text->use_wchar)
5003 draw_line_wrap (GtkText* text, guint height /* baseline height */)
5010 if (text->line_wrap)
5012 bitmap = text->line_wrap_bitmap;
5013 bitmap_width = line_wrap_width;
5014 bitmap_height = line_wrap_height;
5018 bitmap = text->line_arrow_bitmap;
5019 bitmap_width = line_arrow_width;
5020 bitmap_height = line_arrow_height;
5023 gdk_window_get_size (text->text_area, &width, NULL);
5024 width -= LINE_WRAP_ROOM;
5026 gdk_gc_set_stipple (text->gc,
5029 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
5031 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5033 gdk_gc_set_ts_origin (text->gc,
5035 height - bitmap_height - 1);
5037 gdk_draw_rectangle (text->text_area,
5041 height - bitmap_height - 1 /* one pixel above the baseline. */,
5045 gdk_gc_set_ts_origin (text->gc, 0, 0);
5047 gdk_gc_set_fill (text->gc, GDK_SOLID);
5051 undraw_cursor (GtkText* text, gint absolute)
5053 GtkOldEditable *old_editable = (GtkOldEditable *) text;
5055 TDEBUG (("in undraw_cursor\n"));
5058 text->cursor_drawn_level = 0;
5060 if ((text->cursor_drawn_level ++ == 0) &&
5061 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5062 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5066 g_assert(text->cursor_mark.property);
5068 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5070 draw_bg_rect (text, &text->cursor_mark,
5072 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5073 1, font->ascent + 1, FALSE);
5075 if (text->cursor_char)
5077 if (font->type == GDK_FONT_FONT)
5078 gdk_gc_set_font (text->gc, font);
5080 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5082 gdk_draw_text_wc (text->text_area, font,
5085 text->cursor_pos_y - text->cursor_char_offset,
5093 drawn_cursor_min (GtkText* text)
5097 g_assert(text->cursor_mark.property);
5099 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5101 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5105 drawn_cursor_max (GtkText* text)
5107 g_assert(text->cursor_mark.property);
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);
5542 #define __GTK_TEXT_C__
5543 #include "gtkaliasdef.c"