1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #undef GDK_DISABLE_DEPRECATED
28 #undef GTK_DISABLE_DEPRECATED
33 #include "gdk/gdkkeysyms.h"
34 #include "gdk/gdki18n.h"
36 #include "gtkmarshalers.h"
37 #include "gtkselection.h"
38 #include "gtksignal.h"
39 #define GTK_ENABLE_BROKEN
41 #include "line-wrap.xbm"
42 #include "line-arrow.xbm"
46 #define INITIAL_BUFFER_SIZE 1024
47 #define INITIAL_LINE_CACHE_SIZE 256
48 #define MIN_GAP_SIZE 256
49 #define LINE_DELIM '\n'
50 #define MIN_TEXT_WIDTH_LINES 20
51 #define MIN_TEXT_HEIGHT_LINES 10
52 #define TEXT_BORDER_ROOM 1
53 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
54 #define DEFAULT_TAB_STOP_WIDTH 4
55 #define SCROLL_PIXELS 5
56 #define KEY_SCROLL_PIXELS 10
57 #define SCROLL_TIME 100
58 #define FREEZE_LENGTH 1024
59 /* Freeze text when inserting or deleting more than this many characters */
61 #define SET_PROPERTY_MARK(m, p, o) do { \
62 (m)->property = (p); \
65 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
66 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
67 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
68 (mark)->property->prev->data \
70 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
71 #define MARK_LIST_PTR(mark) ((mark)->property)
72 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
73 #define MARK_OFFSET(mark) ((mark)->offset)
74 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
77 #define MARK_CURRENT_FONT(text, mark) \
78 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
79 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
80 gtk_style_get_font (GTK_WIDGET (text)->style))
81 #define MARK_CURRENT_FORE(text, mark) \
82 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
83 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
84 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
85 #define MARK_CURRENT_BACK(text, mark) \
86 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
87 &MARK_CURRENT_PROPERTY(mark)->back_color : \
88 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
89 #define MARK_CURRENT_TEXT_FONT(text, mark) \
90 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
91 MARK_CURRENT_PROPERTY(mark)->font : \
94 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
95 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
96 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
97 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
98 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
99 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
100 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
101 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
111 typedef struct _TextProperty TextProperty;
112 typedef struct _TabStopMark TabStopMark;
113 typedef struct _PrevTabCont PrevTabCont;
114 typedef struct _FetchLinesData FetchLinesData;
115 typedef struct _LineParams LineParams;
116 typedef struct _SetVerticalScrollData SetVerticalScrollData;
118 typedef gint (*LineIteratorFunction) (GtkText* text, LineParams* lp, void* data);
126 struct _SetVerticalScrollData {
128 gint last_didnt_wrap;
129 gint last_line_start;
130 GtkPropertyMark mark;
135 /* The actual font. */
139 gint16 char_widths[256];
143 PROPERTY_FONT = 1 << 0,
144 PROPERTY_FOREGROUND = 1 << 1,
145 PROPERTY_BACKGROUND = 1 << 2
153 /* Background Color. */
156 /* Foreground Color. */
159 /* Show which properties are set */
160 TextPropertyFlags flags;
162 /* Length of this property. */
168 GList* tab_stops; /* Index into list containing the next tab position. If
169 * NULL, using default widths. */
176 TabStopMark tab_start;
179 struct _FetchLinesData
192 guint displayable_chars;
195 PrevTabCont tab_cont;
196 PrevTabCont tab_cont_next;
198 GtkPropertyMark start;
203 static void gtk_text_class_init (GtkTextClass *klass);
204 static void gtk_text_set_property (GObject *object,
208 static void gtk_text_get_property (GObject *object,
212 static void gtk_text_editable_init (GtkEditableClass *iface);
213 static void gtk_text_init (GtkText *text);
214 static void gtk_text_destroy (GtkObject *object);
215 static void gtk_text_finalize (GObject *object);
216 static void gtk_text_realize (GtkWidget *widget);
217 static void gtk_text_unrealize (GtkWidget *widget);
218 static void gtk_text_style_set (GtkWidget *widget,
219 GtkStyle *previous_style);
220 static void gtk_text_state_changed (GtkWidget *widget,
221 GtkStateType previous_state);
222 static void gtk_text_draw_focus (GtkWidget *widget);
223 static void gtk_text_size_request (GtkWidget *widget,
224 GtkRequisition *requisition);
225 static void gtk_text_size_allocate (GtkWidget *widget,
226 GtkAllocation *allocation);
227 static void gtk_text_adjustment (GtkAdjustment *adjustment,
229 static void gtk_text_insert_text (GtkEditable *editable,
230 const gchar *new_text,
231 gint new_text_length,
233 static void gtk_text_delete_text (GtkEditable *editable,
236 static void gtk_text_update_text (GtkOldEditable *old_editable,
239 static gchar *gtk_text_get_chars (GtkOldEditable *old_editable,
242 static void gtk_text_set_selection (GtkOldEditable *old_editable,
245 static void gtk_text_real_set_editable (GtkOldEditable *old_editable,
246 gboolean is_editable);
248 static void gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
252 static gint gtk_text_expose (GtkWidget *widget,
253 GdkEventExpose *event);
254 static gint gtk_text_button_press (GtkWidget *widget,
255 GdkEventButton *event);
256 static gint gtk_text_button_release (GtkWidget *widget,
257 GdkEventButton *event);
258 static gint gtk_text_motion_notify (GtkWidget *widget,
259 GdkEventMotion *event);
260 static gint gtk_text_key_press (GtkWidget *widget,
263 static void move_gap (GtkText* text, guint index);
264 static void make_forward_space (GtkText* text, guint len);
266 /* Property management */
267 static GtkTextFont* get_text_font (GdkFont* gfont);
268 static void text_font_unref (GtkTextFont *text_font);
270 static void insert_text_property (GtkText* text, GdkFont* font,
271 const GdkColor *fore, const GdkColor* back, guint len);
272 static TextProperty* new_text_property (GtkText *text, GdkFont* font,
273 const GdkColor* fore, const GdkColor* back, guint length);
274 static void destroy_text_property (TextProperty *prop);
275 static void init_properties (GtkText *text);
276 static void realize_property (GtkText *text, TextProperty *prop);
277 static void realize_properties (GtkText *text);
278 static void unrealize_property (GtkText *text, TextProperty *prop);
279 static void unrealize_properties (GtkText *text);
281 static void delete_text_property (GtkText* text, guint len);
283 static guint pixel_height_of (GtkText* text, GList* cache_line);
285 /* Property Movement and Size Computations */
286 static void advance_mark (GtkPropertyMark* mark);
287 static void decrement_mark (GtkPropertyMark* mark);
288 static void advance_mark_n (GtkPropertyMark* mark, gint n);
289 static void decrement_mark_n (GtkPropertyMark* mark, gint n);
290 static void move_mark_n (GtkPropertyMark* mark, gint n);
291 static GtkPropertyMark find_mark (GtkText* text, guint mark_position);
292 static GtkPropertyMark find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near);
293 static void find_line_containing_point (GtkText* text, guint point,
297 static void compute_lines_pixels (GtkText* text, guint char_count,
298 guint *lines, guint *pixels);
300 static gint total_line_height (GtkText* text,
303 static LineParams find_line_params (GtkText* text,
304 const GtkPropertyMark *mark,
305 const PrevTabCont *tab_cont,
306 PrevTabCont *next_cont);
307 static void recompute_geometry (GtkText* text);
308 static void insert_expose (GtkText* text, guint old_pixels, gint nchars, guint new_line_count);
309 static void delete_expose (GtkText* text,
313 static GdkGC *create_bg_gc (GtkText *text);
314 static void clear_area (GtkText *text, GdkRectangle *area);
315 static void draw_line (GtkText* text,
318 static void draw_line_wrap (GtkText* text,
320 static void draw_cursor (GtkText* text, gint absolute);
321 static void undraw_cursor (GtkText* text, gint absolute);
322 static gint drawn_cursor_min (GtkText* text);
323 static gint drawn_cursor_max (GtkText* text);
324 static void expose_text (GtkText* text, GdkRectangle *area, gboolean cursor);
326 /* Search and Placement. */
327 static void find_cursor (GtkText* text,
329 static void find_cursor_at_line (GtkText* text,
330 const LineParams* start_line,
332 static void find_mouse_cursor (GtkText* text, gint x, gint y);
335 static void adjust_adj (GtkText* text, GtkAdjustment* adj);
336 static void scroll_up (GtkText* text, gint diff);
337 static void scroll_down (GtkText* text, gint diff);
338 static void scroll_int (GtkText* text, gint diff);
340 static void process_exposes (GtkText *text);
342 /* Cache Management. */
343 static void free_cache (GtkText* text);
344 static GList* remove_cache_line (GtkText* text, GList* list);
347 static void move_cursor_buffer_ver (GtkText *text, int dir);
348 static void move_cursor_page_ver (GtkText *text, int dir);
349 static void move_cursor_ver (GtkText *text, int count);
350 static void move_cursor_hor (GtkText *text, int count);
352 /* Binding actions */
353 static void gtk_text_move_cursor (GtkOldEditable *old_editable,
356 static void gtk_text_move_word (GtkOldEditable *old_editable,
358 static void gtk_text_move_page (GtkOldEditable *old_editable,
361 static void gtk_text_move_to_row (GtkOldEditable *old_editable,
363 static void gtk_text_move_to_column (GtkOldEditable *old_editable,
365 static void gtk_text_kill_char (GtkOldEditable *old_editable,
367 static void gtk_text_kill_word (GtkOldEditable *old_editable,
369 static void gtk_text_kill_line (GtkOldEditable *old_editable,
373 static void gtk_text_move_forward_character (GtkText *text);
374 static void gtk_text_move_backward_character (GtkText *text);
375 static void gtk_text_move_forward_word (GtkText *text);
376 static void gtk_text_move_backward_word (GtkText *text);
377 static void gtk_text_move_beginning_of_line (GtkText *text);
378 static void gtk_text_move_end_of_line (GtkText *text);
379 static void gtk_text_move_next_line (GtkText *text);
380 static void gtk_text_move_previous_line (GtkText *text);
382 static void gtk_text_delete_forward_character (GtkText *text);
383 static void gtk_text_delete_backward_character (GtkText *text);
384 static void gtk_text_delete_forward_word (GtkText *text);
385 static void gtk_text_delete_backward_word (GtkText *text);
386 static void gtk_text_delete_line (GtkText *text);
387 static void gtk_text_delete_to_line_end (GtkText *text);
388 static void gtk_text_select_word (GtkText *text,
390 static void gtk_text_select_line (GtkText *text,
393 static void gtk_text_set_position (GtkOldEditable *old_editable,
396 /* #define DEBUG_GTK_TEXT */
398 #if defined(DEBUG_GTK_TEXT) && defined(__GNUC__)
399 /* Debugging utilities. */
400 static void gtk_text_assert_mark (GtkText *text,
401 GtkPropertyMark *mark,
402 GtkPropertyMark *before,
403 GtkPropertyMark *after,
408 static void gtk_text_assert (GtkText *text,
411 static void gtk_text_show_cache_line (GtkText *text, GList *cache,
412 const char* what, const char* func, gint line);
413 static void gtk_text_show_cache (GtkText *text, const char* func, gint line);
414 static void gtk_text_show_adj (GtkText *text,
419 static void gtk_text_show_props (GtkText* test,
423 #define TDEBUG(args) g_message args
424 #define TEXT_ASSERT(text) gtk_text_assert (text,__PRETTY_FUNCTION__,__LINE__)
425 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_text_assert_mark (text,mark, \
426 __PRETTY_FUNCTION__,msg,__LINE__)
427 #define TEXT_SHOW(text) gtk_text_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
428 #define TEXT_SHOW_LINE(text,line,msg) gtk_text_show_cache_line (text,line,msg,\
429 __PRETTY_FUNCTION__,__LINE__)
430 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_text_show_adj (text,adj,msg, \
431 __PRETTY_FUNCTION__,__LINE__)
434 #define TEXT_ASSERT(text)
435 #define TEXT_ASSERT_MARK(text,mark,msg)
436 #define TEXT_SHOW(text)
437 #define TEXT_SHOW_LINE(text,line,msg)
438 #define TEXT_SHOW_ADJ(text,adj,msg)
441 /* Memory Management. */
442 static GMemChunk *params_mem_chunk = NULL;
443 static GMemChunk *text_property_chunk = NULL;
445 static GtkWidgetClass *parent_class = NULL;
448 static const GtkTextFunction control_keys[26] =
450 (GtkTextFunction)gtk_text_move_beginning_of_line, /* a */
451 (GtkTextFunction)gtk_text_move_backward_character, /* b */
452 (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
453 (GtkTextFunction)gtk_text_delete_forward_character, /* d */
454 (GtkTextFunction)gtk_text_move_end_of_line, /* e */
455 (GtkTextFunction)gtk_text_move_forward_character, /* f */
457 (GtkTextFunction)gtk_text_delete_backward_character, /* h */
460 (GtkTextFunction)gtk_text_delete_to_line_end, /* k */
463 (GtkTextFunction)gtk_text_move_next_line, /* n */
465 (GtkTextFunction)gtk_text_move_previous_line, /* p */
470 (GtkTextFunction)gtk_text_delete_line, /* u */
471 (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
472 (GtkTextFunction)gtk_text_delete_backward_word, /* w */
473 (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
478 static const GtkTextFunction alt_keys[26] =
481 (GtkTextFunction)gtk_text_move_backward_word, /* b */
483 (GtkTextFunction)gtk_text_delete_forward_word, /* d */
485 (GtkTextFunction)gtk_text_move_forward_word, /* f */
509 /**********************************************************************/
511 /**********************************************************************/
514 gtk_text_get_type (void)
516 static GtkType text_type = 0;
520 static const GtkTypeInfo text_info =
524 sizeof (GtkTextClass),
525 (GtkClassInitFunc) gtk_text_class_init,
526 (GtkObjectInitFunc) gtk_text_init,
527 /* reserved_1 */ NULL,
528 /* reserved_2 */ NULL,
529 (GtkClassInitFunc) NULL,
532 static const GInterfaceInfo editable_info =
534 (GInterfaceInitFunc) gtk_text_editable_init, /* interface_init */
535 NULL, /* interface_finalize */
536 NULL /* interface_data */
539 text_type = gtk_type_unique (GTK_TYPE_OLD_EDITABLE, &text_info);
540 g_type_add_interface_static (text_type,
549 gtk_text_class_init (GtkTextClass *class)
551 GObjectClass *gobject_class;
552 GtkObjectClass *object_class;
553 GtkWidgetClass *widget_class;
554 GtkOldEditableClass *old_editable_class;
556 gobject_class = G_OBJECT_CLASS (class);
557 object_class = (GtkObjectClass*) class;
558 widget_class = (GtkWidgetClass*) class;
559 old_editable_class = (GtkOldEditableClass*) class;
560 parent_class = gtk_type_class (GTK_TYPE_OLD_EDITABLE);
562 gobject_class->finalize = gtk_text_finalize;
563 gobject_class->set_property = gtk_text_set_property;
564 gobject_class->get_property = gtk_text_get_property;
566 object_class->destroy = gtk_text_destroy;
568 widget_class->realize = gtk_text_realize;
569 widget_class->unrealize = gtk_text_unrealize;
570 widget_class->style_set = gtk_text_style_set;
571 widget_class->state_changed = gtk_text_state_changed;
572 widget_class->size_request = gtk_text_size_request;
573 widget_class->size_allocate = gtk_text_size_allocate;
574 widget_class->expose_event = gtk_text_expose;
575 widget_class->button_press_event = gtk_text_button_press;
576 widget_class->button_release_event = gtk_text_button_release;
577 widget_class->motion_notify_event = gtk_text_motion_notify;
578 widget_class->key_press_event = gtk_text_key_press;
580 old_editable_class->set_editable = gtk_text_real_set_editable;
582 old_editable_class->move_cursor = gtk_text_move_cursor;
583 old_editable_class->move_word = gtk_text_move_word;
584 old_editable_class->move_page = gtk_text_move_page;
585 old_editable_class->move_to_row = gtk_text_move_to_row;
586 old_editable_class->move_to_column = gtk_text_move_to_column;
588 old_editable_class->kill_char = gtk_text_kill_char;
589 old_editable_class->kill_word = gtk_text_kill_word;
590 old_editable_class->kill_line = gtk_text_kill_line;
592 old_editable_class->update_text = gtk_text_update_text;
593 old_editable_class->get_chars = gtk_text_get_chars;
594 old_editable_class->set_selection = gtk_text_set_selection;
595 old_editable_class->set_position = gtk_text_set_position;
597 class->set_scroll_adjustments = gtk_text_set_adjustments;
599 g_object_class_install_property (gobject_class,
601 g_param_spec_object ("hadjustment",
602 P_("Horizontal Adjustment"),
603 P_("Horizontal adjustment for the text widget"),
607 g_object_class_install_property (gobject_class,
609 g_param_spec_object ("vadjustment",
610 P_("Vertical Adjustment"),
611 P_("Vertical adjustment for the text widget"),
615 g_object_class_install_property (gobject_class,
617 g_param_spec_boolean ("line_wrap",
619 P_("Whether lines are wrapped at widget edges"),
623 g_object_class_install_property (gobject_class,
625 g_param_spec_boolean ("word_wrap",
627 P_("Whether words are wrapped at widget edges"),
631 widget_class->set_scroll_adjustments_signal =
632 gtk_signal_new ("set_scroll_adjustments",
634 GTK_CLASS_TYPE (object_class),
635 GTK_SIGNAL_OFFSET (GtkTextClass, set_scroll_adjustments),
636 _gtk_marshal_VOID__OBJECT_OBJECT,
637 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
641 gtk_text_set_property (GObject *object,
648 text = GTK_TEXT (object);
652 case PROP_HADJUSTMENT:
653 gtk_text_set_adjustments (text,
654 g_value_get_object (value),
657 case PROP_VADJUSTMENT:
658 gtk_text_set_adjustments (text,
660 g_value_get_object (value));
663 gtk_text_set_line_wrap (text, g_value_get_boolean (value));
666 gtk_text_set_word_wrap (text, g_value_get_boolean (value));
669 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
675 gtk_text_get_property (GObject *object,
682 text = GTK_TEXT (object);
686 case PROP_HADJUSTMENT:
687 g_value_set_object (value, text->hadj);
689 case PROP_VADJUSTMENT:
690 g_value_set_object (value, text->vadj);
693 g_value_set_boolean (value, text->line_wrap);
696 g_value_set_boolean (value, text->word_wrap);
699 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
705 gtk_text_editable_init (GtkEditableClass *iface)
707 iface->insert_text = gtk_text_insert_text;
708 iface->delete_text = gtk_text_delete_text;
712 gtk_text_init (GtkText *text)
714 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
716 text->text_area = NULL;
721 text->line_wrap_bitmap = NULL;
722 text->line_arrow_bitmap = NULL;
724 text->use_wchar = FALSE;
725 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
726 text->text_len = INITIAL_BUFFER_SIZE;
728 text->scratch_buffer.ch = NULL;
729 text->scratch_buffer_len = 0;
731 text->freeze_count = 0;
733 if (!params_mem_chunk)
734 params_mem_chunk = g_mem_chunk_new ("LineParams",
736 256 * sizeof (LineParams),
739 text->default_tab_width = 4;
740 text->tab_stops = NULL;
742 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
743 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
745 text->line_start_cache = NULL;
746 text->first_cut_pixels = 0;
748 text->line_wrap = TRUE;
749 text->word_wrap = FALSE;
754 text->current_font = NULL;
756 init_properties (text);
758 GTK_OLD_EDITABLE (text)->editable = FALSE;
760 gtk_text_set_adjustments (text, NULL, NULL);
761 gtk_editable_set_position (GTK_EDITABLE (text), 0);
765 gtk_text_new (GtkAdjustment *hadj,
771 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
773 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
775 text = gtk_widget_new (GTK_TYPE_TEXT,
784 gtk_text_set_word_wrap (GtkText *text,
787 g_return_if_fail (GTK_IS_TEXT (text));
789 text->word_wrap = (word_wrap != FALSE);
791 if (GTK_WIDGET_REALIZED (text))
793 recompute_geometry (text);
794 gtk_widget_queue_draw (GTK_WIDGET (text));
797 g_object_notify (G_OBJECT (text), "word_wrap");
801 gtk_text_set_line_wrap (GtkText *text,
804 g_return_if_fail (GTK_IS_TEXT (text));
806 text->line_wrap = (line_wrap != FALSE);
808 if (GTK_WIDGET_REALIZED (text))
810 recompute_geometry (text);
811 gtk_widget_queue_draw (GTK_WIDGET (text));
814 g_object_notify (G_OBJECT (text), "line_wrap");
818 gtk_text_set_editable (GtkText *text,
819 gboolean is_editable)
821 g_return_if_fail (GTK_IS_TEXT (text));
823 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
827 gtk_text_real_set_editable (GtkOldEditable *old_editable,
828 gboolean is_editable)
832 g_return_if_fail (GTK_IS_TEXT (old_editable));
834 text = GTK_TEXT (old_editable);
836 old_editable->editable = (is_editable != FALSE);
839 draw_cursor (text, TRUE);
841 undraw_cursor (text, TRUE);
845 gtk_text_set_adjustments (GtkText *text,
849 g_return_if_fail (GTK_IS_TEXT (text));
851 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
853 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
855 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
857 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
859 if (text->hadj && (text->hadj != hadj))
861 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
862 gtk_object_unref (GTK_OBJECT (text->hadj));
865 if (text->vadj && (text->vadj != vadj))
867 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
868 gtk_object_unref (GTK_OBJECT (text->vadj));
871 g_object_freeze_notify (G_OBJECT (text));
872 if (text->hadj != hadj)
875 gtk_object_ref (GTK_OBJECT (text->hadj));
876 gtk_object_sink (GTK_OBJECT (text->hadj));
878 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
879 (GtkSignalFunc) gtk_text_adjustment,
881 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
882 (GtkSignalFunc) gtk_text_adjustment,
884 gtk_signal_connect (GTK_OBJECT (text->hadj), "destroy",
885 (GtkSignalFunc) gtk_text_adjustment_destroyed,
887 gtk_text_adjustment (hadj, text);
889 g_object_notify (G_OBJECT (text), "hadjustment");
892 if (text->vadj != vadj)
895 gtk_object_ref (GTK_OBJECT (text->vadj));
896 gtk_object_sink (GTK_OBJECT (text->vadj));
898 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
899 (GtkSignalFunc) gtk_text_adjustment,
901 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
902 (GtkSignalFunc) gtk_text_adjustment,
904 gtk_signal_connect (GTK_OBJECT (text->vadj), "destroy",
905 (GtkSignalFunc) gtk_text_adjustment_destroyed,
907 gtk_text_adjustment (vadj, text);
909 g_object_notify (G_OBJECT (text), "vadjustment");
911 g_object_thaw_notify (G_OBJECT (text));
915 gtk_text_set_point (GtkText *text,
918 g_return_if_fail (GTK_IS_TEXT (text));
919 g_return_if_fail (index <= TEXT_LENGTH (text));
921 text->point = find_mark (text, index);
925 gtk_text_get_point (GtkText *text)
927 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
929 return text->point.index;
933 gtk_text_get_length (GtkText *text)
935 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
937 return TEXT_LENGTH (text);
941 gtk_text_freeze (GtkText *text)
943 g_return_if_fail (GTK_IS_TEXT (text));
945 text->freeze_count++;
949 gtk_text_thaw (GtkText *text)
951 g_return_if_fail (GTK_IS_TEXT (text));
953 if (text->freeze_count)
954 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
956 recompute_geometry (text);
957 gtk_widget_queue_draw (GTK_WIDGET (text));
962 gtk_text_insert (GtkText *text,
964 const GdkColor *fore,
965 const GdkColor *back,
969 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
970 gboolean frozen = FALSE;
972 gint new_line_count = 1;
973 guint old_height = 0;
978 g_return_if_fail (GTK_IS_TEXT (text));
981 length = strlen (chars);
988 if (!text->freeze_count && (length > FREEZE_LENGTH))
990 gtk_text_freeze (text);
994 if (!text->freeze_count && (text->line_start_cache != NULL))
996 find_line_containing_point (text, text->point.index, TRUE);
997 old_height = total_line_height (text, text->current_line, 1);
1000 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
1002 GtkWidget *widget = GTK_WIDGET (text);
1004 gtk_widget_ensure_style (widget);
1005 if ((widget->style) &&
1006 (gtk_style_get_font (widget->style)->type == GDK_FONT_FONTSET))
1008 text->use_wchar = TRUE;
1009 g_free (text->text.ch);
1010 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
1011 text->text_len = INITIAL_BUFFER_SIZE;
1012 if (text->scratch_buffer.ch)
1013 g_free (text->scratch_buffer.ch);
1014 text->scratch_buffer.wc = NULL;
1015 text->scratch_buffer_len = 0;
1019 move_gap (text, text->point.index);
1020 make_forward_space (text, length);
1022 if (text->use_wchar)
1024 char *chars_nt = (char *)chars;
1027 chars_nt = g_new (char, length+1);
1028 memcpy (chars_nt, chars, length);
1029 chars_nt[length] = 0;
1031 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
1033 if (chars_nt != chars)
1041 memcpy(text->text.ch + text->gap_position, chars, length);
1044 if (!text->freeze_count && (text->line_start_cache != NULL))
1046 if (text->use_wchar)
1048 for (i=0; i<numwcs; i++)
1049 if (text->text.wc[text->gap_position + i] == '\n')
1054 for (i=0; i<numwcs; i++)
1055 if (text->text.ch[text->gap_position + i] == '\n')
1062 insert_text_property (text, font, fore, back, numwcs);
1064 text->gap_size -= numwcs;
1065 text->gap_position += numwcs;
1067 if (text->point.index < text->first_line_start_index)
1068 text->first_line_start_index += numwcs;
1069 if (text->point.index < old_editable->selection_start_pos)
1070 old_editable->selection_start_pos += numwcs;
1071 if (text->point.index < old_editable->selection_end_pos)
1072 old_editable->selection_end_pos += numwcs;
1073 /* We'll reset the cursor later anyways if we aren't frozen */
1074 if (text->point.index < text->cursor_mark.index)
1075 text->cursor_mark.index += numwcs;
1077 advance_mark_n (&text->point, numwcs);
1079 if (!text->freeze_count && (text->line_start_cache != NULL))
1080 insert_expose (text, old_height, numwcs, new_line_count);
1084 gtk_text_thaw (text);
1088 gtk_text_backward_delete (GtkText *text,
1091 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1093 if (nchars > text->point.index || nchars <= 0)
1096 gtk_text_set_point (text, text->point.index - nchars);
1098 return gtk_text_forward_delete (text, nchars);
1102 gtk_text_forward_delete (GtkText *text,
1105 guint old_lines, old_height;
1106 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
1107 gboolean frozen = FALSE;
1109 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1111 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1114 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1116 gtk_text_freeze (text);
1120 if (!text->freeze_count && text->line_start_cache != NULL)
1122 /* We need to undraw the cursor here, since we may later
1123 * delete the cursor's property
1125 undraw_cursor (text, FALSE);
1126 find_line_containing_point (text, text->point.index, TRUE);
1127 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1130 /* FIXME, or resizing after deleting will be odd */
1131 if (text->point.index < text->first_line_start_index)
1133 if (text->point.index + nchars >= text->first_line_start_index)
1135 text->first_line_start_index = text->point.index;
1136 while ((text->first_line_start_index > 0) &&
1137 (GTK_TEXT_INDEX (text, text->first_line_start_index - 1)
1139 text->first_line_start_index -= 1;
1143 text->first_line_start_index -= nchars;
1146 if (text->point.index < old_editable->selection_start_pos)
1147 old_editable->selection_start_pos -=
1148 MIN(nchars, old_editable->selection_start_pos - text->point.index);
1149 if (text->point.index < old_editable->selection_end_pos)
1150 old_editable->selection_end_pos -=
1151 MIN(nchars, old_editable->selection_end_pos - text->point.index);
1152 /* We'll reset the cursor later anyways if we aren't frozen */
1153 if (text->point.index < text->cursor_mark.index)
1154 move_mark_n (&text->cursor_mark,
1155 -MIN(nchars, text->cursor_mark.index - text->point.index));
1157 move_gap (text, text->point.index);
1159 text->gap_size += nchars;
1161 delete_text_property (text, nchars);
1163 if (!text->freeze_count && (text->line_start_cache != NULL))
1165 delete_expose (text, nchars, old_lines, old_height);
1166 draw_cursor (text, FALSE);
1170 gtk_text_thaw (text);
1176 gtk_text_set_position (GtkOldEditable *old_editable,
1179 GtkText *text = (GtkText *) old_editable;
1182 position = gtk_text_get_length (text);
1184 undraw_cursor (text, FALSE);
1185 text->cursor_mark = find_mark (text, position);
1186 find_cursor (text, TRUE);
1187 draw_cursor (text, FALSE);
1188 gtk_editable_select_region (GTK_EDITABLE (old_editable), 0, 0);
1192 gtk_text_get_chars (GtkOldEditable *old_editable,
1200 g_return_val_if_fail (GTK_IS_TEXT (old_editable), NULL);
1201 text = GTK_TEXT (old_editable);
1204 end_pos = TEXT_LENGTH (text);
1206 if ((start_pos < 0) ||
1207 (end_pos > TEXT_LENGTH (text)) ||
1208 (end_pos < start_pos))
1211 move_gap (text, TEXT_LENGTH (text));
1212 make_forward_space (text, 1);
1214 if (text->use_wchar)
1217 ch = text->text.wc[end_pos];
1218 text->text.wc[end_pos] = 0;
1219 retval = gdk_wcstombs (text->text.wc + start_pos);
1220 text->text.wc[end_pos] = ch;
1225 ch = text->text.ch[end_pos];
1226 text->text.ch[end_pos] = 0;
1227 retval = g_strdup (text->text.ch + start_pos);
1228 text->text.ch[end_pos] = ch;
1236 gtk_text_destroy (GtkObject *object)
1240 g_return_if_fail (GTK_IS_TEXT (object));
1242 text = GTK_TEXT (object);
1246 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1247 gtk_object_unref (GTK_OBJECT (text->hadj));
1252 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1253 gtk_object_unref (GTK_OBJECT (text->vadj));
1259 g_source_remove (text->timer);
1263 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1267 gtk_text_finalize (GObject *object)
1272 g_return_if_fail (GTK_IS_TEXT (object));
1274 text = GTK_TEXT (object);
1276 /* Clean up the internal structures */
1277 if (text->use_wchar)
1278 g_free (text->text.wc);
1280 g_free (text->text.ch);
1282 tmp_list = text->text_properties;
1285 destroy_text_property (tmp_list->data);
1286 tmp_list = tmp_list->next;
1289 if (text->current_font)
1290 text_font_unref (text->current_font);
1292 g_list_free (text->text_properties);
1294 if (text->use_wchar)
1296 if (text->scratch_buffer.wc)
1297 g_free (text->scratch_buffer.wc);
1301 if (text->scratch_buffer.ch)
1302 g_free (text->scratch_buffer.ch);
1305 g_list_free (text->tab_stops);
1307 G_OBJECT_CLASS (parent_class)->finalize (object);
1311 gtk_text_realize (GtkWidget *widget)
1314 GtkOldEditable *old_editable;
1315 GdkWindowAttr attributes;
1316 gint attributes_mask;
1318 g_return_if_fail (GTK_IS_TEXT (widget));
1320 text = GTK_TEXT (widget);
1321 old_editable = GTK_OLD_EDITABLE (widget);
1322 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1324 attributes.window_type = GDK_WINDOW_CHILD;
1325 attributes.x = widget->allocation.x;
1326 attributes.y = widget->allocation.y;
1327 attributes.width = widget->allocation.width;
1328 attributes.height = widget->allocation.height;
1329 attributes.wclass = GDK_INPUT_OUTPUT;
1330 attributes.visual = gtk_widget_get_visual (widget);
1331 attributes.colormap = gtk_widget_get_colormap (widget);
1332 attributes.event_mask = gtk_widget_get_events (widget);
1333 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1334 GDK_BUTTON_PRESS_MASK |
1335 GDK_BUTTON_RELEASE_MASK |
1336 GDK_BUTTON_MOTION_MASK |
1337 GDK_ENTER_NOTIFY_MASK |
1338 GDK_LEAVE_NOTIFY_MASK |
1339 GDK_KEY_PRESS_MASK);
1340 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1342 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1343 gdk_window_set_user_data (widget->window, text);
1345 attributes.x = (widget->style->xthickness + TEXT_BORDER_ROOM);
1346 attributes.y = (widget->style->ythickness + TEXT_BORDER_ROOM);
1347 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1348 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1350 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1351 attributes_mask |= GDK_WA_CURSOR;
1353 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1354 gdk_window_set_user_data (text->text_area, text);
1356 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1358 widget->style = gtk_style_attach (widget->style, widget->window);
1360 /* Can't call gtk_style_set_background here because it's handled specially */
1361 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1362 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1364 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1365 text->bg_gc = create_bg_gc (text);
1367 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1368 (gchar*) line_wrap_bits,
1372 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1373 (gchar*) line_arrow_bits,
1377 text->gc = gdk_gc_new (text->text_area);
1378 gdk_gc_set_exposures (text->gc, TRUE);
1379 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1381 realize_properties (text);
1382 gdk_window_show (text->text_area);
1383 init_properties (text);
1385 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1386 gtk_old_editable_claim_selection (old_editable, TRUE, GDK_CURRENT_TIME);
1388 recompute_geometry (text);
1392 gtk_text_style_set (GtkWidget *widget,
1393 GtkStyle *previous_style)
1395 GtkText *text = GTK_TEXT (widget);
1397 if (GTK_WIDGET_REALIZED (widget))
1399 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1400 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1404 gdk_gc_destroy (text->bg_gc);
1408 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1409 text->bg_gc = create_bg_gc (text);
1411 recompute_geometry (text);
1414 if (text->current_font)
1415 text_font_unref (text->current_font);
1416 text->current_font = get_text_font (gtk_style_get_font (widget->style));
1420 gtk_text_state_changed (GtkWidget *widget,
1421 GtkStateType previous_state)
1423 GtkText *text = GTK_TEXT (widget);
1425 if (GTK_WIDGET_REALIZED (widget))
1427 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1428 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1433 gtk_text_unrealize (GtkWidget *widget)
1437 g_return_if_fail (GTK_IS_TEXT (widget));
1439 text = GTK_TEXT (widget);
1441 gdk_window_set_user_data (text->text_area, NULL);
1442 gdk_window_destroy (text->text_area);
1443 text->text_area = NULL;
1445 gdk_gc_destroy (text->gc);
1450 gdk_gc_destroy (text->bg_gc);
1454 gdk_pixmap_unref (text->line_wrap_bitmap);
1455 gdk_pixmap_unref (text->line_arrow_bitmap);
1457 unrealize_properties (text);
1461 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1462 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1466 clear_focus_area (GtkText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1468 GtkWidget *widget = GTK_WIDGET (text);
1471 gint ythick = TEXT_BORDER_ROOM + widget->style->ythickness;
1472 gint xthick = TEXT_BORDER_ROOM + widget->style->xthickness;
1476 if (area_width == 0 || area_height == 0)
1479 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1481 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1483 gdk_gc_set_ts_origin (text->bg_gc,
1484 (- text->first_onscreen_hor_pixel + xthick) % width,
1485 (- text->first_onscreen_ver_pixel + ythick) % height);
1490 gc = widget->style->base_gc[widget->state];
1492 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1493 area_x, area_y, area_width, area_height);
1497 gtk_text_draw_focus (GtkWidget *widget)
1503 g_return_if_fail (GTK_IS_TEXT (widget));
1505 text = GTK_TEXT (widget);
1507 if (GTK_WIDGET_DRAWABLE (widget))
1509 gint ythick = widget->style->ythickness;
1510 gint xthick = widget->style->xthickness;
1511 gint xextra = TEXT_BORDER_ROOM;
1512 gint yextra = TEXT_BORDER_ROOM;
1514 TDEBUG (("in gtk_text_draw_focus\n"));
1518 width = widget->allocation.width;
1519 height = widget->allocation.height;
1521 if (GTK_WIDGET_HAS_FOCUS (widget))
1530 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1531 NULL, widget, "text",
1533 widget->allocation.width,
1534 widget->allocation.height);
1537 gtk_paint_shadow (widget->style, widget->window,
1538 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1539 NULL, widget, "text",
1540 x, y, width, height);
1544 width -= 2 * xthick;
1545 height -= 2 * ythick;
1548 clear_focus_area (text, x, y, width, yextra);
1550 clear_focus_area (text, x, y + yextra,
1551 xextra, y + height - 2 * yextra);
1553 clear_focus_area (text, x + width - xextra, y + yextra,
1554 xextra, height - 2 * ythick);
1556 clear_focus_area (text, x, x + height - yextra, width, yextra);
1560 TDEBUG (("in gtk_text_draw_focus (undrawable !!!)\n"));
1565 gtk_text_size_request (GtkWidget *widget,
1566 GtkRequisition *requisition)
1574 g_return_if_fail (GTK_IS_TEXT (widget));
1575 g_return_if_fail (requisition != NULL);
1577 xthickness = widget->style->xthickness + TEXT_BORDER_ROOM;
1578 ythickness = widget->style->ythickness + TEXT_BORDER_ROOM;
1580 font = gtk_style_get_font (widget->style);
1582 char_height = MIN_TEXT_HEIGHT_LINES * (font->ascent +
1585 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (font,
1586 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1590 requisition->width = char_width + xthickness * 2;
1591 requisition->height = char_height + ythickness * 2;
1595 gtk_text_size_allocate (GtkWidget *widget,
1596 GtkAllocation *allocation)
1599 GtkOldEditable *old_editable;
1601 g_return_if_fail (GTK_IS_TEXT (widget));
1602 g_return_if_fail (allocation != NULL);
1604 text = GTK_TEXT (widget);
1605 old_editable = GTK_OLD_EDITABLE (widget);
1607 widget->allocation = *allocation;
1608 if (GTK_WIDGET_REALIZED (widget))
1610 gdk_window_move_resize (widget->window,
1611 allocation->x, allocation->y,
1612 allocation->width, allocation->height);
1614 gdk_window_move_resize (text->text_area,
1615 widget->style->xthickness + TEXT_BORDER_ROOM,
1616 widget->style->ythickness + TEXT_BORDER_ROOM,
1617 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->xthickness +
1618 (gint)TEXT_BORDER_ROOM) * 2),
1619 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->ythickness +
1620 (gint)TEXT_BORDER_ROOM) * 2));
1622 recompute_geometry (text);
1627 gtk_text_expose (GtkWidget *widget,
1628 GdkEventExpose *event)
1630 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1631 g_return_val_if_fail (event != NULL, FALSE);
1633 if (event->window == GTK_TEXT (widget)->text_area)
1635 TDEBUG (("in gtk_text_expose (expose)\n"));
1636 expose_text (GTK_TEXT (widget), &event->area, TRUE);
1638 else if (event->count == 0)
1640 TDEBUG (("in gtk_text_expose (focus)\n"));
1641 gtk_text_draw_focus (widget);
1648 gtk_text_scroll_timeout (gpointer data)
1652 GdkModifierType mask;
1654 GDK_THREADS_ENTER ();
1656 text = GTK_TEXT (data);
1659 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1661 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1663 GdkEvent *event = gdk_event_new (GDK_MOTION_NOTIFY);
1665 event->motion.is_hint = 0;
1666 event->motion.x = x;
1667 event->motion.y = y;
1668 event->motion.state = mask;
1670 gtk_text_motion_notify (GTK_WIDGET (text), (GdkEventMotion *)event);
1672 gdk_event_free (event);
1675 GDK_THREADS_LEAVE ();
1681 gtk_text_button_press (GtkWidget *widget,
1682 GdkEventButton *event)
1685 GtkOldEditable *old_editable;
1687 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1688 g_return_val_if_fail (event != NULL, FALSE);
1690 text = GTK_TEXT (widget);
1691 old_editable = GTK_OLD_EDITABLE (widget);
1693 if (text->button && (event->button != text->button))
1696 text->button = event->button;
1698 if (!GTK_WIDGET_HAS_FOCUS (widget))
1699 gtk_widget_grab_focus (widget);
1701 if (event->button == 1)
1703 switch (event->type)
1705 case GDK_BUTTON_PRESS:
1706 gtk_grab_add (widget);
1708 undraw_cursor (text, FALSE);
1709 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1710 draw_cursor (text, FALSE);
1712 /* Set it now, so we display things right. We'll unset it
1713 * later if things don't work out */
1714 old_editable->has_selection = TRUE;
1715 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1716 text->cursor_mark.index,
1717 text->cursor_mark.index);
1721 case GDK_2BUTTON_PRESS:
1722 gtk_text_select_word (text, event->time);
1725 case GDK_3BUTTON_PRESS:
1726 gtk_text_select_line (text, event->time);
1733 else if (event->type == GDK_BUTTON_PRESS)
1735 if ((event->button == 2) && old_editable->editable)
1737 if (old_editable->selection_start_pos == old_editable->selection_end_pos ||
1738 old_editable->has_selection)
1740 undraw_cursor (text, FALSE);
1741 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1742 draw_cursor (text, FALSE);
1746 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1747 gdk_atom_intern ("UTF8_STRING", FALSE),
1752 GdkDisplay *display = gtk_widget_get_display (widget);
1754 gtk_grab_add (widget);
1756 undraw_cursor (text, FALSE);
1757 find_mouse_cursor (text, event->x, event->y);
1758 draw_cursor (text, FALSE);
1760 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1761 text->cursor_mark.index,
1762 text->cursor_mark.index);
1764 old_editable->has_selection = FALSE;
1765 if (gdk_selection_owner_get_for_display (display,
1766 GDK_SELECTION_PRIMARY) == widget->window)
1767 gtk_selection_owner_set_for_display (display,
1769 GDK_SELECTION_PRIMARY,
1778 gtk_text_button_release (GtkWidget *widget,
1779 GdkEventButton *event)
1782 GtkOldEditable *old_editable;
1783 GdkDisplay *display;
1785 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1786 g_return_val_if_fail (event != NULL, FALSE);
1788 text = GTK_TEXT (widget);
1790 gtk_grab_remove (widget);
1792 if (text->button != event->button)
1799 g_source_remove (text->timer);
1803 if (event->button == 1)
1805 text = GTK_TEXT (widget);
1806 old_editable = GTK_OLD_EDITABLE (widget);
1807 display = gtk_widget_get_display (widget);
1809 gtk_grab_remove (widget);
1811 old_editable->has_selection = FALSE;
1812 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1814 if (gtk_selection_owner_set_for_display (display,
1816 GDK_SELECTION_PRIMARY,
1818 old_editable->has_selection = TRUE;
1820 gtk_text_update_text (old_editable, old_editable->selection_start_pos,
1821 old_editable->selection_end_pos);
1825 if (gdk_selection_owner_get_for_display (display,
1826 GDK_SELECTION_PRIMARY) == widget->window)
1827 gtk_selection_owner_set_for_display (display,
1829 GDK_SELECTION_PRIMARY,
1833 else if (event->button == 3)
1835 gtk_grab_remove (widget);
1838 undraw_cursor (text, FALSE);
1839 find_cursor (text, TRUE);
1840 draw_cursor (text, FALSE);
1846 gtk_text_motion_notify (GtkWidget *widget,
1847 GdkEventMotion *event)
1852 GdkModifierType mask;
1854 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1855 g_return_val_if_fail (event != NULL, FALSE);
1857 text = GTK_TEXT (widget);
1861 mask = event->state;
1862 if (event->is_hint || (text->text_area != event->window))
1864 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1867 if ((text->button == 0) ||
1868 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1871 gdk_window_get_size (text->text_area, NULL, &height);
1873 if ((y < 0) || (y > height))
1875 if (text->timer == 0)
1877 text->timer = g_timeout_add (SCROLL_TIME,
1878 gtk_text_scroll_timeout,
1882 scroll_int (text, y/2);
1884 scroll_int (text, (y - height)/2);
1890 undraw_cursor (GTK_TEXT (widget), FALSE);
1891 find_mouse_cursor (GTK_TEXT (widget), x, y);
1892 draw_cursor (GTK_TEXT (widget), FALSE);
1894 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1895 GTK_OLD_EDITABLE (text)->selection_start_pos,
1896 text->cursor_mark.index);
1902 gtk_text_insert_text (GtkEditable *editable,
1903 const gchar *new_text,
1904 gint new_text_length,
1907 GtkText *text = GTK_TEXT (editable);
1909 GdkColor *fore, *back;
1911 TextProperty *property;
1913 gtk_text_set_point (text, *position);
1915 property = MARK_CURRENT_PROPERTY (&text->point);
1916 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
1917 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
1918 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
1920 gtk_text_insert (text, font, fore, back, new_text, new_text_length);
1922 *position = text->point.index;
1926 gtk_text_delete_text (GtkEditable *editable,
1932 g_return_if_fail (start_pos >= 0);
1934 text = GTK_TEXT (editable);
1936 gtk_text_set_point (text, start_pos);
1938 end_pos = TEXT_LENGTH (text);
1940 if (end_pos > start_pos)
1941 gtk_text_forward_delete (text, end_pos - start_pos);
1945 gtk_text_key_press (GtkWidget *widget,
1949 GtkOldEditable *old_editable;
1954 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1955 g_return_val_if_fail (event != NULL, FALSE);
1957 text = GTK_TEXT (widget);
1958 old_editable = GTK_OLD_EDITABLE (widget);
1960 key = event->keyval;
1963 if ((GTK_OLD_EDITABLE(text)->editable == FALSE))
1965 switch (event->keyval)
1969 if (event->state & GDK_CONTROL_MASK)
1970 scroll_int (text, -text->vadj->value);
1976 if (event->state & GDK_CONTROL_MASK)
1977 scroll_int (text, +text->vadj->upper);
1981 case GDK_KP_Page_Up:
1982 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
1983 case GDK_KP_Page_Down:
1984 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
1986 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
1988 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
1991 if (event->state & GDK_CONTROL_MASK)
1992 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2003 gint extend_selection;
2005 guint initial_pos = old_editable->current_pos;
2007 text->point = find_mark (text, text->cursor_mark.index);
2009 extend_selection = event->state & GDK_SHIFT_MASK;
2010 extend_start = FALSE;
2012 if (extend_selection)
2014 old_editable->has_selection = TRUE;
2016 if (old_editable->selection_start_pos == old_editable->selection_end_pos)
2018 old_editable->selection_start_pos = text->point.index;
2019 old_editable->selection_end_pos = text->point.index;
2022 extend_start = (text->point.index == old_editable->selection_start_pos);
2025 switch (event->keyval)
2029 if (event->state & GDK_CONTROL_MASK)
2030 move_cursor_buffer_ver (text, -1);
2032 gtk_text_move_beginning_of_line (text);
2036 if (event->state & GDK_CONTROL_MASK)
2037 move_cursor_buffer_ver (text, +1);
2039 gtk_text_move_end_of_line (text);
2041 case GDK_KP_Page_Up:
2042 case GDK_Page_Up: move_cursor_page_ver (text, -1); break;
2043 case GDK_KP_Page_Down:
2044 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
2045 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
2047 case GDK_Up: move_cursor_ver (text, -1); break;
2049 case GDK_Down: move_cursor_ver (text, +1); break;
2052 if (event->state & GDK_CONTROL_MASK)
2053 gtk_text_move_backward_word (text);
2055 move_cursor_hor (text, -1);
2059 if (event->state & GDK_CONTROL_MASK)
2060 gtk_text_move_forward_word (text);
2062 move_cursor_hor (text, +1);
2066 if (event->state & GDK_CONTROL_MASK)
2067 gtk_text_delete_backward_word (text);
2069 gtk_text_delete_backward_character (text);
2072 gtk_text_delete_line (text);
2076 if (event->state & GDK_SHIFT_MASK)
2078 extend_selection = FALSE;
2079 gtk_editable_paste_clipboard (GTK_EDITABLE (old_editable));
2081 else if (event->state & GDK_CONTROL_MASK)
2083 gtk_editable_copy_clipboard (GTK_EDITABLE (old_editable));
2087 /* gtk_toggle_insert(text) -- IMPLEMENT */
2092 if (event->state & GDK_CONTROL_MASK)
2093 gtk_text_delete_forward_word (text);
2094 else if (event->state & GDK_SHIFT_MASK)
2096 extend_selection = FALSE;
2097 gtk_editable_cut_clipboard (GTK_EDITABLE (old_editable));
2100 gtk_text_delete_forward_character (text);
2103 case GDK_ISO_Left_Tab:
2105 position = text->point.index;
2106 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\t", 1, &position);
2110 if (event->state & GDK_CONTROL_MASK)
2111 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2114 position = text->point.index;
2115 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\n", 1, &position);
2119 /* Don't insert literally */
2126 if (event->state & GDK_CONTROL_MASK)
2128 if ((key >= 'A') && (key <= 'Z'))
2131 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2133 (* control_keys[(int) (key - 'a')]) (old_editable, event->time);
2139 else if (event->state & GDK_MOD1_MASK)
2141 if ((key >= 'A') && (key <= 'Z'))
2144 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2146 (* alt_keys[(int) (key - 'a')]) (old_editable, event->time);
2152 else if (event->length > 0)
2154 extend_selection = FALSE;
2156 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
2157 position = text->point.index;
2158 gtk_editable_insert_text (GTK_EDITABLE (old_editable), event->string, event->length, &position);
2164 if (return_val && (old_editable->current_pos != initial_pos))
2166 if (extend_selection)
2168 if (old_editable->current_pos < old_editable->selection_start_pos)
2169 gtk_text_set_selection (old_editable, old_editable->current_pos,
2170 old_editable->selection_end_pos);
2171 else if (old_editable->current_pos > old_editable->selection_end_pos)
2172 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2173 old_editable->current_pos);
2177 gtk_text_set_selection (old_editable, old_editable->current_pos,
2178 old_editable->selection_end_pos);
2180 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2181 old_editable->current_pos);
2185 gtk_text_set_selection (old_editable, 0, 0);
2187 gtk_old_editable_claim_selection (old_editable,
2188 old_editable->selection_start_pos != old_editable->selection_end_pos,
2197 gtk_text_adjustment (GtkAdjustment *adjustment,
2200 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2201 g_return_if_fail (GTK_IS_TEXT (text));
2203 /* Just ignore it if we haven't been size-allocated and realized yet */
2204 if (text->line_start_cache == NULL)
2207 if (adjustment == text->hadj)
2209 g_warning ("horizontal scrolling not implemented");
2213 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2217 undraw_cursor (text, FALSE);
2220 scroll_down (text, diff);
2221 else /* if (diff < 0) */
2222 scroll_up (text, diff);
2224 draw_cursor (text, FALSE);
2226 text->last_ver_value = adjustment->value;
2232 gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
2235 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2236 g_return_if_fail (GTK_IS_TEXT (text));
2238 if (adjustment == text->hadj)
2239 gtk_text_set_adjustments (text, NULL, text->vadj);
2240 if (adjustment == text->vadj)
2241 gtk_text_set_adjustments (text, text->hadj, NULL);
2245 static GtkPropertyMark
2246 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2248 GtkPropertyMark mark;
2250 mark = find_mark_near (text, point_position, near);
2252 while (mark.index > 0 &&
2253 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2254 decrement_mark (&mark);
2260 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2262 tab_cont->pixel_offset = 0;
2263 tab_cont->tab_start.tab_stops = text->tab_stops;
2264 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2266 if (!tab_cont->tab_start.to_next_tab)
2267 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2271 line_params_iterate (GtkText* text,
2272 const GtkPropertyMark* mark0,
2273 const PrevTabCont* tab_mark0,
2276 LineIteratorFunction iter)
2277 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2278 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2279 * for each line following MARK, iteration continues unless ITER_CALL
2282 GtkPropertyMark mark = *mark0;
2283 PrevTabCont tab_conts[2];
2284 LineParams *lp, lpbuf;
2285 gint tab_cont_index = 0;
2288 tab_conts[0] = *tab_mark0;
2290 init_tab_cont (text, tab_conts);
2295 lp = g_chunk_new (LineParams, params_mem_chunk);
2299 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2300 tab_conts + (tab_cont_index + 1) % 2);
2302 if ((*iter) (text, lp, data))
2305 if (LAST_INDEX (text, lp->end))
2309 advance_mark (&mark);
2310 tab_cont_index = (tab_cont_index + 1) % 2;
2315 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2317 FetchLinesData *fldata = (FetchLinesData*) data;
2319 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2321 switch (fldata->fl_type)
2323 case FetchLinesCount:
2324 if (!text->line_wrap || !lp->wraps)
2327 if (fldata->data >= fldata->data_max)
2331 case FetchLinesPixels:
2333 fldata->data += LINE_HEIGHT(*lp);
2335 if (fldata->data >= fldata->data_max)
2345 fetch_lines (GtkText* text,
2346 const GtkPropertyMark* mark0,
2347 const PrevTabCont* tab_cont0,
2351 FetchLinesData fl_data;
2353 fl_data.new_lines = NULL;
2355 fl_data.data_max = data;
2356 fl_data.fl_type = fl_type;
2358 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2360 return g_list_reverse (fl_data.new_lines);
2364 fetch_lines_backward (GtkText* text)
2366 GList* new_lines = NULL, *new_line_start;
2367 GtkPropertyMark mark;
2369 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2372 mark = find_this_line_start_mark (text,
2373 CACHE_DATA(text->line_start_cache).start.index - 1,
2374 &CACHE_DATA(text->line_start_cache).start);
2376 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2378 while (new_line_start->next)
2379 new_line_start = new_line_start->next;
2381 new_line_start->next = text->line_start_cache;
2382 text->line_start_cache->prev = new_line_start;
2386 fetch_lines_forward (GtkText* text, gint line_count)
2388 GtkPropertyMark mark;
2389 GList* line = text->line_start_cache;
2394 mark = CACHE_DATA(line).end;
2396 if (LAST_INDEX (text, mark))
2399 advance_mark(&mark);
2401 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2404 line->next->prev = line;
2407 /* Compute the number of lines, and vertical pixels for n characters
2408 * starting from the point
2411 compute_lines_pixels (GtkText* text, guint char_count,
2412 guint *lines, guint *pixels)
2414 GList *line = text->current_line;
2415 gint chars_left = char_count;
2420 /* If chars_left == 0, that means we're joining two lines in a
2421 * deletion, so add in the values for the next line as well
2423 for (; line && chars_left >= 0; line = line->next)
2425 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2427 if (line == text->current_line)
2428 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2430 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2432 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2436 chars_left = 0; /* force another loop */
2439 fetch_lines_forward (text, 1);
2444 total_line_height (GtkText* text, GList* line, gint line_count)
2448 for (; line && line_count > 0; line = line->next)
2450 height += LINE_HEIGHT(CACHE_DATA(line));
2452 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2456 fetch_lines_forward (text, line_count);
2463 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2465 if (old == text->line_start_cache)
2469 for (; old_line_count > 0; old_line_count -= 1)
2471 while (text->line_start_cache &&
2473 CACHE_DATA(text->line_start_cache).wraps)
2474 remove_cache_line(text, text->line_start_cache);
2476 remove_cache_line(text, text->line_start_cache);
2479 last = g_list_last (new);
2481 last->next = text->line_start_cache;
2483 if (text->line_start_cache)
2484 text->line_start_cache->prev = last;
2486 text->line_start_cache = new;
2492 g_assert (old->prev);
2496 for (; old_line_count > 0; old_line_count -= 1)
2498 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2499 old = remove_cache_line (text, old);
2501 old = remove_cache_line (text, old);
2507 last = g_list_last (new);
2517 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2519 GList* cache = text->current_line;
2522 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2525 for (; cache; cache = cache->next)
2527 GtkPropertyMark *start = &CACHE_DATA(cache).start;
2528 GtkPropertyMark *end = &CACHE_DATA(cache).end;
2530 start->index -= nchars;
2531 end->index -= nchars;
2533 if (LAST_INDEX (text, text->point) &&
2534 start->index == text->point.index)
2535 *start = text->point;
2536 else if (start->property == text->point.property)
2537 start->offset = start->index - (text->point.index - text->point.offset);
2539 if (LAST_INDEX (text, text->point) &&
2540 end->index == text->point.index)
2542 if (end->property == text->point.property)
2543 end->offset = end->index - (text->point.index - text->point.offset);
2545 /*TEXT_ASSERT_MARK(text, start, "start");*/
2546 /*TEXT_ASSERT_MARK(text, end, "end");*/
2551 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2553 GtkWidget *widget = GTK_WIDGET (text);
2556 guint new_pixels = 0;
2558 GList* new_line = NULL;
2561 text->cursor_virtual_x = 0;
2563 correct_cache_delete (text, nchars, old_lines);
2565 pixel_height = pixel_height_of(text, text->current_line) -
2566 LINE_HEIGHT(CACHE_DATA(text->current_line));
2568 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2569 CACHE_DATA(text->current_line).start = text->point;
2571 new_line = fetch_lines (text,
2572 &CACHE_DATA(text->current_line).start,
2573 &CACHE_DATA(text->current_line).tab_cont,
2577 swap_lines (text, text->current_line, new_line, old_lines);
2579 text->current_line = new_line;
2581 new_pixels = total_line_height (text, new_line, 1);
2583 gdk_window_get_size (text->text_area, &width, &height);
2585 if (old_pixels != new_pixels)
2587 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2589 gdk_draw_pixmap (text->text_area,
2593 pixel_height + old_pixels,
2595 pixel_height + new_pixels,
2599 text->vadj->upper += new_pixels;
2600 text->vadj->upper -= old_pixels;
2601 adjust_adj (text, text->vadj);
2605 rect.y = pixel_height;
2607 rect.height = new_pixels;
2609 expose_text (text, &rect, FALSE);
2610 gtk_text_draw_focus ( (GtkWidget *) text);
2612 text->cursor_mark = text->point;
2614 find_cursor (text, TRUE);
2616 if (old_pixels != new_pixels)
2618 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2621 rect.y = pixel_height + new_pixels;
2623 rect.height = height - rect.y;
2625 expose_text (text, &rect, FALSE);
2628 process_exposes (text);
2635 /* note, the point has already been moved forward */
2637 correct_cache_insert (GtkText* text, gint nchars)
2640 GtkPropertyMark *start;
2641 GtkPropertyMark *end;
2642 gboolean was_split = FALSE;
2644 /* We need to distinguish whether the property was split in the
2645 * insert or not, so we check if the point (which points after
2646 * the insertion here), points to the same character as the
2647 * point before. Ugh.
2651 GtkPropertyMark tmp_mark = text->point;
2652 move_mark_n (&tmp_mark, -1);
2654 if (tmp_mark.property != text->point.property)
2658 /* If we inserted a property exactly at the beginning of the
2659 * line, we have to correct here, or fetch_lines will
2662 start = &CACHE_DATA(text->current_line).start;
2664 /* Check if if we split exactly at the beginning of the line:
2665 * (was_split won't be set if we are inserting at the end of the text,
2666 * so we don't check)
2668 if (start->offset == MARK_CURRENT_PROPERTY (start)->length)
2669 SET_PROPERTY_MARK (start, start->property->next, 0);
2670 /* Check if we inserted a property at the beginning of the text: */
2671 else if (was_split &&
2672 (start->property == text->point.property) &&
2673 (start->index == text->point.index - nchars))
2674 SET_PROPERTY_MARK (start, start->property->prev, 0);
2676 /* Now correct the offsets, and check for start or end marks that
2677 * are after the point, yet point to a property before the point's
2678 * property. This indicates that they are meant to point to the
2679 * second half of a property we split in insert_text_property(), so
2680 * we fix them up that way.
2682 cache = text->current_line->next;
2684 for (; cache; cache = cache->next)
2686 start = &CACHE_DATA(cache).start;
2687 end = &CACHE_DATA(cache).end;
2689 if (LAST_INDEX (text, text->point) &&
2690 start->index == text->point.index)
2691 *start = text->point;
2692 else if (start->index >= text->point.index - nchars)
2694 if (!was_split && start->property == text->point.property)
2695 move_mark_n(start, nchars);
2698 if (start->property->next &&
2699 (start->property->next->next == text->point.property))
2701 g_assert (start->offset >= MARK_CURRENT_PROPERTY (start)->length);
2702 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2703 start->property = text->point.property;
2705 start->index += nchars;
2709 if (LAST_INDEX (text, text->point) &&
2710 end->index == text->point.index)
2712 if (end->index >= text->point.index - nchars)
2714 if (!was_split && end->property == text->point.property)
2715 move_mark_n(end, nchars);
2718 if (end->property->next &&
2719 (end->property->next->next == text->point.property))
2721 g_assert (end->offset >= MARK_CURRENT_PROPERTY (end)->length);
2722 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2723 end->property = text->point.property;
2725 end->index += nchars;
2729 /*TEXT_ASSERT_MARK(text, start, "start");*/
2730 /*TEXT_ASSERT_MARK(text, end, "end");*/
2736 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2737 guint new_line_count)
2739 GtkWidget *widget = GTK_WIDGET (text);
2742 guint new_pixels = 0;
2744 GList* new_lines = NULL;
2747 text->cursor_virtual_x = 0;
2749 undraw_cursor (text, FALSE);
2751 correct_cache_insert (text, nchars);
2753 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2755 pixel_height = pixel_height_of(text, text->current_line) -
2756 LINE_HEIGHT(CACHE_DATA(text->current_line));
2758 new_lines = fetch_lines (text,
2759 &CACHE_DATA(text->current_line).start,
2760 &CACHE_DATA(text->current_line).tab_cont,
2764 swap_lines (text, text->current_line, new_lines, 1);
2766 text->current_line = new_lines;
2768 new_pixels = total_line_height (text, new_lines, new_line_count);
2770 gdk_window_get_size (text->text_area, &width, &height);
2772 if (old_pixels != new_pixels)
2774 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2776 gdk_draw_pixmap (text->text_area,
2780 pixel_height + old_pixels,
2782 pixel_height + new_pixels,
2784 height + (old_pixels - new_pixels) - pixel_height);
2787 text->vadj->upper += new_pixels;
2788 text->vadj->upper -= old_pixels;
2789 adjust_adj (text, text->vadj);
2793 rect.y = pixel_height;
2795 rect.height = new_pixels;
2797 expose_text (text, &rect, FALSE);
2798 gtk_text_draw_focus ( (GtkWidget *) text);
2800 text->cursor_mark = text->point;
2802 find_cursor (text, TRUE);
2804 draw_cursor (text, FALSE);
2806 if (old_pixels != new_pixels)
2808 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2811 rect.y = pixel_height + new_pixels;
2813 rect.height = height - rect.y;
2815 expose_text (text, &rect, FALSE);
2818 process_exposes (text);
2821 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2826 /* Text property functions */
2829 font_hash (gconstpointer font)
2831 return gdk_font_id ((const GdkFont*) font);
2834 static GHashTable *font_cache_table = NULL;
2837 get_text_font (GdkFont* gfont)
2842 if (!font_cache_table)
2843 font_cache_table = g_hash_table_new (font_hash, (GEqualFunc) gdk_font_equal);
2845 tf = g_hash_table_lookup (font_cache_table, gfont);
2853 tf = g_new (GtkTextFont, 1);
2856 tf->gdk_font = gfont;
2857 gdk_font_ref (gfont);
2859 for(i = 0; i < 256; i += 1)
2860 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2862 g_hash_table_insert (font_cache_table, gfont, tf);
2868 text_font_unref (GtkTextFont *text_font)
2870 text_font->ref_count--;
2871 if (text_font->ref_count == 0)
2873 g_hash_table_remove (font_cache_table, text_font->gdk_font);
2874 gdk_font_unref (text_font->gdk_font);
2880 text_properties_equal (TextProperty* prop, GdkFont* font, const GdkColor *fore, const GdkColor *back)
2882 if (prop->flags & PROPERTY_FONT)
2885 GtkTextFont *text_font;
2890 text_font = get_text_font (font);
2892 retval = (prop->font == text_font);
2893 text_font_unref (text_font);
2902 if (prop->flags & PROPERTY_FOREGROUND)
2904 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
2911 if (prop->flags & PROPERTY_BACKGROUND)
2913 if (!back || !gdk_color_equal (&prop->back_color, back))
2924 realize_property (GtkText *text, TextProperty *prop)
2926 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2928 if (prop->flags & PROPERTY_FOREGROUND)
2929 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
2931 if (prop->flags & PROPERTY_BACKGROUND)
2932 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
2936 realize_properties (GtkText *text)
2938 GList *tmp_list = text->text_properties;
2942 realize_property (text, tmp_list->data);
2944 tmp_list = tmp_list->next;
2949 unrealize_property (GtkText *text, TextProperty *prop)
2951 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2953 if (prop->flags & PROPERTY_FOREGROUND)
2954 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
2956 if (prop->flags & PROPERTY_BACKGROUND)
2957 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
2961 unrealize_properties (GtkText *text)
2963 GList *tmp_list = text->text_properties;
2967 unrealize_property (text, tmp_list->data);
2969 tmp_list = tmp_list->next;
2973 static TextProperty*
2974 new_text_property (GtkText *text, GdkFont *font, const GdkColor* fore,
2975 const GdkColor* back, guint length)
2979 if (text_property_chunk == NULL)
2981 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
2982 sizeof(TextProperty),
2983 1024*sizeof(TextProperty),
2987 prop = g_chunk_new(TextProperty, text_property_chunk);
2992 prop->flags |= PROPERTY_FONT;
2993 prop->font = get_text_font (font);
3000 prop->flags |= PROPERTY_FOREGROUND;
3001 prop->fore_color = *fore;
3006 prop->flags |= PROPERTY_BACKGROUND;
3007 prop->back_color = *back;
3010 prop->length = length;
3012 if (GTK_WIDGET_REALIZED (text))
3013 realize_property (text, prop);
3019 destroy_text_property (TextProperty *prop)
3022 text_font_unref (prop->font);
3024 g_mem_chunk_free (text_property_chunk, prop);
3027 /* Flop the memory between the point and the gap around like a
3030 move_gap (GtkText* text, guint index)
3032 if (text->gap_position < index)
3034 gint diff = index - text->gap_position;
3036 if (text->use_wchar)
3037 g_memmove (text->text.wc + text->gap_position,
3038 text->text.wc + text->gap_position + text->gap_size,
3039 diff*sizeof (GdkWChar));
3041 g_memmove (text->text.ch + text->gap_position,
3042 text->text.ch + text->gap_position + text->gap_size,
3045 text->gap_position = index;
3047 else if (text->gap_position > index)
3049 gint diff = text->gap_position - index;
3051 if (text->use_wchar)
3052 g_memmove (text->text.wc + index + text->gap_size,
3053 text->text.wc + index,
3054 diff*sizeof (GdkWChar));
3056 g_memmove (text->text.ch + index + text->gap_size,
3057 text->text.ch + index,
3060 text->gap_position = index;
3064 /* Increase the gap size. */
3066 make_forward_space (GtkText* text, guint len)
3068 if (text->gap_size < len)
3070 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3072 if (sum >= text->text_len)
3076 while (i <= sum) i <<= 1;
3078 if (text->use_wchar)
3079 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3080 i*sizeof(GdkWChar));
3082 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3086 if (text->use_wchar)
3087 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3088 text->text.wc + text->gap_position + text->gap_size,
3089 (text->text_end - (text->gap_position + text->gap_size))
3092 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3093 text->text.ch + text->gap_position + text->gap_size,
3094 text->text_end - (text->gap_position + text->gap_size));
3096 text->text_end += len*2;
3097 text->gap_size += len*2;
3101 /* Inserts into the text property list a list element that guarantees
3102 * that for len characters following the point, text has the correct
3103 * property. does not move point. adjusts text_properties_point and
3104 * text_properties_point_offset relative to the current value of
3107 insert_text_property (GtkText* text, GdkFont* font,
3108 const GdkColor *fore, const GdkColor* back, guint len)
3110 GtkPropertyMark *mark = &text->point;
3111 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3112 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3114 if (MARK_OFFSET(mark) == 0)
3116 /* Point is on the boundary of two properties.
3117 * If it is the same as either, grow, else insert
3120 if (text_properties_equal(forward_prop, font, fore, back))
3122 /* Grow the property in front of us. */
3124 MARK_PROPERTY_LENGTH(mark) += len;
3126 else if (backward_prop &&
3127 text_properties_equal(backward_prop, font, fore, back))
3129 /* Grow property behind us, point property and offset
3132 SET_PROPERTY_MARK (&text->point,
3133 MARK_PREV_LIST_PTR (mark),
3134 backward_prop->length);
3136 backward_prop->length += len;
3138 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3139 (forward_prop->length == 1))
3141 /* Next property just has last position, take it over */
3143 if (GTK_WIDGET_REALIZED (text))
3144 unrealize_property (text, forward_prop);
3146 forward_prop->flags = 0;
3149 forward_prop->flags |= PROPERTY_FONT;
3150 forward_prop->font = get_text_font (font);
3153 forward_prop->font = NULL;
3157 forward_prop->flags |= PROPERTY_FOREGROUND;
3158 forward_prop->fore_color = *fore;
3162 forward_prop->flags |= PROPERTY_BACKGROUND;
3163 forward_prop->back_color = *back;
3165 forward_prop->length += len;
3167 if (GTK_WIDGET_REALIZED (text))
3168 realize_property (text, forward_prop);
3172 /* Splice a new property into the list. */
3174 GList* new_prop = g_list_alloc();
3176 new_prop->next = MARK_LIST_PTR(mark);
3177 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3178 new_prop->next->prev = new_prop;
3181 new_prop->prev->next = new_prop;
3183 new_prop->data = new_text_property (text, font, fore, back, len);
3185 SET_PROPERTY_MARK (mark, new_prop, 0);
3190 /* The following will screw up the line_start cache,
3191 * we'll fix it up in correct_cache_insert
3194 /* In the middle of forward_prop, if properties are equal,
3195 * just add to its length, else split it into two and splice
3197 if (text_properties_equal (forward_prop, font, fore, back))
3199 forward_prop->length += len;
3201 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3202 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3204 /* Inserting before only the last position in the text */
3207 forward_prop->length -= 1;
3209 new_prop = g_list_alloc();
3210 new_prop->data = new_text_property (text, font, fore, back, len+1);
3211 new_prop->prev = MARK_LIST_PTR(mark);
3212 new_prop->next = NULL;
3213 MARK_NEXT_LIST_PTR(mark) = new_prop;
3215 SET_PROPERTY_MARK (mark, new_prop, 0);
3219 GList* new_prop = g_list_alloc();
3220 GList* new_prop_forward = g_list_alloc();
3221 gint old_length = forward_prop->length;
3222 GList* next = MARK_NEXT_LIST_PTR(mark);
3224 /* Set the new lengths according to where they are split. Construct
3225 * two new properties. */
3226 forward_prop->length = MARK_OFFSET(mark);
3228 new_prop_forward->data =
3229 new_text_property(text,
3230 forward_prop->flags & PROPERTY_FONT ?
3231 forward_prop->font->gdk_font : NULL,
3232 forward_prop->flags & PROPERTY_FOREGROUND ?
3233 &forward_prop->fore_color : NULL,
3234 forward_prop->flags & PROPERTY_BACKGROUND ?
3235 &forward_prop->back_color : NULL,
3236 old_length - forward_prop->length);
3238 new_prop->data = new_text_property(text, font, fore, back, len);
3240 /* Now splice things in. */
3241 MARK_NEXT_LIST_PTR(mark) = new_prop;
3242 new_prop->prev = MARK_LIST_PTR(mark);
3244 new_prop->next = new_prop_forward;
3245 new_prop_forward->prev = new_prop;
3247 new_prop_forward->next = next;
3250 next->prev = new_prop_forward;
3252 SET_PROPERTY_MARK (mark, new_prop, 0);
3256 while (text->text_properties_end->next)
3257 text->text_properties_end = text->text_properties_end->next;
3259 while (text->text_properties->prev)
3260 text->text_properties = text->text_properties->prev;
3264 delete_text_property (GtkText* text, guint nchars)
3266 /* Delete nchars forward from point. */
3268 /* Deleting text properties is problematical, because we
3269 * might be storing around marks pointing to a property.
3271 * The marks in question and how we handle them are:
3273 * point: We know the new value, since it will be at the
3274 * end of the deleted text, and we move it there
3276 * cursor: We just remove the mark and set it equal to the
3277 * point after the operation.
3278 * line-start cache: We replace most affected lines.
3279 * The current line gets used to fetch the new
3280 * lines so, if necessary, (delete at the beginning
3281 * of a line) we fix it up by setting it equal to the
3289 for(; nchars; nchars -= 1)
3291 prop = MARK_CURRENT_PROPERTY(&text->point);
3295 if (prop->length == 0)
3297 tmp = MARK_LIST_PTR (&text->point);
3299 is_first = tmp == text->text_properties;
3301 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3302 text->point.offset = 0;
3304 if (GTK_WIDGET_REALIZED (text))
3305 unrealize_property (text, prop);
3307 destroy_text_property (prop);
3308 g_list_free_1 (tmp);
3310 prop = MARK_CURRENT_PROPERTY (&text->point);
3313 text->text_properties = MARK_LIST_PTR (&text->point);
3315 g_assert (prop->length != 0);
3317 else if (prop->length == text->point.offset)
3319 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3320 text->point.offset = 0;
3324 /* Check to see if we have just the single final position remaining
3325 * along in a property; if so, combine it with the previous property
3327 if (LAST_INDEX (text, text->point) &&
3328 (MARK_OFFSET (&text->point) == 0) &&
3329 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3331 tmp = MARK_LIST_PTR (&text->point);
3332 prop = MARK_CURRENT_PROPERTY(&text->point);
3334 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3335 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3336 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3338 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3340 if (GTK_WIDGET_REALIZED (text))
3341 unrealize_property (text, prop);
3343 destroy_text_property (prop);
3344 g_list_free_1 (tmp);
3349 init_properties (GtkText *text)
3351 if (!text->text_properties)
3353 text->text_properties = g_list_alloc();
3354 text->text_properties->next = NULL;
3355 text->text_properties->prev = NULL;
3356 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3357 text->text_properties_end = text->text_properties;
3359 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3361 text->point.index = 0;
3366 /**********************************************************************/
3367 /* Property Movement */
3368 /**********************************************************************/
3371 move_mark_n (GtkPropertyMark* mark, gint n)
3374 advance_mark_n(mark, n);
3376 decrement_mark_n(mark, -n);
3380 advance_mark (GtkPropertyMark* mark)
3382 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3386 if (prop->length > mark->offset + 1)
3390 mark->property = MARK_NEXT_LIST_PTR (mark);
3396 advance_mark_n (GtkPropertyMark* mark, gint n)
3403 i = 0; /* otherwise it migth not be init. */
3404 prop = MARK_CURRENT_PROPERTY(mark);
3406 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3407 /* to make it easier */
3408 n += (mark->offset);
3409 mark->index -= mark->offset;
3411 /* first we take seven-mile-leaps to get to the right text
3413 while ((n-i) > prop->length - 1) {
3415 mark->index += prop->length;
3416 mark->property = MARK_NEXT_LIST_PTR (mark);
3417 prop = MARK_CURRENT_PROPERTY (mark);
3421 /* and then the rest */
3422 mark->index += n - i;
3423 mark->offset += n - i;
3427 decrement_mark (GtkPropertyMark* mark)
3431 if (mark->offset > 0)
3435 mark->property = MARK_PREV_LIST_PTR (mark);
3436 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3441 decrement_mark_n (GtkPropertyMark* mark, gint n)
3445 while (mark->offset < n) {
3446 /* jump to end of prev */
3447 n -= mark->offset + 1;
3448 mark->index -= mark->offset + 1;
3449 mark->property = MARK_PREV_LIST_PTR (mark);
3450 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3458 static GtkPropertyMark
3459 find_mark (GtkText* text, guint mark_position)
3461 return find_mark_near (text, mark_position, &text->point);
3465 * You can also start from the end, what a drag.
3467 static GtkPropertyMark
3468 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3473 GtkPropertyMark mark;
3476 diffa = mark_position + 1;
3478 diffa = mark_position - near->index;
3480 diffb = mark_position;
3492 mark.property = text->text_properties;
3496 move_mark_n (&mark, mark_position - mark.index);
3501 /* This routine must be called with scroll == FALSE, only when
3502 * point is at least partially on screen
3506 find_line_containing_point (GtkText* text, guint point,
3512 text->current_line = NULL;
3516 /* Scroll backwards until the point is on screen
3518 while (CACHE_DATA(text->line_start_cache).start.index > point)
3519 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3521 /* Now additionally try to make sure that the point is fully on screen
3525 while (text->first_cut_pixels != 0 &&
3526 text->line_start_cache->next &&
3527 CACHE_DATA(text->line_start_cache->next).start.index > point)
3528 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3531 gdk_window_get_size (text->text_area, NULL, &height);
3533 for (cache = text->line_start_cache; cache; cache = cache->next)
3537 if (CACHE_DATA(cache).end.index >= point ||
3538 LAST_INDEX(text, CACHE_DATA(cache).end))
3540 text->current_line = cache; /* LOOK HERE, this proc has an
3541 * important side effect. */
3545 TEXT_SHOW_LINE (text, cache, "cache");
3547 if (cache->next == NULL)
3548 fetch_lines_forward (text, 1);
3552 lph = pixel_height_of (text, cache->next);
3554 /* Scroll the bottom of the line is on screen, or until
3555 * the line is the first onscreen line.
3557 while (cache->next != text->line_start_cache && lph > height)
3559 TEXT_SHOW_LINE (text, cache, "cache");
3560 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3561 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3562 lph = pixel_height_of (text, cache->next);
3567 g_assert_not_reached (); /* Must set text->current_line here */
3571 pixel_height_of (GtkText* text, GList* cache_line)
3573 gint pixels = - text->first_cut_pixels;
3574 GList *cache = text->line_start_cache;
3577 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3579 if (cache->data == cache_line->data)
3582 cache = cache->next;
3588 /**********************************************************************/
3589 /* Search and Placement */
3590 /**********************************************************************/
3593 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3596 gint16* char_widths;
3598 if (LAST_INDEX (text, *mark))
3601 ch = GTK_TEXT_INDEX (text, mark->index);
3602 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3606 return tab_mark->to_next_tab * char_widths[' '];
3610 return char_widths[ch];
3614 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3619 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, GdkWChar ch)
3621 if (tab_mark->to_next_tab == 1 || ch == '\t')
3623 if (tab_mark->tab_stops->next)
3625 tab_mark->tab_stops = tab_mark->tab_stops->next;
3626 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3630 tab_mark->to_next_tab = text->default_tab_width;
3635 tab_mark->to_next_tab -= 1;
3640 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3644 advance_tab_mark (text, tab_mark, 0);
3648 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3652 GtkPropertyMark mark = start_line->start;
3653 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3654 gint pixel_width = LINE_START_PIXEL (*start_line);
3656 while (mark.index < text->cursor_mark.index)
3658 pixel_width += find_char_width (text, &mark, &tab_mark);
3660 advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3661 advance_mark (&mark);
3664 text->cursor_pos_x = pixel_width;
3665 text->cursor_pos_y = pixel_height;
3666 text->cursor_char_offset = start_line->font_descent;
3667 text->cursor_mark = mark;
3669 ch = LAST_INDEX (text, mark) ?
3670 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3672 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3673 text->cursor_char = 0;
3675 text->cursor_char = ch;
3679 find_cursor (GtkText* text, gboolean scroll)
3681 if (GTK_WIDGET_REALIZED (text))
3683 find_line_containing_point (text, text->cursor_mark.index, scroll);
3685 if (text->current_line)
3686 find_cursor_at_line (text,
3687 &CACHE_DATA(text->current_line),
3688 pixel_height_of(text, text->current_line));
3691 GTK_OLD_EDITABLE (text)->current_pos = text->cursor_mark.index;
3695 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3696 guint line_pixel_height,
3699 GtkPropertyMark mark = lp->start;
3700 TabStopMark tab_mark = lp->tab_cont.tab_start;
3702 gint char_width = find_char_width(text, &mark, &tab_mark);
3703 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3705 text->cursor_pos_y = line_pixel_height;
3709 GdkWChar ch = LAST_INDEX (text, mark) ?
3710 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3712 if (button_x < pixel_width || mark.index == lp->end.index)
3714 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3715 text->cursor_mark = mark;
3716 text->cursor_char_offset = lp->font_descent;
3718 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3719 text->cursor_char = 0;
3721 text->cursor_char = ch;
3726 advance_tab_mark (text, &tab_mark, ch);
3727 advance_mark (&mark);
3729 pixel_width += char_width/2;
3731 char_width = find_char_width (text, &mark, &tab_mark);
3733 pixel_width += (char_width+1)/2;
3738 find_mouse_cursor (GtkText* text, gint x, gint y)
3741 GList* cache = text->line_start_cache;
3745 pixel_height = - text->first_cut_pixels;
3747 for (; cache; cache = cache->next)
3749 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3751 if (y < pixel_height || !cache->next)
3753 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3755 find_cursor (text, FALSE);
3762 /**********************************************************************/
3764 /**********************************************************************/
3767 free_cache (GtkText* text)
3769 GList* cache = text->line_start_cache;
3774 cache = cache->prev;
3776 text->line_start_cache = cache;
3779 for (; cache; cache = cache->next)
3780 g_mem_chunk_free (params_mem_chunk, cache->data);
3782 g_list_free (text->line_start_cache);
3784 text->line_start_cache = NULL;
3788 remove_cache_line (GtkText* text, GList* member)
3795 if (member == text->line_start_cache)
3796 text->line_start_cache = text->line_start_cache->next;
3799 member->prev->next = member->next;
3802 member->next->prev = member->prev;
3804 list = member->next;
3806 g_mem_chunk_free (params_mem_chunk, member->data);
3807 g_list_free_1 (member);
3812 /**********************************************************************/
3814 /**********************************************************************/
3817 move_cursor_buffer_ver (GtkText *text, int dir)
3819 undraw_cursor (text, FALSE);
3823 scroll_int (text, text->vadj->upper);
3824 text->cursor_mark = find_this_line_start_mark (text,
3826 &text->cursor_mark);
3830 scroll_int (text, - text->vadj->value);
3831 text->cursor_mark = find_this_line_start_mark (text,
3833 &text->cursor_mark);
3836 find_cursor (text, TRUE);
3837 draw_cursor (text, FALSE);
3841 move_cursor_page_ver (GtkText *text, int dir)
3843 scroll_int (text, dir * text->vadj->page_increment);
3847 move_cursor_ver (GtkText *text, int count)
3850 GtkPropertyMark mark;
3853 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3854 offset = text->cursor_mark.index - mark.index;
3856 if (offset > text->cursor_virtual_x)
3857 text->cursor_virtual_x = offset;
3861 if (mark.index == 0)
3864 decrement_mark (&mark);
3865 mark = find_this_line_start_mark (text, mark.index, &mark);
3869 mark = text->cursor_mark;
3871 while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3872 advance_mark (&mark);
3874 if (LAST_INDEX(text, mark))
3877 advance_mark (&mark);
3880 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3881 if (LAST_INDEX(text, mark) ||
3882 GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3885 undraw_cursor (text, FALSE);
3887 text->cursor_mark = mark;
3889 find_cursor (text, TRUE);
3891 draw_cursor (text, FALSE);
3895 move_cursor_hor (GtkText *text, int count)
3897 /* count should be +-1. */
3898 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3899 (count < 0 && text->cursor_mark.index < (- count)) ||
3903 text->cursor_virtual_x = 0;
3905 undraw_cursor (text, FALSE);
3907 move_mark_n (&text->cursor_mark, count);
3909 find_cursor (text, TRUE);
3911 draw_cursor (text, FALSE);
3915 gtk_text_move_cursor (GtkOldEditable *old_editable,
3922 move_cursor_hor (GTK_TEXT (old_editable), 1);
3927 move_cursor_hor (GTK_TEXT (old_editable), -1);
3933 move_cursor_ver (GTK_TEXT (old_editable), 1);
3938 move_cursor_ver (GTK_TEXT (old_editable), -1);
3943 gtk_text_move_forward_character (GtkText *text)
3945 move_cursor_hor (text, 1);
3949 gtk_text_move_backward_character (GtkText *text)
3951 move_cursor_hor (text, -1);
3955 gtk_text_move_next_line (GtkText *text)
3957 move_cursor_ver (text, 1);
3961 gtk_text_move_previous_line (GtkText *text)
3963 move_cursor_ver (text, -1);
3967 gtk_text_move_word (GtkOldEditable *old_editable,
3973 gtk_text_move_forward_word (GTK_TEXT (old_editable));
3978 gtk_text_move_backward_word (GTK_TEXT (old_editable));
3983 gtk_text_move_forward_word (GtkText *text)
3985 text->cursor_virtual_x = 0;
3987 undraw_cursor (text, FALSE);
3989 if (text->use_wchar)
3991 while (!LAST_INDEX (text, text->cursor_mark) &&
3992 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3993 advance_mark (&text->cursor_mark);
3995 while (!LAST_INDEX (text, text->cursor_mark) &&
3996 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3997 advance_mark (&text->cursor_mark);
4001 while (!LAST_INDEX (text, text->cursor_mark) &&
4002 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4003 advance_mark (&text->cursor_mark);
4005 while (!LAST_INDEX (text, text->cursor_mark) &&
4006 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4007 advance_mark (&text->cursor_mark);
4010 find_cursor (text, TRUE);
4011 draw_cursor (text, FALSE);
4015 gtk_text_move_backward_word (GtkText *text)
4017 text->cursor_virtual_x = 0;
4019 undraw_cursor (text, FALSE);
4021 if (text->use_wchar)
4023 while ((text->cursor_mark.index > 0) &&
4024 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4025 decrement_mark (&text->cursor_mark);
4027 while ((text->cursor_mark.index > 0) &&
4028 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4029 decrement_mark (&text->cursor_mark);
4033 while ((text->cursor_mark.index > 0) &&
4034 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4035 decrement_mark (&text->cursor_mark);
4037 while ((text->cursor_mark.index > 0) &&
4038 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4039 decrement_mark (&text->cursor_mark);
4042 find_cursor (text, TRUE);
4043 draw_cursor (text, FALSE);
4047 gtk_text_move_page (GtkOldEditable *old_editable,
4052 scroll_int (GTK_TEXT (old_editable),
4053 y * GTK_TEXT(old_editable)->vadj->page_increment);
4057 gtk_text_move_to_row (GtkOldEditable *old_editable,
4063 gtk_text_move_to_column (GtkOldEditable *old_editable,
4068 text = GTK_TEXT (old_editable);
4070 text->cursor_virtual_x = 0; /* FIXME */
4072 undraw_cursor (text, FALSE);
4074 /* Move to the beginning of the line */
4075 while ((text->cursor_mark.index > 0) &&
4076 (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4077 decrement_mark (&text->cursor_mark);
4079 while (!LAST_INDEX (text, text->cursor_mark) &&
4080 (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4084 else if (column == 0)
4087 advance_mark (&text->cursor_mark);
4090 find_cursor (text, TRUE);
4091 draw_cursor (text, FALSE);
4095 gtk_text_move_beginning_of_line (GtkText *text)
4097 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4102 gtk_text_move_end_of_line (GtkText *text)
4104 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), -1);
4108 gtk_text_kill_char (GtkOldEditable *old_editable,
4113 text = GTK_TEXT (old_editable);
4115 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4116 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4121 if (text->point.index + 1 <= TEXT_LENGTH (text))
4122 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index, text->point.index + 1);
4126 if (text->point.index > 0)
4127 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index - 1, text->point.index);
4133 gtk_text_delete_forward_character (GtkText *text)
4135 gtk_text_kill_char (GTK_OLD_EDITABLE (text), 1);
4139 gtk_text_delete_backward_character (GtkText *text)
4141 gtk_text_kill_char (GTK_OLD_EDITABLE (text), -1);
4145 gtk_text_kill_word (GtkOldEditable *old_editable,
4148 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4149 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4152 gint old_pos = old_editable->current_pos;
4155 gtk_text_move_word (old_editable, 1);
4156 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4160 gtk_text_move_word (old_editable, -1);
4161 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4167 gtk_text_delete_forward_word (GtkText *text)
4169 gtk_text_kill_word (GTK_OLD_EDITABLE (text), 1);
4173 gtk_text_delete_backward_word (GtkText *text)
4175 gtk_text_kill_word (GTK_OLD_EDITABLE (text), -1);
4179 gtk_text_kill_line (GtkOldEditable *old_editable,
4182 gint old_pos = old_editable->current_pos;
4185 gtk_text_move_to_column (old_editable, -1);
4186 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4190 gtk_text_move_to_column (old_editable, 0);
4191 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4196 gtk_text_delete_line (GtkText *text)
4198 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4199 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4203 gtk_text_delete_to_line_end (GtkText *text)
4205 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4209 gtk_text_select_word (GtkText *text, guint32 time)
4214 GtkOldEditable *old_editable;
4215 old_editable = GTK_OLD_EDITABLE (text);
4217 gtk_text_move_backward_word (text);
4218 start_pos = text->cursor_mark.index;
4220 gtk_text_move_forward_word (text);
4221 end_pos = text->cursor_mark.index;
4223 old_editable->has_selection = TRUE;
4224 gtk_text_set_selection (old_editable, start_pos, end_pos);
4225 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4229 gtk_text_select_line (GtkText *text, guint32 time)
4234 GtkOldEditable *old_editable;
4235 old_editable = GTK_OLD_EDITABLE (text);
4237 gtk_text_move_beginning_of_line (text);
4238 start_pos = text->cursor_mark.index;
4240 gtk_text_move_end_of_line (text);
4241 gtk_text_move_forward_character (text);
4242 end_pos = text->cursor_mark.index;
4244 old_editable->has_selection = TRUE;
4245 gtk_text_set_selection (old_editable, start_pos, end_pos);
4246 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4249 /**********************************************************************/
4251 /**********************************************************************/
4254 adjust_adj (GtkText* text, GtkAdjustment* adj)
4258 gdk_window_get_size (text->text_area, NULL, &height);
4260 adj->step_increment = MIN (adj->upper, SCROLL_PIXELS);
4261 adj->page_increment = MIN (adj->upper, height - KEY_SCROLL_PIXELS);
4262 adj->page_size = MIN (adj->upper, height);
4263 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4264 adj->value = MAX (adj->value, 0.0);
4266 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4270 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
4272 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4274 if ((text->first_line_start_index >= lp->start.index) &&
4275 (text->first_line_start_index <= lp->end.index))
4277 svdata->mark = lp->start;
4279 if (text->first_line_start_index == lp->start.index)
4281 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4285 text->first_onscreen_ver_pixel = svdata->pixel_height;
4286 text->first_cut_pixels = 0;
4289 text->vadj->value = text->first_onscreen_ver_pixel;
4292 svdata->pixel_height += LINE_HEIGHT (*lp);
4298 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
4300 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4303 if (svdata->pixel_height <= (gint) text->vadj->value &&
4304 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4306 svdata->mark = lp->start;
4308 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4309 text->first_onscreen_ver_pixel = svdata->pixel_height;
4310 text->first_line_start_index = lp->start.index;
4316 svdata->pixel_height += LINE_HEIGHT (*lp);
4324 static GtkPropertyMark
4325 set_vertical_scroll (GtkText* text)
4327 GtkPropertyMark mark = find_mark (text, 0);
4328 SetVerticalScrollData data;
4332 data.pixel_height = 0;
4333 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4335 text->vadj->upper = data.pixel_height;
4336 orig_value = (gint) text->vadj->value;
4338 gdk_window_get_size (text->text_area, NULL, &height);
4340 text->vadj->step_increment = MIN (text->vadj->upper, SCROLL_PIXELS);
4341 text->vadj->page_increment = MIN (text->vadj->upper, height - KEY_SCROLL_PIXELS);
4342 text->vadj->page_size = MIN (text->vadj->upper, height);
4343 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4344 text->vadj->value = MAX (text->vadj->value, 0.0);
4346 text->last_ver_value = (gint)text->vadj->value;
4348 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4350 if (text->vadj->value != orig_value)
4352 /* We got clipped, and don't really know which line to put first. */
4353 data.pixel_height = 0;
4354 data.last_didnt_wrap = TRUE;
4356 line_params_iterate (text, &mark, NULL,
4358 set_vertical_scroll_find_iterator);
4365 scroll_int (GtkText* text, gint diff)
4369 text->vadj->value += diff;
4371 upper = text->vadj->upper - text->vadj->page_size;
4372 text->vadj->value = MIN (text->vadj->value, upper);
4373 text->vadj->value = MAX (text->vadj->value, 0.0);
4375 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4379 process_exposes (GtkText *text)
4383 /* Make sure graphics expose events are processed before scrolling
4386 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4388 gtk_widget_send_expose (GTK_WIDGET (text), event);
4389 if (event->expose.count == 0)
4391 gdk_event_free (event);
4394 gdk_event_free (event);
4398 static gint last_visible_line_height (GtkText* text)
4400 GList *cache = text->line_start_cache;
4403 gdk_window_get_size (text->text_area, NULL, &height);
4405 for (; cache->next; cache = cache->next)
4406 if (pixel_height_of(text, cache->next) > height)
4410 return pixel_height_of(text, cache) - 1;
4415 static gint first_visible_line_height (GtkText* text)
4417 if (text->first_cut_pixels)
4418 return pixel_height_of(text, text->line_start_cache) + 1;
4424 scroll_down (GtkText* text, gint diff0)
4430 text->first_onscreen_ver_pixel += diff0;
4434 g_assert (text->line_start_cache);
4436 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4438 text->first_cut_pixels += 1;
4442 text->first_cut_pixels = 0;
4444 text->line_start_cache = text->line_start_cache->next;
4446 text->first_line_start_index =
4447 CACHE_DATA(text->line_start_cache).start.index;
4449 if (!text->line_start_cache->next)
4450 fetch_lines_forward (text, 1);
4456 gdk_window_get_size (text->text_area, &width, &height);
4457 if (height > real_diff)
4458 gdk_draw_pixmap (text->text_area,
4466 height - real_diff);
4469 rect.y = MAX (0, height - real_diff);
4471 rect.height = MIN (height, real_diff);
4473 expose_text (text, &rect, FALSE);
4474 gtk_text_draw_focus ( (GtkWidget *) text);
4476 if (text->current_line)
4480 text->cursor_pos_y -= real_diff;
4481 cursor_min = drawn_cursor_min(text);
4484 find_mouse_cursor (text, text->cursor_pos_x,
4485 first_visible_line_height (text));
4488 if (height > real_diff)
4489 process_exposes (text);
4493 scroll_up (GtkText* text, gint diff0)
4499 text->first_onscreen_ver_pixel += diff0;
4503 g_assert (text->line_start_cache);
4505 if (text->first_cut_pixels > 0)
4507 text->first_cut_pixels -= 1;
4511 if (!text->line_start_cache->prev)
4512 fetch_lines_backward (text);
4514 text->line_start_cache = text->line_start_cache->prev;
4516 text->first_line_start_index =
4517 CACHE_DATA(text->line_start_cache).start.index;
4519 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4525 gdk_window_get_size (text->text_area, &width, &height);
4526 if (height > real_diff)
4527 gdk_draw_pixmap (text->text_area,
4535 height - real_diff);
4540 rect.height = MIN (height, real_diff);
4542 expose_text (text, &rect, FALSE);
4543 gtk_text_draw_focus ( (GtkWidget *) text);
4545 if (text->current_line)
4550 text->cursor_pos_y += real_diff;
4551 cursor_max = drawn_cursor_max(text);
4552 gdk_window_get_size (text->text_area, NULL, &height);
4554 if (cursor_max >= height)
4555 find_mouse_cursor (text, text->cursor_pos_x,
4556 last_visible_line_height (text));
4559 if (height > real_diff)
4560 process_exposes (text);
4563 /**********************************************************************/
4565 /**********************************************************************/
4567 /* Assumes mark starts a line. Calculates the height, width, and
4568 * displayable character count of a single DISPLAYABLE line. That
4569 * means that in line-wrap mode, this does may not compute the
4570 * properties of an entire line. */
4572 find_line_params (GtkText* text,
4573 const GtkPropertyMark* mark,
4574 const PrevTabCont *tab_cont,
4575 PrevTabCont *next_cont)
4578 TabStopMark tab_mark = tab_cont->tab_start;
4579 guint max_display_pixels;
4584 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4585 max_display_pixels -= LINE_WRAP_ROOM;
4588 lp.tab_cont = *tab_cont;
4591 lp.pixel_width = tab_cont->pixel_offset;
4592 lp.displayable_chars = 0;
4594 lp.font_descent = 0;
4596 init_tab_cont (text, next_cont);
4598 while (!LAST_INDEX(text, lp.end))
4600 g_assert (lp.end.property);
4602 ch = GTK_TEXT_INDEX (text, lp.end.index);
4603 font = MARK_CURRENT_FONT (text, &lp.end);
4605 if (ch == LINE_DELIM)
4607 /* Newline doesn't count in computation of line height, even
4608 * if its in a bigger font than the rest of the line. Unless,
4609 * of course, there are no other characters. */
4611 if (!lp.font_ascent && !lp.font_descent)
4613 lp.font_ascent = font->ascent;
4614 lp.font_descent = font->descent;
4617 lp.tab_cont_next = *next_cont;
4622 ch_width = find_char_width (text, &lp.end, &tab_mark);
4624 if ((ch_width + lp.pixel_width > max_display_pixels) &&
4625 (lp.end.index > lp.start.index))
4629 if (text->line_wrap)
4631 next_cont->tab_start = tab_mark;
4632 next_cont->pixel_offset = 0;
4636 /* Here's the tough case, a tab is wrapping. */
4637 gint pixels_avail = max_display_pixels - lp.pixel_width;
4638 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
4639 gint spaces_avail = pixels_avail / space_width;
4641 if (spaces_avail == 0)
4643 decrement_mark (&lp.end);
4647 advance_tab_mark (text, &next_cont->tab_start, '\t');
4648 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4650 lp.displayable_chars += 1;
4655 if (text->word_wrap)
4657 GtkPropertyMark saved_mark = lp.end;
4658 guint saved_characters = lp.displayable_chars;
4660 lp.displayable_chars += 1;
4662 if (text->use_wchar)
4664 while (!gdk_iswspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4665 (lp.end.index > lp.start.index))
4667 decrement_mark (&lp.end);
4668 lp.displayable_chars -= 1;
4673 while (!isspace(GTK_TEXT_INDEX (text, lp.end.index)) &&
4674 (lp.end.index > lp.start.index))
4676 decrement_mark (&lp.end);
4677 lp.displayable_chars -= 1;
4681 /* If whole line is one word, revert to char wrapping */
4682 if (lp.end.index == lp.start.index)
4684 lp.end = saved_mark;
4685 lp.displayable_chars = saved_characters;
4686 decrement_mark (&lp.end);
4691 /* Don't include this character, it will wrap. */
4692 decrement_mark (&lp.end);
4696 lp.tab_cont_next = *next_cont;
4703 lp.displayable_chars += 1;
4706 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4707 lp.font_descent = MAX (font->descent, lp.font_descent);
4708 lp.pixel_width += ch_width;
4710 advance_mark(&lp.end);
4711 advance_tab_mark (text, &tab_mark, ch);
4714 if (LAST_INDEX(text, lp.start))
4716 /* Special case, empty last line. */
4717 font = MARK_CURRENT_FONT (text, &lp.end);
4719 lp.font_ascent = font->ascent;
4720 lp.font_descent = font->descent;
4723 lp.tab_cont_next = *next_cont;
4729 expand_scratch_buffer (GtkText* text, guint len)
4731 if (len >= text->scratch_buffer_len)
4735 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4737 if (text->use_wchar)
4739 if (text->scratch_buffer.wc)
4740 text->scratch_buffer.wc = g_new (GdkWChar, i);
4742 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
4743 i*sizeof (GdkWChar));
4747 if (text->scratch_buffer.ch)
4748 text->scratch_buffer.ch = g_new (guchar, i);
4750 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
4753 text->scratch_buffer_len = i;
4757 /* Side effect: modifies text->gc
4760 draw_bg_rect (GtkText* text, GtkPropertyMark *mark,
4761 gint x, gint y, gint width, gint height,
4762 gboolean already_cleared)
4764 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4766 if ((mark->index >= MIN(old_editable->selection_start_pos, old_editable->selection_end_pos) &&
4767 mark->index < MAX(old_editable->selection_start_pos, old_editable->selection_end_pos)))
4769 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
4770 old_editable->has_selection ?
4771 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
4773 NULL, GTK_WIDGET(text), "text",
4774 x, y, width, height);
4776 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
4777 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
4779 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
4781 gdk_draw_rectangle (text->text_area,
4783 TRUE, x, y, width, height);
4785 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4792 rect.height = height;
4794 clear_area (text, &rect);
4796 else if (!already_cleared)
4797 gdk_window_clear_area (text->text_area, x, y, width, height);
4801 draw_line (GtkText* text,
4802 gint pixel_start_height,
4805 GdkGCValues gc_values;
4808 guint running_offset = lp->tab_cont.pixel_offset;
4809 union { GdkWChar *wc; guchar *ch; } buffer;
4812 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4814 guint selection_start_pos = MIN (old_editable->selection_start_pos, old_editable->selection_end_pos);
4815 guint selection_end_pos = MAX (old_editable->selection_start_pos, old_editable->selection_end_pos);
4817 GtkPropertyMark mark = lp->start;
4818 TabStopMark tab_mark = lp->tab_cont.tab_start;
4819 gint pixel_height = pixel_start_height + lp->font_ascent;
4820 guint chars = lp->displayable_chars;
4822 /* First provide a contiguous segment of memory. This makes reading
4823 * the code below *much* easier, and only incurs the cost of copying
4824 * when the line being displayed spans the gap. */
4825 if (mark.index <= text->gap_position &&
4826 mark.index + chars > text->gap_position)
4828 expand_scratch_buffer (text, chars);
4830 if (text->use_wchar)
4832 for (i = 0; i < chars; i += 1)
4833 text->scratch_buffer.wc[i] = GTK_TEXT_INDEX(text, mark.index + i);
4834 buffer.wc = text->scratch_buffer.wc;
4838 for (i = 0; i < chars; i += 1)
4839 text->scratch_buffer.ch[i] = GTK_TEXT_INDEX(text, mark.index + i);
4840 buffer.ch = text->scratch_buffer.ch;
4845 if (text->use_wchar)
4847 if (mark.index >= text->gap_position)
4848 buffer.wc = text->text.wc + mark.index + text->gap_size;
4850 buffer.wc = text->text.wc + mark.index;
4854 if (mark.index >= text->gap_position)
4855 buffer.ch = text->text.ch + mark.index + text->gap_size;
4857 buffer.ch = text->text.ch + mark.index;
4862 if (running_offset > 0)
4864 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
4865 LINE_HEIGHT (*lp), TRUE);
4871 if ((text->use_wchar && buffer.wc[0] != '\t') ||
4872 (!text->use_wchar && buffer.ch[0] != '\t'))
4874 union { GdkWChar *wc; guchar *ch; } next_tab;
4879 if (text->use_wchar)
4880 for (i=0; i<chars; i++)
4882 if (buffer.wc[i] == '\t')
4884 next_tab.wc = buffer.wc + i;
4889 next_tab.ch = memchr (buffer.ch, '\t', chars);
4891 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4893 if (text->use_wchar)
4896 len = MIN (len, next_tab.wc - buffer.wc);
4901 len = MIN (len, next_tab.ch - buffer.ch);
4904 if (mark.index < selection_start_pos)
4905 len = MIN (len, selection_start_pos - mark.index);
4906 else if (mark.index < selection_end_pos)
4907 len = MIN (len, selection_end_pos - mark.index);
4909 font = MARK_CURRENT_FONT (text, &mark);
4910 if (font->type == GDK_FONT_FONT)
4912 gdk_gc_set_font (text->gc, font);
4913 gdk_gc_get_values (text->gc, &gc_values);
4914 if (text->use_wchar)
4915 pixel_width = gdk_text_width_wc (gc_values.font,
4918 pixel_width = gdk_text_width (gc_values.font,
4923 if (text->use_wchar)
4924 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
4926 pixel_width = gdk_text_width (font, buffer.ch, len);
4929 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4930 pixel_width, LINE_HEIGHT (*lp), TRUE);
4932 if ((mark.index >= selection_start_pos) &&
4933 (mark.index < selection_end_pos))
4935 if (old_editable->has_selection)
4936 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_SELECTED];
4938 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_ACTIVE];
4942 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
4946 if (text->use_wchar)
4947 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
4954 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
4961 running_offset += pixel_width;
4963 advance_tab_mark_n (text, &tab_mark, len);
4967 gint pixels_remaining;
4973 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
4974 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
4976 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4978 spaces_avail = pixels_remaining / space_width;
4979 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
4981 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4982 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
4984 running_offset += tab_mark.to_next_tab *
4985 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4987 advance_tab_mark (text, &tab_mark, '\t');
4990 advance_mark_n (&mark, len);
4991 if (text->use_wchar)
5000 draw_line_wrap (GtkText* text, guint height /* baseline height */)
5007 if (text->line_wrap)
5009 bitmap = text->line_wrap_bitmap;
5010 bitmap_width = line_wrap_width;
5011 bitmap_height = line_wrap_height;
5015 bitmap = text->line_arrow_bitmap;
5016 bitmap_width = line_arrow_width;
5017 bitmap_height = line_arrow_height;
5020 gdk_window_get_size (text->text_area, &width, NULL);
5021 width -= LINE_WRAP_ROOM;
5023 gdk_gc_set_stipple (text->gc,
5026 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
5028 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5030 gdk_gc_set_ts_origin (text->gc,
5032 height - bitmap_height - 1);
5034 gdk_draw_rectangle (text->text_area,
5038 height - bitmap_height - 1 /* one pixel above the baseline. */,
5042 gdk_gc_set_ts_origin (text->gc, 0, 0);
5044 gdk_gc_set_fill (text->gc, GDK_SOLID);
5048 undraw_cursor (GtkText* text, gint absolute)
5050 GtkOldEditable *old_editable = (GtkOldEditable *) text;
5052 TDEBUG (("in undraw_cursor\n"));
5055 text->cursor_drawn_level = 0;
5057 if ((text->cursor_drawn_level ++ == 0) &&
5058 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5059 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5063 g_assert(text->cursor_mark.property);
5065 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5067 draw_bg_rect (text, &text->cursor_mark,
5069 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5070 1, font->ascent + 1, FALSE);
5072 if (text->cursor_char)
5074 if (font->type == GDK_FONT_FONT)
5075 gdk_gc_set_font (text->gc, font);
5077 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5079 gdk_draw_text_wc (text->text_area, font,
5082 text->cursor_pos_y - text->cursor_char_offset,
5090 drawn_cursor_min (GtkText* text)
5094 g_assert(text->cursor_mark.property);
5096 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5098 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5102 drawn_cursor_max (GtkText* text)
5106 g_assert(text->cursor_mark.property);
5108 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5110 return text->cursor_pos_y - text->cursor_char_offset;
5114 draw_cursor (GtkText* text, gint absolute)
5116 GtkOldEditable *old_editable = (GtkOldEditable *)text;
5118 TDEBUG (("in draw_cursor\n"));
5121 text->cursor_drawn_level = 1;
5123 if ((--text->cursor_drawn_level == 0) &&
5124 old_editable->editable &&
5125 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5126 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5130 g_assert (text->cursor_mark.property);
5132 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5134 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5136 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
5137 text->cursor_pos_y - text->cursor_char_offset,
5139 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5144 create_bg_gc (GtkText *text)
5148 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5149 values.fill = GDK_TILED;
5151 return gdk_gc_new_with_values (text->text_area, &values,
5152 GDK_GC_FILL | GDK_GC_TILE);
5156 clear_area (GtkText *text, GdkRectangle *area)
5158 GtkWidget *widget = GTK_WIDGET (text);
5164 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5166 gdk_gc_set_ts_origin (text->bg_gc,
5167 (- text->first_onscreen_hor_pixel) % width,
5168 (- text->first_onscreen_ver_pixel) % height);
5170 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5171 area->x, area->y, area->width, area->height);
5174 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5178 expose_text (GtkText* text, GdkRectangle *area, gboolean cursor)
5180 GList *cache = text->line_start_cache;
5181 gint pixels = - text->first_cut_pixels;
5182 gint min_y = MAX (0, area->y);
5183 gint max_y = MAX (0, area->y + area->height);
5186 gdk_window_get_size (text->text_area, NULL, &height);
5187 max_y = MIN (max_y, height);
5189 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5191 clear_area (text, area);
5193 for (; pixels < height; cache = cache->next)
5195 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5197 draw_line (text, pixels, &CACHE_DATA(cache));
5199 if (CACHE_DATA(cache).wraps)
5200 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5203 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5205 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5206 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5208 /* We undraw and draw the cursor here to get the drawn
5209 * level right ... FIXME - maybe the second parameter
5210 * of draw_cursor should work differently
5212 undraw_cursor (text, FALSE);
5213 draw_cursor (text, FALSE);
5217 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5221 fetch_lines_forward (text, 1);
5230 gtk_text_update_text (GtkOldEditable *old_editable,
5234 GtkText *text = GTK_TEXT (old_editable);
5236 GList *cache = text->line_start_cache;
5237 gint pixels = - text->first_cut_pixels;
5243 end_pos = TEXT_LENGTH (text);
5245 if (end_pos < start_pos)
5248 gdk_window_get_size (text->text_area, &width, &height);
5254 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5256 for (; pixels < height; cache = cache->next)
5258 if (CACHE_DATA(cache).start.index < end_pos)
5260 if (CACHE_DATA(cache).end.index >= start_pos)
5263 area.y = MAX(0,pixels);
5264 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5270 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5274 fetch_lines_forward (text, 1);
5282 expose_text (text, &area, TRUE);
5286 recompute_geometry (GtkText* text)
5288 GtkPropertyMark mark, start_mark;
5295 mark = start_mark = set_vertical_scroll (text);
5297 /* We need a real start of a line when calling fetch_lines().
5298 * not the start of a wrapped line.
5300 while (mark.index > 0 &&
5301 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5302 decrement_mark (&mark);
5304 gdk_window_get_size (text->text_area, &width, &height);
5306 /* Fetch an entire line, to make sure that we get all the text
5307 * we backed over above, in addition to enough text to fill up
5308 * the space vertically
5311 new_lines = fetch_lines (text,
5317 mark = CACHE_DATA (g_list_last (new_lines)).end;
5318 if (!LAST_INDEX (text, mark))
5320 advance_mark (&mark);
5322 new_lines = g_list_concat (new_lines,
5327 height + text->first_cut_pixels));
5330 /* Now work forward to the actual first onscreen line */
5332 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5333 new_lines = new_lines->next;
5335 text->line_start_cache = new_lines;
5337 find_cursor (text, TRUE);
5340 /**********************************************************************/
5342 /**********************************************************************/
5345 gtk_text_set_selection (GtkOldEditable *old_editable,
5349 GtkText *text = GTK_TEXT (old_editable);
5351 guint start1, end1, start2, end2;
5354 end = TEXT_LENGTH (text);
5356 start1 = MIN(start,end);
5357 end1 = MAX(start,end);
5358 start2 = MIN(old_editable->selection_start_pos, old_editable->selection_end_pos);
5359 end2 = MAX(old_editable->selection_start_pos, old_editable->selection_end_pos);
5361 if (start2 < start1)
5365 tmp = start1; start1 = start2; start2 = tmp;
5366 tmp = end1; end1 = end2; end2 = tmp;
5369 undraw_cursor (text, FALSE);
5370 old_editable->selection_start_pos = start;
5371 old_editable->selection_end_pos = end;
5372 draw_cursor (text, FALSE);
5374 /* Expose only what changed */
5376 if (start1 < start2)
5377 gtk_text_update_text (old_editable, start1, MIN(end1, start2));
5380 gtk_text_update_text (old_editable, MAX(end1, start2), end2);
5381 else if (end2 < end1)
5382 gtk_text_update_text (old_editable, end2, end1);
5386 /**********************************************************************/
5388 /**********************************************************************/
5390 #ifdef DEBUG_GTK_TEXT
5392 gtk_text_show_cache_line (GtkText *text, GList *cache,
5393 const char* what, const char* func, gint line)
5395 LineParams *lp = &CACHE_DATA(cache);
5398 if (cache == text->line_start_cache)
5399 g_message ("Line Start Cache: ");
5401 if (cache == text->current_line)
5402 g_message("Current Line: ");
5404 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
5412 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
5413 g_message ("%c", GTK_TEXT_INDEX (text, i));
5419 gtk_text_show_cache (GtkText *text, const char* func, gint line)
5421 GList *l = text->line_start_cache;
5427 /* back up to the absolute beginning of the line cache */
5431 g_message ("*** line cache ***\n");
5432 for (; l; l = l->next)
5433 gtk_text_show_cache_line (text, l, "all", func, line);
5437 gtk_text_assert_mark (GtkText *text,
5438 GtkPropertyMark *mark,
5439 GtkPropertyMark *before,
5440 GtkPropertyMark *after,
5445 GtkPropertyMark correct_mark = find_mark (text, mark->index);
5447 if (mark->offset != correct_mark.offset ||
5448 mark->property != correct_mark.property)
5449 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
5453 gtk_text_assert (GtkText *text,
5457 GList* cache = text->line_start_cache;
5458 GtkPropertyMark* before_mark = NULL;
5459 GtkPropertyMark* after_mark = NULL;
5461 gtk_text_show_props (text, msg, line);
5463 for (; cache->prev; cache = cache->prev)
5466 g_message ("*** line markers ***\n");
5468 for (; cache; cache = cache->next)
5470 after_mark = &CACHE_DATA(cache).end;
5471 gtk_text_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
5472 before_mark = &CACHE_DATA(cache).start;
5475 after_mark = &CACHE_DATA(cache->next).start;
5479 gtk_text_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
5480 before_mark = &CACHE_DATA(cache).end;
5485 gtk_text_show_adj (GtkText *text,
5491 g_message ("*** adjustment ***\n");
5493 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
5500 adj->step_increment,
5501 adj->page_increment,
5506 gtk_text_show_props (GtkText *text,
5510 GList* props = text->text_properties;
5513 g_message ("%s:%d: ", msg, line);
5515 for (; props; props = props->next)
5517 TextProperty *p = (TextProperty*)props->data;
5519 proplen += p->length;
5521 g_message ("[%d,%p,", p->length, p);
5522 if (p->flags & PROPERTY_FONT)
5523 g_message ("%p,", p->font);
5526 if (p->flags & PROPERTY_FOREGROUND)
5527 g_message ("%ld, ", p->fore_color.pixel);
5530 if (p->flags & PROPERTY_BACKGROUND)
5531 g_message ("%ld] ", p->back_color.pixel);
5538 if (proplen - 1 != TEXT_LENGTH(text))
5539 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);