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/.
29 #include "gdk/gdkkeysyms.h"
30 #include "gdk/gdki18n.h"
32 #include "gtkmarshalers.h"
33 #include "gtkselection.h"
34 #include "gtksignal.h"
35 #define GTK_ENABLE_BROKEN
37 #include "line-wrap.xbm"
38 #include "line-arrow.xbm"
42 #define INITIAL_BUFFER_SIZE 1024
43 #define INITIAL_LINE_CACHE_SIZE 256
44 #define MIN_GAP_SIZE 256
45 #define LINE_DELIM '\n'
46 #define MIN_TEXT_WIDTH_LINES 20
47 #define MIN_TEXT_HEIGHT_LINES 10
48 #define TEXT_BORDER_ROOM 1
49 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
50 #define DEFAULT_TAB_STOP_WIDTH 4
51 #define SCROLL_PIXELS 5
52 #define KEY_SCROLL_PIXELS 10
53 #define SCROLL_TIME 100
54 #define FREEZE_LENGTH 1024
55 /* Freeze text when inserting or deleting more than this many characters */
57 #define SET_PROPERTY_MARK(m, p, o) do { \
58 (m)->property = (p); \
61 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
62 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
63 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
64 (mark)->property->prev->data \
66 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
67 #define MARK_LIST_PTR(mark) ((mark)->property)
68 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
69 #define MARK_OFFSET(mark) ((mark)->offset)
70 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
73 #define MARK_CURRENT_FONT(text, mark) \
74 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
75 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
76 gtk_style_get_font (GTK_WIDGET (text)->style))
77 #define MARK_CURRENT_FORE(text, mark) \
78 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
79 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
80 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
81 #define MARK_CURRENT_BACK(text, mark) \
82 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
83 &MARK_CURRENT_PROPERTY(mark)->back_color : \
84 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
85 #define MARK_CURRENT_TEXT_FONT(text, mark) \
86 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
87 MARK_CURRENT_PROPERTY(mark)->font : \
90 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
91 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
92 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
93 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
94 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
95 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
96 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
97 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
107 typedef struct _TextProperty TextProperty;
108 typedef struct _TabStopMark TabStopMark;
109 typedef struct _PrevTabCont PrevTabCont;
110 typedef struct _FetchLinesData FetchLinesData;
111 typedef struct _LineParams LineParams;
112 typedef struct _SetVerticalScrollData SetVerticalScrollData;
114 typedef gint (*LineIteratorFunction) (GtkText* text, LineParams* lp, void* data);
122 struct _SetVerticalScrollData {
124 gint last_didnt_wrap;
125 gint last_line_start;
126 GtkPropertyMark mark;
131 /* The actual font. */
135 gint16 char_widths[256];
139 PROPERTY_FONT = 1 << 0,
140 PROPERTY_FOREGROUND = 1 << 1,
141 PROPERTY_BACKGROUND = 1 << 2
149 /* Background Color. */
152 /* Foreground Color. */
155 /* Show which properties are set */
156 TextPropertyFlags flags;
158 /* Length of this property. */
164 GList* tab_stops; /* Index into list containing the next tab position. If
165 * NULL, using default widths. */
172 TabStopMark tab_start;
175 struct _FetchLinesData
188 guint displayable_chars;
191 PrevTabCont tab_cont;
192 PrevTabCont tab_cont_next;
194 GtkPropertyMark start;
199 static void gtk_text_class_init (GtkTextClass *klass);
200 static void gtk_text_set_property (GObject *object,
204 static void gtk_text_get_property (GObject *object,
208 static void gtk_text_editable_init (GtkEditableClass *iface);
209 static void gtk_text_init (GtkText *text);
210 static void gtk_text_destroy (GtkObject *object);
211 static void gtk_text_finalize (GObject *object);
212 static void gtk_text_realize (GtkWidget *widget);
213 static void gtk_text_unrealize (GtkWidget *widget);
214 static void gtk_text_style_set (GtkWidget *widget,
215 GtkStyle *previous_style);
216 static void gtk_text_state_changed (GtkWidget *widget,
217 GtkStateType previous_state);
218 static void gtk_text_draw_focus (GtkWidget *widget);
219 static void gtk_text_size_request (GtkWidget *widget,
220 GtkRequisition *requisition);
221 static void gtk_text_size_allocate (GtkWidget *widget,
222 GtkAllocation *allocation);
223 static void gtk_text_adjustment (GtkAdjustment *adjustment,
225 static void gtk_text_insert_text (GtkEditable *editable,
226 const gchar *new_text,
227 gint new_text_length,
229 static void gtk_text_delete_text (GtkEditable *editable,
232 static void gtk_text_update_text (GtkOldEditable *old_editable,
235 static gchar *gtk_text_get_chars (GtkOldEditable *old_editable,
238 static void gtk_text_set_selection (GtkOldEditable *old_editable,
241 static void gtk_text_real_set_editable (GtkOldEditable *old_editable,
242 gboolean is_editable);
244 static void gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
248 static gint gtk_text_expose (GtkWidget *widget,
249 GdkEventExpose *event);
250 static gint gtk_text_button_press (GtkWidget *widget,
251 GdkEventButton *event);
252 static gint gtk_text_button_release (GtkWidget *widget,
253 GdkEventButton *event);
254 static gint gtk_text_motion_notify (GtkWidget *widget,
255 GdkEventMotion *event);
256 static gint gtk_text_key_press (GtkWidget *widget,
259 static void move_gap (GtkText* text, guint index);
260 static void make_forward_space (GtkText* text, guint len);
262 /* Property management */
263 static GtkTextFont* get_text_font (GdkFont* gfont);
264 static void text_font_unref (GtkTextFont *text_font);
266 static void insert_text_property (GtkText* text, GdkFont* font,
267 GdkColor *fore, GdkColor* back, guint len);
268 static TextProperty* new_text_property (GtkText *text, GdkFont* font,
269 GdkColor* fore, GdkColor* back, guint length);
270 static void destroy_text_property (TextProperty *prop);
271 static void init_properties (GtkText *text);
272 static void realize_property (GtkText *text, TextProperty *prop);
273 static void realize_properties (GtkText *text);
274 static void unrealize_property (GtkText *text, TextProperty *prop);
275 static void unrealize_properties (GtkText *text);
277 static void delete_text_property (GtkText* text, guint len);
279 static guint pixel_height_of (GtkText* text, GList* cache_line);
281 /* Property Movement and Size Computations */
282 static void advance_mark (GtkPropertyMark* mark);
283 static void decrement_mark (GtkPropertyMark* mark);
284 static void advance_mark_n (GtkPropertyMark* mark, gint n);
285 static void decrement_mark_n (GtkPropertyMark* mark, gint n);
286 static void move_mark_n (GtkPropertyMark* mark, gint n);
287 static GtkPropertyMark find_mark (GtkText* text, guint mark_position);
288 static GtkPropertyMark find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near);
289 static void find_line_containing_point (GtkText* text, guint point,
293 static void compute_lines_pixels (GtkText* text, guint char_count,
294 guint *lines, guint *pixels);
296 static gint total_line_height (GtkText* text,
299 static LineParams find_line_params (GtkText* text,
300 const GtkPropertyMark *mark,
301 const PrevTabCont *tab_cont,
302 PrevTabCont *next_cont);
303 static void recompute_geometry (GtkText* text);
304 static void insert_expose (GtkText* text, guint old_pixels, gint nchars, guint new_line_count);
305 static void delete_expose (GtkText* text,
309 static GdkGC *create_bg_gc (GtkText *text);
310 static void clear_area (GtkText *text, GdkRectangle *area);
311 static void draw_line (GtkText* text,
314 static void draw_line_wrap (GtkText* text,
316 static void draw_cursor (GtkText* text, gint absolute);
317 static void undraw_cursor (GtkText* text, gint absolute);
318 static gint drawn_cursor_min (GtkText* text);
319 static gint drawn_cursor_max (GtkText* text);
320 static void expose_text (GtkText* text, GdkRectangle *area, gboolean cursor);
322 /* Search and Placement. */
323 static void find_cursor (GtkText* text,
325 static void find_cursor_at_line (GtkText* text,
326 const LineParams* start_line,
328 static void find_mouse_cursor (GtkText* text, gint x, gint y);
331 static void adjust_adj (GtkText* text, GtkAdjustment* adj);
332 static void scroll_up (GtkText* text, gint diff);
333 static void scroll_down (GtkText* text, gint diff);
334 static void scroll_int (GtkText* text, gint diff);
336 static void process_exposes (GtkText *text);
338 /* Cache Management. */
339 static void free_cache (GtkText* text);
340 static GList* remove_cache_line (GtkText* text, GList* list);
343 static void move_cursor_buffer_ver (GtkText *text, int dir);
344 static void move_cursor_page_ver (GtkText *text, int dir);
345 static void move_cursor_ver (GtkText *text, int count);
346 static void move_cursor_hor (GtkText *text, int count);
348 /* Binding actions */
349 static void gtk_text_move_cursor (GtkOldEditable *old_editable,
352 static void gtk_text_move_word (GtkOldEditable *old_editable,
354 static void gtk_text_move_page (GtkOldEditable *old_editable,
357 static void gtk_text_move_to_row (GtkOldEditable *old_editable,
359 static void gtk_text_move_to_column (GtkOldEditable *old_editable,
361 static void gtk_text_kill_char (GtkOldEditable *old_editable,
363 static void gtk_text_kill_word (GtkOldEditable *old_editable,
365 static void gtk_text_kill_line (GtkOldEditable *old_editable,
369 static void gtk_text_move_forward_character (GtkText *text);
370 static void gtk_text_move_backward_character (GtkText *text);
371 static void gtk_text_move_forward_word (GtkText *text);
372 static void gtk_text_move_backward_word (GtkText *text);
373 static void gtk_text_move_beginning_of_line (GtkText *text);
374 static void gtk_text_move_end_of_line (GtkText *text);
375 static void gtk_text_move_next_line (GtkText *text);
376 static void gtk_text_move_previous_line (GtkText *text);
378 static void gtk_text_delete_forward_character (GtkText *text);
379 static void gtk_text_delete_backward_character (GtkText *text);
380 static void gtk_text_delete_forward_word (GtkText *text);
381 static void gtk_text_delete_backward_word (GtkText *text);
382 static void gtk_text_delete_line (GtkText *text);
383 static void gtk_text_delete_to_line_end (GtkText *text);
384 static void gtk_text_select_word (GtkText *text,
386 static void gtk_text_select_line (GtkText *text,
389 static void gtk_text_set_position (GtkOldEditable *old_editable,
392 /* #define DEBUG_GTK_TEXT */
394 #if defined(DEBUG_GTK_TEXT) && defined(__GNUC__)
395 /* Debugging utilities. */
396 static void gtk_text_assert_mark (GtkText *text,
397 GtkPropertyMark *mark,
398 GtkPropertyMark *before,
399 GtkPropertyMark *after,
404 static void gtk_text_assert (GtkText *text,
407 static void gtk_text_show_cache_line (GtkText *text, GList *cache,
408 const char* what, const char* func, gint line);
409 static void gtk_text_show_cache (GtkText *text, const char* func, gint line);
410 static void gtk_text_show_adj (GtkText *text,
415 static void gtk_text_show_props (GtkText* test,
419 #define TDEBUG(args) g_message args
420 #define TEXT_ASSERT(text) gtk_text_assert (text,__PRETTY_FUNCTION__,__LINE__)
421 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_text_assert_mark (text,mark, \
422 __PRETTY_FUNCTION__,msg,__LINE__)
423 #define TEXT_SHOW(text) gtk_text_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
424 #define TEXT_SHOW_LINE(text,line,msg) gtk_text_show_cache_line (text,line,msg,\
425 __PRETTY_FUNCTION__,__LINE__)
426 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_text_show_adj (text,adj,msg, \
427 __PRETTY_FUNCTION__,__LINE__)
430 #define TEXT_ASSERT(text)
431 #define TEXT_ASSERT_MARK(text,mark,msg)
432 #define TEXT_SHOW(text)
433 #define TEXT_SHOW_LINE(text,line,msg)
434 #define TEXT_SHOW_ADJ(text,adj,msg)
437 /* Memory Management. */
438 static GMemChunk *params_mem_chunk = NULL;
439 static GMemChunk *text_property_chunk = NULL;
441 static GtkWidgetClass *parent_class = NULL;
444 static const GtkTextFunction control_keys[26] =
446 (GtkTextFunction)gtk_text_move_beginning_of_line, /* a */
447 (GtkTextFunction)gtk_text_move_backward_character, /* b */
448 (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
449 (GtkTextFunction)gtk_text_delete_forward_character, /* d */
450 (GtkTextFunction)gtk_text_move_end_of_line, /* e */
451 (GtkTextFunction)gtk_text_move_forward_character, /* f */
453 (GtkTextFunction)gtk_text_delete_backward_character, /* h */
456 (GtkTextFunction)gtk_text_delete_to_line_end, /* k */
459 (GtkTextFunction)gtk_text_move_next_line, /* n */
461 (GtkTextFunction)gtk_text_move_previous_line, /* p */
466 (GtkTextFunction)gtk_text_delete_line, /* u */
467 (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
468 (GtkTextFunction)gtk_text_delete_backward_word, /* w */
469 (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
474 static const GtkTextFunction alt_keys[26] =
477 (GtkTextFunction)gtk_text_move_backward_word, /* b */
479 (GtkTextFunction)gtk_text_delete_forward_word, /* d */
481 (GtkTextFunction)gtk_text_move_forward_word, /* f */
505 /**********************************************************************/
507 /**********************************************************************/
510 gtk_text_get_type (void)
512 static GtkType text_type = 0;
516 static const GtkTypeInfo text_info =
520 sizeof (GtkTextClass),
521 (GtkClassInitFunc) gtk_text_class_init,
522 (GtkObjectInitFunc) gtk_text_init,
523 /* reserved_1 */ NULL,
524 /* reserved_2 */ NULL,
525 (GtkClassInitFunc) NULL,
528 static const GInterfaceInfo editable_info =
530 (GInterfaceInitFunc) gtk_text_editable_init, /* interface_init */
531 NULL, /* interface_finalize */
532 NULL /* interface_data */
535 text_type = gtk_type_unique (GTK_TYPE_OLD_EDITABLE, &text_info);
536 g_type_add_interface_static (text_type,
545 gtk_text_class_init (GtkTextClass *class)
547 GObjectClass *gobject_class;
548 GtkObjectClass *object_class;
549 GtkWidgetClass *widget_class;
550 GtkOldEditableClass *old_editable_class;
552 gobject_class = G_OBJECT_CLASS (class);
553 object_class = (GtkObjectClass*) class;
554 widget_class = (GtkWidgetClass*) class;
555 old_editable_class = (GtkOldEditableClass*) class;
556 parent_class = gtk_type_class (GTK_TYPE_OLD_EDITABLE);
558 gobject_class->finalize = gtk_text_finalize;
559 gobject_class->set_property = gtk_text_set_property;
560 gobject_class->get_property = gtk_text_get_property;
562 object_class->destroy = gtk_text_destroy;
564 widget_class->realize = gtk_text_realize;
565 widget_class->unrealize = gtk_text_unrealize;
566 widget_class->style_set = gtk_text_style_set;
567 widget_class->state_changed = gtk_text_state_changed;
568 widget_class->size_request = gtk_text_size_request;
569 widget_class->size_allocate = gtk_text_size_allocate;
570 widget_class->expose_event = gtk_text_expose;
571 widget_class->button_press_event = gtk_text_button_press;
572 widget_class->button_release_event = gtk_text_button_release;
573 widget_class->motion_notify_event = gtk_text_motion_notify;
574 widget_class->key_press_event = gtk_text_key_press;
576 old_editable_class->set_editable = gtk_text_real_set_editable;
578 old_editable_class->move_cursor = gtk_text_move_cursor;
579 old_editable_class->move_word = gtk_text_move_word;
580 old_editable_class->move_page = gtk_text_move_page;
581 old_editable_class->move_to_row = gtk_text_move_to_row;
582 old_editable_class->move_to_column = gtk_text_move_to_column;
584 old_editable_class->kill_char = gtk_text_kill_char;
585 old_editable_class->kill_word = gtk_text_kill_word;
586 old_editable_class->kill_line = gtk_text_kill_line;
588 old_editable_class->update_text = gtk_text_update_text;
589 old_editable_class->get_chars = gtk_text_get_chars;
590 old_editable_class->set_selection = gtk_text_set_selection;
591 old_editable_class->set_position = gtk_text_set_position;
593 class->set_scroll_adjustments = gtk_text_set_adjustments;
595 g_object_class_install_property (gobject_class,
597 g_param_spec_object ("hadjustment",
598 _("Horizontal Adjustment"),
599 _("Horizontal adjustment for the text widget"),
603 g_object_class_install_property (gobject_class,
605 g_param_spec_object ("vadjustment",
606 _("Vertical Adjustment"),
607 _("Vertical adjustment for the text widget"),
611 g_object_class_install_property (gobject_class,
613 g_param_spec_boolean ("line_wrap",
615 _("Whether lines are wrapped at widget edges"),
619 g_object_class_install_property (gobject_class,
621 g_param_spec_boolean ("word_wrap",
623 _("Whether words are wrapped at widget edges"),
627 widget_class->set_scroll_adjustments_signal =
628 gtk_signal_new ("set_scroll_adjustments",
630 GTK_CLASS_TYPE (object_class),
631 GTK_SIGNAL_OFFSET (GtkTextClass, set_scroll_adjustments),
632 _gtk_marshal_VOID__OBJECT_OBJECT,
633 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
637 gtk_text_set_property (GObject *object,
644 text = GTK_TEXT (object);
648 case PROP_HADJUSTMENT:
649 gtk_text_set_adjustments (text,
650 g_value_get_object (value),
653 case PROP_VADJUSTMENT:
654 gtk_text_set_adjustments (text,
656 g_value_get_object (value));
659 gtk_text_set_line_wrap (text, g_value_get_boolean (value));
662 gtk_text_set_word_wrap (text, g_value_get_boolean (value));
665 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
671 gtk_text_get_property (GObject *object,
678 text = GTK_TEXT (object);
682 case PROP_HADJUSTMENT:
683 g_value_set_object (value, text->hadj);
685 case PROP_VADJUSTMENT:
686 g_value_set_object (value, text->vadj);
689 g_value_set_boolean (value, text->line_wrap);
692 g_value_set_boolean (value, text->word_wrap);
695 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
701 gtk_text_editable_init (GtkEditableClass *iface)
703 iface->insert_text = gtk_text_insert_text;
704 iface->delete_text = gtk_text_delete_text;
708 gtk_text_init (GtkText *text)
710 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
712 text->text_area = NULL;
717 text->line_wrap_bitmap = NULL;
718 text->line_arrow_bitmap = NULL;
720 text->use_wchar = FALSE;
721 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
722 text->text_len = INITIAL_BUFFER_SIZE;
724 text->scratch_buffer.ch = NULL;
725 text->scratch_buffer_len = 0;
727 text->freeze_count = 0;
729 if (!params_mem_chunk)
730 params_mem_chunk = g_mem_chunk_new ("LineParams",
732 256 * sizeof (LineParams),
735 text->default_tab_width = 4;
736 text->tab_stops = NULL;
738 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
739 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
741 text->line_start_cache = NULL;
742 text->first_cut_pixels = 0;
744 text->line_wrap = TRUE;
745 text->word_wrap = FALSE;
750 text->current_font = NULL;
752 init_properties (text);
754 GTK_OLD_EDITABLE (text)->editable = FALSE;
756 gtk_text_set_adjustments (text, NULL, NULL);
757 gtk_editable_set_position (GTK_EDITABLE (text), 0);
761 gtk_text_new (GtkAdjustment *hadj,
767 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
769 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
771 text = gtk_widget_new (GTK_TYPE_TEXT,
780 gtk_text_set_word_wrap (GtkText *text,
783 g_return_if_fail (GTK_IS_TEXT (text));
785 text->word_wrap = (word_wrap != FALSE);
787 if (GTK_WIDGET_REALIZED (text))
789 recompute_geometry (text);
790 gtk_widget_queue_draw (GTK_WIDGET (text));
793 g_object_notify (G_OBJECT (text), "word_wrap");
797 gtk_text_set_line_wrap (GtkText *text,
800 g_return_if_fail (GTK_IS_TEXT (text));
802 text->line_wrap = (line_wrap != FALSE);
804 if (GTK_WIDGET_REALIZED (text))
806 recompute_geometry (text);
807 gtk_widget_queue_draw (GTK_WIDGET (text));
810 g_object_notify (G_OBJECT (text), "line_wrap");
814 gtk_text_set_editable (GtkText *text,
815 gboolean is_editable)
817 g_return_if_fail (GTK_IS_TEXT (text));
819 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
823 gtk_text_real_set_editable (GtkOldEditable *old_editable,
824 gboolean is_editable)
828 g_return_if_fail (GTK_IS_TEXT (old_editable));
830 text = GTK_TEXT (old_editable);
832 old_editable->editable = (is_editable != FALSE);
835 draw_cursor (text, TRUE);
837 undraw_cursor (text, TRUE);
841 gtk_text_set_adjustments (GtkText *text,
845 g_return_if_fail (GTK_IS_TEXT (text));
847 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
849 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
851 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
853 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
855 if (text->hadj && (text->hadj != hadj))
857 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
858 gtk_object_unref (GTK_OBJECT (text->hadj));
861 if (text->vadj && (text->vadj != vadj))
863 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
864 gtk_object_unref (GTK_OBJECT (text->vadj));
867 g_object_freeze_notify (G_OBJECT (text));
868 if (text->hadj != hadj)
871 gtk_object_ref (GTK_OBJECT (text->hadj));
872 gtk_object_sink (GTK_OBJECT (text->hadj));
874 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
875 (GtkSignalFunc) gtk_text_adjustment,
877 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
878 (GtkSignalFunc) gtk_text_adjustment,
880 gtk_signal_connect (GTK_OBJECT (text->hadj), "destroy",
881 (GtkSignalFunc) gtk_text_adjustment_destroyed,
883 gtk_text_adjustment (hadj, text);
885 g_object_notify (G_OBJECT (text), "hadjustment");
888 if (text->vadj != vadj)
891 gtk_object_ref (GTK_OBJECT (text->vadj));
892 gtk_object_sink (GTK_OBJECT (text->vadj));
894 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
895 (GtkSignalFunc) gtk_text_adjustment,
897 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
898 (GtkSignalFunc) gtk_text_adjustment,
900 gtk_signal_connect (GTK_OBJECT (text->vadj), "destroy",
901 (GtkSignalFunc) gtk_text_adjustment_destroyed,
903 gtk_text_adjustment (vadj, text);
905 g_object_notify (G_OBJECT (text), "vadjustment");
907 g_object_thaw_notify (G_OBJECT (text));
911 gtk_text_set_point (GtkText *text,
914 g_return_if_fail (GTK_IS_TEXT (text));
915 g_return_if_fail (index <= TEXT_LENGTH (text));
917 text->point = find_mark (text, index);
921 gtk_text_get_point (GtkText *text)
923 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
925 return text->point.index;
929 gtk_text_get_length (GtkText *text)
931 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
933 return TEXT_LENGTH (text);
937 gtk_text_freeze (GtkText *text)
939 g_return_if_fail (GTK_IS_TEXT (text));
941 text->freeze_count++;
945 gtk_text_thaw (GtkText *text)
947 g_return_if_fail (GTK_IS_TEXT (text));
949 if (text->freeze_count)
950 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
952 recompute_geometry (text);
953 gtk_widget_queue_draw (GTK_WIDGET (text));
958 gtk_text_insert (GtkText *text,
965 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
966 gboolean frozen = FALSE;
968 gint new_line_count = 1;
969 guint old_height = 0;
974 g_return_if_fail (GTK_IS_TEXT (text));
977 length = strlen (chars);
984 if (!text->freeze_count && (length > FREEZE_LENGTH))
986 gtk_text_freeze (text);
990 if (!text->freeze_count && (text->line_start_cache != NULL))
992 find_line_containing_point (text, text->point.index, TRUE);
993 old_height = total_line_height (text, text->current_line, 1);
996 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
999 widget = GTK_WIDGET (text);
1000 gtk_widget_ensure_style (widget);
1001 if ((widget->style) && (gtk_style_get_font (widget->style)->type == GDK_FONT_FONTSET))
1003 text->use_wchar = TRUE;
1004 g_free (text->text.ch);
1005 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
1006 text->text_len = INITIAL_BUFFER_SIZE;
1007 if (text->scratch_buffer.ch)
1008 g_free (text->scratch_buffer.ch);
1009 text->scratch_buffer.wc = NULL;
1010 text->scratch_buffer_len = 0;
1014 move_gap (text, text->point.index);
1015 make_forward_space (text, length);
1017 if (text->use_wchar)
1019 char *chars_nt = (char *)chars;
1022 chars_nt = g_new (char, length+1);
1023 memcpy (chars_nt, chars, length);
1024 chars_nt[length] = 0;
1026 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
1028 if (chars_nt != chars)
1036 memcpy(text->text.ch + text->gap_position, chars, length);
1039 if (!text->freeze_count && (text->line_start_cache != NULL))
1041 if (text->use_wchar)
1043 for (i=0; i<numwcs; i++)
1044 if (text->text.wc[text->gap_position + i] == '\n')
1049 for (i=0; i<numwcs; i++)
1050 if (text->text.ch[text->gap_position + i] == '\n')
1057 insert_text_property (text, font, fore, back, numwcs);
1059 text->gap_size -= numwcs;
1060 text->gap_position += numwcs;
1062 if (text->point.index < text->first_line_start_index)
1063 text->first_line_start_index += numwcs;
1064 if (text->point.index < old_editable->selection_start_pos)
1065 old_editable->selection_start_pos += numwcs;
1066 if (text->point.index < old_editable->selection_end_pos)
1067 old_editable->selection_end_pos += numwcs;
1068 /* We'll reset the cursor later anyways if we aren't frozen */
1069 if (text->point.index < text->cursor_mark.index)
1070 text->cursor_mark.index += numwcs;
1072 advance_mark_n (&text->point, numwcs);
1074 if (!text->freeze_count && (text->line_start_cache != NULL))
1075 insert_expose (text, old_height, numwcs, new_line_count);
1079 gtk_text_thaw (text);
1083 gtk_text_backward_delete (GtkText *text,
1086 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1088 if (nchars > text->point.index || nchars <= 0)
1091 gtk_text_set_point (text, text->point.index - nchars);
1093 return gtk_text_forward_delete (text, nchars);
1097 gtk_text_forward_delete (GtkText *text,
1100 guint old_lines, old_height;
1101 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
1102 gboolean frozen = FALSE;
1104 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1106 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1109 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1111 gtk_text_freeze (text);
1115 if (!text->freeze_count && text->line_start_cache != NULL)
1117 /* We need to undraw the cursor here, since we may later
1118 * delete the cursor's property
1120 undraw_cursor (text, FALSE);
1121 find_line_containing_point (text, text->point.index, TRUE);
1122 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1125 /* FIXME, or resizing after deleting will be odd */
1126 if (text->point.index < text->first_line_start_index)
1128 if (text->point.index + nchars >= text->first_line_start_index)
1130 text->first_line_start_index = text->point.index;
1131 while ((text->first_line_start_index > 0) &&
1132 (GTK_TEXT_INDEX (text, text->first_line_start_index - 1)
1134 text->first_line_start_index -= 1;
1138 text->first_line_start_index -= nchars;
1141 if (text->point.index < old_editable->selection_start_pos)
1142 old_editable->selection_start_pos -=
1143 MIN(nchars, old_editable->selection_start_pos - text->point.index);
1144 if (text->point.index < old_editable->selection_end_pos)
1145 old_editable->selection_end_pos -=
1146 MIN(nchars, old_editable->selection_end_pos - text->point.index);
1147 /* We'll reset the cursor later anyways if we aren't frozen */
1148 if (text->point.index < text->cursor_mark.index)
1149 move_mark_n (&text->cursor_mark,
1150 -MIN(nchars, text->cursor_mark.index - text->point.index));
1152 move_gap (text, text->point.index);
1154 text->gap_size += nchars;
1156 delete_text_property (text, nchars);
1158 if (!text->freeze_count && (text->line_start_cache != NULL))
1160 delete_expose (text, nchars, old_lines, old_height);
1161 draw_cursor (text, FALSE);
1165 gtk_text_thaw (text);
1171 gtk_text_set_position (GtkOldEditable *old_editable,
1174 GtkText *text = (GtkText *) old_editable;
1177 position = gtk_text_get_length (text);
1179 undraw_cursor (text, FALSE);
1180 text->cursor_mark = find_mark (text, position);
1181 find_cursor (text, TRUE);
1182 draw_cursor (text, FALSE);
1183 gtk_editable_select_region (GTK_EDITABLE (old_editable), 0, 0);
1187 gtk_text_get_chars (GtkOldEditable *old_editable,
1195 g_return_val_if_fail (GTK_IS_TEXT (old_editable), NULL);
1196 text = GTK_TEXT (old_editable);
1199 end_pos = TEXT_LENGTH (text);
1201 if ((start_pos < 0) ||
1202 (end_pos > TEXT_LENGTH (text)) ||
1203 (end_pos < start_pos))
1206 move_gap (text, TEXT_LENGTH (text));
1207 make_forward_space (text, 1);
1209 if (text->use_wchar)
1212 ch = text->text.wc[end_pos];
1213 text->text.wc[end_pos] = 0;
1214 retval = gdk_wcstombs (text->text.wc + start_pos);
1215 text->text.wc[end_pos] = ch;
1220 ch = text->text.ch[end_pos];
1221 text->text.ch[end_pos] = 0;
1222 retval = g_strdup (text->text.ch + start_pos);
1223 text->text.ch[end_pos] = ch;
1231 gtk_text_destroy (GtkObject *object)
1235 g_return_if_fail (GTK_IS_TEXT (object));
1237 text = GTK_TEXT (object);
1241 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1242 gtk_object_unref (GTK_OBJECT (text->hadj));
1247 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1248 gtk_object_unref (GTK_OBJECT (text->vadj));
1254 gtk_timeout_remove (text->timer);
1258 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1262 gtk_text_finalize (GObject *object)
1267 g_return_if_fail (GTK_IS_TEXT (object));
1269 text = GTK_TEXT (object);
1271 /* Clean up the internal structures */
1272 if (text->use_wchar)
1273 g_free (text->text.wc);
1275 g_free (text->text.ch);
1277 tmp_list = text->text_properties;
1280 destroy_text_property (tmp_list->data);
1281 tmp_list = tmp_list->next;
1284 if (text->current_font)
1285 text_font_unref (text->current_font);
1287 g_list_free (text->text_properties);
1289 if (text->use_wchar)
1291 if (text->scratch_buffer.wc)
1292 g_free (text->scratch_buffer.wc);
1296 if (text->scratch_buffer.ch)
1297 g_free (text->scratch_buffer.ch);
1300 g_list_free (text->tab_stops);
1302 G_OBJECT_CLASS (parent_class)->finalize (object);
1306 gtk_text_realize (GtkWidget *widget)
1309 GtkOldEditable *old_editable;
1310 GdkWindowAttr attributes;
1311 gint attributes_mask;
1313 g_return_if_fail (GTK_IS_TEXT (widget));
1315 text = GTK_TEXT (widget);
1316 old_editable = GTK_OLD_EDITABLE (widget);
1317 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1319 attributes.window_type = GDK_WINDOW_CHILD;
1320 attributes.x = widget->allocation.x;
1321 attributes.y = widget->allocation.y;
1322 attributes.width = widget->allocation.width;
1323 attributes.height = widget->allocation.height;
1324 attributes.wclass = GDK_INPUT_OUTPUT;
1325 attributes.visual = gtk_widget_get_visual (widget);
1326 attributes.colormap = gtk_widget_get_colormap (widget);
1327 attributes.event_mask = gtk_widget_get_events (widget);
1328 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1329 GDK_BUTTON_PRESS_MASK |
1330 GDK_BUTTON_RELEASE_MASK |
1331 GDK_BUTTON_MOTION_MASK |
1332 GDK_ENTER_NOTIFY_MASK |
1333 GDK_LEAVE_NOTIFY_MASK |
1334 GDK_KEY_PRESS_MASK);
1335 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1337 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1338 gdk_window_set_user_data (widget->window, text);
1340 attributes.x = (widget->style->xthickness + TEXT_BORDER_ROOM);
1341 attributes.y = (widget->style->ythickness + TEXT_BORDER_ROOM);
1342 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1343 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1345 attributes.cursor = gdk_cursor_new (GDK_XTERM);
1346 attributes_mask |= GDK_WA_CURSOR;
1348 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1349 gdk_window_set_user_data (text->text_area, text);
1351 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1353 widget->style = gtk_style_attach (widget->style, widget->window);
1355 /* Can't call gtk_style_set_background here because it's handled specially */
1356 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1357 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1359 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1360 text->bg_gc = create_bg_gc (text);
1362 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1363 (gchar*) line_wrap_bits,
1367 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1368 (gchar*) line_arrow_bits,
1372 text->gc = gdk_gc_new (text->text_area);
1373 gdk_gc_set_exposures (text->gc, TRUE);
1374 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1376 realize_properties (text);
1377 gdk_window_show (text->text_area);
1378 init_properties (text);
1380 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1381 gtk_old_editable_claim_selection (old_editable, TRUE, GDK_CURRENT_TIME);
1383 recompute_geometry (text);
1387 gtk_text_style_set (GtkWidget *widget,
1388 GtkStyle *previous_style)
1390 GtkText *text = GTK_TEXT (widget);
1392 if (GTK_WIDGET_REALIZED (widget))
1394 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1395 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1399 gdk_gc_destroy (text->bg_gc);
1403 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1404 text->bg_gc = create_bg_gc (text);
1406 recompute_geometry (text);
1409 if (text->current_font)
1410 text_font_unref (text->current_font);
1411 text->current_font = get_text_font (gtk_style_get_font (widget->style));
1415 gtk_text_state_changed (GtkWidget *widget,
1416 GtkStateType previous_state)
1418 GtkText *text = GTK_TEXT (widget);
1420 if (GTK_WIDGET_REALIZED (widget))
1422 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1423 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1428 gtk_text_unrealize (GtkWidget *widget)
1432 g_return_if_fail (GTK_IS_TEXT (widget));
1434 text = GTK_TEXT (widget);
1436 gdk_window_set_user_data (text->text_area, NULL);
1437 gdk_window_destroy (text->text_area);
1438 text->text_area = NULL;
1440 gdk_gc_destroy (text->gc);
1445 gdk_gc_destroy (text->bg_gc);
1449 gdk_pixmap_unref (text->line_wrap_bitmap);
1450 gdk_pixmap_unref (text->line_arrow_bitmap);
1452 unrealize_properties (text);
1456 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1457 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1461 clear_focus_area (GtkText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1463 GtkWidget *widget = GTK_WIDGET (text);
1466 gint ythick = TEXT_BORDER_ROOM + widget->style->ythickness;
1467 gint xthick = TEXT_BORDER_ROOM + widget->style->xthickness;
1471 if (area_width == 0 || area_height == 0)
1474 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1476 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1478 gdk_gc_set_ts_origin (text->bg_gc,
1479 (- text->first_onscreen_hor_pixel + xthick) % width,
1480 (- text->first_onscreen_ver_pixel + ythick) % height);
1485 gc = widget->style->base_gc[widget->state];
1487 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1488 area_x, area_y, area_width, area_height);
1492 gtk_text_draw_focus (GtkWidget *widget)
1498 g_return_if_fail (GTK_IS_TEXT (widget));
1500 text = GTK_TEXT (widget);
1502 if (GTK_WIDGET_DRAWABLE (widget))
1504 gint ythick = widget->style->ythickness;
1505 gint xthick = widget->style->xthickness;
1506 gint xextra = TEXT_BORDER_ROOM;
1507 gint yextra = TEXT_BORDER_ROOM;
1509 TDEBUG (("in gtk_text_draw_focus\n"));
1513 width = widget->allocation.width;
1514 height = widget->allocation.height;
1516 if (GTK_WIDGET_HAS_FOCUS (widget))
1525 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1526 NULL, widget, "text",
1528 widget->allocation.width,
1529 widget->allocation.height);
1532 gtk_paint_shadow (widget->style, widget->window,
1533 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1534 NULL, widget, "text",
1535 x, y, width, height);
1539 width -= 2 * xthick;
1540 height -= 2 * ythick;
1543 clear_focus_area (text, x, y, width, yextra);
1545 clear_focus_area (text, x, y + yextra,
1546 xextra, y + height - 2 * yextra);
1548 clear_focus_area (text, x + width - xextra, y + yextra,
1549 xextra, height - 2 * ythick);
1551 clear_focus_area (text, x, x + height - yextra, width, yextra);
1555 TDEBUG (("in gtk_text_draw_focus (undrawable !!!)\n"));
1560 gtk_text_size_request (GtkWidget *widget,
1561 GtkRequisition *requisition)
1569 g_return_if_fail (GTK_IS_TEXT (widget));
1570 g_return_if_fail (requisition != NULL);
1572 xthickness = widget->style->xthickness + TEXT_BORDER_ROOM;
1573 ythickness = widget->style->ythickness + TEXT_BORDER_ROOM;
1575 font = gtk_style_get_font (widget->style);
1577 char_height = MIN_TEXT_HEIGHT_LINES * (font->ascent +
1580 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (font,
1581 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1585 requisition->width = char_width + xthickness * 2;
1586 requisition->height = char_height + ythickness * 2;
1590 gtk_text_size_allocate (GtkWidget *widget,
1591 GtkAllocation *allocation)
1594 GtkOldEditable *old_editable;
1596 g_return_if_fail (GTK_IS_TEXT (widget));
1597 g_return_if_fail (allocation != NULL);
1599 text = GTK_TEXT (widget);
1600 old_editable = GTK_OLD_EDITABLE (widget);
1602 widget->allocation = *allocation;
1603 if (GTK_WIDGET_REALIZED (widget))
1605 gdk_window_move_resize (widget->window,
1606 allocation->x, allocation->y,
1607 allocation->width, allocation->height);
1609 gdk_window_move_resize (text->text_area,
1610 widget->style->xthickness + TEXT_BORDER_ROOM,
1611 widget->style->ythickness + TEXT_BORDER_ROOM,
1612 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->xthickness +
1613 (gint)TEXT_BORDER_ROOM) * 2),
1614 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->ythickness +
1615 (gint)TEXT_BORDER_ROOM) * 2));
1617 recompute_geometry (text);
1622 gtk_text_expose (GtkWidget *widget,
1623 GdkEventExpose *event)
1625 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1626 g_return_val_if_fail (event != NULL, FALSE);
1628 if (event->window == GTK_TEXT (widget)->text_area)
1630 TDEBUG (("in gtk_text_expose (expose)\n"));
1631 expose_text (GTK_TEXT (widget), &event->area, TRUE);
1633 else if (event->count == 0)
1635 TDEBUG (("in gtk_text_expose (focus)\n"));
1636 gtk_text_draw_focus (widget);
1643 gtk_text_scroll_timeout (gpointer data)
1646 GdkEventMotion event;
1648 GdkModifierType mask;
1650 GDK_THREADS_ENTER ();
1652 text = GTK_TEXT (data);
1655 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1657 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1664 gtk_text_motion_notify (GTK_WIDGET (text), &event);
1667 GDK_THREADS_LEAVE ();
1673 gtk_text_button_press (GtkWidget *widget,
1674 GdkEventButton *event)
1677 GtkOldEditable *old_editable;
1679 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1680 g_return_val_if_fail (event != NULL, FALSE);
1682 text = GTK_TEXT (widget);
1683 old_editable = GTK_OLD_EDITABLE (widget);
1685 if (text->button && (event->button != text->button))
1688 text->button = event->button;
1690 if (!GTK_WIDGET_HAS_FOCUS (widget))
1691 gtk_widget_grab_focus (widget);
1693 if (event->button == 1)
1695 switch (event->type)
1697 case GDK_BUTTON_PRESS:
1698 gtk_grab_add (widget);
1700 undraw_cursor (text, FALSE);
1701 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1702 draw_cursor (text, FALSE);
1704 /* Set it now, so we display things right. We'll unset it
1705 * later if things don't work out */
1706 old_editable->has_selection = TRUE;
1707 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1708 text->cursor_mark.index,
1709 text->cursor_mark.index);
1713 case GDK_2BUTTON_PRESS:
1714 gtk_text_select_word (text, event->time);
1717 case GDK_3BUTTON_PRESS:
1718 gtk_text_select_line (text, event->time);
1725 else if (event->type == GDK_BUTTON_PRESS)
1727 if ((event->button == 2) && old_editable->editable)
1729 if (old_editable->selection_start_pos == old_editable->selection_end_pos ||
1730 old_editable->has_selection)
1732 undraw_cursor (text, FALSE);
1733 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1734 draw_cursor (text, FALSE);
1738 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1739 gdk_atom_intern ("UTF8_STRING", FALSE),
1744 gtk_grab_add (widget);
1746 undraw_cursor (text, FALSE);
1747 find_mouse_cursor (text, event->x, event->y);
1748 draw_cursor (text, FALSE);
1750 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1751 text->cursor_mark.index,
1752 text->cursor_mark.index);
1754 old_editable->has_selection = FALSE;
1755 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1756 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1764 gtk_text_button_release (GtkWidget *widget,
1765 GdkEventButton *event)
1768 GtkOldEditable *old_editable;
1769 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1770 g_return_val_if_fail (event != NULL, FALSE);
1772 text = GTK_TEXT (widget);
1774 gtk_grab_remove (widget);
1776 if (text->button != event->button)
1783 gtk_timeout_remove (text->timer);
1787 if (event->button == 1)
1789 text = GTK_TEXT (widget);
1790 old_editable = GTK_OLD_EDITABLE (widget);
1792 gtk_grab_remove (widget);
1794 old_editable->has_selection = FALSE;
1795 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1797 if (gtk_selection_owner_set (widget,
1798 GDK_SELECTION_PRIMARY,
1800 old_editable->has_selection = TRUE;
1802 gtk_text_update_text (old_editable, old_editable->selection_start_pos,
1803 old_editable->selection_end_pos);
1807 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1808 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1811 else if (event->button == 3)
1813 gtk_grab_remove (widget);
1816 undraw_cursor (text, FALSE);
1817 find_cursor (text, TRUE);
1818 draw_cursor (text, FALSE);
1824 gtk_text_motion_notify (GtkWidget *widget,
1825 GdkEventMotion *event)
1830 GdkModifierType mask;
1832 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1833 g_return_val_if_fail (event != NULL, FALSE);
1835 text = GTK_TEXT (widget);
1839 mask = event->state;
1840 if (event->is_hint || (text->text_area != event->window))
1842 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1845 if ((text->button == 0) ||
1846 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1849 gdk_window_get_size (text->text_area, NULL, &height);
1851 if ((y < 0) || (y > height))
1853 if (text->timer == 0)
1855 text->timer = gtk_timeout_add (SCROLL_TIME,
1856 gtk_text_scroll_timeout,
1860 scroll_int (text, y/2);
1862 scroll_int (text, (y - height)/2);
1868 undraw_cursor (GTK_TEXT (widget), FALSE);
1869 find_mouse_cursor (GTK_TEXT (widget), x, y);
1870 draw_cursor (GTK_TEXT (widget), FALSE);
1872 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1873 GTK_OLD_EDITABLE (text)->selection_start_pos,
1874 text->cursor_mark.index);
1880 gtk_text_insert_text (GtkEditable *editable,
1881 const gchar *new_text,
1882 gint new_text_length,
1885 GtkText *text = GTK_TEXT (editable);
1887 GdkColor *fore, *back;
1889 TextProperty *property;
1891 gtk_text_set_point (text, *position);
1893 property = MARK_CURRENT_PROPERTY (&text->point);
1894 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
1895 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
1896 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
1898 gtk_text_insert (text, font, fore, back, new_text, new_text_length);
1900 *position = text->point.index;
1904 gtk_text_delete_text (GtkEditable *editable,
1910 g_return_if_fail (start_pos >= 0);
1912 text = GTK_TEXT (editable);
1914 gtk_text_set_point (text, start_pos);
1916 end_pos = TEXT_LENGTH (text);
1918 if (end_pos > start_pos)
1919 gtk_text_forward_delete (text, end_pos - start_pos);
1923 gtk_text_key_press (GtkWidget *widget,
1927 GtkOldEditable *old_editable;
1932 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1933 g_return_val_if_fail (event != NULL, FALSE);
1935 text = GTK_TEXT (widget);
1936 old_editable = GTK_OLD_EDITABLE (widget);
1938 key = event->keyval;
1941 if ((GTK_OLD_EDITABLE(text)->editable == FALSE))
1943 switch (event->keyval)
1947 if (event->state & GDK_CONTROL_MASK)
1948 scroll_int (text, -text->vadj->value);
1954 if (event->state & GDK_CONTROL_MASK)
1955 scroll_int (text, +text->vadj->upper);
1959 case GDK_KP_Page_Up:
1960 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
1961 case GDK_KP_Page_Down:
1962 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
1964 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
1966 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
1969 if (event->state & GDK_CONTROL_MASK)
1970 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
1981 gint extend_selection;
1983 guint initial_pos = old_editable->current_pos;
1985 text->point = find_mark (text, text->cursor_mark.index);
1987 extend_selection = event->state & GDK_SHIFT_MASK;
1988 extend_start = FALSE;
1990 if (extend_selection)
1992 old_editable->has_selection = TRUE;
1994 if (old_editable->selection_start_pos == old_editable->selection_end_pos)
1996 old_editable->selection_start_pos = text->point.index;
1997 old_editable->selection_end_pos = text->point.index;
2000 extend_start = (text->point.index == old_editable->selection_start_pos);
2003 switch (event->keyval)
2007 if (event->state & GDK_CONTROL_MASK)
2008 move_cursor_buffer_ver (text, -1);
2010 gtk_text_move_beginning_of_line (text);
2014 if (event->state & GDK_CONTROL_MASK)
2015 move_cursor_buffer_ver (text, +1);
2017 gtk_text_move_end_of_line (text);
2019 case GDK_KP_Page_Up:
2020 case GDK_Page_Up: move_cursor_page_ver (text, -1); break;
2021 case GDK_KP_Page_Down:
2022 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
2023 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
2025 case GDK_Up: move_cursor_ver (text, -1); break;
2027 case GDK_Down: move_cursor_ver (text, +1); break;
2030 if (event->state & GDK_CONTROL_MASK)
2031 gtk_text_move_backward_word (text);
2033 move_cursor_hor (text, -1);
2037 if (event->state & GDK_CONTROL_MASK)
2038 gtk_text_move_forward_word (text);
2040 move_cursor_hor (text, +1);
2044 if (event->state & GDK_CONTROL_MASK)
2045 gtk_text_delete_backward_word (text);
2047 gtk_text_delete_backward_character (text);
2050 gtk_text_delete_line (text);
2054 if (event->state & GDK_SHIFT_MASK)
2056 extend_selection = FALSE;
2057 gtk_editable_paste_clipboard (GTK_EDITABLE (old_editable));
2059 else if (event->state & GDK_CONTROL_MASK)
2061 gtk_editable_copy_clipboard (GTK_EDITABLE (old_editable));
2065 /* gtk_toggle_insert(text) -- IMPLEMENT */
2070 if (event->state & GDK_CONTROL_MASK)
2071 gtk_text_delete_forward_word (text);
2072 else if (event->state & GDK_SHIFT_MASK)
2074 extend_selection = FALSE;
2075 gtk_editable_cut_clipboard (GTK_EDITABLE (old_editable));
2078 gtk_text_delete_forward_character (text);
2081 case GDK_ISO_Left_Tab:
2083 position = text->point.index;
2084 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\t", 1, &position);
2088 if (event->state & GDK_CONTROL_MASK)
2089 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2092 position = text->point.index;
2093 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\n", 1, &position);
2097 /* Don't insert literally */
2104 if (event->state & GDK_CONTROL_MASK)
2106 if ((key >= 'A') && (key <= 'Z'))
2109 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2111 (* control_keys[(int) (key - 'a')]) (old_editable, event->time);
2117 else if (event->state & GDK_MOD1_MASK)
2119 if ((key >= 'A') && (key <= 'Z'))
2122 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2124 (* alt_keys[(int) (key - 'a')]) (old_editable, event->time);
2130 else if (event->length > 0)
2132 extend_selection = FALSE;
2134 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
2135 position = text->point.index;
2136 gtk_editable_insert_text (GTK_EDITABLE (old_editable), event->string, event->length, &position);
2142 if (return_val && (old_editable->current_pos != initial_pos))
2144 if (extend_selection)
2146 if (old_editable->current_pos < old_editable->selection_start_pos)
2147 gtk_text_set_selection (old_editable, old_editable->current_pos,
2148 old_editable->selection_end_pos);
2149 else if (old_editable->current_pos > old_editable->selection_end_pos)
2150 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2151 old_editable->current_pos);
2155 gtk_text_set_selection (old_editable, old_editable->current_pos,
2156 old_editable->selection_end_pos);
2158 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2159 old_editable->current_pos);
2163 gtk_text_set_selection (old_editable, 0, 0);
2165 gtk_old_editable_claim_selection (old_editable,
2166 old_editable->selection_start_pos != old_editable->selection_end_pos,
2175 gtk_text_adjustment (GtkAdjustment *adjustment,
2178 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2179 g_return_if_fail (GTK_IS_TEXT (text));
2181 /* Just ignore it if we haven't been size-allocated and realized yet */
2182 if (text->line_start_cache == NULL)
2185 if (adjustment == text->hadj)
2187 g_warning ("horizontal scrolling not implemented");
2191 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2195 undraw_cursor (text, FALSE);
2198 scroll_down (text, diff);
2199 else /* if (diff < 0) */
2200 scroll_up (text, diff);
2202 draw_cursor (text, FALSE);
2204 text->last_ver_value = adjustment->value;
2210 gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
2213 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2214 g_return_if_fail (GTK_IS_TEXT (text));
2216 if (adjustment == text->hadj)
2217 gtk_text_set_adjustments (text, NULL, text->vadj);
2218 if (adjustment == text->vadj)
2219 gtk_text_set_adjustments (text, text->hadj, NULL);
2223 static GtkPropertyMark
2224 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2226 GtkPropertyMark mark;
2228 mark = find_mark_near (text, point_position, near);
2230 while (mark.index > 0 &&
2231 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2232 decrement_mark (&mark);
2238 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2240 tab_cont->pixel_offset = 0;
2241 tab_cont->tab_start.tab_stops = text->tab_stops;
2242 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2244 if (!tab_cont->tab_start.to_next_tab)
2245 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2249 line_params_iterate (GtkText* text,
2250 const GtkPropertyMark* mark0,
2251 const PrevTabCont* tab_mark0,
2254 LineIteratorFunction iter)
2255 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2256 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2257 * for each line following MARK, iteration continues unless ITER_CALL
2260 GtkPropertyMark mark = *mark0;
2261 PrevTabCont tab_conts[2];
2262 LineParams *lp, lpbuf;
2263 gint tab_cont_index = 0;
2266 tab_conts[0] = *tab_mark0;
2268 init_tab_cont (text, tab_conts);
2273 lp = g_chunk_new (LineParams, params_mem_chunk);
2277 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2278 tab_conts + (tab_cont_index + 1) % 2);
2280 if ((*iter) (text, lp, data))
2283 if (LAST_INDEX (text, lp->end))
2287 advance_mark (&mark);
2288 tab_cont_index = (tab_cont_index + 1) % 2;
2293 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2295 FetchLinesData *fldata = (FetchLinesData*) data;
2297 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2299 switch (fldata->fl_type)
2301 case FetchLinesCount:
2302 if (!text->line_wrap || !lp->wraps)
2305 if (fldata->data >= fldata->data_max)
2309 case FetchLinesPixels:
2311 fldata->data += LINE_HEIGHT(*lp);
2313 if (fldata->data >= fldata->data_max)
2323 fetch_lines (GtkText* text,
2324 const GtkPropertyMark* mark0,
2325 const PrevTabCont* tab_cont0,
2329 FetchLinesData fl_data;
2331 fl_data.new_lines = NULL;
2333 fl_data.data_max = data;
2334 fl_data.fl_type = fl_type;
2336 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2338 return g_list_reverse (fl_data.new_lines);
2342 fetch_lines_backward (GtkText* text)
2344 GList* new_lines = NULL, *new_line_start;
2345 GtkPropertyMark mark;
2347 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2350 mark = find_this_line_start_mark (text,
2351 CACHE_DATA(text->line_start_cache).start.index - 1,
2352 &CACHE_DATA(text->line_start_cache).start);
2354 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2356 while (new_line_start->next)
2357 new_line_start = new_line_start->next;
2359 new_line_start->next = text->line_start_cache;
2360 text->line_start_cache->prev = new_line_start;
2364 fetch_lines_forward (GtkText* text, gint line_count)
2366 GtkPropertyMark mark;
2367 GList* line = text->line_start_cache;
2372 mark = CACHE_DATA(line).end;
2374 if (LAST_INDEX (text, mark))
2377 advance_mark(&mark);
2379 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2382 line->next->prev = line;
2385 /* Compute the number of lines, and vertical pixels for n characters
2386 * starting from the point
2389 compute_lines_pixels (GtkText* text, guint char_count,
2390 guint *lines, guint *pixels)
2392 GList *line = text->current_line;
2393 gint chars_left = char_count;
2398 /* If chars_left == 0, that means we're joining two lines in a
2399 * deletion, so add in the values for the next line as well
2401 for (; line && chars_left >= 0; line = line->next)
2403 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2405 if (line == text->current_line)
2406 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2408 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2410 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2414 chars_left = 0; /* force another loop */
2417 fetch_lines_forward (text, 1);
2422 total_line_height (GtkText* text, GList* line, gint line_count)
2426 for (; line && line_count > 0; line = line->next)
2428 height += LINE_HEIGHT(CACHE_DATA(line));
2430 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2434 fetch_lines_forward (text, line_count);
2441 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2443 if (old == text->line_start_cache)
2447 for (; old_line_count > 0; old_line_count -= 1)
2449 while (text->line_start_cache &&
2451 CACHE_DATA(text->line_start_cache).wraps)
2452 remove_cache_line(text, text->line_start_cache);
2454 remove_cache_line(text, text->line_start_cache);
2457 last = g_list_last (new);
2459 last->next = text->line_start_cache;
2461 if (text->line_start_cache)
2462 text->line_start_cache->prev = last;
2464 text->line_start_cache = new;
2470 g_assert (old->prev);
2474 for (; old_line_count > 0; old_line_count -= 1)
2476 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2477 old = remove_cache_line (text, old);
2479 old = remove_cache_line (text, old);
2485 last = g_list_last (new);
2495 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2497 GList* cache = text->current_line;
2500 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2503 for (; cache; cache = cache->next)
2505 GtkPropertyMark *start = &CACHE_DATA(cache).start;
2506 GtkPropertyMark *end = &CACHE_DATA(cache).end;
2508 start->index -= nchars;
2509 end->index -= nchars;
2511 if (LAST_INDEX (text, text->point) &&
2512 start->index == text->point.index)
2513 *start = text->point;
2514 else if (start->property == text->point.property)
2515 start->offset = start->index - (text->point.index - text->point.offset);
2517 if (LAST_INDEX (text, text->point) &&
2518 end->index == text->point.index)
2520 if (end->property == text->point.property)
2521 end->offset = end->index - (text->point.index - text->point.offset);
2523 /*TEXT_ASSERT_MARK(text, start, "start");*/
2524 /*TEXT_ASSERT_MARK(text, end, "end");*/
2529 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2531 GtkWidget *widget = GTK_WIDGET (text);
2534 guint new_pixels = 0;
2536 GList* new_line = NULL;
2539 text->cursor_virtual_x = 0;
2541 correct_cache_delete (text, nchars, old_lines);
2543 pixel_height = pixel_height_of(text, text->current_line) -
2544 LINE_HEIGHT(CACHE_DATA(text->current_line));
2546 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2547 CACHE_DATA(text->current_line).start = text->point;
2549 new_line = fetch_lines (text,
2550 &CACHE_DATA(text->current_line).start,
2551 &CACHE_DATA(text->current_line).tab_cont,
2555 swap_lines (text, text->current_line, new_line, old_lines);
2557 text->current_line = new_line;
2559 new_pixels = total_line_height (text, new_line, 1);
2561 gdk_window_get_size (text->text_area, &width, &height);
2563 if (old_pixels != new_pixels)
2565 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2567 gdk_draw_pixmap (text->text_area,
2571 pixel_height + old_pixels,
2573 pixel_height + new_pixels,
2577 text->vadj->upper += new_pixels;
2578 text->vadj->upper -= old_pixels;
2579 adjust_adj (text, text->vadj);
2583 rect.y = pixel_height;
2585 rect.height = new_pixels;
2587 expose_text (text, &rect, FALSE);
2588 gtk_text_draw_focus ( (GtkWidget *) text);
2590 text->cursor_mark = text->point;
2592 find_cursor (text, TRUE);
2594 if (old_pixels != new_pixels)
2596 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2599 rect.y = pixel_height + new_pixels;
2601 rect.height = height - rect.y;
2603 expose_text (text, &rect, FALSE);
2606 process_exposes (text);
2613 /* note, the point has already been moved forward */
2615 correct_cache_insert (GtkText* text, gint nchars)
2618 GtkPropertyMark *start;
2619 GtkPropertyMark *end;
2620 gboolean was_split = FALSE;
2622 /* We need to distinguish whether the property was split in the
2623 * insert or not, so we check if the point (which points after
2624 * the insertion here), points to the same character as the
2625 * point before. Ugh.
2629 GtkPropertyMark tmp_mark = text->point;
2630 move_mark_n (&tmp_mark, -1);
2632 if (tmp_mark.property != text->point.property)
2636 /* If we inserted a property exactly at the beginning of the
2637 * line, we have to correct here, or fetch_lines will
2640 start = &CACHE_DATA(text->current_line).start;
2642 /* Check if if we split exactly at the beginning of the line:
2643 * (was_split won't be set if we are inserting at the end of the text,
2644 * so we don't check)
2646 if (start->offset == MARK_CURRENT_PROPERTY (start)->length)
2647 SET_PROPERTY_MARK (start, start->property->next, 0);
2648 /* Check if we inserted a property at the beginning of the text: */
2649 else if (was_split &&
2650 (start->property == text->point.property) &&
2651 (start->index == text->point.index - nchars))
2652 SET_PROPERTY_MARK (start, start->property->prev, 0);
2654 /* Now correct the offsets, and check for start or end marks that
2655 * are after the point, yet point to a property before the point's
2656 * property. This indicates that they are meant to point to the
2657 * second half of a property we split in insert_text_property(), so
2658 * we fix them up that way.
2660 cache = text->current_line->next;
2662 for (; cache; cache = cache->next)
2664 start = &CACHE_DATA(cache).start;
2665 end = &CACHE_DATA(cache).end;
2667 if (LAST_INDEX (text, text->point) &&
2668 start->index == text->point.index)
2669 *start = text->point;
2670 else if (start->index >= text->point.index - nchars)
2672 if (!was_split && start->property == text->point.property)
2673 move_mark_n(start, nchars);
2676 if (start->property->next &&
2677 (start->property->next->next == text->point.property))
2679 g_assert (start->offset >= MARK_CURRENT_PROPERTY (start)->length);
2680 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2681 start->property = text->point.property;
2683 start->index += nchars;
2687 if (LAST_INDEX (text, text->point) &&
2688 end->index == text->point.index)
2690 if (end->index >= text->point.index - nchars)
2692 if (!was_split && end->property == text->point.property)
2693 move_mark_n(end, nchars);
2696 if (end->property->next &&
2697 (end->property->next->next == text->point.property))
2699 g_assert (end->offset >= MARK_CURRENT_PROPERTY (end)->length);
2700 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2701 end->property = text->point.property;
2703 end->index += nchars;
2707 /*TEXT_ASSERT_MARK(text, start, "start");*/
2708 /*TEXT_ASSERT_MARK(text, end, "end");*/
2714 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2715 guint new_line_count)
2717 GtkWidget *widget = GTK_WIDGET (text);
2720 guint new_pixels = 0;
2722 GList* new_lines = NULL;
2725 text->cursor_virtual_x = 0;
2727 undraw_cursor (text, FALSE);
2729 correct_cache_insert (text, nchars);
2731 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2733 pixel_height = pixel_height_of(text, text->current_line) -
2734 LINE_HEIGHT(CACHE_DATA(text->current_line));
2736 new_lines = fetch_lines (text,
2737 &CACHE_DATA(text->current_line).start,
2738 &CACHE_DATA(text->current_line).tab_cont,
2742 swap_lines (text, text->current_line, new_lines, 1);
2744 text->current_line = new_lines;
2746 new_pixels = total_line_height (text, new_lines, new_line_count);
2748 gdk_window_get_size (text->text_area, &width, &height);
2750 if (old_pixels != new_pixels)
2752 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2754 gdk_draw_pixmap (text->text_area,
2758 pixel_height + old_pixels,
2760 pixel_height + new_pixels,
2762 height + (old_pixels - new_pixels) - pixel_height);
2765 text->vadj->upper += new_pixels;
2766 text->vadj->upper -= old_pixels;
2767 adjust_adj (text, text->vadj);
2771 rect.y = pixel_height;
2773 rect.height = new_pixels;
2775 expose_text (text, &rect, FALSE);
2776 gtk_text_draw_focus ( (GtkWidget *) text);
2778 text->cursor_mark = text->point;
2780 find_cursor (text, TRUE);
2782 draw_cursor (text, FALSE);
2784 if (old_pixels != new_pixels)
2786 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2789 rect.y = pixel_height + new_pixels;
2791 rect.height = height - rect.y;
2793 expose_text (text, &rect, FALSE);
2796 process_exposes (text);
2799 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2804 /* Text property functions */
2807 font_hash (gconstpointer font)
2809 return gdk_font_id ((const GdkFont*) font);
2812 static GHashTable *font_cache_table = NULL;
2815 get_text_font (GdkFont* gfont)
2820 if (!font_cache_table)
2821 font_cache_table = g_hash_table_new (font_hash, (GEqualFunc) gdk_font_equal);
2823 tf = g_hash_table_lookup (font_cache_table, gfont);
2831 tf = g_new (GtkTextFont, 1);
2834 tf->gdk_font = gfont;
2835 gdk_font_ref (gfont);
2837 for(i = 0; i < 256; i += 1)
2838 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2840 g_hash_table_insert (font_cache_table, gfont, tf);
2846 text_font_unref (GtkTextFont *text_font)
2848 text_font->ref_count--;
2849 if (text_font->ref_count == 0)
2851 g_hash_table_remove (font_cache_table, text_font->gdk_font);
2852 gdk_font_unref (text_font->gdk_font);
2858 text_properties_equal (TextProperty* prop, GdkFont* font, GdkColor *fore, GdkColor *back)
2860 if (prop->flags & PROPERTY_FONT)
2863 GtkTextFont *text_font;
2868 text_font = get_text_font (font);
2870 retval = (prop->font == text_font);
2871 text_font_unref (text_font);
2880 if (prop->flags & PROPERTY_FOREGROUND)
2882 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
2889 if (prop->flags & PROPERTY_BACKGROUND)
2891 if (!back || !gdk_color_equal (&prop->back_color, back))
2902 realize_property (GtkText *text, TextProperty *prop)
2904 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2906 if (prop->flags & PROPERTY_FOREGROUND)
2907 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
2909 if (prop->flags & PROPERTY_BACKGROUND)
2910 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
2914 realize_properties (GtkText *text)
2916 GList *tmp_list = text->text_properties;
2920 realize_property (text, tmp_list->data);
2922 tmp_list = tmp_list->next;
2927 unrealize_property (GtkText *text, TextProperty *prop)
2929 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2931 if (prop->flags & PROPERTY_FOREGROUND)
2932 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
2934 if (prop->flags & PROPERTY_BACKGROUND)
2935 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
2939 unrealize_properties (GtkText *text)
2941 GList *tmp_list = text->text_properties;
2945 unrealize_property (text, tmp_list->data);
2947 tmp_list = tmp_list->next;
2951 static TextProperty*
2952 new_text_property (GtkText *text, GdkFont *font, GdkColor* fore,
2953 GdkColor* back, guint length)
2957 if (text_property_chunk == NULL)
2959 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
2960 sizeof(TextProperty),
2961 1024*sizeof(TextProperty),
2965 prop = g_chunk_new(TextProperty, text_property_chunk);
2970 prop->flags |= PROPERTY_FONT;
2971 prop->font = get_text_font (font);
2978 prop->flags |= PROPERTY_FOREGROUND;
2979 prop->fore_color = *fore;
2984 prop->flags |= PROPERTY_BACKGROUND;
2985 prop->back_color = *back;
2988 prop->length = length;
2990 if (GTK_WIDGET_REALIZED (text))
2991 realize_property (text, prop);
2997 destroy_text_property (TextProperty *prop)
3000 text_font_unref (prop->font);
3002 g_mem_chunk_free (text_property_chunk, prop);
3005 /* Flop the memory between the point and the gap around like a
3008 move_gap (GtkText* text, guint index)
3010 if (text->gap_position < index)
3012 gint diff = index - text->gap_position;
3014 if (text->use_wchar)
3015 g_memmove (text->text.wc + text->gap_position,
3016 text->text.wc + text->gap_position + text->gap_size,
3017 diff*sizeof (GdkWChar));
3019 g_memmove (text->text.ch + text->gap_position,
3020 text->text.ch + text->gap_position + text->gap_size,
3023 text->gap_position = index;
3025 else if (text->gap_position > index)
3027 gint diff = text->gap_position - index;
3029 if (text->use_wchar)
3030 g_memmove (text->text.wc + index + text->gap_size,
3031 text->text.wc + index,
3032 diff*sizeof (GdkWChar));
3034 g_memmove (text->text.ch + index + text->gap_size,
3035 text->text.ch + index,
3038 text->gap_position = index;
3042 /* Increase the gap size. */
3044 make_forward_space (GtkText* text, guint len)
3046 if (text->gap_size < len)
3048 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3050 if (sum >= text->text_len)
3054 while (i <= sum) i <<= 1;
3056 if (text->use_wchar)
3057 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3058 i*sizeof(GdkWChar));
3060 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3064 if (text->use_wchar)
3065 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3066 text->text.wc + text->gap_position + text->gap_size,
3067 (text->text_end - (text->gap_position + text->gap_size))
3070 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3071 text->text.ch + text->gap_position + text->gap_size,
3072 text->text_end - (text->gap_position + text->gap_size));
3074 text->text_end += len*2;
3075 text->gap_size += len*2;
3079 /* Inserts into the text property list a list element that guarantees
3080 * that for len characters following the point, text has the correct
3081 * property. does not move point. adjusts text_properties_point and
3082 * text_properties_point_offset relative to the current value of
3085 insert_text_property (GtkText* text, GdkFont* font,
3086 GdkColor *fore, GdkColor* back, guint len)
3088 GtkPropertyMark *mark = &text->point;
3089 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3090 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3092 if (MARK_OFFSET(mark) == 0)
3094 /* Point is on the boundary of two properties.
3095 * If it is the same as either, grow, else insert
3098 if (text_properties_equal(forward_prop, font, fore, back))
3100 /* Grow the property in front of us. */
3102 MARK_PROPERTY_LENGTH(mark) += len;
3104 else if (backward_prop &&
3105 text_properties_equal(backward_prop, font, fore, back))
3107 /* Grow property behind us, point property and offset
3110 SET_PROPERTY_MARK (&text->point,
3111 MARK_PREV_LIST_PTR (mark),
3112 backward_prop->length);
3114 backward_prop->length += len;
3116 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3117 (forward_prop->length == 1))
3119 /* Next property just has last position, take it over */
3121 if (GTK_WIDGET_REALIZED (text))
3122 unrealize_property (text, forward_prop);
3124 forward_prop->flags = 0;
3127 forward_prop->flags |= PROPERTY_FONT;
3128 forward_prop->font = get_text_font (font);
3131 forward_prop->font = NULL;
3135 forward_prop->flags |= PROPERTY_FOREGROUND;
3136 forward_prop->fore_color = *fore;
3140 forward_prop->flags |= PROPERTY_BACKGROUND;
3141 forward_prop->back_color = *back;
3143 forward_prop->length += len;
3145 if (GTK_WIDGET_REALIZED (text))
3146 realize_property (text, forward_prop);
3150 /* Splice a new property into the list. */
3152 GList* new_prop = g_list_alloc();
3154 new_prop->next = MARK_LIST_PTR(mark);
3155 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3156 new_prop->next->prev = new_prop;
3159 new_prop->prev->next = new_prop;
3161 new_prop->data = new_text_property (text, font, fore, back, len);
3163 SET_PROPERTY_MARK (mark, new_prop, 0);
3168 /* The following will screw up the line_start cache,
3169 * we'll fix it up in correct_cache_insert
3172 /* In the middle of forward_prop, if properties are equal,
3173 * just add to its length, else split it into two and splice
3175 if (text_properties_equal (forward_prop, font, fore, back))
3177 forward_prop->length += len;
3179 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3180 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3182 /* Inserting before only the last position in the text */
3185 forward_prop->length -= 1;
3187 new_prop = g_list_alloc();
3188 new_prop->data = new_text_property (text, font, fore, back, len+1);
3189 new_prop->prev = MARK_LIST_PTR(mark);
3190 new_prop->next = NULL;
3191 MARK_NEXT_LIST_PTR(mark) = new_prop;
3193 SET_PROPERTY_MARK (mark, new_prop, 0);
3197 GList* new_prop = g_list_alloc();
3198 GList* new_prop_forward = g_list_alloc();
3199 gint old_length = forward_prop->length;
3200 GList* next = MARK_NEXT_LIST_PTR(mark);
3202 /* Set the new lengths according to where they are split. Construct
3203 * two new properties. */
3204 forward_prop->length = MARK_OFFSET(mark);
3206 new_prop_forward->data =
3207 new_text_property(text,
3208 forward_prop->flags & PROPERTY_FONT ?
3209 forward_prop->font->gdk_font : NULL,
3210 forward_prop->flags & PROPERTY_FOREGROUND ?
3211 &forward_prop->fore_color : NULL,
3212 forward_prop->flags & PROPERTY_BACKGROUND ?
3213 &forward_prop->back_color : NULL,
3214 old_length - forward_prop->length);
3216 new_prop->data = new_text_property(text, font, fore, back, len);
3218 /* Now splice things in. */
3219 MARK_NEXT_LIST_PTR(mark) = new_prop;
3220 new_prop->prev = MARK_LIST_PTR(mark);
3222 new_prop->next = new_prop_forward;
3223 new_prop_forward->prev = new_prop;
3225 new_prop_forward->next = next;
3228 next->prev = new_prop_forward;
3230 SET_PROPERTY_MARK (mark, new_prop, 0);
3234 while (text->text_properties_end->next)
3235 text->text_properties_end = text->text_properties_end->next;
3237 while (text->text_properties->prev)
3238 text->text_properties = text->text_properties->prev;
3242 delete_text_property (GtkText* text, guint nchars)
3244 /* Delete nchars forward from point. */
3246 /* Deleting text properties is problematical, because we
3247 * might be storing around marks pointing to a property.
3249 * The marks in question and how we handle them are:
3251 * point: We know the new value, since it will be at the
3252 * end of the deleted text, and we move it there
3254 * cursor: We just remove the mark and set it equal to the
3255 * point after the operation.
3256 * line-start cache: We replace most affected lines.
3257 * The current line gets used to fetch the new
3258 * lines so, if necessary, (delete at the beginning
3259 * of a line) we fix it up by setting it equal to the
3267 for(; nchars; nchars -= 1)
3269 prop = MARK_CURRENT_PROPERTY(&text->point);
3273 if (prop->length == 0)
3275 tmp = MARK_LIST_PTR (&text->point);
3277 is_first = tmp == text->text_properties;
3279 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3280 text->point.offset = 0;
3282 if (GTK_WIDGET_REALIZED (text))
3283 unrealize_property (text, prop);
3285 destroy_text_property (prop);
3286 g_list_free_1 (tmp);
3288 prop = MARK_CURRENT_PROPERTY (&text->point);
3291 text->text_properties = MARK_LIST_PTR (&text->point);
3293 g_assert (prop->length != 0);
3295 else if (prop->length == text->point.offset)
3297 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3298 text->point.offset = 0;
3302 /* Check to see if we have just the single final position remaining
3303 * along in a property; if so, combine it with the previous property
3305 if (LAST_INDEX (text, text->point) &&
3306 (MARK_OFFSET (&text->point) == 0) &&
3307 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3309 tmp = MARK_LIST_PTR (&text->point);
3310 prop = MARK_CURRENT_PROPERTY(&text->point);
3312 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3313 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3314 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3316 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3318 if (GTK_WIDGET_REALIZED (text))
3319 unrealize_property (text, prop);
3321 destroy_text_property (prop);
3322 g_list_free_1 (tmp);
3327 init_properties (GtkText *text)
3329 if (!text->text_properties)
3331 text->text_properties = g_list_alloc();
3332 text->text_properties->next = NULL;
3333 text->text_properties->prev = NULL;
3334 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3335 text->text_properties_end = text->text_properties;
3337 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3339 text->point.index = 0;
3344 /**********************************************************************/
3345 /* Property Movement */
3346 /**********************************************************************/
3349 move_mark_n (GtkPropertyMark* mark, gint n)
3352 advance_mark_n(mark, n);
3354 decrement_mark_n(mark, -n);
3358 advance_mark (GtkPropertyMark* mark)
3360 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3364 if (prop->length > mark->offset + 1)
3368 mark->property = MARK_NEXT_LIST_PTR (mark);
3374 advance_mark_n (GtkPropertyMark* mark, gint n)
3381 i = 0; /* otherwise it migth not be init. */
3382 prop = MARK_CURRENT_PROPERTY(mark);
3384 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3385 /* to make it easier */
3386 n += (mark->offset);
3387 mark->index -= mark->offset;
3389 /* first we take seven-mile-leaps to get to the right text
3391 while ((n-i) > prop->length - 1) {
3393 mark->index += prop->length;
3394 mark->property = MARK_NEXT_LIST_PTR (mark);
3395 prop = MARK_CURRENT_PROPERTY (mark);
3399 /* and then the rest */
3400 mark->index += n - i;
3401 mark->offset += n - i;
3405 decrement_mark (GtkPropertyMark* mark)
3409 if (mark->offset > 0)
3413 mark->property = MARK_PREV_LIST_PTR (mark);
3414 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3419 decrement_mark_n (GtkPropertyMark* mark, gint n)
3423 while (mark->offset < n) {
3424 /* jump to end of prev */
3425 n -= mark->offset + 1;
3426 mark->index -= mark->offset + 1;
3427 mark->property = MARK_PREV_LIST_PTR (mark);
3428 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3436 static GtkPropertyMark
3437 find_mark (GtkText* text, guint mark_position)
3439 return find_mark_near (text, mark_position, &text->point);
3443 * You can also start from the end, what a drag.
3445 static GtkPropertyMark
3446 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3451 GtkPropertyMark mark;
3454 diffa = mark_position + 1;
3456 diffa = mark_position - near->index;
3458 diffb = mark_position;
3470 mark.property = text->text_properties;
3474 move_mark_n (&mark, mark_position - mark.index);
3479 /* This routine must be called with scroll == FALSE, only when
3480 * point is at least partially on screen
3484 find_line_containing_point (GtkText* text, guint point,
3490 text->current_line = NULL;
3494 /* Scroll backwards until the point is on screen
3496 while (CACHE_DATA(text->line_start_cache).start.index > point)
3497 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3499 /* Now additionally try to make sure that the point is fully on screen
3503 while (text->first_cut_pixels != 0 &&
3504 text->line_start_cache->next &&
3505 CACHE_DATA(text->line_start_cache->next).start.index > point)
3506 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3509 gdk_window_get_size (text->text_area, NULL, &height);
3511 for (cache = text->line_start_cache; cache; cache = cache->next)
3515 if (CACHE_DATA(cache).end.index >= point ||
3516 LAST_INDEX(text, CACHE_DATA(cache).end))
3518 text->current_line = cache; /* LOOK HERE, this proc has an
3519 * important side effect. */
3523 TEXT_SHOW_LINE (text, cache, "cache");
3525 if (cache->next == NULL)
3526 fetch_lines_forward (text, 1);
3530 lph = pixel_height_of (text, cache->next);
3532 /* Scroll the bottom of the line is on screen, or until
3533 * the line is the first onscreen line.
3535 while (cache->next != text->line_start_cache && lph > height)
3537 TEXT_SHOW_LINE (text, cache, "cache");
3538 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3539 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3540 lph = pixel_height_of (text, cache->next);
3545 g_assert_not_reached (); /* Must set text->current_line here */
3549 pixel_height_of (GtkText* text, GList* cache_line)
3551 gint pixels = - text->first_cut_pixels;
3552 GList *cache = text->line_start_cache;
3555 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3557 if (cache->data == cache_line->data)
3560 cache = cache->next;
3566 /**********************************************************************/
3567 /* Search and Placement */
3568 /**********************************************************************/
3571 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3574 gint16* char_widths;
3576 if (LAST_INDEX (text, *mark))
3579 ch = GTK_TEXT_INDEX (text, mark->index);
3580 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3584 return tab_mark->to_next_tab * char_widths[' '];
3588 return char_widths[ch];
3592 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3597 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, GdkWChar ch)
3599 if (tab_mark->to_next_tab == 1 || ch == '\t')
3601 if (tab_mark->tab_stops->next)
3603 tab_mark->tab_stops = tab_mark->tab_stops->next;
3604 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3608 tab_mark->to_next_tab = text->default_tab_width;
3613 tab_mark->to_next_tab -= 1;
3618 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3622 advance_tab_mark (text, tab_mark, 0);
3626 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3630 GtkPropertyMark mark = start_line->start;
3631 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3632 gint pixel_width = LINE_START_PIXEL (*start_line);
3634 while (mark.index < text->cursor_mark.index)
3636 pixel_width += find_char_width (text, &mark, &tab_mark);
3638 advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3639 advance_mark (&mark);
3642 text->cursor_pos_x = pixel_width;
3643 text->cursor_pos_y = pixel_height;
3644 text->cursor_char_offset = start_line->font_descent;
3645 text->cursor_mark = mark;
3647 ch = LAST_INDEX (text, mark) ?
3648 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3650 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3651 text->cursor_char = 0;
3653 text->cursor_char = ch;
3657 find_cursor (GtkText* text, gboolean scroll)
3659 if (GTK_WIDGET_REALIZED (text))
3661 find_line_containing_point (text, text->cursor_mark.index, scroll);
3663 if (text->current_line)
3664 find_cursor_at_line (text,
3665 &CACHE_DATA(text->current_line),
3666 pixel_height_of(text, text->current_line));
3669 GTK_OLD_EDITABLE (text)->current_pos = text->cursor_mark.index;
3673 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3674 guint line_pixel_height,
3677 GtkPropertyMark mark = lp->start;
3678 TabStopMark tab_mark = lp->tab_cont.tab_start;
3680 gint char_width = find_char_width(text, &mark, &tab_mark);
3681 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3683 text->cursor_pos_y = line_pixel_height;
3687 GdkWChar ch = LAST_INDEX (text, mark) ?
3688 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3690 if (button_x < pixel_width || mark.index == lp->end.index)
3692 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3693 text->cursor_mark = mark;
3694 text->cursor_char_offset = lp->font_descent;
3696 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3697 text->cursor_char = 0;
3699 text->cursor_char = ch;
3704 advance_tab_mark (text, &tab_mark, ch);
3705 advance_mark (&mark);
3707 pixel_width += char_width/2;
3709 char_width = find_char_width (text, &mark, &tab_mark);
3711 pixel_width += (char_width+1)/2;
3716 find_mouse_cursor (GtkText* text, gint x, gint y)
3719 GList* cache = text->line_start_cache;
3723 pixel_height = - text->first_cut_pixels;
3725 for (; cache; cache = cache->next)
3727 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3729 if (y < pixel_height || !cache->next)
3731 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3733 find_cursor (text, FALSE);
3740 /**********************************************************************/
3742 /**********************************************************************/
3745 free_cache (GtkText* text)
3747 GList* cache = text->line_start_cache;
3752 cache = cache->prev;
3754 text->line_start_cache = cache;
3757 for (; cache; cache = cache->next)
3758 g_mem_chunk_free (params_mem_chunk, cache->data);
3760 g_list_free (text->line_start_cache);
3762 text->line_start_cache = NULL;
3766 remove_cache_line (GtkText* text, GList* member)
3773 if (member == text->line_start_cache)
3774 text->line_start_cache = text->line_start_cache->next;
3777 member->prev->next = member->next;
3780 member->next->prev = member->prev;
3782 list = member->next;
3784 g_mem_chunk_free (params_mem_chunk, member->data);
3785 g_list_free_1 (member);
3790 /**********************************************************************/
3792 /**********************************************************************/
3795 move_cursor_buffer_ver (GtkText *text, int dir)
3797 undraw_cursor (text, FALSE);
3801 scroll_int (text, text->vadj->upper);
3802 text->cursor_mark = find_this_line_start_mark (text,
3804 &text->cursor_mark);
3808 scroll_int (text, - text->vadj->value);
3809 text->cursor_mark = find_this_line_start_mark (text,
3811 &text->cursor_mark);
3814 find_cursor (text, TRUE);
3815 draw_cursor (text, FALSE);
3819 move_cursor_page_ver (GtkText *text, int dir)
3821 scroll_int (text, dir * text->vadj->page_increment);
3825 move_cursor_ver (GtkText *text, int count)
3828 GtkPropertyMark mark;
3831 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3832 offset = text->cursor_mark.index - mark.index;
3834 if (offset > text->cursor_virtual_x)
3835 text->cursor_virtual_x = offset;
3839 if (mark.index == 0)
3842 decrement_mark (&mark);
3843 mark = find_this_line_start_mark (text, mark.index, &mark);
3847 mark = text->cursor_mark;
3849 while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3850 advance_mark (&mark);
3852 if (LAST_INDEX(text, mark))
3855 advance_mark (&mark);
3858 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3859 if (LAST_INDEX(text, mark) ||
3860 GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3863 undraw_cursor (text, FALSE);
3865 text->cursor_mark = mark;
3867 find_cursor (text, TRUE);
3869 draw_cursor (text, FALSE);
3873 move_cursor_hor (GtkText *text, int count)
3875 /* count should be +-1. */
3876 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3877 (count < 0 && text->cursor_mark.index < (- count)) ||
3881 text->cursor_virtual_x = 0;
3883 undraw_cursor (text, FALSE);
3885 move_mark_n (&text->cursor_mark, count);
3887 find_cursor (text, TRUE);
3889 draw_cursor (text, FALSE);
3893 gtk_text_move_cursor (GtkOldEditable *old_editable,
3900 move_cursor_hor (GTK_TEXT (old_editable), 1);
3905 move_cursor_hor (GTK_TEXT (old_editable), -1);
3911 move_cursor_ver (GTK_TEXT (old_editable), 1);
3916 move_cursor_ver (GTK_TEXT (old_editable), -1);
3921 gtk_text_move_forward_character (GtkText *text)
3923 move_cursor_hor (text, 1);
3927 gtk_text_move_backward_character (GtkText *text)
3929 move_cursor_hor (text, -1);
3933 gtk_text_move_next_line (GtkText *text)
3935 move_cursor_ver (text, 1);
3939 gtk_text_move_previous_line (GtkText *text)
3941 move_cursor_ver (text, -1);
3945 gtk_text_move_word (GtkOldEditable *old_editable,
3951 gtk_text_move_forward_word (GTK_TEXT (old_editable));
3956 gtk_text_move_backward_word (GTK_TEXT (old_editable));
3961 gtk_text_move_forward_word (GtkText *text)
3963 text->cursor_virtual_x = 0;
3965 undraw_cursor (text, FALSE);
3967 if (text->use_wchar)
3969 while (!LAST_INDEX (text, text->cursor_mark) &&
3970 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3971 advance_mark (&text->cursor_mark);
3973 while (!LAST_INDEX (text, text->cursor_mark) &&
3974 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3975 advance_mark (&text->cursor_mark);
3979 while (!LAST_INDEX (text, text->cursor_mark) &&
3980 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3981 advance_mark (&text->cursor_mark);
3983 while (!LAST_INDEX (text, text->cursor_mark) &&
3984 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3985 advance_mark (&text->cursor_mark);
3988 find_cursor (text, TRUE);
3989 draw_cursor (text, FALSE);
3993 gtk_text_move_backward_word (GtkText *text)
3995 text->cursor_virtual_x = 0;
3997 undraw_cursor (text, FALSE);
3999 if (text->use_wchar)
4001 while ((text->cursor_mark.index > 0) &&
4002 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4003 decrement_mark (&text->cursor_mark);
4005 while ((text->cursor_mark.index > 0) &&
4006 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4007 decrement_mark (&text->cursor_mark);
4011 while ((text->cursor_mark.index > 0) &&
4012 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4013 decrement_mark (&text->cursor_mark);
4015 while ((text->cursor_mark.index > 0) &&
4016 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4017 decrement_mark (&text->cursor_mark);
4020 find_cursor (text, TRUE);
4021 draw_cursor (text, FALSE);
4025 gtk_text_move_page (GtkOldEditable *old_editable,
4030 scroll_int (GTK_TEXT (old_editable),
4031 y * GTK_TEXT(old_editable)->vadj->page_increment);
4035 gtk_text_move_to_row (GtkOldEditable *old_editable,
4041 gtk_text_move_to_column (GtkOldEditable *old_editable,
4046 text = GTK_TEXT (old_editable);
4048 text->cursor_virtual_x = 0; /* FIXME */
4050 undraw_cursor (text, FALSE);
4052 /* Move to the beginning of the line */
4053 while ((text->cursor_mark.index > 0) &&
4054 (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4055 decrement_mark (&text->cursor_mark);
4057 while (!LAST_INDEX (text, text->cursor_mark) &&
4058 (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4062 else if (column == 0)
4065 advance_mark (&text->cursor_mark);
4068 find_cursor (text, TRUE);
4069 draw_cursor (text, FALSE);
4073 gtk_text_move_beginning_of_line (GtkText *text)
4075 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4080 gtk_text_move_end_of_line (GtkText *text)
4082 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), -1);
4086 gtk_text_kill_char (GtkOldEditable *old_editable,
4091 text = GTK_TEXT (old_editable);
4093 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4094 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4099 if (text->point.index + 1 <= TEXT_LENGTH (text))
4100 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index, text->point.index + 1);
4104 if (text->point.index > 0)
4105 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index - 1, text->point.index);
4111 gtk_text_delete_forward_character (GtkText *text)
4113 gtk_text_kill_char (GTK_OLD_EDITABLE (text), 1);
4117 gtk_text_delete_backward_character (GtkText *text)
4119 gtk_text_kill_char (GTK_OLD_EDITABLE (text), -1);
4123 gtk_text_kill_word (GtkOldEditable *old_editable,
4126 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4127 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4130 gint old_pos = old_editable->current_pos;
4133 gtk_text_move_word (old_editable, 1);
4134 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4138 gtk_text_move_word (old_editable, -1);
4139 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4145 gtk_text_delete_forward_word (GtkText *text)
4147 gtk_text_kill_word (GTK_OLD_EDITABLE (text), 1);
4151 gtk_text_delete_backward_word (GtkText *text)
4153 gtk_text_kill_word (GTK_OLD_EDITABLE (text), -1);
4157 gtk_text_kill_line (GtkOldEditable *old_editable,
4160 gint old_pos = old_editable->current_pos;
4163 gtk_text_move_to_column (old_editable, -1);
4164 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4168 gtk_text_move_to_column (old_editable, 0);
4169 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4174 gtk_text_delete_line (GtkText *text)
4176 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4177 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4181 gtk_text_delete_to_line_end (GtkText *text)
4183 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4187 gtk_text_select_word (GtkText *text, guint32 time)
4192 GtkOldEditable *old_editable;
4193 old_editable = GTK_OLD_EDITABLE (text);
4195 gtk_text_move_backward_word (text);
4196 start_pos = text->cursor_mark.index;
4198 gtk_text_move_forward_word (text);
4199 end_pos = text->cursor_mark.index;
4201 old_editable->has_selection = TRUE;
4202 gtk_text_set_selection (old_editable, start_pos, end_pos);
4203 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4207 gtk_text_select_line (GtkText *text, guint32 time)
4212 GtkOldEditable *old_editable;
4213 old_editable = GTK_OLD_EDITABLE (text);
4215 gtk_text_move_beginning_of_line (text);
4216 start_pos = text->cursor_mark.index;
4218 gtk_text_move_end_of_line (text);
4219 gtk_text_move_forward_character (text);
4220 end_pos = text->cursor_mark.index;
4222 old_editable->has_selection = TRUE;
4223 gtk_text_set_selection (old_editable, start_pos, end_pos);
4224 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4227 /**********************************************************************/
4229 /**********************************************************************/
4232 adjust_adj (GtkText* text, GtkAdjustment* adj)
4236 gdk_window_get_size (text->text_area, NULL, &height);
4238 adj->step_increment = MIN (adj->upper, SCROLL_PIXELS);
4239 adj->page_increment = MIN (adj->upper, height - KEY_SCROLL_PIXELS);
4240 adj->page_size = MIN (adj->upper, height);
4241 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4242 adj->value = MAX (adj->value, 0.0);
4244 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4248 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
4250 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4252 if ((text->first_line_start_index >= lp->start.index) &&
4253 (text->first_line_start_index <= lp->end.index))
4255 svdata->mark = lp->start;
4257 if (text->first_line_start_index == lp->start.index)
4259 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4263 text->first_onscreen_ver_pixel = svdata->pixel_height;
4264 text->first_cut_pixels = 0;
4267 text->vadj->value = text->first_onscreen_ver_pixel;
4270 svdata->pixel_height += LINE_HEIGHT (*lp);
4276 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
4278 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4281 if (svdata->pixel_height <= (gint) text->vadj->value &&
4282 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4284 svdata->mark = lp->start;
4286 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4287 text->first_onscreen_ver_pixel = svdata->pixel_height;
4288 text->first_line_start_index = lp->start.index;
4294 svdata->pixel_height += LINE_HEIGHT (*lp);
4302 static GtkPropertyMark
4303 set_vertical_scroll (GtkText* text)
4305 GtkPropertyMark mark = find_mark (text, 0);
4306 SetVerticalScrollData data;
4310 data.pixel_height = 0;
4311 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4313 text->vadj->upper = data.pixel_height;
4314 orig_value = (gint) text->vadj->value;
4316 gdk_window_get_size (text->text_area, NULL, &height);
4318 text->vadj->step_increment = MIN (text->vadj->upper, SCROLL_PIXELS);
4319 text->vadj->page_increment = MIN (text->vadj->upper, height - KEY_SCROLL_PIXELS);
4320 text->vadj->page_size = MIN (text->vadj->upper, height);
4321 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4322 text->vadj->value = MAX (text->vadj->value, 0.0);
4324 text->last_ver_value = (gint)text->vadj->value;
4326 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4328 if (text->vadj->value != orig_value)
4330 /* We got clipped, and don't really know which line to put first. */
4331 data.pixel_height = 0;
4332 data.last_didnt_wrap = TRUE;
4334 line_params_iterate (text, &mark, NULL,
4336 set_vertical_scroll_find_iterator);
4343 scroll_int (GtkText* text, gint diff)
4347 text->vadj->value += diff;
4349 upper = text->vadj->upper - text->vadj->page_size;
4350 text->vadj->value = MIN (text->vadj->value, upper);
4351 text->vadj->value = MAX (text->vadj->value, 0.0);
4353 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4357 process_exposes (GtkText *text)
4361 /* Make sure graphics expose events are processed before scrolling
4364 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4366 gtk_widget_send_expose (GTK_WIDGET (text), event);
4367 if (event->expose.count == 0)
4369 gdk_event_free (event);
4372 gdk_event_free (event);
4376 static gint last_visible_line_height (GtkText* text)
4378 GList *cache = text->line_start_cache;
4381 gdk_window_get_size (text->text_area, NULL, &height);
4383 for (; cache->next; cache = cache->next)
4384 if (pixel_height_of(text, cache->next) > height)
4388 return pixel_height_of(text, cache) - 1;
4393 static gint first_visible_line_height (GtkText* text)
4395 if (text->first_cut_pixels)
4396 return pixel_height_of(text, text->line_start_cache) + 1;
4402 scroll_down (GtkText* text, gint diff0)
4408 text->first_onscreen_ver_pixel += diff0;
4412 g_assert (text->line_start_cache);
4414 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4416 text->first_cut_pixels += 1;
4420 text->first_cut_pixels = 0;
4422 text->line_start_cache = text->line_start_cache->next;
4424 text->first_line_start_index =
4425 CACHE_DATA(text->line_start_cache).start.index;
4427 if (!text->line_start_cache->next)
4428 fetch_lines_forward (text, 1);
4434 gdk_window_get_size (text->text_area, &width, &height);
4435 if (height > real_diff)
4436 gdk_draw_pixmap (text->text_area,
4444 height - real_diff);
4447 rect.y = MAX (0, height - real_diff);
4449 rect.height = MIN (height, real_diff);
4451 expose_text (text, &rect, FALSE);
4452 gtk_text_draw_focus ( (GtkWidget *) text);
4454 if (text->current_line)
4458 text->cursor_pos_y -= real_diff;
4459 cursor_min = drawn_cursor_min(text);
4462 find_mouse_cursor (text, text->cursor_pos_x,
4463 first_visible_line_height (text));
4466 if (height > real_diff)
4467 process_exposes (text);
4471 scroll_up (GtkText* text, gint diff0)
4477 text->first_onscreen_ver_pixel += diff0;
4481 g_assert (text->line_start_cache);
4483 if (text->first_cut_pixels > 0)
4485 text->first_cut_pixels -= 1;
4489 if (!text->line_start_cache->prev)
4490 fetch_lines_backward (text);
4492 text->line_start_cache = text->line_start_cache->prev;
4494 text->first_line_start_index =
4495 CACHE_DATA(text->line_start_cache).start.index;
4497 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4503 gdk_window_get_size (text->text_area, &width, &height);
4504 if (height > real_diff)
4505 gdk_draw_pixmap (text->text_area,
4513 height - real_diff);
4518 rect.height = MIN (height, real_diff);
4520 expose_text (text, &rect, FALSE);
4521 gtk_text_draw_focus ( (GtkWidget *) text);
4523 if (text->current_line)
4528 text->cursor_pos_y += real_diff;
4529 cursor_max = drawn_cursor_max(text);
4530 gdk_window_get_size (text->text_area, NULL, &height);
4532 if (cursor_max >= height)
4533 find_mouse_cursor (text, text->cursor_pos_x,
4534 last_visible_line_height (text));
4537 if (height > real_diff)
4538 process_exposes (text);
4541 /**********************************************************************/
4543 /**********************************************************************/
4545 /* Assumes mark starts a line. Calculates the height, width, and
4546 * displayable character count of a single DISPLAYABLE line. That
4547 * means that in line-wrap mode, this does may not compute the
4548 * properties of an entire line. */
4550 find_line_params (GtkText* text,
4551 const GtkPropertyMark* mark,
4552 const PrevTabCont *tab_cont,
4553 PrevTabCont *next_cont)
4556 TabStopMark tab_mark = tab_cont->tab_start;
4557 guint max_display_pixels;
4562 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4563 max_display_pixels -= LINE_WRAP_ROOM;
4566 lp.tab_cont = *tab_cont;
4569 lp.pixel_width = tab_cont->pixel_offset;
4570 lp.displayable_chars = 0;
4572 lp.font_descent = 0;
4574 init_tab_cont (text, next_cont);
4576 while (!LAST_INDEX(text, lp.end))
4578 g_assert (lp.end.property);
4580 ch = GTK_TEXT_INDEX (text, lp.end.index);
4581 font = MARK_CURRENT_FONT (text, &lp.end);
4583 if (ch == LINE_DELIM)
4585 /* Newline doesn't count in computation of line height, even
4586 * if its in a bigger font than the rest of the line. Unless,
4587 * of course, there are no other characters. */
4589 if (!lp.font_ascent && !lp.font_descent)
4591 lp.font_ascent = font->ascent;
4592 lp.font_descent = font->descent;
4595 lp.tab_cont_next = *next_cont;
4600 ch_width = find_char_width (text, &lp.end, &tab_mark);
4602 if ((ch_width + lp.pixel_width > max_display_pixels) &&
4603 (lp.end.index > lp.start.index))
4607 if (text->line_wrap)
4609 next_cont->tab_start = tab_mark;
4610 next_cont->pixel_offset = 0;
4614 /* Here's the tough case, a tab is wrapping. */
4615 gint pixels_avail = max_display_pixels - lp.pixel_width;
4616 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
4617 gint spaces_avail = pixels_avail / space_width;
4619 if (spaces_avail == 0)
4621 decrement_mark (&lp.end);
4625 advance_tab_mark (text, &next_cont->tab_start, '\t');
4626 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4628 lp.displayable_chars += 1;
4633 if (text->word_wrap)
4635 GtkPropertyMark saved_mark = lp.end;
4636 guint saved_characters = lp.displayable_chars;
4638 lp.displayable_chars += 1;
4640 if (text->use_wchar)
4642 while (!gdk_iswspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4643 (lp.end.index > lp.start.index))
4645 decrement_mark (&lp.end);
4646 lp.displayable_chars -= 1;
4651 while (!isspace(GTK_TEXT_INDEX (text, lp.end.index)) &&
4652 (lp.end.index > lp.start.index))
4654 decrement_mark (&lp.end);
4655 lp.displayable_chars -= 1;
4659 /* If whole line is one word, revert to char wrapping */
4660 if (lp.end.index == lp.start.index)
4662 lp.end = saved_mark;
4663 lp.displayable_chars = saved_characters;
4664 decrement_mark (&lp.end);
4669 /* Don't include this character, it will wrap. */
4670 decrement_mark (&lp.end);
4674 lp.tab_cont_next = *next_cont;
4681 lp.displayable_chars += 1;
4684 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4685 lp.font_descent = MAX (font->descent, lp.font_descent);
4686 lp.pixel_width += ch_width;
4688 advance_mark(&lp.end);
4689 advance_tab_mark (text, &tab_mark, ch);
4692 if (LAST_INDEX(text, lp.start))
4694 /* Special case, empty last line. */
4695 font = MARK_CURRENT_FONT (text, &lp.end);
4697 lp.font_ascent = font->ascent;
4698 lp.font_descent = font->descent;
4701 lp.tab_cont_next = *next_cont;
4707 expand_scratch_buffer (GtkText* text, guint len)
4709 if (len >= text->scratch_buffer_len)
4713 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4715 if (text->use_wchar)
4717 if (text->scratch_buffer.wc)
4718 text->scratch_buffer.wc = g_new (GdkWChar, i);
4720 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
4721 i*sizeof (GdkWChar));
4725 if (text->scratch_buffer.ch)
4726 text->scratch_buffer.ch = g_new (guchar, i);
4728 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
4731 text->scratch_buffer_len = i;
4735 /* Side effect: modifies text->gc
4738 draw_bg_rect (GtkText* text, GtkPropertyMark *mark,
4739 gint x, gint y, gint width, gint height,
4740 gboolean already_cleared)
4742 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4744 if ((mark->index >= MIN(old_editable->selection_start_pos, old_editable->selection_end_pos) &&
4745 mark->index < MAX(old_editable->selection_start_pos, old_editable->selection_end_pos)))
4747 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
4748 old_editable->has_selection ?
4749 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
4751 NULL, GTK_WIDGET(text), "text",
4752 x, y, width, height);
4754 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
4755 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
4757 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
4759 gdk_draw_rectangle (text->text_area,
4761 TRUE, x, y, width, height);
4763 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4770 rect.height = height;
4772 clear_area (text, &rect);
4774 else if (!already_cleared)
4775 gdk_window_clear_area (text->text_area, x, y, width, height);
4779 draw_line (GtkText* text,
4780 gint pixel_start_height,
4783 GdkGCValues gc_values;
4786 guint running_offset = lp->tab_cont.pixel_offset;
4787 union { GdkWChar *wc; guchar *ch; } buffer;
4790 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4792 guint selection_start_pos = MIN (old_editable->selection_start_pos, old_editable->selection_end_pos);
4793 guint selection_end_pos = MAX (old_editable->selection_start_pos, old_editable->selection_end_pos);
4795 GtkPropertyMark mark = lp->start;
4796 TabStopMark tab_mark = lp->tab_cont.tab_start;
4797 gint pixel_height = pixel_start_height + lp->font_ascent;
4798 guint chars = lp->displayable_chars;
4800 /* First provide a contiguous segment of memory. This makes reading
4801 * the code below *much* easier, and only incurs the cost of copying
4802 * when the line being displayed spans the gap. */
4803 if (mark.index <= text->gap_position &&
4804 mark.index + chars > text->gap_position)
4806 expand_scratch_buffer (text, chars);
4808 if (text->use_wchar)
4810 for (i = 0; i < chars; i += 1)
4811 text->scratch_buffer.wc[i] = GTK_TEXT_INDEX(text, mark.index + i);
4812 buffer.wc = text->scratch_buffer.wc;
4816 for (i = 0; i < chars; i += 1)
4817 text->scratch_buffer.ch[i] = GTK_TEXT_INDEX(text, mark.index + i);
4818 buffer.ch = text->scratch_buffer.ch;
4823 if (text->use_wchar)
4825 if (mark.index >= text->gap_position)
4826 buffer.wc = text->text.wc + mark.index + text->gap_size;
4828 buffer.wc = text->text.wc + mark.index;
4832 if (mark.index >= text->gap_position)
4833 buffer.ch = text->text.ch + mark.index + text->gap_size;
4835 buffer.ch = text->text.ch + mark.index;
4840 if (running_offset > 0)
4842 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
4843 LINE_HEIGHT (*lp), TRUE);
4849 if ((text->use_wchar && buffer.wc[0] != '\t') ||
4850 (!text->use_wchar && buffer.ch[0] != '\t'))
4852 union { GdkWChar *wc; guchar *ch; } next_tab;
4857 if (text->use_wchar)
4858 for (i=0; i<chars; i++)
4860 if (buffer.wc[i] == '\t')
4862 next_tab.wc = buffer.wc + i;
4867 next_tab.ch = memchr (buffer.ch, '\t', chars);
4869 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4871 if (text->use_wchar)
4874 len = MIN (len, next_tab.wc - buffer.wc);
4879 len = MIN (len, next_tab.ch - buffer.ch);
4882 if (mark.index < selection_start_pos)
4883 len = MIN (len, selection_start_pos - mark.index);
4884 else if (mark.index < selection_end_pos)
4885 len = MIN (len, selection_end_pos - mark.index);
4887 font = MARK_CURRENT_FONT (text, &mark);
4888 if (font->type == GDK_FONT_FONT)
4890 gdk_gc_set_font (text->gc, font);
4891 gdk_gc_get_values (text->gc, &gc_values);
4892 if (text->use_wchar)
4893 pixel_width = gdk_text_width_wc (gc_values.font,
4896 pixel_width = gdk_text_width (gc_values.font,
4901 if (text->use_wchar)
4902 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
4904 pixel_width = gdk_text_width (font, buffer.ch, len);
4907 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4908 pixel_width, LINE_HEIGHT (*lp), TRUE);
4910 if ((mark.index >= selection_start_pos) &&
4911 (mark.index < selection_end_pos))
4913 if (old_editable->has_selection)
4914 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_SELECTED];
4916 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_ACTIVE];
4920 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
4924 if (text->use_wchar)
4925 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
4932 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
4939 running_offset += pixel_width;
4941 advance_tab_mark_n (text, &tab_mark, len);
4945 gint pixels_remaining;
4951 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
4952 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
4954 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4956 spaces_avail = pixels_remaining / space_width;
4957 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
4959 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4960 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
4962 running_offset += tab_mark.to_next_tab *
4963 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4965 advance_tab_mark (text, &tab_mark, '\t');
4968 advance_mark_n (&mark, len);
4969 if (text->use_wchar)
4978 draw_line_wrap (GtkText* text, guint height /* baseline height */)
4985 if (text->line_wrap)
4987 bitmap = text->line_wrap_bitmap;
4988 bitmap_width = line_wrap_width;
4989 bitmap_height = line_wrap_height;
4993 bitmap = text->line_arrow_bitmap;
4994 bitmap_width = line_arrow_width;
4995 bitmap_height = line_arrow_height;
4998 gdk_window_get_size (text->text_area, &width, NULL);
4999 width -= LINE_WRAP_ROOM;
5001 gdk_gc_set_stipple (text->gc,
5004 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
5006 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5008 gdk_gc_set_ts_origin (text->gc,
5010 height - bitmap_height - 1);
5012 gdk_draw_rectangle (text->text_area,
5016 height - bitmap_height - 1 /* one pixel above the baseline. */,
5020 gdk_gc_set_ts_origin (text->gc, 0, 0);
5022 gdk_gc_set_fill (text->gc, GDK_SOLID);
5026 undraw_cursor (GtkText* text, gint absolute)
5028 GtkOldEditable *old_editable = (GtkOldEditable *) text;
5030 TDEBUG (("in undraw_cursor\n"));
5033 text->cursor_drawn_level = 0;
5035 if ((text->cursor_drawn_level ++ == 0) &&
5036 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5037 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5041 g_assert(text->cursor_mark.property);
5043 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5045 draw_bg_rect (text, &text->cursor_mark,
5047 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5048 1, font->ascent + 1, FALSE);
5050 if (text->cursor_char)
5052 if (font->type == GDK_FONT_FONT)
5053 gdk_gc_set_font (text->gc, font);
5055 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5057 gdk_draw_text_wc (text->text_area, font,
5060 text->cursor_pos_y - text->cursor_char_offset,
5068 drawn_cursor_min (GtkText* text)
5072 g_assert(text->cursor_mark.property);
5074 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5076 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5080 drawn_cursor_max (GtkText* text)
5084 g_assert(text->cursor_mark.property);
5086 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5088 return text->cursor_pos_y - text->cursor_char_offset;
5092 draw_cursor (GtkText* text, gint absolute)
5094 GtkOldEditable *old_editable = (GtkOldEditable *)text;
5096 TDEBUG (("in draw_cursor\n"));
5099 text->cursor_drawn_level = 1;
5101 if ((--text->cursor_drawn_level == 0) &&
5102 old_editable->editable &&
5103 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5104 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5108 g_assert (text->cursor_mark.property);
5110 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5112 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5114 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
5115 text->cursor_pos_y - text->cursor_char_offset,
5117 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5122 create_bg_gc (GtkText *text)
5126 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5127 values.fill = GDK_TILED;
5129 return gdk_gc_new_with_values (text->text_area, &values,
5130 GDK_GC_FILL | GDK_GC_TILE);
5134 clear_area (GtkText *text, GdkRectangle *area)
5136 GtkWidget *widget = GTK_WIDGET (text);
5142 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5144 gdk_gc_set_ts_origin (text->bg_gc,
5145 (- text->first_onscreen_hor_pixel) % width,
5146 (- text->first_onscreen_ver_pixel) % height);
5148 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5149 area->x, area->y, area->width, area->height);
5152 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5156 expose_text (GtkText* text, GdkRectangle *area, gboolean cursor)
5158 GList *cache = text->line_start_cache;
5159 gint pixels = - text->first_cut_pixels;
5160 gint min_y = MAX (0, area->y);
5161 gint max_y = MAX (0, area->y + area->height);
5164 gdk_window_get_size (text->text_area, NULL, &height);
5165 max_y = MIN (max_y, height);
5167 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5169 clear_area (text, area);
5171 for (; pixels < height; cache = cache->next)
5173 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5175 draw_line (text, pixels, &CACHE_DATA(cache));
5177 if (CACHE_DATA(cache).wraps)
5178 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5181 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5183 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5184 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5186 /* We undraw and draw the cursor here to get the drawn
5187 * level right ... FIXME - maybe the second parameter
5188 * of draw_cursor should work differently
5190 undraw_cursor (text, FALSE);
5191 draw_cursor (text, FALSE);
5195 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5199 fetch_lines_forward (text, 1);
5208 gtk_text_update_text (GtkOldEditable *old_editable,
5212 GtkText *text = GTK_TEXT (old_editable);
5214 GList *cache = text->line_start_cache;
5215 gint pixels = - text->first_cut_pixels;
5221 end_pos = TEXT_LENGTH (text);
5223 if (end_pos < start_pos)
5226 gdk_window_get_size (text->text_area, &width, &height);
5232 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5234 for (; pixels < height; cache = cache->next)
5236 if (CACHE_DATA(cache).start.index < end_pos)
5238 if (CACHE_DATA(cache).end.index >= start_pos)
5241 area.y = MAX(0,pixels);
5242 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5248 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5252 fetch_lines_forward (text, 1);
5260 expose_text (text, &area, TRUE);
5264 recompute_geometry (GtkText* text)
5266 GtkPropertyMark mark, start_mark;
5273 mark = start_mark = set_vertical_scroll (text);
5275 /* We need a real start of a line when calling fetch_lines().
5276 * not the start of a wrapped line.
5278 while (mark.index > 0 &&
5279 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5280 decrement_mark (&mark);
5282 gdk_window_get_size (text->text_area, &width, &height);
5284 /* Fetch an entire line, to make sure that we get all the text
5285 * we backed over above, in addition to enough text to fill up
5286 * the space vertically
5289 new_lines = fetch_lines (text,
5295 mark = CACHE_DATA (g_list_last (new_lines)).end;
5296 if (!LAST_INDEX (text, mark))
5298 advance_mark (&mark);
5300 new_lines = g_list_concat (new_lines,
5305 height + text->first_cut_pixels));
5308 /* Now work forward to the actual first onscreen line */
5310 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5311 new_lines = new_lines->next;
5313 text->line_start_cache = new_lines;
5315 find_cursor (text, TRUE);
5318 /**********************************************************************/
5320 /**********************************************************************/
5323 gtk_text_set_selection (GtkOldEditable *old_editable,
5327 GtkText *text = GTK_TEXT (old_editable);
5329 guint start1, end1, start2, end2;
5332 end = TEXT_LENGTH (text);
5334 start1 = MIN(start,end);
5335 end1 = MAX(start,end);
5336 start2 = MIN(old_editable->selection_start_pos, old_editable->selection_end_pos);
5337 end2 = MAX(old_editable->selection_start_pos, old_editable->selection_end_pos);
5339 if (start2 < start1)
5343 tmp = start1; start1 = start2; start2 = tmp;
5344 tmp = end1; end1 = end2; end2 = tmp;
5347 undraw_cursor (text, FALSE);
5348 old_editable->selection_start_pos = start;
5349 old_editable->selection_end_pos = end;
5350 draw_cursor (text, FALSE);
5352 /* Expose only what changed */
5354 if (start1 < start2)
5355 gtk_text_update_text (old_editable, start1, MIN(end1, start2));
5358 gtk_text_update_text (old_editable, MAX(end1, start2), end2);
5359 else if (end2 < end1)
5360 gtk_text_update_text (old_editable, end2, end1);
5364 /**********************************************************************/
5366 /**********************************************************************/
5368 #ifdef DEBUG_GTK_TEXT
5370 gtk_text_show_cache_line (GtkText *text, GList *cache,
5371 const char* what, const char* func, gint line)
5373 LineParams *lp = &CACHE_DATA(cache);
5376 if (cache == text->line_start_cache)
5377 g_message ("Line Start Cache: ");
5379 if (cache == text->current_line)
5380 g_message("Current Line: ");
5382 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
5390 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
5391 g_message ("%c", GTK_TEXT_INDEX (text, i));
5397 gtk_text_show_cache (GtkText *text, const char* func, gint line)
5399 GList *l = text->line_start_cache;
5405 /* back up to the absolute beginning of the line cache */
5409 g_message ("*** line cache ***\n");
5410 for (; l; l = l->next)
5411 gtk_text_show_cache_line (text, l, "all", func, line);
5415 gtk_text_assert_mark (GtkText *text,
5416 GtkPropertyMark *mark,
5417 GtkPropertyMark *before,
5418 GtkPropertyMark *after,
5423 GtkPropertyMark correct_mark = find_mark (text, mark->index);
5425 if (mark->offset != correct_mark.offset ||
5426 mark->property != correct_mark.property)
5427 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
5431 gtk_text_assert (GtkText *text,
5435 GList* cache = text->line_start_cache;
5436 GtkPropertyMark* before_mark = NULL;
5437 GtkPropertyMark* after_mark = NULL;
5439 gtk_text_show_props (text, msg, line);
5441 for (; cache->prev; cache = cache->prev)
5444 g_message ("*** line markers ***\n");
5446 for (; cache; cache = cache->next)
5448 after_mark = &CACHE_DATA(cache).end;
5449 gtk_text_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
5450 before_mark = &CACHE_DATA(cache).start;
5453 after_mark = &CACHE_DATA(cache->next).start;
5457 gtk_text_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
5458 before_mark = &CACHE_DATA(cache).end;
5463 gtk_text_show_adj (GtkText *text,
5469 g_message ("*** adjustment ***\n");
5471 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
5478 adj->step_increment,
5479 adj->page_increment,
5484 gtk_text_show_props (GtkText *text,
5488 GList* props = text->text_properties;
5491 g_message ("%s:%d: ", msg, line);
5493 for (; props; props = props->next)
5495 TextProperty *p = (TextProperty*)props->data;
5497 proplen += p->length;
5499 g_message ("[%d,%p,", p->length, p);
5500 if (p->flags & PROPERTY_FONT)
5501 g_message ("%p,", p->font);
5504 if (p->flags & PROPERTY_FOREGROUND)
5505 g_message ("%ld, ", p->fore_color.pixel);
5508 if (p->flags & PROPERTY_BACKGROUND)
5509 g_message ("%ld] ", p->back_color.pixel);
5516 if (proplen - 1 != TEXT_LENGTH(text))
5517 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);