1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #undef GDK_DISABLE_DEPRECATED
28 #undef GTK_DISABLE_DEPRECATED
33 #include "gdk/gdkkeysyms.h"
34 #include "gdk/gdki18n.h"
36 #include "gtkmarshalers.h"
37 #include "gtkselection.h"
38 #include "gtksignal.h"
40 #include "gtkobject.h"
41 #define GTK_ENABLE_BROKEN
43 #include "line-wrap.xbm"
44 #include "line-arrow.xbm"
45 #include "gtkprivate.h"
50 #define INITIAL_BUFFER_SIZE 1024
51 #define INITIAL_LINE_CACHE_SIZE 256
52 #define MIN_GAP_SIZE 256
53 #define LINE_DELIM '\n'
54 #define MIN_TEXT_WIDTH_LINES 20
55 #define MIN_TEXT_HEIGHT_LINES 10
56 #define TEXT_BORDER_ROOM 1
57 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
58 #define DEFAULT_TAB_STOP_WIDTH 4
59 #define SCROLL_PIXELS 5
60 #define KEY_SCROLL_PIXELS 10
61 #define SCROLL_TIME 100
62 #define FREEZE_LENGTH 1024
63 /* Freeze text when inserting or deleting more than this many characters */
65 #define SET_PROPERTY_MARK(m, p, o) do { \
66 (m)->property = (p); \
69 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
70 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
71 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
72 (mark)->property->prev->data \
74 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
75 #define MARK_LIST_PTR(mark) ((mark)->property)
76 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
77 #define MARK_OFFSET(mark) ((mark)->offset)
78 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
81 #define MARK_CURRENT_FONT(text, mark) \
82 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
83 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
84 gtk_style_get_font (GTK_WIDGET (text)->style))
85 #define MARK_CURRENT_FORE(text, mark) \
86 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
87 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
88 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
89 #define MARK_CURRENT_BACK(text, mark) \
90 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
91 &MARK_CURRENT_PROPERTY(mark)->back_color : \
92 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
93 #define MARK_CURRENT_TEXT_FONT(text, mark) \
94 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
95 MARK_CURRENT_PROPERTY(mark)->font : \
98 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
99 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
100 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
101 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
102 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
103 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
104 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
105 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
115 typedef struct _TextProperty TextProperty;
116 typedef struct _TabStopMark TabStopMark;
117 typedef struct _PrevTabCont PrevTabCont;
118 typedef struct _FetchLinesData FetchLinesData;
119 typedef struct _LineParams LineParams;
120 typedef struct _SetVerticalScrollData SetVerticalScrollData;
122 typedef gint (*LineIteratorFunction) (GtkText* text, LineParams* lp, void* data);
130 struct _SetVerticalScrollData {
132 gint last_didnt_wrap;
133 gint last_line_start;
134 GtkPropertyMark mark;
139 /* The actual font. */
143 gint16 char_widths[256];
147 PROPERTY_FONT = 1 << 0,
148 PROPERTY_FOREGROUND = 1 << 1,
149 PROPERTY_BACKGROUND = 1 << 2
157 /* Background Color. */
160 /* Foreground Color. */
163 /* Show which properties are set */
164 TextPropertyFlags flags;
166 /* Length of this property. */
172 GList* tab_stops; /* Index into list containing the next tab position. If
173 * NULL, using default widths. */
180 TabStopMark tab_start;
183 struct _FetchLinesData
196 guint displayable_chars;
199 PrevTabCont tab_cont;
200 PrevTabCont tab_cont_next;
202 GtkPropertyMark start;
207 static void gtk_text_class_init (GtkTextClass *klass);
208 static void gtk_text_set_property (GObject *object,
212 static void gtk_text_get_property (GObject *object,
216 static void gtk_text_editable_init (GtkEditableClass *iface);
217 static void gtk_text_init (GtkText *text);
218 static void gtk_text_destroy (GtkObject *object);
219 static void gtk_text_finalize (GObject *object);
220 static void gtk_text_realize (GtkWidget *widget);
221 static void gtk_text_unrealize (GtkWidget *widget);
222 static void gtk_text_style_set (GtkWidget *widget,
223 GtkStyle *previous_style);
224 static void gtk_text_state_changed (GtkWidget *widget,
225 GtkStateType previous_state);
226 static void gtk_text_draw_focus (GtkWidget *widget);
227 static void gtk_text_size_request (GtkWidget *widget,
228 GtkRequisition *requisition);
229 static void gtk_text_size_allocate (GtkWidget *widget,
230 GtkAllocation *allocation);
231 static void gtk_text_adjustment (GtkAdjustment *adjustment,
233 static void gtk_text_insert_text (GtkEditable *editable,
234 const gchar *new_text,
235 gint new_text_length,
237 static void gtk_text_delete_text (GtkEditable *editable,
240 static void gtk_text_update_text (GtkOldEditable *old_editable,
243 static gchar *gtk_text_get_chars (GtkOldEditable *old_editable,
246 static void gtk_text_set_selection (GtkOldEditable *old_editable,
249 static void gtk_text_real_set_editable (GtkOldEditable *old_editable,
250 gboolean is_editable);
252 static void gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
256 static gint gtk_text_expose (GtkWidget *widget,
257 GdkEventExpose *event);
258 static gint gtk_text_button_press (GtkWidget *widget,
259 GdkEventButton *event);
260 static gint gtk_text_button_release (GtkWidget *widget,
261 GdkEventButton *event);
262 static gint gtk_text_motion_notify (GtkWidget *widget,
263 GdkEventMotion *event);
264 static gint gtk_text_key_press (GtkWidget *widget,
267 static void move_gap (GtkText* text, guint index);
268 static void make_forward_space (GtkText* text, guint len);
270 /* Property management */
271 static GtkTextFont* get_text_font (GdkFont* gfont);
272 static void text_font_unref (GtkTextFont *text_font);
274 static void insert_text_property (GtkText* text, GdkFont* font,
275 const GdkColor *fore, const GdkColor* back, guint len);
276 static TextProperty* new_text_property (GtkText *text, GdkFont* font,
277 const GdkColor* fore, const GdkColor* back, guint length);
278 static void destroy_text_property (TextProperty *prop);
279 static void init_properties (GtkText *text);
280 static void realize_property (GtkText *text, TextProperty *prop);
281 static void realize_properties (GtkText *text);
282 static void unrealize_property (GtkText *text, TextProperty *prop);
283 static void unrealize_properties (GtkText *text);
285 static void delete_text_property (GtkText* text, guint len);
287 static guint pixel_height_of (GtkText* text, GList* cache_line);
289 /* Property Movement and Size Computations */
290 static void advance_mark (GtkPropertyMark* mark);
291 static void decrement_mark (GtkPropertyMark* mark);
292 static void advance_mark_n (GtkPropertyMark* mark, gint n);
293 static void decrement_mark_n (GtkPropertyMark* mark, gint n);
294 static void move_mark_n (GtkPropertyMark* mark, gint n);
295 static GtkPropertyMark find_mark (GtkText* text, guint mark_position);
296 static GtkPropertyMark find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near);
297 static void find_line_containing_point (GtkText* text, guint point,
301 static void compute_lines_pixels (GtkText* text, guint char_count,
302 guint *lines, guint *pixels);
304 static gint total_line_height (GtkText* text,
307 static LineParams find_line_params (GtkText* text,
308 const GtkPropertyMark *mark,
309 const PrevTabCont *tab_cont,
310 PrevTabCont *next_cont);
311 static void recompute_geometry (GtkText* text);
312 static void insert_expose (GtkText* text, guint old_pixels, gint nchars, guint new_line_count);
313 static void delete_expose (GtkText* text,
317 static GdkGC *create_bg_gc (GtkText *text);
318 static void clear_area (GtkText *text, GdkRectangle *area);
319 static void draw_line (GtkText* text,
322 static void draw_line_wrap (GtkText* text,
324 static void draw_cursor (GtkText* text, gint absolute);
325 static void undraw_cursor (GtkText* text, gint absolute);
326 static gint drawn_cursor_min (GtkText* text);
327 static gint drawn_cursor_max (GtkText* text);
328 static void expose_text (GtkText* text, GdkRectangle *area, gboolean cursor);
330 /* Search and Placement. */
331 static void find_cursor (GtkText* text,
333 static void find_cursor_at_line (GtkText* text,
334 const LineParams* start_line,
336 static void find_mouse_cursor (GtkText* text, gint x, gint y);
339 static void adjust_adj (GtkText* text, GtkAdjustment* adj);
340 static void scroll_up (GtkText* text, gint diff);
341 static void scroll_down (GtkText* text, gint diff);
342 static void scroll_int (GtkText* text, gint diff);
344 static void process_exposes (GtkText *text);
346 /* Cache Management. */
347 static void free_cache (GtkText* text);
348 static GList* remove_cache_line (GtkText* text, GList* list);
351 static void move_cursor_buffer_ver (GtkText *text, int dir);
352 static void move_cursor_page_ver (GtkText *text, int dir);
353 static void move_cursor_ver (GtkText *text, int count);
354 static void move_cursor_hor (GtkText *text, int count);
356 /* Binding actions */
357 static void gtk_text_move_cursor (GtkOldEditable *old_editable,
360 static void gtk_text_move_word (GtkOldEditable *old_editable,
362 static void gtk_text_move_page (GtkOldEditable *old_editable,
365 static void gtk_text_move_to_row (GtkOldEditable *old_editable,
367 static void gtk_text_move_to_column (GtkOldEditable *old_editable,
369 static void gtk_text_kill_char (GtkOldEditable *old_editable,
371 static void gtk_text_kill_word (GtkOldEditable *old_editable,
373 static void gtk_text_kill_line (GtkOldEditable *old_editable,
377 static void gtk_text_move_forward_character (GtkText *text);
378 static void gtk_text_move_backward_character (GtkText *text);
379 static void gtk_text_move_forward_word (GtkText *text);
380 static void gtk_text_move_backward_word (GtkText *text);
381 static void gtk_text_move_beginning_of_line (GtkText *text);
382 static void gtk_text_move_end_of_line (GtkText *text);
383 static void gtk_text_move_next_line (GtkText *text);
384 static void gtk_text_move_previous_line (GtkText *text);
386 static void gtk_text_delete_forward_character (GtkText *text);
387 static void gtk_text_delete_backward_character (GtkText *text);
388 static void gtk_text_delete_forward_word (GtkText *text);
389 static void gtk_text_delete_backward_word (GtkText *text);
390 static void gtk_text_delete_line (GtkText *text);
391 static void gtk_text_delete_to_line_end (GtkText *text);
392 static void gtk_text_select_word (GtkText *text,
394 static void gtk_text_select_line (GtkText *text,
397 static void gtk_text_set_position (GtkOldEditable *old_editable,
400 /* #define DEBUG_GTK_TEXT */
402 #if defined(DEBUG_GTK_TEXT) && defined(__GNUC__)
403 /* Debugging utilities. */
404 static void gtk_text_assert_mark (GtkText *text,
405 GtkPropertyMark *mark,
406 GtkPropertyMark *before,
407 GtkPropertyMark *after,
412 static void gtk_text_assert (GtkText *text,
415 static void gtk_text_show_cache_line (GtkText *text, GList *cache,
416 const char* what, const char* func, gint line);
417 static void gtk_text_show_cache (GtkText *text, const char* func, gint line);
418 static void gtk_text_show_adj (GtkText *text,
423 static void gtk_text_show_props (GtkText* test,
427 #define TDEBUG(args) g_message args
428 #define TEXT_ASSERT(text) gtk_text_assert (text,__PRETTY_FUNCTION__,__LINE__)
429 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_text_assert_mark (text,mark, \
430 __PRETTY_FUNCTION__,msg,__LINE__)
431 #define TEXT_SHOW(text) gtk_text_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
432 #define TEXT_SHOW_LINE(text,line,msg) gtk_text_show_cache_line (text,line,msg,\
433 __PRETTY_FUNCTION__,__LINE__)
434 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_text_show_adj (text,adj,msg, \
435 __PRETTY_FUNCTION__,__LINE__)
438 #define TEXT_ASSERT(text)
439 #define TEXT_ASSERT_MARK(text,mark,msg)
440 #define TEXT_SHOW(text)
441 #define TEXT_SHOW_LINE(text,line,msg)
442 #define TEXT_SHOW_ADJ(text,adj,msg)
445 static GtkWidgetClass *parent_class = NULL;
447 /**********************************************************************/
449 /**********************************************************************/
452 gtk_text_get_type (void)
454 static GtkType text_type = 0;
458 static const GtkTypeInfo text_info =
462 sizeof (GtkTextClass),
463 (GtkClassInitFunc) gtk_text_class_init,
464 (GtkObjectInitFunc) gtk_text_init,
465 /* reserved_1 */ NULL,
466 /* reserved_2 */ NULL,
467 (GtkClassInitFunc) NULL,
470 static const GInterfaceInfo editable_info =
472 (GInterfaceInitFunc) gtk_text_editable_init, /* interface_init */
473 NULL, /* interface_finalize */
474 NULL /* interface_data */
478 text_type = gtk_type_unique (GTK_TYPE_OLD_EDITABLE, &text_info);
479 g_type_add_interface_static (text_type,
488 gtk_text_class_init (GtkTextClass *class)
490 GObjectClass *gobject_class;
491 GtkObjectClass *object_class;
492 GtkWidgetClass *widget_class;
493 GtkOldEditableClass *old_editable_class;
495 gobject_class = G_OBJECT_CLASS (class);
496 object_class = (GtkObjectClass*) class;
497 widget_class = (GtkWidgetClass*) class;
498 old_editable_class = (GtkOldEditableClass*) class;
499 parent_class = gtk_type_class (GTK_TYPE_OLD_EDITABLE);
501 gobject_class->finalize = gtk_text_finalize;
502 gobject_class->set_property = gtk_text_set_property;
503 gobject_class->get_property = gtk_text_get_property;
505 object_class->destroy = gtk_text_destroy;
507 widget_class->realize = gtk_text_realize;
508 widget_class->unrealize = gtk_text_unrealize;
509 widget_class->style_set = gtk_text_style_set;
510 widget_class->state_changed = gtk_text_state_changed;
511 widget_class->size_request = gtk_text_size_request;
512 widget_class->size_allocate = gtk_text_size_allocate;
513 widget_class->expose_event = gtk_text_expose;
514 widget_class->button_press_event = gtk_text_button_press;
515 widget_class->button_release_event = gtk_text_button_release;
516 widget_class->motion_notify_event = gtk_text_motion_notify;
517 widget_class->key_press_event = gtk_text_key_press;
519 old_editable_class->set_editable = gtk_text_real_set_editable;
521 old_editable_class->move_cursor = gtk_text_move_cursor;
522 old_editable_class->move_word = gtk_text_move_word;
523 old_editable_class->move_page = gtk_text_move_page;
524 old_editable_class->move_to_row = gtk_text_move_to_row;
525 old_editable_class->move_to_column = gtk_text_move_to_column;
527 old_editable_class->kill_char = gtk_text_kill_char;
528 old_editable_class->kill_word = gtk_text_kill_word;
529 old_editable_class->kill_line = gtk_text_kill_line;
531 old_editable_class->update_text = gtk_text_update_text;
532 old_editable_class->get_chars = gtk_text_get_chars;
533 old_editable_class->set_selection = gtk_text_set_selection;
534 old_editable_class->set_position = gtk_text_set_position;
536 class->set_scroll_adjustments = gtk_text_set_adjustments;
538 g_object_class_install_property (gobject_class,
540 g_param_spec_object ("hadjustment",
541 P_("Horizontal Adjustment"),
542 P_("Horizontal adjustment for the text widget"),
544 GTK_PARAM_READWRITE));
546 g_object_class_install_property (gobject_class,
548 g_param_spec_object ("vadjustment",
549 P_("Vertical Adjustment"),
550 P_("Vertical adjustment for the text widget"),
552 GTK_PARAM_READWRITE));
554 g_object_class_install_property (gobject_class,
556 g_param_spec_boolean ("line-wrap",
558 P_("Whether lines are wrapped at widget edges"),
560 GTK_PARAM_READWRITE));
562 g_object_class_install_property (gobject_class,
564 g_param_spec_boolean ("word-wrap",
566 P_("Whether words are wrapped at widget edges"),
568 GTK_PARAM_READWRITE));
570 widget_class->set_scroll_adjustments_signal =
571 gtk_signal_new (I_("set_scroll_adjustments"),
573 GTK_CLASS_TYPE (object_class),
574 GTK_SIGNAL_OFFSET (GtkTextClass, set_scroll_adjustments),
575 _gtk_marshal_VOID__OBJECT_OBJECT,
576 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
580 gtk_text_set_property (GObject *object,
587 text = GTK_TEXT (object);
591 case PROP_HADJUSTMENT:
592 gtk_text_set_adjustments (text,
593 g_value_get_object (value),
596 case PROP_VADJUSTMENT:
597 gtk_text_set_adjustments (text,
599 g_value_get_object (value));
602 gtk_text_set_line_wrap (text, g_value_get_boolean (value));
605 gtk_text_set_word_wrap (text, g_value_get_boolean (value));
608 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
614 gtk_text_get_property (GObject *object,
621 text = GTK_TEXT (object);
625 case PROP_HADJUSTMENT:
626 g_value_set_object (value, text->hadj);
628 case PROP_VADJUSTMENT:
629 g_value_set_object (value, text->vadj);
632 g_value_set_boolean (value, text->line_wrap);
635 g_value_set_boolean (value, text->word_wrap);
638 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
644 gtk_text_editable_init (GtkEditableClass *iface)
646 iface->insert_text = gtk_text_insert_text;
647 iface->delete_text = gtk_text_delete_text;
651 gtk_text_init (GtkText *text)
653 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
655 text->text_area = NULL;
660 text->line_wrap_bitmap = NULL;
661 text->line_arrow_bitmap = NULL;
663 text->use_wchar = FALSE;
664 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
665 text->text_len = INITIAL_BUFFER_SIZE;
667 text->scratch_buffer.ch = NULL;
668 text->scratch_buffer_len = 0;
670 text->freeze_count = 0;
672 text->default_tab_width = 4;
673 text->tab_stops = NULL;
675 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
676 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
678 text->line_start_cache = NULL;
679 text->first_cut_pixels = 0;
681 text->line_wrap = TRUE;
682 text->word_wrap = FALSE;
687 text->current_font = NULL;
689 init_properties (text);
691 GTK_OLD_EDITABLE (text)->editable = FALSE;
693 gtk_text_set_adjustments (text, NULL, NULL);
694 gtk_editable_set_position (GTK_EDITABLE (text), 0);
698 gtk_text_new (GtkAdjustment *hadj,
704 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
706 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
708 text = gtk_widget_new (GTK_TYPE_TEXT,
717 gtk_text_set_word_wrap (GtkText *text,
720 g_return_if_fail (GTK_IS_TEXT (text));
722 text->word_wrap = (word_wrap != FALSE);
724 if (GTK_WIDGET_REALIZED (text))
726 recompute_geometry (text);
727 gtk_widget_queue_draw (GTK_WIDGET (text));
730 g_object_notify (G_OBJECT (text), "word-wrap");
734 gtk_text_set_line_wrap (GtkText *text,
737 g_return_if_fail (GTK_IS_TEXT (text));
739 text->line_wrap = (line_wrap != FALSE);
741 if (GTK_WIDGET_REALIZED (text))
743 recompute_geometry (text);
744 gtk_widget_queue_draw (GTK_WIDGET (text));
747 g_object_notify (G_OBJECT (text), "line-wrap");
751 gtk_text_set_editable (GtkText *text,
752 gboolean is_editable)
754 g_return_if_fail (GTK_IS_TEXT (text));
756 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
760 gtk_text_real_set_editable (GtkOldEditable *old_editable,
761 gboolean is_editable)
765 g_return_if_fail (GTK_IS_TEXT (old_editable));
767 text = GTK_TEXT (old_editable);
769 old_editable->editable = (is_editable != FALSE);
772 draw_cursor (text, TRUE);
774 undraw_cursor (text, TRUE);
778 gtk_text_set_adjustments (GtkText *text,
782 g_return_if_fail (GTK_IS_TEXT (text));
784 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
786 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
788 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
790 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
792 if (text->hadj && (text->hadj != hadj))
794 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
795 gtk_object_unref (GTK_OBJECT (text->hadj));
798 if (text->vadj && (text->vadj != vadj))
800 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
801 gtk_object_unref (GTK_OBJECT (text->vadj));
804 g_object_freeze_notify (G_OBJECT (text));
805 if (text->hadj != hadj)
808 g_object_ref_sink (text->hadj);
810 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
811 (GtkSignalFunc) gtk_text_adjustment,
813 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
814 (GtkSignalFunc) gtk_text_adjustment,
816 gtk_signal_connect (GTK_OBJECT (text->hadj), "destroy",
817 (GtkSignalFunc) gtk_text_adjustment_destroyed,
819 gtk_text_adjustment (hadj, text);
821 g_object_notify (G_OBJECT (text), "hadjustment");
824 if (text->vadj != vadj)
827 g_object_ref_sink (text->vadj);
829 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
830 (GtkSignalFunc) gtk_text_adjustment,
832 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
833 (GtkSignalFunc) gtk_text_adjustment,
835 gtk_signal_connect (GTK_OBJECT (text->vadj), "destroy",
836 (GtkSignalFunc) gtk_text_adjustment_destroyed,
838 gtk_text_adjustment (vadj, text);
840 g_object_notify (G_OBJECT (text), "vadjustment");
842 g_object_thaw_notify (G_OBJECT (text));
846 gtk_text_set_point (GtkText *text,
849 g_return_if_fail (GTK_IS_TEXT (text));
850 g_return_if_fail (index <= TEXT_LENGTH (text));
852 text->point = find_mark (text, index);
856 gtk_text_get_point (GtkText *text)
858 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
860 return text->point.index;
864 gtk_text_get_length (GtkText *text)
866 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
868 return TEXT_LENGTH (text);
872 gtk_text_freeze (GtkText *text)
874 g_return_if_fail (GTK_IS_TEXT (text));
876 text->freeze_count++;
880 gtk_text_thaw (GtkText *text)
882 g_return_if_fail (GTK_IS_TEXT (text));
884 if (text->freeze_count)
885 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
887 recompute_geometry (text);
888 gtk_widget_queue_draw (GTK_WIDGET (text));
893 gtk_text_insert (GtkText *text,
895 const GdkColor *fore,
896 const GdkColor *back,
900 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
901 gboolean frozen = FALSE;
903 gint new_line_count = 1;
904 guint old_height = 0;
909 g_return_if_fail (GTK_IS_TEXT (text));
912 length = strlen (chars);
919 if (!text->freeze_count && (length > FREEZE_LENGTH))
921 gtk_text_freeze (text);
925 if (!text->freeze_count && (text->line_start_cache != NULL))
927 find_line_containing_point (text, text->point.index, TRUE);
928 old_height = total_line_height (text, text->current_line, 1);
931 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
933 GtkWidget *widget = GTK_WIDGET (text);
935 gtk_widget_ensure_style (widget);
936 if ((widget->style) &&
937 (gtk_style_get_font (widget->style)->type == GDK_FONT_FONTSET))
939 text->use_wchar = TRUE;
940 g_free (text->text.ch);
941 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
942 text->text_len = INITIAL_BUFFER_SIZE;
943 if (text->scratch_buffer.ch)
944 g_free (text->scratch_buffer.ch);
945 text->scratch_buffer.wc = NULL;
946 text->scratch_buffer_len = 0;
950 move_gap (text, text->point.index);
951 make_forward_space (text, length);
955 char *chars_nt = (char *)chars;
958 chars_nt = g_new (char, length+1);
959 memcpy (chars_nt, chars, length);
960 chars_nt[length] = 0;
962 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
964 if (chars_nt != chars)
972 memcpy(text->text.ch + text->gap_position, chars, length);
975 if (!text->freeze_count && (text->line_start_cache != NULL))
979 for (i=0; i<numwcs; i++)
980 if (text->text.wc[text->gap_position + i] == '\n')
985 for (i=0; i<numwcs; i++)
986 if (text->text.ch[text->gap_position + i] == '\n')
993 insert_text_property (text, font, fore, back, numwcs);
995 text->gap_size -= numwcs;
996 text->gap_position += numwcs;
998 if (text->point.index < text->first_line_start_index)
999 text->first_line_start_index += numwcs;
1000 if (text->point.index < old_editable->selection_start_pos)
1001 old_editable->selection_start_pos += numwcs;
1002 if (text->point.index < old_editable->selection_end_pos)
1003 old_editable->selection_end_pos += numwcs;
1004 /* We'll reset the cursor later anyways if we aren't frozen */
1005 if (text->point.index < text->cursor_mark.index)
1006 text->cursor_mark.index += numwcs;
1008 advance_mark_n (&text->point, numwcs);
1010 if (!text->freeze_count && (text->line_start_cache != NULL))
1011 insert_expose (text, old_height, numwcs, new_line_count);
1015 gtk_text_thaw (text);
1019 gtk_text_backward_delete (GtkText *text,
1022 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1024 if (nchars > text->point.index || nchars <= 0)
1027 gtk_text_set_point (text, text->point.index - nchars);
1029 return gtk_text_forward_delete (text, nchars);
1033 gtk_text_forward_delete (GtkText *text,
1036 guint old_lines = 0, old_height = 0;
1037 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
1038 gboolean frozen = FALSE;
1040 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1042 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1045 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1047 gtk_text_freeze (text);
1051 if (!text->freeze_count && text->line_start_cache != NULL)
1053 /* We need to undraw the cursor here, since we may later
1054 * delete the cursor's property
1056 undraw_cursor (text, FALSE);
1057 find_line_containing_point (text, text->point.index, TRUE);
1058 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1061 /* FIXME, or resizing after deleting will be odd */
1062 if (text->point.index < text->first_line_start_index)
1064 if (text->point.index + nchars >= text->first_line_start_index)
1066 text->first_line_start_index = text->point.index;
1067 while ((text->first_line_start_index > 0) &&
1068 (GTK_TEXT_INDEX (text, text->first_line_start_index - 1)
1070 text->first_line_start_index -= 1;
1074 text->first_line_start_index -= nchars;
1077 if (text->point.index < old_editable->selection_start_pos)
1078 old_editable->selection_start_pos -=
1079 MIN(nchars, old_editable->selection_start_pos - text->point.index);
1080 if (text->point.index < old_editable->selection_end_pos)
1081 old_editable->selection_end_pos -=
1082 MIN(nchars, old_editable->selection_end_pos - text->point.index);
1083 /* We'll reset the cursor later anyways if we aren't frozen */
1084 if (text->point.index < text->cursor_mark.index)
1085 move_mark_n (&text->cursor_mark,
1086 -MIN(nchars, text->cursor_mark.index - text->point.index));
1088 move_gap (text, text->point.index);
1090 text->gap_size += nchars;
1092 delete_text_property (text, nchars);
1094 if (!text->freeze_count && (text->line_start_cache != NULL))
1096 delete_expose (text, nchars, old_lines, old_height);
1097 draw_cursor (text, FALSE);
1101 gtk_text_thaw (text);
1107 gtk_text_set_position (GtkOldEditable *old_editable,
1110 GtkText *text = (GtkText *) old_editable;
1113 position = gtk_text_get_length (text);
1115 undraw_cursor (text, FALSE);
1116 text->cursor_mark = find_mark (text, position);
1117 find_cursor (text, TRUE);
1118 draw_cursor (text, FALSE);
1119 gtk_editable_select_region (GTK_EDITABLE (old_editable), 0, 0);
1123 gtk_text_get_chars (GtkOldEditable *old_editable,
1131 g_return_val_if_fail (GTK_IS_TEXT (old_editable), NULL);
1132 text = GTK_TEXT (old_editable);
1135 end_pos = TEXT_LENGTH (text);
1137 if ((start_pos < 0) ||
1138 (end_pos > TEXT_LENGTH (text)) ||
1139 (end_pos < start_pos))
1142 move_gap (text, TEXT_LENGTH (text));
1143 make_forward_space (text, 1);
1145 if (text->use_wchar)
1148 ch = text->text.wc[end_pos];
1149 text->text.wc[end_pos] = 0;
1150 retval = gdk_wcstombs (text->text.wc + start_pos);
1151 text->text.wc[end_pos] = ch;
1156 ch = text->text.ch[end_pos];
1157 text->text.ch[end_pos] = 0;
1158 retval = g_strdup (text->text.ch + start_pos);
1159 text->text.ch[end_pos] = ch;
1167 gtk_text_destroy (GtkObject *object)
1171 g_return_if_fail (GTK_IS_TEXT (object));
1173 text = GTK_TEXT (object);
1177 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1178 gtk_object_unref (GTK_OBJECT (text->hadj));
1183 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1184 gtk_object_unref (GTK_OBJECT (text->vadj));
1190 g_source_remove (text->timer);
1194 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1198 gtk_text_finalize (GObject *object)
1203 g_return_if_fail (GTK_IS_TEXT (object));
1205 text = GTK_TEXT (object);
1207 /* Clean up the internal structures */
1208 if (text->use_wchar)
1209 g_free (text->text.wc);
1211 g_free (text->text.ch);
1213 tmp_list = text->text_properties;
1216 destroy_text_property (tmp_list->data);
1217 tmp_list = tmp_list->next;
1220 if (text->current_font)
1221 text_font_unref (text->current_font);
1223 g_list_free (text->text_properties);
1225 if (text->use_wchar)
1227 if (text->scratch_buffer.wc)
1228 g_free (text->scratch_buffer.wc);
1232 if (text->scratch_buffer.ch)
1233 g_free (text->scratch_buffer.ch);
1236 g_list_free (text->tab_stops);
1238 G_OBJECT_CLASS (parent_class)->finalize (object);
1242 gtk_text_realize (GtkWidget *widget)
1245 GtkOldEditable *old_editable;
1246 GdkWindowAttr attributes;
1247 gint attributes_mask;
1249 g_return_if_fail (GTK_IS_TEXT (widget));
1251 text = GTK_TEXT (widget);
1252 old_editable = GTK_OLD_EDITABLE (widget);
1253 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1255 attributes.window_type = GDK_WINDOW_CHILD;
1256 attributes.x = widget->allocation.x;
1257 attributes.y = widget->allocation.y;
1258 attributes.width = widget->allocation.width;
1259 attributes.height = widget->allocation.height;
1260 attributes.wclass = GDK_INPUT_OUTPUT;
1261 attributes.visual = gtk_widget_get_visual (widget);
1262 attributes.colormap = gtk_widget_get_colormap (widget);
1263 attributes.event_mask = gtk_widget_get_events (widget);
1264 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1265 GDK_BUTTON_PRESS_MASK |
1266 GDK_BUTTON_RELEASE_MASK |
1267 GDK_BUTTON_MOTION_MASK |
1268 GDK_ENTER_NOTIFY_MASK |
1269 GDK_LEAVE_NOTIFY_MASK |
1270 GDK_KEY_PRESS_MASK);
1271 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1273 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1274 gdk_window_set_user_data (widget->window, text);
1276 attributes.x = (widget->style->xthickness + TEXT_BORDER_ROOM);
1277 attributes.y = (widget->style->ythickness + TEXT_BORDER_ROOM);
1278 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1279 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1281 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1282 attributes_mask |= GDK_WA_CURSOR;
1284 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1285 gdk_window_set_user_data (text->text_area, text);
1287 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1289 widget->style = gtk_style_attach (widget->style, widget->window);
1291 /* Can't call gtk_style_set_background here because it's handled specially */
1292 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1293 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1295 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1296 text->bg_gc = create_bg_gc (text);
1298 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1299 (gchar*) line_wrap_bits,
1303 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1304 (gchar*) line_arrow_bits,
1308 text->gc = gdk_gc_new (text->text_area);
1309 gdk_gc_set_exposures (text->gc, TRUE);
1310 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1312 realize_properties (text);
1313 gdk_window_show (text->text_area);
1314 init_properties (text);
1316 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1317 gtk_old_editable_claim_selection (old_editable, TRUE, GDK_CURRENT_TIME);
1319 recompute_geometry (text);
1323 gtk_text_style_set (GtkWidget *widget,
1324 GtkStyle *previous_style)
1326 GtkText *text = GTK_TEXT (widget);
1328 if (GTK_WIDGET_REALIZED (widget))
1330 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1331 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1335 gdk_gc_destroy (text->bg_gc);
1339 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1340 text->bg_gc = create_bg_gc (text);
1342 recompute_geometry (text);
1345 if (text->current_font)
1346 text_font_unref (text->current_font);
1347 text->current_font = get_text_font (gtk_style_get_font (widget->style));
1351 gtk_text_state_changed (GtkWidget *widget,
1352 GtkStateType previous_state)
1354 GtkText *text = GTK_TEXT (widget);
1356 if (GTK_WIDGET_REALIZED (widget))
1358 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1359 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1364 gtk_text_unrealize (GtkWidget *widget)
1368 g_return_if_fail (GTK_IS_TEXT (widget));
1370 text = GTK_TEXT (widget);
1372 gdk_window_set_user_data (text->text_area, NULL);
1373 gdk_window_destroy (text->text_area);
1374 text->text_area = NULL;
1376 gdk_gc_destroy (text->gc);
1381 gdk_gc_destroy (text->bg_gc);
1385 gdk_pixmap_unref (text->line_wrap_bitmap);
1386 gdk_pixmap_unref (text->line_arrow_bitmap);
1388 unrealize_properties (text);
1392 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1393 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1397 clear_focus_area (GtkText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1399 GtkWidget *widget = GTK_WIDGET (text);
1402 gint ythick = TEXT_BORDER_ROOM + widget->style->ythickness;
1403 gint xthick = TEXT_BORDER_ROOM + widget->style->xthickness;
1407 if (area_width == 0 || area_height == 0)
1410 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1412 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1414 gdk_gc_set_ts_origin (text->bg_gc,
1415 (- text->first_onscreen_hor_pixel + xthick) % width,
1416 (- text->first_onscreen_ver_pixel + ythick) % height);
1421 gc = widget->style->base_gc[widget->state];
1423 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1424 area_x, area_y, area_width, area_height);
1428 gtk_text_draw_focus (GtkWidget *widget)
1434 g_return_if_fail (GTK_IS_TEXT (widget));
1436 text = GTK_TEXT (widget);
1438 if (GTK_WIDGET_DRAWABLE (widget))
1440 gint ythick = widget->style->ythickness;
1441 gint xthick = widget->style->xthickness;
1442 gint xextra = TEXT_BORDER_ROOM;
1443 gint yextra = TEXT_BORDER_ROOM;
1445 TDEBUG (("in gtk_text_draw_focus\n"));
1449 width = widget->allocation.width;
1450 height = widget->allocation.height;
1452 if (GTK_WIDGET_HAS_FOCUS (widget))
1461 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1462 NULL, widget, "text",
1464 widget->allocation.width,
1465 widget->allocation.height);
1468 gtk_paint_shadow (widget->style, widget->window,
1469 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1470 NULL, widget, "text",
1471 x, y, width, height);
1475 width -= 2 * xthick;
1476 height -= 2 * ythick;
1479 clear_focus_area (text, x, y, width, yextra);
1481 clear_focus_area (text, x, y + yextra,
1482 xextra, y + height - 2 * yextra);
1484 clear_focus_area (text, x + width - xextra, y + yextra,
1485 xextra, height - 2 * ythick);
1487 clear_focus_area (text, x, x + height - yextra, width, yextra);
1491 TDEBUG (("in gtk_text_draw_focus (undrawable !!!)\n"));
1496 gtk_text_size_request (GtkWidget *widget,
1497 GtkRequisition *requisition)
1505 g_return_if_fail (GTK_IS_TEXT (widget));
1506 g_return_if_fail (requisition != NULL);
1508 xthickness = widget->style->xthickness + TEXT_BORDER_ROOM;
1509 ythickness = widget->style->ythickness + TEXT_BORDER_ROOM;
1511 font = gtk_style_get_font (widget->style);
1513 char_height = MIN_TEXT_HEIGHT_LINES * (font->ascent +
1516 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (font,
1517 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1521 requisition->width = char_width + xthickness * 2;
1522 requisition->height = char_height + ythickness * 2;
1526 gtk_text_size_allocate (GtkWidget *widget,
1527 GtkAllocation *allocation)
1531 g_return_if_fail (GTK_IS_TEXT (widget));
1532 g_return_if_fail (allocation != NULL);
1534 text = GTK_TEXT (widget);
1536 widget->allocation = *allocation;
1537 if (GTK_WIDGET_REALIZED (widget))
1539 gdk_window_move_resize (widget->window,
1540 allocation->x, allocation->y,
1541 allocation->width, allocation->height);
1543 gdk_window_move_resize (text->text_area,
1544 widget->style->xthickness + TEXT_BORDER_ROOM,
1545 widget->style->ythickness + TEXT_BORDER_ROOM,
1546 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->xthickness +
1547 (gint)TEXT_BORDER_ROOM) * 2),
1548 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->ythickness +
1549 (gint)TEXT_BORDER_ROOM) * 2));
1551 recompute_geometry (text);
1556 gtk_text_expose (GtkWidget *widget,
1557 GdkEventExpose *event)
1559 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1560 g_return_val_if_fail (event != NULL, FALSE);
1562 if (event->window == GTK_TEXT (widget)->text_area)
1564 TDEBUG (("in gtk_text_expose (expose)\n"));
1565 expose_text (GTK_TEXT (widget), &event->area, TRUE);
1567 else if (event->count == 0)
1569 TDEBUG (("in gtk_text_expose (focus)\n"));
1570 gtk_text_draw_focus (widget);
1577 gtk_text_scroll_timeout (gpointer data)
1581 GdkModifierType mask;
1583 text = GTK_TEXT (data);
1586 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1588 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1590 GdkEvent *event = gdk_event_new (GDK_MOTION_NOTIFY);
1592 event->motion.is_hint = 0;
1593 event->motion.x = x;
1594 event->motion.y = y;
1595 event->motion.state = mask;
1597 gtk_text_motion_notify (GTK_WIDGET (text), (GdkEventMotion *)event);
1599 gdk_event_free (event);
1606 gtk_text_button_press (GtkWidget *widget,
1607 GdkEventButton *event)
1610 GtkOldEditable *old_editable;
1612 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1613 g_return_val_if_fail (event != NULL, FALSE);
1615 text = GTK_TEXT (widget);
1616 old_editable = GTK_OLD_EDITABLE (widget);
1618 if (text->button && (event->button != text->button))
1621 text->button = event->button;
1623 if (!GTK_WIDGET_HAS_FOCUS (widget))
1624 gtk_widget_grab_focus (widget);
1626 if (event->button == 1)
1628 switch (event->type)
1630 case GDK_BUTTON_PRESS:
1631 gtk_grab_add (widget);
1633 undraw_cursor (text, FALSE);
1634 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1635 draw_cursor (text, FALSE);
1637 /* Set it now, so we display things right. We'll unset it
1638 * later if things don't work out */
1639 old_editable->has_selection = TRUE;
1640 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1641 text->cursor_mark.index,
1642 text->cursor_mark.index);
1646 case GDK_2BUTTON_PRESS:
1647 gtk_text_select_word (text, event->time);
1650 case GDK_3BUTTON_PRESS:
1651 gtk_text_select_line (text, event->time);
1658 else if (event->type == GDK_BUTTON_PRESS)
1660 if ((event->button == 2) && old_editable->editable)
1662 if (old_editable->selection_start_pos == old_editable->selection_end_pos ||
1663 old_editable->has_selection)
1665 undraw_cursor (text, FALSE);
1666 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1667 draw_cursor (text, FALSE);
1671 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1672 gdk_atom_intern_static_string ("UTF8_STRING"),
1677 GdkDisplay *display = gtk_widget_get_display (widget);
1679 gtk_grab_add (widget);
1681 undraw_cursor (text, FALSE);
1682 find_mouse_cursor (text, event->x, event->y);
1683 draw_cursor (text, FALSE);
1685 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1686 text->cursor_mark.index,
1687 text->cursor_mark.index);
1689 old_editable->has_selection = FALSE;
1690 if (gdk_selection_owner_get_for_display (display,
1691 GDK_SELECTION_PRIMARY) == widget->window)
1692 gtk_selection_owner_set_for_display (display,
1694 GDK_SELECTION_PRIMARY,
1703 gtk_text_button_release (GtkWidget *widget,
1704 GdkEventButton *event)
1707 GtkOldEditable *old_editable;
1708 GdkDisplay *display;
1710 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1711 g_return_val_if_fail (event != NULL, FALSE);
1713 text = GTK_TEXT (widget);
1715 gtk_grab_remove (widget);
1717 if (text->button != event->button)
1724 g_source_remove (text->timer);
1728 if (event->button == 1)
1730 text = GTK_TEXT (widget);
1731 old_editable = GTK_OLD_EDITABLE (widget);
1732 display = gtk_widget_get_display (widget);
1734 gtk_grab_remove (widget);
1736 old_editable->has_selection = FALSE;
1737 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1739 if (gtk_selection_owner_set_for_display (display,
1741 GDK_SELECTION_PRIMARY,
1743 old_editable->has_selection = TRUE;
1745 gtk_text_update_text (old_editable, old_editable->selection_start_pos,
1746 old_editable->selection_end_pos);
1750 if (gdk_selection_owner_get_for_display (display,
1751 GDK_SELECTION_PRIMARY) == widget->window)
1752 gtk_selection_owner_set_for_display (display,
1754 GDK_SELECTION_PRIMARY,
1758 else if (event->button == 3)
1760 gtk_grab_remove (widget);
1763 undraw_cursor (text, FALSE);
1764 find_cursor (text, TRUE);
1765 draw_cursor (text, FALSE);
1771 gtk_text_motion_notify (GtkWidget *widget,
1772 GdkEventMotion *event)
1777 GdkModifierType mask;
1779 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1780 g_return_val_if_fail (event != NULL, FALSE);
1782 text = GTK_TEXT (widget);
1786 mask = event->state;
1787 if (event->is_hint || (text->text_area != event->window))
1789 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1792 if ((text->button == 0) ||
1793 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1796 gdk_window_get_size (text->text_area, NULL, &height);
1798 if ((y < 0) || (y > height))
1800 if (text->timer == 0)
1802 text->timer = gdk_threads_add_timeout (SCROLL_TIME,
1803 gtk_text_scroll_timeout,
1807 scroll_int (text, y/2);
1809 scroll_int (text, (y - height)/2);
1815 undraw_cursor (GTK_TEXT (widget), FALSE);
1816 find_mouse_cursor (GTK_TEXT (widget), x, y);
1817 draw_cursor (GTK_TEXT (widget), FALSE);
1819 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1820 GTK_OLD_EDITABLE (text)->selection_start_pos,
1821 text->cursor_mark.index);
1827 gtk_text_insert_text (GtkEditable *editable,
1828 const gchar *new_text,
1829 gint new_text_length,
1832 GtkText *text = GTK_TEXT (editable);
1834 GdkColor *fore, *back;
1836 TextProperty *property;
1838 gtk_text_set_point (text, *position);
1840 property = MARK_CURRENT_PROPERTY (&text->point);
1841 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
1842 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
1843 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
1845 gtk_text_insert (text, font, fore, back, new_text, new_text_length);
1847 *position = text->point.index;
1851 gtk_text_delete_text (GtkEditable *editable,
1857 g_return_if_fail (start_pos >= 0);
1859 text = GTK_TEXT (editable);
1861 gtk_text_set_point (text, start_pos);
1863 end_pos = TEXT_LENGTH (text);
1865 if (end_pos > start_pos)
1866 gtk_text_forward_delete (text, end_pos - start_pos);
1870 gtk_text_key_press (GtkWidget *widget,
1874 GtkOldEditable *old_editable;
1879 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1880 g_return_val_if_fail (event != NULL, FALSE);
1882 text = GTK_TEXT (widget);
1883 old_editable = GTK_OLD_EDITABLE (widget);
1885 key = event->keyval;
1888 if ((GTK_OLD_EDITABLE(text)->editable == FALSE))
1890 switch (event->keyval)
1894 if (event->state & GDK_CONTROL_MASK)
1895 scroll_int (text, -text->vadj->value);
1901 if (event->state & GDK_CONTROL_MASK)
1902 scroll_int (text, +text->vadj->upper);
1906 case GDK_KP_Page_Up:
1907 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
1908 case GDK_KP_Page_Down:
1909 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
1911 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
1913 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
1916 if (event->state & GDK_CONTROL_MASK)
1917 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
1928 gint extend_selection;
1930 guint initial_pos = old_editable->current_pos;
1932 text->point = find_mark (text, text->cursor_mark.index);
1934 extend_selection = event->state & GDK_SHIFT_MASK;
1935 extend_start = FALSE;
1937 if (extend_selection)
1939 old_editable->has_selection = TRUE;
1941 if (old_editable->selection_start_pos == old_editable->selection_end_pos)
1943 old_editable->selection_start_pos = text->point.index;
1944 old_editable->selection_end_pos = text->point.index;
1947 extend_start = (text->point.index == old_editable->selection_start_pos);
1950 switch (event->keyval)
1954 if (event->state & GDK_CONTROL_MASK)
1955 move_cursor_buffer_ver (text, -1);
1957 gtk_text_move_beginning_of_line (text);
1961 if (event->state & GDK_CONTROL_MASK)
1962 move_cursor_buffer_ver (text, +1);
1964 gtk_text_move_end_of_line (text);
1966 case GDK_KP_Page_Up:
1967 case GDK_Page_Up: move_cursor_page_ver (text, -1); break;
1968 case GDK_KP_Page_Down:
1969 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
1970 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
1972 case GDK_Up: move_cursor_ver (text, -1); break;
1974 case GDK_Down: move_cursor_ver (text, +1); break;
1977 if (event->state & GDK_CONTROL_MASK)
1978 gtk_text_move_backward_word (text);
1980 move_cursor_hor (text, -1);
1984 if (event->state & GDK_CONTROL_MASK)
1985 gtk_text_move_forward_word (text);
1987 move_cursor_hor (text, +1);
1991 if (event->state & GDK_CONTROL_MASK)
1992 gtk_text_delete_backward_word (text);
1994 gtk_text_delete_backward_character (text);
1997 gtk_text_delete_line (text);
2001 if (event->state & GDK_SHIFT_MASK)
2003 extend_selection = FALSE;
2004 gtk_editable_paste_clipboard (GTK_EDITABLE (old_editable));
2006 else if (event->state & GDK_CONTROL_MASK)
2008 gtk_editable_copy_clipboard (GTK_EDITABLE (old_editable));
2012 /* gtk_toggle_insert(text) -- IMPLEMENT */
2017 if (event->state & GDK_CONTROL_MASK)
2018 gtk_text_delete_forward_word (text);
2019 else if (event->state & GDK_SHIFT_MASK)
2021 extend_selection = FALSE;
2022 gtk_editable_cut_clipboard (GTK_EDITABLE (old_editable));
2025 gtk_text_delete_forward_character (text);
2028 case GDK_ISO_Left_Tab:
2030 position = text->point.index;
2031 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\t", 1, &position);
2035 if (event->state & GDK_CONTROL_MASK)
2036 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2039 position = text->point.index;
2040 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\n", 1, &position);
2044 /* Don't insert literally */
2051 if (event->state & GDK_CONTROL_MASK)
2054 if ((key >= 'A') && (key <= 'Z'))
2060 gtk_text_move_beginning_of_line (text);
2063 gtk_text_move_backward_character (text);
2066 gtk_editable_copy_clipboard (text);
2069 gtk_text_delete_forward_character (text);
2072 gtk_text_move_end_of_line (text);
2075 gtk_text_move_forward_character (text);
2078 gtk_text_delete_backward_character (text);
2081 gtk_text_delete_to_line_end (text);
2084 gtk_text_move_next_line (text);
2087 gtk_text_move_previous_line (text);
2090 gtk_text_delete_line (text);
2093 gtk_editable_paste_clipboard (text);
2096 gtk_text_delete_backward_word (text);
2099 gtk_editable_cut_clipboard (text);
2107 else if (event->state & GDK_MOD1_MASK)
2110 if ((key >= 'A') && (key <= 'Z'))
2116 gtk_text_move_backward_word (text);
2119 gtk_text_delete_forward_word (text);
2122 gtk_text_move_forward_word (text);
2130 else if (event->length > 0)
2132 extend_selection = FALSE;
2134 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
2135 position = text->point.index;
2136 gtk_editable_insert_text (GTK_EDITABLE (old_editable), event->string, event->length, &position);
2142 if (return_val && (old_editable->current_pos != initial_pos))
2144 if (extend_selection)
2146 if (old_editable->current_pos < old_editable->selection_start_pos)
2147 gtk_text_set_selection (old_editable, old_editable->current_pos,
2148 old_editable->selection_end_pos);
2149 else if (old_editable->current_pos > old_editable->selection_end_pos)
2150 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2151 old_editable->current_pos);
2155 gtk_text_set_selection (old_editable, old_editable->current_pos,
2156 old_editable->selection_end_pos);
2158 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2159 old_editable->current_pos);
2163 gtk_text_set_selection (old_editable, 0, 0);
2165 gtk_old_editable_claim_selection (old_editable,
2166 old_editable->selection_start_pos != old_editable->selection_end_pos,
2175 gtk_text_adjustment (GtkAdjustment *adjustment,
2178 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2179 g_return_if_fail (GTK_IS_TEXT (text));
2181 /* Just ignore it if we haven't been size-allocated and realized yet */
2182 if (text->line_start_cache == NULL)
2185 if (adjustment == text->hadj)
2187 g_warning ("horizontal scrolling not implemented");
2191 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2195 undraw_cursor (text, FALSE);
2198 scroll_down (text, diff);
2199 else /* if (diff < 0) */
2200 scroll_up (text, diff);
2202 draw_cursor (text, FALSE);
2204 text->last_ver_value = adjustment->value;
2210 gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
2213 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2214 g_return_if_fail (GTK_IS_TEXT (text));
2216 if (adjustment == text->hadj)
2217 gtk_text_set_adjustments (text, NULL, text->vadj);
2218 if (adjustment == text->vadj)
2219 gtk_text_set_adjustments (text, text->hadj, NULL);
2223 static GtkPropertyMark
2224 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2226 GtkPropertyMark mark;
2228 mark = find_mark_near (text, point_position, near);
2230 while (mark.index > 0 &&
2231 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2232 decrement_mark (&mark);
2238 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2240 tab_cont->pixel_offset = 0;
2241 tab_cont->tab_start.tab_stops = text->tab_stops;
2242 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2244 if (!tab_cont->tab_start.to_next_tab)
2245 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2249 line_params_iterate (GtkText* text,
2250 const GtkPropertyMark* mark0,
2251 const PrevTabCont* tab_mark0,
2254 LineIteratorFunction iter)
2255 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2256 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2257 * for each line following MARK, iteration continues unless ITER_CALL
2260 GtkPropertyMark mark = *mark0;
2261 PrevTabCont tab_conts[2];
2262 LineParams *lp, lpbuf;
2263 gint tab_cont_index = 0;
2266 tab_conts[0] = *tab_mark0;
2268 init_tab_cont (text, tab_conts);
2273 lp = g_slice_new (LineParams);
2277 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2278 tab_conts + (tab_cont_index + 1) % 2);
2280 if ((*iter) (text, lp, data))
2283 if (LAST_INDEX (text, lp->end))
2287 advance_mark (&mark);
2288 tab_cont_index = (tab_cont_index + 1) % 2;
2293 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2295 FetchLinesData *fldata = (FetchLinesData*) data;
2297 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2299 switch (fldata->fl_type)
2301 case FetchLinesCount:
2302 if (!text->line_wrap || !lp->wraps)
2305 if (fldata->data >= fldata->data_max)
2309 case FetchLinesPixels:
2311 fldata->data += LINE_HEIGHT(*lp);
2313 if (fldata->data >= fldata->data_max)
2323 fetch_lines (GtkText* text,
2324 const GtkPropertyMark* mark0,
2325 const PrevTabCont* tab_cont0,
2329 FetchLinesData fl_data;
2331 fl_data.new_lines = NULL;
2333 fl_data.data_max = data;
2334 fl_data.fl_type = fl_type;
2336 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2338 return g_list_reverse (fl_data.new_lines);
2342 fetch_lines_backward (GtkText* text)
2344 GList *new_line_start;
2345 GtkPropertyMark mark;
2347 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2350 mark = find_this_line_start_mark (text,
2351 CACHE_DATA(text->line_start_cache).start.index - 1,
2352 &CACHE_DATA(text->line_start_cache).start);
2354 new_line_start = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2356 while (new_line_start->next)
2357 new_line_start = new_line_start->next;
2359 new_line_start->next = text->line_start_cache;
2360 text->line_start_cache->prev = new_line_start;
2364 fetch_lines_forward (GtkText* text, gint line_count)
2366 GtkPropertyMark mark;
2367 GList* line = text->line_start_cache;
2372 mark = CACHE_DATA(line).end;
2374 if (LAST_INDEX (text, mark))
2377 advance_mark(&mark);
2379 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2382 line->next->prev = line;
2385 /* Compute the number of lines, and vertical pixels for n characters
2386 * starting from the point
2389 compute_lines_pixels (GtkText* text, guint char_count,
2390 guint *lines, guint *pixels)
2392 GList *line = text->current_line;
2393 gint chars_left = char_count;
2398 /* If chars_left == 0, that means we're joining two lines in a
2399 * deletion, so add in the values for the next line as well
2401 for (; line && chars_left >= 0; line = line->next)
2403 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2405 if (line == text->current_line)
2406 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2408 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2410 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2414 chars_left = 0; /* force another loop */
2417 fetch_lines_forward (text, 1);
2422 total_line_height (GtkText* text, GList* line, gint line_count)
2426 for (; line && line_count > 0; line = line->next)
2428 height += LINE_HEIGHT(CACHE_DATA(line));
2430 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2434 fetch_lines_forward (text, line_count);
2441 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2443 if (old == text->line_start_cache)
2447 for (; old_line_count > 0; old_line_count -= 1)
2449 while (text->line_start_cache &&
2451 CACHE_DATA(text->line_start_cache).wraps)
2452 remove_cache_line(text, text->line_start_cache);
2454 remove_cache_line(text, text->line_start_cache);
2457 last = g_list_last (new);
2459 last->next = text->line_start_cache;
2461 if (text->line_start_cache)
2462 text->line_start_cache->prev = last;
2464 text->line_start_cache = new;
2470 g_assert (old->prev);
2474 for (; old_line_count > 0; old_line_count -= 1)
2476 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2477 old = remove_cache_line (text, old);
2479 old = remove_cache_line (text, old);
2485 last = g_list_last (new);
2495 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2497 GList* cache = text->current_line;
2500 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2503 for (; cache; cache = cache->next)
2505 GtkPropertyMark *start = &CACHE_DATA(cache).start;
2506 GtkPropertyMark *end = &CACHE_DATA(cache).end;
2508 start->index -= nchars;
2509 end->index -= nchars;
2511 if (LAST_INDEX (text, text->point) &&
2512 start->index == text->point.index)
2513 *start = text->point;
2514 else if (start->property == text->point.property)
2515 start->offset = start->index - (text->point.index - text->point.offset);
2517 if (LAST_INDEX (text, text->point) &&
2518 end->index == text->point.index)
2520 if (end->property == text->point.property)
2521 end->offset = end->index - (text->point.index - text->point.offset);
2523 /*TEXT_ASSERT_MARK(text, start, "start");*/
2524 /*TEXT_ASSERT_MARK(text, end, "end");*/
2529 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2531 GtkWidget *widget = GTK_WIDGET (text);
2534 guint new_pixels = 0;
2536 GList* new_line = NULL;
2539 text->cursor_virtual_x = 0;
2541 correct_cache_delete (text, nchars, old_lines);
2543 pixel_height = pixel_height_of(text, text->current_line) -
2544 LINE_HEIGHT(CACHE_DATA(text->current_line));
2546 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2547 CACHE_DATA(text->current_line).start = text->point;
2549 new_line = fetch_lines (text,
2550 &CACHE_DATA(text->current_line).start,
2551 &CACHE_DATA(text->current_line).tab_cont,
2555 swap_lines (text, text->current_line, new_line, old_lines);
2557 text->current_line = new_line;
2559 new_pixels = total_line_height (text, new_line, 1);
2561 gdk_window_get_size (text->text_area, &width, &height);
2563 if (old_pixels != new_pixels)
2565 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2567 gdk_draw_pixmap (text->text_area,
2571 pixel_height + old_pixels,
2573 pixel_height + new_pixels,
2577 text->vadj->upper += new_pixels;
2578 text->vadj->upper -= old_pixels;
2579 adjust_adj (text, text->vadj);
2583 rect.y = pixel_height;
2585 rect.height = new_pixels;
2587 expose_text (text, &rect, FALSE);
2588 gtk_text_draw_focus ( (GtkWidget *) text);
2590 text->cursor_mark = text->point;
2592 find_cursor (text, TRUE);
2594 if (old_pixels != new_pixels)
2596 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2599 rect.y = pixel_height + new_pixels;
2601 rect.height = height - rect.y;
2603 expose_text (text, &rect, FALSE);
2606 process_exposes (text);
2613 /* note, the point has already been moved forward */
2615 correct_cache_insert (GtkText* text, gint nchars)
2618 GtkPropertyMark *start;
2619 GtkPropertyMark *end;
2620 gboolean was_split = FALSE;
2622 /* We need to distinguish whether the property was split in the
2623 * insert or not, so we check if the point (which points after
2624 * the insertion here), points to the same character as the
2625 * point before. Ugh.
2629 GtkPropertyMark tmp_mark = text->point;
2630 move_mark_n (&tmp_mark, -1);
2632 if (tmp_mark.property != text->point.property)
2636 /* If we inserted a property exactly at the beginning of the
2637 * line, we have to correct here, or fetch_lines will
2640 start = &CACHE_DATA(text->current_line).start;
2642 /* Check if if we split exactly at the beginning of the line:
2643 * (was_split won't be set if we are inserting at the end of the text,
2644 * so we don't check)
2646 if (start->offset == MARK_CURRENT_PROPERTY (start)->length)
2647 SET_PROPERTY_MARK (start, start->property->next, 0);
2648 /* Check if we inserted a property at the beginning of the text: */
2649 else if (was_split &&
2650 (start->property == text->point.property) &&
2651 (start->index == text->point.index - nchars))
2652 SET_PROPERTY_MARK (start, start->property->prev, 0);
2654 /* Now correct the offsets, and check for start or end marks that
2655 * are after the point, yet point to a property before the point's
2656 * property. This indicates that they are meant to point to the
2657 * second half of a property we split in insert_text_property(), so
2658 * we fix them up that way.
2660 cache = text->current_line->next;
2662 for (; cache; cache = cache->next)
2664 start = &CACHE_DATA(cache).start;
2665 end = &CACHE_DATA(cache).end;
2667 if (LAST_INDEX (text, text->point) &&
2668 start->index == text->point.index)
2669 *start = text->point;
2670 else if (start->index >= text->point.index - nchars)
2672 if (!was_split && start->property == text->point.property)
2673 move_mark_n(start, nchars);
2676 if (start->property->next &&
2677 (start->property->next->next == text->point.property))
2679 g_assert (start->offset >= MARK_CURRENT_PROPERTY (start)->length);
2680 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2681 start->property = text->point.property;
2683 start->index += nchars;
2687 if (LAST_INDEX (text, text->point) &&
2688 end->index == text->point.index)
2690 if (end->index >= text->point.index - nchars)
2692 if (!was_split && end->property == text->point.property)
2693 move_mark_n(end, nchars);
2696 if (end->property->next &&
2697 (end->property->next->next == text->point.property))
2699 g_assert (end->offset >= MARK_CURRENT_PROPERTY (end)->length);
2700 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2701 end->property = text->point.property;
2703 end->index += nchars;
2707 /*TEXT_ASSERT_MARK(text, start, "start");*/
2708 /*TEXT_ASSERT_MARK(text, end, "end");*/
2714 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2715 guint new_line_count)
2717 GtkWidget *widget = GTK_WIDGET (text);
2720 guint new_pixels = 0;
2722 GList* new_lines = NULL;
2725 text->cursor_virtual_x = 0;
2727 undraw_cursor (text, FALSE);
2729 correct_cache_insert (text, nchars);
2731 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2733 pixel_height = pixel_height_of(text, text->current_line) -
2734 LINE_HEIGHT(CACHE_DATA(text->current_line));
2736 new_lines = fetch_lines (text,
2737 &CACHE_DATA(text->current_line).start,
2738 &CACHE_DATA(text->current_line).tab_cont,
2742 swap_lines (text, text->current_line, new_lines, 1);
2744 text->current_line = new_lines;
2746 new_pixels = total_line_height (text, new_lines, new_line_count);
2748 gdk_window_get_size (text->text_area, &width, &height);
2750 if (old_pixels != new_pixels)
2752 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2754 gdk_draw_pixmap (text->text_area,
2758 pixel_height + old_pixels,
2760 pixel_height + new_pixels,
2762 height + (old_pixels - new_pixels) - pixel_height);
2765 text->vadj->upper += new_pixels;
2766 text->vadj->upper -= old_pixels;
2767 adjust_adj (text, text->vadj);
2771 rect.y = pixel_height;
2773 rect.height = new_pixels;
2775 expose_text (text, &rect, FALSE);
2776 gtk_text_draw_focus ( (GtkWidget *) text);
2778 text->cursor_mark = text->point;
2780 find_cursor (text, TRUE);
2782 draw_cursor (text, FALSE);
2784 if (old_pixels != new_pixels)
2786 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2789 rect.y = pixel_height + new_pixels;
2791 rect.height = height - rect.y;
2793 expose_text (text, &rect, FALSE);
2796 process_exposes (text);
2799 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2804 /* Text property functions */
2807 font_hash (gconstpointer font)
2809 return gdk_font_id ((const GdkFont*) font);
2812 static GHashTable *font_cache_table = NULL;
2815 get_text_font (GdkFont* gfont)
2820 if (!font_cache_table)
2821 font_cache_table = g_hash_table_new (font_hash, (GEqualFunc) gdk_font_equal);
2823 tf = g_hash_table_lookup (font_cache_table, gfont);
2831 tf = g_new (GtkTextFont, 1);
2834 tf->gdk_font = gfont;
2835 gdk_font_ref (gfont);
2837 for(i = 0; i < 256; i += 1)
2838 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2840 g_hash_table_insert (font_cache_table, gfont, tf);
2846 text_font_unref (GtkTextFont *text_font)
2848 text_font->ref_count--;
2849 if (text_font->ref_count == 0)
2851 g_hash_table_remove (font_cache_table, text_font->gdk_font);
2852 gdk_font_unref (text_font->gdk_font);
2858 text_properties_equal (TextProperty* prop, GdkFont* font, const GdkColor *fore, const GdkColor *back)
2860 if (prop->flags & PROPERTY_FONT)
2863 GtkTextFont *text_font;
2868 text_font = get_text_font (font);
2870 retval = (prop->font == text_font);
2871 text_font_unref (text_font);
2880 if (prop->flags & PROPERTY_FOREGROUND)
2882 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
2889 if (prop->flags & PROPERTY_BACKGROUND)
2891 if (!back || !gdk_color_equal (&prop->back_color, back))
2902 realize_property (GtkText *text, TextProperty *prop)
2904 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2906 if (prop->flags & PROPERTY_FOREGROUND)
2907 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
2909 if (prop->flags & PROPERTY_BACKGROUND)
2910 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
2914 realize_properties (GtkText *text)
2916 GList *tmp_list = text->text_properties;
2920 realize_property (text, tmp_list->data);
2922 tmp_list = tmp_list->next;
2927 unrealize_property (GtkText *text, TextProperty *prop)
2929 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2931 if (prop->flags & PROPERTY_FOREGROUND)
2932 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
2934 if (prop->flags & PROPERTY_BACKGROUND)
2935 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
2939 unrealize_properties (GtkText *text)
2941 GList *tmp_list = text->text_properties;
2945 unrealize_property (text, tmp_list->data);
2947 tmp_list = tmp_list->next;
2951 static TextProperty*
2952 new_text_property (GtkText *text, GdkFont *font, const GdkColor* fore,
2953 const GdkColor* back, guint length)
2957 prop = g_slice_new (TextProperty);
2962 prop->flags |= PROPERTY_FONT;
2963 prop->font = get_text_font (font);
2970 prop->flags |= PROPERTY_FOREGROUND;
2971 prop->fore_color = *fore;
2976 prop->flags |= PROPERTY_BACKGROUND;
2977 prop->back_color = *back;
2980 prop->length = length;
2982 if (GTK_WIDGET_REALIZED (text))
2983 realize_property (text, prop);
2989 destroy_text_property (TextProperty *prop)
2992 text_font_unref (prop->font);
2994 g_slice_free (TextProperty, prop);
2997 /* Flop the memory between the point and the gap around like a
3000 move_gap (GtkText* text, guint index)
3002 if (text->gap_position < index)
3004 gint diff = index - text->gap_position;
3006 if (text->use_wchar)
3007 g_memmove (text->text.wc + text->gap_position,
3008 text->text.wc + text->gap_position + text->gap_size,
3009 diff*sizeof (GdkWChar));
3011 g_memmove (text->text.ch + text->gap_position,
3012 text->text.ch + text->gap_position + text->gap_size,
3015 text->gap_position = index;
3017 else if (text->gap_position > index)
3019 gint diff = text->gap_position - index;
3021 if (text->use_wchar)
3022 g_memmove (text->text.wc + index + text->gap_size,
3023 text->text.wc + index,
3024 diff*sizeof (GdkWChar));
3026 g_memmove (text->text.ch + index + text->gap_size,
3027 text->text.ch + index,
3030 text->gap_position = index;
3034 /* Increase the gap size. */
3036 make_forward_space (GtkText* text, guint len)
3038 if (text->gap_size < len)
3040 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3042 if (sum >= text->text_len)
3046 while (i <= sum) i <<= 1;
3048 if (text->use_wchar)
3049 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3050 i*sizeof(GdkWChar));
3052 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3056 if (text->use_wchar)
3057 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3058 text->text.wc + text->gap_position + text->gap_size,
3059 (text->text_end - (text->gap_position + text->gap_size))
3062 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3063 text->text.ch + text->gap_position + text->gap_size,
3064 text->text_end - (text->gap_position + text->gap_size));
3066 text->text_end += len*2;
3067 text->gap_size += len*2;
3071 /* Inserts into the text property list a list element that guarantees
3072 * that for len characters following the point, text has the correct
3073 * property. does not move point. adjusts text_properties_point and
3074 * text_properties_point_offset relative to the current value of
3077 insert_text_property (GtkText* text, GdkFont* font,
3078 const GdkColor *fore, const GdkColor* back, guint len)
3080 GtkPropertyMark *mark = &text->point;
3081 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3082 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3084 if (MARK_OFFSET(mark) == 0)
3086 /* Point is on the boundary of two properties.
3087 * If it is the same as either, grow, else insert
3090 if (text_properties_equal(forward_prop, font, fore, back))
3092 /* Grow the property in front of us. */
3094 MARK_PROPERTY_LENGTH(mark) += len;
3096 else if (backward_prop &&
3097 text_properties_equal(backward_prop, font, fore, back))
3099 /* Grow property behind us, point property and offset
3102 SET_PROPERTY_MARK (&text->point,
3103 MARK_PREV_LIST_PTR (mark),
3104 backward_prop->length);
3106 backward_prop->length += len;
3108 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3109 (forward_prop->length == 1))
3111 /* Next property just has last position, take it over */
3113 if (GTK_WIDGET_REALIZED (text))
3114 unrealize_property (text, forward_prop);
3116 forward_prop->flags = 0;
3119 forward_prop->flags |= PROPERTY_FONT;
3120 forward_prop->font = get_text_font (font);
3123 forward_prop->font = NULL;
3127 forward_prop->flags |= PROPERTY_FOREGROUND;
3128 forward_prop->fore_color = *fore;
3132 forward_prop->flags |= PROPERTY_BACKGROUND;
3133 forward_prop->back_color = *back;
3135 forward_prop->length += len;
3137 if (GTK_WIDGET_REALIZED (text))
3138 realize_property (text, forward_prop);
3142 /* Splice a new property into the list. */
3144 GList* new_prop = g_list_alloc();
3146 new_prop->next = MARK_LIST_PTR(mark);
3147 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3148 new_prop->next->prev = new_prop;
3151 new_prop->prev->next = new_prop;
3153 new_prop->data = new_text_property (text, font, fore, back, len);
3155 SET_PROPERTY_MARK (mark, new_prop, 0);
3160 /* The following will screw up the line_start cache,
3161 * we'll fix it up in correct_cache_insert
3164 /* In the middle of forward_prop, if properties are equal,
3165 * just add to its length, else split it into two and splice
3167 if (text_properties_equal (forward_prop, font, fore, back))
3169 forward_prop->length += len;
3171 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3172 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3174 /* Inserting before only the last position in the text */
3177 forward_prop->length -= 1;
3179 new_prop = g_list_alloc();
3180 new_prop->data = new_text_property (text, font, fore, back, len+1);
3181 new_prop->prev = MARK_LIST_PTR(mark);
3182 new_prop->next = NULL;
3183 MARK_NEXT_LIST_PTR(mark) = new_prop;
3185 SET_PROPERTY_MARK (mark, new_prop, 0);
3189 GList* new_prop = g_list_alloc();
3190 GList* new_prop_forward = g_list_alloc();
3191 gint old_length = forward_prop->length;
3192 GList* next = MARK_NEXT_LIST_PTR(mark);
3194 /* Set the new lengths according to where they are split. Construct
3195 * two new properties. */
3196 forward_prop->length = MARK_OFFSET(mark);
3198 new_prop_forward->data =
3199 new_text_property(text,
3200 forward_prop->flags & PROPERTY_FONT ?
3201 forward_prop->font->gdk_font : NULL,
3202 forward_prop->flags & PROPERTY_FOREGROUND ?
3203 &forward_prop->fore_color : NULL,
3204 forward_prop->flags & PROPERTY_BACKGROUND ?
3205 &forward_prop->back_color : NULL,
3206 old_length - forward_prop->length);
3208 new_prop->data = new_text_property(text, font, fore, back, len);
3210 /* Now splice things in. */
3211 MARK_NEXT_LIST_PTR(mark) = new_prop;
3212 new_prop->prev = MARK_LIST_PTR(mark);
3214 new_prop->next = new_prop_forward;
3215 new_prop_forward->prev = new_prop;
3217 new_prop_forward->next = next;
3220 next->prev = new_prop_forward;
3222 SET_PROPERTY_MARK (mark, new_prop, 0);
3226 while (text->text_properties_end->next)
3227 text->text_properties_end = text->text_properties_end->next;
3229 while (text->text_properties->prev)
3230 text->text_properties = text->text_properties->prev;
3234 delete_text_property (GtkText* text, guint nchars)
3236 /* Delete nchars forward from point. */
3238 /* Deleting text properties is problematical, because we
3239 * might be storing around marks pointing to a property.
3241 * The marks in question and how we handle them are:
3243 * point: We know the new value, since it will be at the
3244 * end of the deleted text, and we move it there
3246 * cursor: We just remove the mark and set it equal to the
3247 * point after the operation.
3248 * line-start cache: We replace most affected lines.
3249 * The current line gets used to fetch the new
3250 * lines so, if necessary, (delete at the beginning
3251 * of a line) we fix it up by setting it equal to the
3259 for(; nchars; nchars -= 1)
3261 prop = MARK_CURRENT_PROPERTY(&text->point);
3265 if (prop->length == 0)
3267 tmp = MARK_LIST_PTR (&text->point);
3269 is_first = tmp == text->text_properties;
3271 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3272 text->point.offset = 0;
3274 if (GTK_WIDGET_REALIZED (text))
3275 unrealize_property (text, prop);
3277 destroy_text_property (prop);
3278 g_list_free_1 (tmp);
3280 prop = MARK_CURRENT_PROPERTY (&text->point);
3283 text->text_properties = MARK_LIST_PTR (&text->point);
3285 g_assert (prop->length != 0);
3287 else if (prop->length == text->point.offset)
3289 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3290 text->point.offset = 0;
3294 /* Check to see if we have just the single final position remaining
3295 * along in a property; if so, combine it with the previous property
3297 if (LAST_INDEX (text, text->point) &&
3298 (MARK_OFFSET (&text->point) == 0) &&
3299 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3301 tmp = MARK_LIST_PTR (&text->point);
3302 prop = MARK_CURRENT_PROPERTY(&text->point);
3304 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3305 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3306 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3308 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3310 if (GTK_WIDGET_REALIZED (text))
3311 unrealize_property (text, prop);
3313 destroy_text_property (prop);
3314 g_list_free_1 (tmp);
3319 init_properties (GtkText *text)
3321 if (!text->text_properties)
3323 text->text_properties = g_list_alloc();
3324 text->text_properties->next = NULL;
3325 text->text_properties->prev = NULL;
3326 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3327 text->text_properties_end = text->text_properties;
3329 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3331 text->point.index = 0;
3336 /**********************************************************************/
3337 /* Property Movement */
3338 /**********************************************************************/
3341 move_mark_n (GtkPropertyMark* mark, gint n)
3344 advance_mark_n(mark, n);
3346 decrement_mark_n(mark, -n);
3350 advance_mark (GtkPropertyMark* mark)
3352 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3356 if (prop->length > mark->offset + 1)
3360 mark->property = MARK_NEXT_LIST_PTR (mark);
3366 advance_mark_n (GtkPropertyMark* mark, gint n)
3373 i = 0; /* otherwise it migth not be init. */
3374 prop = MARK_CURRENT_PROPERTY(mark);
3376 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3377 /* to make it easier */
3378 n += (mark->offset);
3379 mark->index -= mark->offset;
3381 /* first we take seven-mile-leaps to get to the right text
3383 while ((n-i) > prop->length - 1) {
3385 mark->index += prop->length;
3386 mark->property = MARK_NEXT_LIST_PTR (mark);
3387 prop = MARK_CURRENT_PROPERTY (mark);
3391 /* and then the rest */
3392 mark->index += n - i;
3393 mark->offset += n - i;
3397 decrement_mark (GtkPropertyMark* mark)
3401 if (mark->offset > 0)
3405 mark->property = MARK_PREV_LIST_PTR (mark);
3406 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3411 decrement_mark_n (GtkPropertyMark* mark, gint n)
3415 while (mark->offset < n) {
3416 /* jump to end of prev */
3417 n -= mark->offset + 1;
3418 mark->index -= mark->offset + 1;
3419 mark->property = MARK_PREV_LIST_PTR (mark);
3420 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3428 static GtkPropertyMark
3429 find_mark (GtkText* text, guint mark_position)
3431 return find_mark_near (text, mark_position, &text->point);
3435 * You can also start from the end, what a drag.
3437 static GtkPropertyMark
3438 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3443 GtkPropertyMark mark;
3446 diffa = mark_position + 1;
3448 diffa = mark_position - near->index;
3450 diffb = mark_position;
3462 mark.property = text->text_properties;
3466 move_mark_n (&mark, mark_position - mark.index);
3471 /* This routine must be called with scroll == FALSE, only when
3472 * point is at least partially on screen
3476 find_line_containing_point (GtkText* text, guint point,
3482 text->current_line = NULL;
3486 /* Scroll backwards until the point is on screen
3488 while (CACHE_DATA(text->line_start_cache).start.index > point)
3489 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3491 /* Now additionally try to make sure that the point is fully on screen
3495 while (text->first_cut_pixels != 0 &&
3496 text->line_start_cache->next &&
3497 CACHE_DATA(text->line_start_cache->next).start.index > point)
3498 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3501 gdk_window_get_size (text->text_area, NULL, &height);
3503 for (cache = text->line_start_cache; cache; cache = cache->next)
3507 if (CACHE_DATA(cache).end.index >= point ||
3508 LAST_INDEX(text, CACHE_DATA(cache).end))
3510 text->current_line = cache; /* LOOK HERE, this proc has an
3511 * important side effect. */
3515 TEXT_SHOW_LINE (text, cache, "cache");
3517 if (cache->next == NULL)
3518 fetch_lines_forward (text, 1);
3522 lph = pixel_height_of (text, cache->next);
3524 /* Scroll the bottom of the line is on screen, or until
3525 * the line is the first onscreen line.
3527 while (cache->next != text->line_start_cache && lph > height)
3529 TEXT_SHOW_LINE (text, cache, "cache");
3530 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3531 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3532 lph = pixel_height_of (text, cache->next);
3537 g_assert_not_reached (); /* Must set text->current_line here */
3541 pixel_height_of (GtkText* text, GList* cache_line)
3543 gint pixels = - text->first_cut_pixels;
3544 GList *cache = text->line_start_cache;
3547 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3549 if (cache->data == cache_line->data)
3552 cache = cache->next;
3558 /**********************************************************************/
3559 /* Search and Placement */
3560 /**********************************************************************/
3563 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3566 gint16* char_widths;
3568 if (LAST_INDEX (text, *mark))
3571 ch = GTK_TEXT_INDEX (text, mark->index);
3572 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3576 return tab_mark->to_next_tab * char_widths[' '];
3580 return char_widths[ch];
3584 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3589 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, GdkWChar ch)
3591 if (tab_mark->to_next_tab == 1 || ch == '\t')
3593 if (tab_mark->tab_stops->next)
3595 tab_mark->tab_stops = tab_mark->tab_stops->next;
3596 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3600 tab_mark->to_next_tab = text->default_tab_width;
3605 tab_mark->to_next_tab -= 1;
3610 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3614 advance_tab_mark (text, tab_mark, 0);
3618 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3622 GtkPropertyMark mark = start_line->start;
3623 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3624 gint pixel_width = LINE_START_PIXEL (*start_line);
3626 while (mark.index < text->cursor_mark.index)
3628 pixel_width += find_char_width (text, &mark, &tab_mark);
3630 advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3631 advance_mark (&mark);
3634 text->cursor_pos_x = pixel_width;
3635 text->cursor_pos_y = pixel_height;
3636 text->cursor_char_offset = start_line->font_descent;
3637 text->cursor_mark = mark;
3639 ch = LAST_INDEX (text, mark) ?
3640 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3642 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3643 text->cursor_char = 0;
3645 text->cursor_char = ch;
3649 find_cursor (GtkText* text, gboolean scroll)
3651 if (GTK_WIDGET_REALIZED (text))
3653 find_line_containing_point (text, text->cursor_mark.index, scroll);
3655 if (text->current_line)
3656 find_cursor_at_line (text,
3657 &CACHE_DATA(text->current_line),
3658 pixel_height_of(text, text->current_line));
3661 GTK_OLD_EDITABLE (text)->current_pos = text->cursor_mark.index;
3665 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3666 guint line_pixel_height,
3669 GtkPropertyMark mark = lp->start;
3670 TabStopMark tab_mark = lp->tab_cont.tab_start;
3672 gint char_width = find_char_width(text, &mark, &tab_mark);
3673 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3675 text->cursor_pos_y = line_pixel_height;
3679 GdkWChar ch = LAST_INDEX (text, mark) ?
3680 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3682 if (button_x < pixel_width || mark.index == lp->end.index)
3684 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3685 text->cursor_mark = mark;
3686 text->cursor_char_offset = lp->font_descent;
3688 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3689 text->cursor_char = 0;
3691 text->cursor_char = ch;
3696 advance_tab_mark (text, &tab_mark, ch);
3697 advance_mark (&mark);
3699 pixel_width += char_width/2;
3701 char_width = find_char_width (text, &mark, &tab_mark);
3703 pixel_width += (char_width+1)/2;
3708 find_mouse_cursor (GtkText* text, gint x, gint y)
3711 GList* cache = text->line_start_cache;
3715 pixel_height = - text->first_cut_pixels;
3717 for (; cache; cache = cache->next)
3719 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3721 if (y < pixel_height || !cache->next)
3723 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3725 find_cursor (text, FALSE);
3732 /**********************************************************************/
3734 /**********************************************************************/
3737 free_cache (GtkText* text)
3739 GList* cache = text->line_start_cache;
3744 cache = cache->prev;
3746 text->line_start_cache = cache;
3749 for (; cache; cache = cache->next)
3750 g_slice_free (LineParams, cache->data);
3752 g_list_free (text->line_start_cache);
3754 text->line_start_cache = NULL;
3758 remove_cache_line (GtkText* text, GList* member)
3765 if (member == text->line_start_cache)
3766 text->line_start_cache = text->line_start_cache->next;
3769 member->prev->next = member->next;
3772 member->next->prev = member->prev;
3774 list = member->next;
3776 g_slice_free (LineParams, member->data);
3777 g_list_free_1 (member);
3782 /**********************************************************************/
3784 /**********************************************************************/
3787 move_cursor_buffer_ver (GtkText *text, int dir)
3789 undraw_cursor (text, FALSE);
3793 scroll_int (text, text->vadj->upper);
3794 text->cursor_mark = find_this_line_start_mark (text,
3796 &text->cursor_mark);
3800 scroll_int (text, - text->vadj->value);
3801 text->cursor_mark = find_this_line_start_mark (text,
3803 &text->cursor_mark);
3806 find_cursor (text, TRUE);
3807 draw_cursor (text, FALSE);
3811 move_cursor_page_ver (GtkText *text, int dir)
3813 scroll_int (text, dir * text->vadj->page_increment);
3817 move_cursor_ver (GtkText *text, int count)
3820 GtkPropertyMark mark;
3823 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3824 offset = text->cursor_mark.index - mark.index;
3826 if (offset > text->cursor_virtual_x)
3827 text->cursor_virtual_x = offset;
3831 if (mark.index == 0)
3834 decrement_mark (&mark);
3835 mark = find_this_line_start_mark (text, mark.index, &mark);
3839 mark = text->cursor_mark;
3841 while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3842 advance_mark (&mark);
3844 if (LAST_INDEX(text, mark))
3847 advance_mark (&mark);
3850 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3851 if (LAST_INDEX(text, mark) ||
3852 GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3855 undraw_cursor (text, FALSE);
3857 text->cursor_mark = mark;
3859 find_cursor (text, TRUE);
3861 draw_cursor (text, FALSE);
3865 move_cursor_hor (GtkText *text, int count)
3867 /* count should be +-1. */
3868 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3869 (count < 0 && text->cursor_mark.index < (- count)) ||
3873 text->cursor_virtual_x = 0;
3875 undraw_cursor (text, FALSE);
3877 move_mark_n (&text->cursor_mark, count);
3879 find_cursor (text, TRUE);
3881 draw_cursor (text, FALSE);
3885 gtk_text_move_cursor (GtkOldEditable *old_editable,
3892 move_cursor_hor (GTK_TEXT (old_editable), 1);
3897 move_cursor_hor (GTK_TEXT (old_editable), -1);
3903 move_cursor_ver (GTK_TEXT (old_editable), 1);
3908 move_cursor_ver (GTK_TEXT (old_editable), -1);
3913 gtk_text_move_forward_character (GtkText *text)
3915 move_cursor_hor (text, 1);
3919 gtk_text_move_backward_character (GtkText *text)
3921 move_cursor_hor (text, -1);
3925 gtk_text_move_next_line (GtkText *text)
3927 move_cursor_ver (text, 1);
3931 gtk_text_move_previous_line (GtkText *text)
3933 move_cursor_ver (text, -1);
3937 gtk_text_move_word (GtkOldEditable *old_editable,
3943 gtk_text_move_forward_word (GTK_TEXT (old_editable));
3948 gtk_text_move_backward_word (GTK_TEXT (old_editable));
3953 gtk_text_move_forward_word (GtkText *text)
3955 text->cursor_virtual_x = 0;
3957 undraw_cursor (text, FALSE);
3959 if (text->use_wchar)
3961 while (!LAST_INDEX (text, text->cursor_mark) &&
3962 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3963 advance_mark (&text->cursor_mark);
3965 while (!LAST_INDEX (text, text->cursor_mark) &&
3966 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3967 advance_mark (&text->cursor_mark);
3971 while (!LAST_INDEX (text, text->cursor_mark) &&
3972 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3973 advance_mark (&text->cursor_mark);
3975 while (!LAST_INDEX (text, text->cursor_mark) &&
3976 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3977 advance_mark (&text->cursor_mark);
3980 find_cursor (text, TRUE);
3981 draw_cursor (text, FALSE);
3985 gtk_text_move_backward_word (GtkText *text)
3987 text->cursor_virtual_x = 0;
3989 undraw_cursor (text, FALSE);
3991 if (text->use_wchar)
3993 while ((text->cursor_mark.index > 0) &&
3994 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3995 decrement_mark (&text->cursor_mark);
3997 while ((text->cursor_mark.index > 0) &&
3998 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3999 decrement_mark (&text->cursor_mark);
4003 while ((text->cursor_mark.index > 0) &&
4004 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4005 decrement_mark (&text->cursor_mark);
4007 while ((text->cursor_mark.index > 0) &&
4008 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4009 decrement_mark (&text->cursor_mark);
4012 find_cursor (text, TRUE);
4013 draw_cursor (text, FALSE);
4017 gtk_text_move_page (GtkOldEditable *old_editable,
4022 scroll_int (GTK_TEXT (old_editable),
4023 y * GTK_TEXT(old_editable)->vadj->page_increment);
4027 gtk_text_move_to_row (GtkOldEditable *old_editable,
4033 gtk_text_move_to_column (GtkOldEditable *old_editable,
4038 text = GTK_TEXT (old_editable);
4040 text->cursor_virtual_x = 0; /* FIXME */
4042 undraw_cursor (text, FALSE);
4044 /* Move to the beginning of the line */
4045 while ((text->cursor_mark.index > 0) &&
4046 (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4047 decrement_mark (&text->cursor_mark);
4049 while (!LAST_INDEX (text, text->cursor_mark) &&
4050 (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4054 else if (column == 0)
4057 advance_mark (&text->cursor_mark);
4060 find_cursor (text, TRUE);
4061 draw_cursor (text, FALSE);
4065 gtk_text_move_beginning_of_line (GtkText *text)
4067 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4072 gtk_text_move_end_of_line (GtkText *text)
4074 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), -1);
4078 gtk_text_kill_char (GtkOldEditable *old_editable,
4083 text = GTK_TEXT (old_editable);
4085 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4086 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4091 if (text->point.index + 1 <= TEXT_LENGTH (text))
4092 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index, text->point.index + 1);
4096 if (text->point.index > 0)
4097 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index - 1, text->point.index);
4103 gtk_text_delete_forward_character (GtkText *text)
4105 gtk_text_kill_char (GTK_OLD_EDITABLE (text), 1);
4109 gtk_text_delete_backward_character (GtkText *text)
4111 gtk_text_kill_char (GTK_OLD_EDITABLE (text), -1);
4115 gtk_text_kill_word (GtkOldEditable *old_editable,
4118 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4119 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4122 gint old_pos = old_editable->current_pos;
4125 gtk_text_move_word (old_editable, 1);
4126 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4130 gtk_text_move_word (old_editable, -1);
4131 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4137 gtk_text_delete_forward_word (GtkText *text)
4139 gtk_text_kill_word (GTK_OLD_EDITABLE (text), 1);
4143 gtk_text_delete_backward_word (GtkText *text)
4145 gtk_text_kill_word (GTK_OLD_EDITABLE (text), -1);
4149 gtk_text_kill_line (GtkOldEditable *old_editable,
4152 gint old_pos = old_editable->current_pos;
4155 gtk_text_move_to_column (old_editable, -1);
4156 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4160 gtk_text_move_to_column (old_editable, 0);
4161 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4166 gtk_text_delete_line (GtkText *text)
4168 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4169 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4173 gtk_text_delete_to_line_end (GtkText *text)
4175 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4179 gtk_text_select_word (GtkText *text, guint32 time)
4184 GtkOldEditable *old_editable;
4185 old_editable = GTK_OLD_EDITABLE (text);
4187 gtk_text_move_backward_word (text);
4188 start_pos = text->cursor_mark.index;
4190 gtk_text_move_forward_word (text);
4191 end_pos = text->cursor_mark.index;
4193 old_editable->has_selection = TRUE;
4194 gtk_text_set_selection (old_editable, start_pos, end_pos);
4195 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4199 gtk_text_select_line (GtkText *text, guint32 time)
4204 GtkOldEditable *old_editable;
4205 old_editable = GTK_OLD_EDITABLE (text);
4207 gtk_text_move_beginning_of_line (text);
4208 start_pos = text->cursor_mark.index;
4210 gtk_text_move_end_of_line (text);
4211 gtk_text_move_forward_character (text);
4212 end_pos = text->cursor_mark.index;
4214 old_editable->has_selection = TRUE;
4215 gtk_text_set_selection (old_editable, start_pos, end_pos);
4216 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4219 /**********************************************************************/
4221 /**********************************************************************/
4224 adjust_adj (GtkText* text, GtkAdjustment* adj)
4228 gdk_window_get_size (text->text_area, NULL, &height);
4230 adj->step_increment = MIN (adj->upper, SCROLL_PIXELS);
4231 adj->page_increment = MIN (adj->upper, height - KEY_SCROLL_PIXELS);
4232 adj->page_size = MIN (adj->upper, height);
4233 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4234 adj->value = MAX (adj->value, 0.0);
4236 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4240 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
4242 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4244 if ((text->first_line_start_index >= lp->start.index) &&
4245 (text->first_line_start_index <= lp->end.index))
4247 svdata->mark = lp->start;
4249 if (text->first_line_start_index == lp->start.index)
4251 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4255 text->first_onscreen_ver_pixel = svdata->pixel_height;
4256 text->first_cut_pixels = 0;
4259 text->vadj->value = text->first_onscreen_ver_pixel;
4262 svdata->pixel_height += LINE_HEIGHT (*lp);
4268 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
4270 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4273 if (svdata->pixel_height <= (gint) text->vadj->value &&
4274 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4276 svdata->mark = lp->start;
4278 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4279 text->first_onscreen_ver_pixel = svdata->pixel_height;
4280 text->first_line_start_index = lp->start.index;
4286 svdata->pixel_height += LINE_HEIGHT (*lp);
4294 static GtkPropertyMark
4295 set_vertical_scroll (GtkText* text)
4297 GtkPropertyMark mark = find_mark (text, 0);
4298 SetVerticalScrollData data;
4302 data.pixel_height = 0;
4303 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4305 text->vadj->upper = data.pixel_height;
4306 orig_value = (gint) text->vadj->value;
4308 gdk_window_get_size (text->text_area, NULL, &height);
4310 text->vadj->step_increment = MIN (text->vadj->upper, SCROLL_PIXELS);
4311 text->vadj->page_increment = MIN (text->vadj->upper, height - KEY_SCROLL_PIXELS);
4312 text->vadj->page_size = MIN (text->vadj->upper, height);
4313 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4314 text->vadj->value = MAX (text->vadj->value, 0.0);
4316 text->last_ver_value = (gint)text->vadj->value;
4318 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4320 if (text->vadj->value != orig_value)
4322 /* We got clipped, and don't really know which line to put first. */
4323 data.pixel_height = 0;
4324 data.last_didnt_wrap = TRUE;
4326 line_params_iterate (text, &mark, NULL,
4328 set_vertical_scroll_find_iterator);
4335 scroll_int (GtkText* text, gint diff)
4339 text->vadj->value += diff;
4341 upper = text->vadj->upper - text->vadj->page_size;
4342 text->vadj->value = MIN (text->vadj->value, upper);
4343 text->vadj->value = MAX (text->vadj->value, 0.0);
4345 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4349 process_exposes (GtkText *text)
4353 /* Make sure graphics expose events are processed before scrolling
4356 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4358 gtk_widget_send_expose (GTK_WIDGET (text), event);
4359 if (event->expose.count == 0)
4361 gdk_event_free (event);
4364 gdk_event_free (event);
4368 static gint last_visible_line_height (GtkText* text)
4370 GList *cache = text->line_start_cache;
4373 gdk_window_get_size (text->text_area, NULL, &height);
4375 for (; cache->next; cache = cache->next)
4376 if (pixel_height_of(text, cache->next) > height)
4380 return pixel_height_of(text, cache) - 1;
4385 static gint first_visible_line_height (GtkText* text)
4387 if (text->first_cut_pixels)
4388 return pixel_height_of(text, text->line_start_cache) + 1;
4394 scroll_down (GtkText* text, gint diff0)
4400 text->first_onscreen_ver_pixel += diff0;
4404 g_assert (text->line_start_cache);
4406 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4408 text->first_cut_pixels += 1;
4412 text->first_cut_pixels = 0;
4414 text->line_start_cache = text->line_start_cache->next;
4416 text->first_line_start_index =
4417 CACHE_DATA(text->line_start_cache).start.index;
4419 if (!text->line_start_cache->next)
4420 fetch_lines_forward (text, 1);
4426 gdk_window_get_size (text->text_area, &width, &height);
4427 if (height > real_diff)
4428 gdk_draw_pixmap (text->text_area,
4436 height - real_diff);
4439 rect.y = MAX (0, height - real_diff);
4441 rect.height = MIN (height, real_diff);
4443 expose_text (text, &rect, FALSE);
4444 gtk_text_draw_focus ( (GtkWidget *) text);
4446 if (text->current_line)
4450 text->cursor_pos_y -= real_diff;
4451 cursor_min = drawn_cursor_min(text);
4454 find_mouse_cursor (text, text->cursor_pos_x,
4455 first_visible_line_height (text));
4458 if (height > real_diff)
4459 process_exposes (text);
4463 scroll_up (GtkText* text, gint diff0)
4469 text->first_onscreen_ver_pixel += diff0;
4473 g_assert (text->line_start_cache);
4475 if (text->first_cut_pixels > 0)
4477 text->first_cut_pixels -= 1;
4481 if (!text->line_start_cache->prev)
4482 fetch_lines_backward (text);
4484 text->line_start_cache = text->line_start_cache->prev;
4486 text->first_line_start_index =
4487 CACHE_DATA(text->line_start_cache).start.index;
4489 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4495 gdk_window_get_size (text->text_area, &width, &height);
4496 if (height > real_diff)
4497 gdk_draw_pixmap (text->text_area,
4505 height - real_diff);
4510 rect.height = MIN (height, real_diff);
4512 expose_text (text, &rect, FALSE);
4513 gtk_text_draw_focus ( (GtkWidget *) text);
4515 if (text->current_line)
4520 text->cursor_pos_y += real_diff;
4521 cursor_max = drawn_cursor_max(text);
4522 gdk_window_get_size (text->text_area, NULL, &height);
4524 if (cursor_max >= height)
4525 find_mouse_cursor (text, text->cursor_pos_x,
4526 last_visible_line_height (text));
4529 if (height > real_diff)
4530 process_exposes (text);
4533 /**********************************************************************/
4535 /**********************************************************************/
4537 /* Assumes mark starts a line. Calculates the height, width, and
4538 * displayable character count of a single DISPLAYABLE line. That
4539 * means that in line-wrap mode, this does may not compute the
4540 * properties of an entire line. */
4542 find_line_params (GtkText* text,
4543 const GtkPropertyMark* mark,
4544 const PrevTabCont *tab_cont,
4545 PrevTabCont *next_cont)
4548 TabStopMark tab_mark = tab_cont->tab_start;
4549 guint max_display_pixels;
4554 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4555 max_display_pixels -= LINE_WRAP_ROOM;
4558 lp.tab_cont = *tab_cont;
4561 lp.pixel_width = tab_cont->pixel_offset;
4562 lp.displayable_chars = 0;
4564 lp.font_descent = 0;
4566 init_tab_cont (text, next_cont);
4568 while (!LAST_INDEX(text, lp.end))
4570 g_assert (lp.end.property);
4572 ch = GTK_TEXT_INDEX (text, lp.end.index);
4573 font = MARK_CURRENT_FONT (text, &lp.end);
4575 if (ch == LINE_DELIM)
4577 /* Newline doesn't count in computation of line height, even
4578 * if its in a bigger font than the rest of the line. Unless,
4579 * of course, there are no other characters. */
4581 if (!lp.font_ascent && !lp.font_descent)
4583 lp.font_ascent = font->ascent;
4584 lp.font_descent = font->descent;
4587 lp.tab_cont_next = *next_cont;
4592 ch_width = find_char_width (text, &lp.end, &tab_mark);
4594 if ((ch_width + lp.pixel_width > max_display_pixels) &&
4595 (lp.end.index > lp.start.index))
4599 if (text->line_wrap)
4601 next_cont->tab_start = tab_mark;
4602 next_cont->pixel_offset = 0;
4606 /* Here's the tough case, a tab is wrapping. */
4607 gint pixels_avail = max_display_pixels - lp.pixel_width;
4608 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
4609 gint spaces_avail = pixels_avail / space_width;
4611 if (spaces_avail == 0)
4613 decrement_mark (&lp.end);
4617 advance_tab_mark (text, &next_cont->tab_start, '\t');
4618 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4620 lp.displayable_chars += 1;
4625 if (text->word_wrap)
4627 GtkPropertyMark saved_mark = lp.end;
4628 guint saved_characters = lp.displayable_chars;
4630 lp.displayable_chars += 1;
4632 if (text->use_wchar)
4634 while (!gdk_iswspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4635 (lp.end.index > lp.start.index))
4637 decrement_mark (&lp.end);
4638 lp.displayable_chars -= 1;
4643 while (!isspace(GTK_TEXT_INDEX (text, lp.end.index)) &&
4644 (lp.end.index > lp.start.index))
4646 decrement_mark (&lp.end);
4647 lp.displayable_chars -= 1;
4651 /* If whole line is one word, revert to char wrapping */
4652 if (lp.end.index == lp.start.index)
4654 lp.end = saved_mark;
4655 lp.displayable_chars = saved_characters;
4656 decrement_mark (&lp.end);
4661 /* Don't include this character, it will wrap. */
4662 decrement_mark (&lp.end);
4666 lp.tab_cont_next = *next_cont;
4673 lp.displayable_chars += 1;
4676 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4677 lp.font_descent = MAX (font->descent, lp.font_descent);
4678 lp.pixel_width += ch_width;
4680 advance_mark(&lp.end);
4681 advance_tab_mark (text, &tab_mark, ch);
4684 if (LAST_INDEX(text, lp.start))
4686 /* Special case, empty last line. */
4687 font = MARK_CURRENT_FONT (text, &lp.end);
4689 lp.font_ascent = font->ascent;
4690 lp.font_descent = font->descent;
4693 lp.tab_cont_next = *next_cont;
4699 expand_scratch_buffer (GtkText* text, guint len)
4701 if (len >= text->scratch_buffer_len)
4705 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4707 if (text->use_wchar)
4709 if (text->scratch_buffer.wc)
4710 text->scratch_buffer.wc = g_new (GdkWChar, i);
4712 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
4713 i*sizeof (GdkWChar));
4717 if (text->scratch_buffer.ch)
4718 text->scratch_buffer.ch = g_new (guchar, i);
4720 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
4723 text->scratch_buffer_len = i;
4727 /* Side effect: modifies text->gc
4730 draw_bg_rect (GtkText* text, GtkPropertyMark *mark,
4731 gint x, gint y, gint width, gint height,
4732 gboolean already_cleared)
4734 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4736 if ((mark->index >= MIN(old_editable->selection_start_pos, old_editable->selection_end_pos) &&
4737 mark->index < MAX(old_editable->selection_start_pos, old_editable->selection_end_pos)))
4739 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
4740 old_editable->has_selection ?
4741 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
4743 NULL, GTK_WIDGET(text), "text",
4744 x, y, width, height);
4746 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
4747 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
4749 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
4751 gdk_draw_rectangle (text->text_area,
4753 TRUE, x, y, width, height);
4755 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4762 rect.height = height;
4764 clear_area (text, &rect);
4766 else if (!already_cleared)
4767 gdk_window_clear_area (text->text_area, x, y, width, height);
4771 draw_line (GtkText* text,
4772 gint pixel_start_height,
4775 GdkGCValues gc_values;
4778 guint running_offset = lp->tab_cont.pixel_offset;
4779 union { GdkWChar *wc; guchar *ch; } buffer;
4782 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4784 guint selection_start_pos = MIN (old_editable->selection_start_pos, old_editable->selection_end_pos);
4785 guint selection_end_pos = MAX (old_editable->selection_start_pos, old_editable->selection_end_pos);
4787 GtkPropertyMark mark = lp->start;
4788 TabStopMark tab_mark = lp->tab_cont.tab_start;
4789 gint pixel_height = pixel_start_height + lp->font_ascent;
4790 guint chars = lp->displayable_chars;
4792 /* First provide a contiguous segment of memory. This makes reading
4793 * the code below *much* easier, and only incurs the cost of copying
4794 * when the line being displayed spans the gap. */
4795 if (mark.index <= text->gap_position &&
4796 mark.index + chars > text->gap_position)
4798 expand_scratch_buffer (text, chars);
4800 if (text->use_wchar)
4802 for (i = 0; i < chars; i += 1)
4803 text->scratch_buffer.wc[i] = GTK_TEXT_INDEX(text, mark.index + i);
4804 buffer.wc = text->scratch_buffer.wc;
4808 for (i = 0; i < chars; i += 1)
4809 text->scratch_buffer.ch[i] = GTK_TEXT_INDEX(text, mark.index + i);
4810 buffer.ch = text->scratch_buffer.ch;
4815 if (text->use_wchar)
4817 if (mark.index >= text->gap_position)
4818 buffer.wc = text->text.wc + mark.index + text->gap_size;
4820 buffer.wc = text->text.wc + mark.index;
4824 if (mark.index >= text->gap_position)
4825 buffer.ch = text->text.ch + mark.index + text->gap_size;
4827 buffer.ch = text->text.ch + mark.index;
4832 if (running_offset > 0)
4834 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
4835 LINE_HEIGHT (*lp), TRUE);
4841 if ((text->use_wchar && buffer.wc[0] != '\t') ||
4842 (!text->use_wchar && buffer.ch[0] != '\t'))
4844 union { GdkWChar *wc; guchar *ch; } next_tab;
4849 if (text->use_wchar)
4850 for (i=0; i<chars; i++)
4852 if (buffer.wc[i] == '\t')
4854 next_tab.wc = buffer.wc + i;
4859 next_tab.ch = memchr (buffer.ch, '\t', chars);
4861 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4863 if (text->use_wchar)
4866 len = MIN (len, next_tab.wc - buffer.wc);
4871 len = MIN (len, next_tab.ch - buffer.ch);
4874 if (mark.index < selection_start_pos)
4875 len = MIN (len, selection_start_pos - mark.index);
4876 else if (mark.index < selection_end_pos)
4877 len = MIN (len, selection_end_pos - mark.index);
4879 font = MARK_CURRENT_FONT (text, &mark);
4880 if (font->type == GDK_FONT_FONT)
4882 gdk_gc_set_font (text->gc, font);
4883 gdk_gc_get_values (text->gc, &gc_values);
4884 if (text->use_wchar)
4885 pixel_width = gdk_text_width_wc (gc_values.font,
4888 pixel_width = gdk_text_width (gc_values.font,
4893 if (text->use_wchar)
4894 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
4896 pixel_width = gdk_text_width (font, buffer.ch, len);
4899 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4900 pixel_width, LINE_HEIGHT (*lp), TRUE);
4902 if ((mark.index >= selection_start_pos) &&
4903 (mark.index < selection_end_pos))
4905 if (old_editable->has_selection)
4906 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_SELECTED];
4908 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_ACTIVE];
4912 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
4916 if (text->use_wchar)
4917 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
4924 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
4931 running_offset += pixel_width;
4933 advance_tab_mark_n (text, &tab_mark, len);
4937 gint pixels_remaining;
4943 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
4944 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
4946 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4948 spaces_avail = pixels_remaining / space_width;
4949 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
4951 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4952 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
4954 running_offset += tab_mark.to_next_tab *
4955 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4957 advance_tab_mark (text, &tab_mark, '\t');
4960 advance_mark_n (&mark, len);
4961 if (text->use_wchar)
4970 draw_line_wrap (GtkText* text, guint height /* baseline height */)
4977 if (text->line_wrap)
4979 bitmap = text->line_wrap_bitmap;
4980 bitmap_width = line_wrap_width;
4981 bitmap_height = line_wrap_height;
4985 bitmap = text->line_arrow_bitmap;
4986 bitmap_width = line_arrow_width;
4987 bitmap_height = line_arrow_height;
4990 gdk_window_get_size (text->text_area, &width, NULL);
4991 width -= LINE_WRAP_ROOM;
4993 gdk_gc_set_stipple (text->gc,
4996 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
4998 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5000 gdk_gc_set_ts_origin (text->gc,
5002 height - bitmap_height - 1);
5004 gdk_draw_rectangle (text->text_area,
5008 height - bitmap_height - 1 /* one pixel above the baseline. */,
5012 gdk_gc_set_ts_origin (text->gc, 0, 0);
5014 gdk_gc_set_fill (text->gc, GDK_SOLID);
5018 undraw_cursor (GtkText* text, gint absolute)
5020 GtkOldEditable *old_editable = (GtkOldEditable *) text;
5022 TDEBUG (("in undraw_cursor\n"));
5025 text->cursor_drawn_level = 0;
5027 if ((text->cursor_drawn_level ++ == 0) &&
5028 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5029 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5033 g_assert(text->cursor_mark.property);
5035 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5037 draw_bg_rect (text, &text->cursor_mark,
5039 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5040 1, font->ascent + 1, FALSE);
5042 if (text->cursor_char)
5044 if (font->type == GDK_FONT_FONT)
5045 gdk_gc_set_font (text->gc, font);
5047 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5049 gdk_draw_text_wc (text->text_area, font,
5052 text->cursor_pos_y - text->cursor_char_offset,
5060 drawn_cursor_min (GtkText* text)
5064 g_assert(text->cursor_mark.property);
5066 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5068 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5072 drawn_cursor_max (GtkText* text)
5074 g_assert(text->cursor_mark.property);
5076 return text->cursor_pos_y - text->cursor_char_offset;
5080 draw_cursor (GtkText* text, gint absolute)
5082 GtkOldEditable *old_editable = (GtkOldEditable *)text;
5084 TDEBUG (("in draw_cursor\n"));
5087 text->cursor_drawn_level = 1;
5089 if ((--text->cursor_drawn_level == 0) &&
5090 old_editable->editable &&
5091 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5092 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5096 g_assert (text->cursor_mark.property);
5098 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5100 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5102 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
5103 text->cursor_pos_y - text->cursor_char_offset,
5105 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5110 create_bg_gc (GtkText *text)
5114 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5115 values.fill = GDK_TILED;
5117 return gdk_gc_new_with_values (text->text_area, &values,
5118 GDK_GC_FILL | GDK_GC_TILE);
5122 clear_area (GtkText *text, GdkRectangle *area)
5124 GtkWidget *widget = GTK_WIDGET (text);
5130 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5132 gdk_gc_set_ts_origin (text->bg_gc,
5133 (- text->first_onscreen_hor_pixel) % width,
5134 (- text->first_onscreen_ver_pixel) % height);
5136 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5137 area->x, area->y, area->width, area->height);
5140 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5144 expose_text (GtkText* text, GdkRectangle *area, gboolean cursor)
5146 GList *cache = text->line_start_cache;
5147 gint pixels = - text->first_cut_pixels;
5148 gint min_y = MAX (0, area->y);
5149 gint max_y = MAX (0, area->y + area->height);
5152 gdk_window_get_size (text->text_area, NULL, &height);
5153 max_y = MIN (max_y, height);
5155 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5157 clear_area (text, area);
5159 for (; pixels < height; cache = cache->next)
5161 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5163 draw_line (text, pixels, &CACHE_DATA(cache));
5165 if (CACHE_DATA(cache).wraps)
5166 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5169 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5171 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5172 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5174 /* We undraw and draw the cursor here to get the drawn
5175 * level right ... FIXME - maybe the second parameter
5176 * of draw_cursor should work differently
5178 undraw_cursor (text, FALSE);
5179 draw_cursor (text, FALSE);
5183 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5187 fetch_lines_forward (text, 1);
5196 gtk_text_update_text (GtkOldEditable *old_editable,
5200 GtkText *text = GTK_TEXT (old_editable);
5202 GList *cache = text->line_start_cache;
5203 gint pixels = - text->first_cut_pixels;
5209 end_pos = TEXT_LENGTH (text);
5211 if (end_pos < start_pos)
5214 gdk_window_get_size (text->text_area, &width, &height);
5220 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5222 for (; pixels < height; cache = cache->next)
5224 if (CACHE_DATA(cache).start.index < end_pos)
5226 if (CACHE_DATA(cache).end.index >= start_pos)
5229 area.y = MAX(0,pixels);
5230 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5236 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5240 fetch_lines_forward (text, 1);
5248 expose_text (text, &area, TRUE);
5252 recompute_geometry (GtkText* text)
5254 GtkPropertyMark mark, start_mark;
5261 mark = start_mark = set_vertical_scroll (text);
5263 /* We need a real start of a line when calling fetch_lines().
5264 * not the start of a wrapped line.
5266 while (mark.index > 0 &&
5267 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5268 decrement_mark (&mark);
5270 gdk_window_get_size (text->text_area, &width, &height);
5272 /* Fetch an entire line, to make sure that we get all the text
5273 * we backed over above, in addition to enough text to fill up
5274 * the space vertically
5277 new_lines = fetch_lines (text,
5283 mark = CACHE_DATA (g_list_last (new_lines)).end;
5284 if (!LAST_INDEX (text, mark))
5286 advance_mark (&mark);
5288 new_lines = g_list_concat (new_lines,
5293 height + text->first_cut_pixels));
5296 /* Now work forward to the actual first onscreen line */
5298 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5299 new_lines = new_lines->next;
5301 text->line_start_cache = new_lines;
5303 find_cursor (text, TRUE);
5306 /**********************************************************************/
5308 /**********************************************************************/
5311 gtk_text_set_selection (GtkOldEditable *old_editable,
5315 GtkText *text = GTK_TEXT (old_editable);
5317 guint start1, end1, start2, end2;
5320 end = TEXT_LENGTH (text);
5322 start1 = MIN(start,end);
5323 end1 = MAX(start,end);
5324 start2 = MIN(old_editable->selection_start_pos, old_editable->selection_end_pos);
5325 end2 = MAX(old_editable->selection_start_pos, old_editable->selection_end_pos);
5327 if (start2 < start1)
5331 tmp = start1; start1 = start2; start2 = tmp;
5332 tmp = end1; end1 = end2; end2 = tmp;
5335 undraw_cursor (text, FALSE);
5336 old_editable->selection_start_pos = start;
5337 old_editable->selection_end_pos = end;
5338 draw_cursor (text, FALSE);
5340 /* Expose only what changed */
5342 if (start1 < start2)
5343 gtk_text_update_text (old_editable, start1, MIN(end1, start2));
5346 gtk_text_update_text (old_editable, MAX(end1, start2), end2);
5347 else if (end2 < end1)
5348 gtk_text_update_text (old_editable, end2, end1);
5352 /**********************************************************************/
5354 /**********************************************************************/
5356 #ifdef DEBUG_GTK_TEXT
5358 gtk_text_show_cache_line (GtkText *text, GList *cache,
5359 const char* what, const char* func, gint line)
5361 LineParams *lp = &CACHE_DATA(cache);
5364 if (cache == text->line_start_cache)
5365 g_message ("Line Start Cache: ");
5367 if (cache == text->current_line)
5368 g_message("Current Line: ");
5370 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
5378 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
5379 g_message ("%c", GTK_TEXT_INDEX (text, i));
5385 gtk_text_show_cache (GtkText *text, const char* func, gint line)
5387 GList *l = text->line_start_cache;
5393 /* back up to the absolute beginning of the line cache */
5397 g_message ("*** line cache ***\n");
5398 for (; l; l = l->next)
5399 gtk_text_show_cache_line (text, l, "all", func, line);
5403 gtk_text_assert_mark (GtkText *text,
5404 GtkPropertyMark *mark,
5405 GtkPropertyMark *before,
5406 GtkPropertyMark *after,
5411 GtkPropertyMark correct_mark = find_mark (text, mark->index);
5413 if (mark->offset != correct_mark.offset ||
5414 mark->property != correct_mark.property)
5415 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
5419 gtk_text_assert (GtkText *text,
5423 GList* cache = text->line_start_cache;
5424 GtkPropertyMark* before_mark = NULL;
5425 GtkPropertyMark* after_mark = NULL;
5427 gtk_text_show_props (text, msg, line);
5429 for (; cache->prev; cache = cache->prev)
5432 g_message ("*** line markers ***\n");
5434 for (; cache; cache = cache->next)
5436 after_mark = &CACHE_DATA(cache).end;
5437 gtk_text_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
5438 before_mark = &CACHE_DATA(cache).start;
5441 after_mark = &CACHE_DATA(cache->next).start;
5445 gtk_text_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
5446 before_mark = &CACHE_DATA(cache).end;
5451 gtk_text_show_adj (GtkText *text,
5457 g_message ("*** adjustment ***\n");
5459 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
5466 adj->step_increment,
5467 adj->page_increment,
5472 gtk_text_show_props (GtkText *text,
5476 GList* props = text->text_properties;
5479 g_message ("%s:%d: ", msg, line);
5481 for (; props; props = props->next)
5483 TextProperty *p = (TextProperty*)props->data;
5485 proplen += p->length;
5487 g_message ("[%d,%p,", p->length, p);
5488 if (p->flags & PROPERTY_FONT)
5489 g_message ("%p,", p->font);
5492 if (p->flags & PROPERTY_FOREGROUND)
5493 g_message ("%ld, ", p->fore_color.pixel);
5496 if (p->flags & PROPERTY_BACKGROUND)
5497 g_message ("%ld] ", p->back_color.pixel);
5504 if (proplen - 1 != TEXT_LENGTH(text))
5505 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);
5509 #define __GTK_TEXT_C__
5510 #include "gtkaliasdef.c"