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 #define GTK_ENABLE_BROKEN
42 #include "line-wrap.xbm"
43 #include "line-arrow.xbm"
44 #include "gtkprivate.h"
49 #define INITIAL_BUFFER_SIZE 1024
50 #define INITIAL_LINE_CACHE_SIZE 256
51 #define MIN_GAP_SIZE 256
52 #define LINE_DELIM '\n'
53 #define MIN_TEXT_WIDTH_LINES 20
54 #define MIN_TEXT_HEIGHT_LINES 10
55 #define TEXT_BORDER_ROOM 1
56 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
57 #define DEFAULT_TAB_STOP_WIDTH 4
58 #define SCROLL_PIXELS 5
59 #define KEY_SCROLL_PIXELS 10
60 #define SCROLL_TIME 100
61 #define FREEZE_LENGTH 1024
62 /* Freeze text when inserting or deleting more than this many characters */
64 #define SET_PROPERTY_MARK(m, p, o) do { \
65 (m)->property = (p); \
68 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
69 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
70 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
71 (mark)->property->prev->data \
73 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
74 #define MARK_LIST_PTR(mark) ((mark)->property)
75 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
76 #define MARK_OFFSET(mark) ((mark)->offset)
77 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
80 #define MARK_CURRENT_FONT(text, mark) \
81 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
82 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
83 gtk_style_get_font (GTK_WIDGET (text)->style))
84 #define MARK_CURRENT_FORE(text, mark) \
85 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
86 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
87 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
88 #define MARK_CURRENT_BACK(text, mark) \
89 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
90 &MARK_CURRENT_PROPERTY(mark)->back_color : \
91 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
92 #define MARK_CURRENT_TEXT_FONT(text, mark) \
93 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
94 MARK_CURRENT_PROPERTY(mark)->font : \
97 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
98 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
99 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
100 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
101 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
102 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
103 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
104 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
114 typedef struct _TextProperty TextProperty;
115 typedef struct _TabStopMark TabStopMark;
116 typedef struct _PrevTabCont PrevTabCont;
117 typedef struct _FetchLinesData FetchLinesData;
118 typedef struct _LineParams LineParams;
119 typedef struct _SetVerticalScrollData SetVerticalScrollData;
121 typedef gint (*LineIteratorFunction) (GtkText* text, LineParams* lp, void* data);
129 struct _SetVerticalScrollData {
131 gint last_didnt_wrap;
132 gint last_line_start;
133 GtkPropertyMark mark;
138 /* The actual font. */
142 gint16 char_widths[256];
146 PROPERTY_FONT = 1 << 0,
147 PROPERTY_FOREGROUND = 1 << 1,
148 PROPERTY_BACKGROUND = 1 << 2
156 /* Background Color. */
159 /* Foreground Color. */
162 /* Show which properties are set */
163 TextPropertyFlags flags;
165 /* Length of this property. */
171 GList* tab_stops; /* Index into list containing the next tab position. If
172 * NULL, using default widths. */
179 TabStopMark tab_start;
182 struct _FetchLinesData
195 guint displayable_chars;
198 PrevTabCont tab_cont;
199 PrevTabCont tab_cont_next;
201 GtkPropertyMark start;
206 static void gtk_text_class_init (GtkTextClass *klass);
207 static void gtk_text_set_property (GObject *object,
211 static void gtk_text_get_property (GObject *object,
215 static void gtk_text_editable_init (GtkEditableClass *iface);
216 static void gtk_text_init (GtkText *text);
217 static void gtk_text_destroy (GtkObject *object);
218 static void gtk_text_finalize (GObject *object);
219 static void gtk_text_realize (GtkWidget *widget);
220 static void gtk_text_unrealize (GtkWidget *widget);
221 static void gtk_text_style_set (GtkWidget *widget,
222 GtkStyle *previous_style);
223 static void gtk_text_state_changed (GtkWidget *widget,
224 GtkStateType previous_state);
225 static void gtk_text_draw_focus (GtkWidget *widget);
226 static void gtk_text_size_request (GtkWidget *widget,
227 GtkRequisition *requisition);
228 static void gtk_text_size_allocate (GtkWidget *widget,
229 GtkAllocation *allocation);
230 static void gtk_text_adjustment (GtkAdjustment *adjustment,
232 static void gtk_text_insert_text (GtkEditable *editable,
233 const gchar *new_text,
234 gint new_text_length,
236 static void gtk_text_delete_text (GtkEditable *editable,
239 static void gtk_text_update_text (GtkOldEditable *old_editable,
242 static gchar *gtk_text_get_chars (GtkOldEditable *old_editable,
245 static void gtk_text_set_selection (GtkOldEditable *old_editable,
248 static void gtk_text_real_set_editable (GtkOldEditable *old_editable,
249 gboolean is_editable);
251 static void gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
255 static gint gtk_text_expose (GtkWidget *widget,
256 GdkEventExpose *event);
257 static gint gtk_text_button_press (GtkWidget *widget,
258 GdkEventButton *event);
259 static gint gtk_text_button_release (GtkWidget *widget,
260 GdkEventButton *event);
261 static gint gtk_text_motion_notify (GtkWidget *widget,
262 GdkEventMotion *event);
263 static gint gtk_text_key_press (GtkWidget *widget,
266 static void move_gap (GtkText* text, guint index);
267 static void make_forward_space (GtkText* text, guint len);
269 /* Property management */
270 static GtkTextFont* get_text_font (GdkFont* gfont);
271 static void text_font_unref (GtkTextFont *text_font);
273 static void insert_text_property (GtkText* text, GdkFont* font,
274 const GdkColor *fore, const GdkColor* back, guint len);
275 static TextProperty* new_text_property (GtkText *text, GdkFont* font,
276 const GdkColor* fore, const GdkColor* back, guint length);
277 static void destroy_text_property (TextProperty *prop);
278 static void init_properties (GtkText *text);
279 static void realize_property (GtkText *text, TextProperty *prop);
280 static void realize_properties (GtkText *text);
281 static void unrealize_property (GtkText *text, TextProperty *prop);
282 static void unrealize_properties (GtkText *text);
284 static void delete_text_property (GtkText* text, guint len);
286 static guint pixel_height_of (GtkText* text, GList* cache_line);
288 /* Property Movement and Size Computations */
289 static void advance_mark (GtkPropertyMark* mark);
290 static void decrement_mark (GtkPropertyMark* mark);
291 static void advance_mark_n (GtkPropertyMark* mark, gint n);
292 static void decrement_mark_n (GtkPropertyMark* mark, gint n);
293 static void move_mark_n (GtkPropertyMark* mark, gint n);
294 static GtkPropertyMark find_mark (GtkText* text, guint mark_position);
295 static GtkPropertyMark find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near);
296 static void find_line_containing_point (GtkText* text, guint point,
300 static void compute_lines_pixels (GtkText* text, guint char_count,
301 guint *lines, guint *pixels);
303 static gint total_line_height (GtkText* text,
306 static LineParams find_line_params (GtkText* text,
307 const GtkPropertyMark *mark,
308 const PrevTabCont *tab_cont,
309 PrevTabCont *next_cont);
310 static void recompute_geometry (GtkText* text);
311 static void insert_expose (GtkText* text, guint old_pixels, gint nchars, guint new_line_count);
312 static void delete_expose (GtkText* text,
316 static GdkGC *create_bg_gc (GtkText *text);
317 static void clear_area (GtkText *text, GdkRectangle *area);
318 static void draw_line (GtkText* text,
321 static void draw_line_wrap (GtkText* text,
323 static void draw_cursor (GtkText* text, gint absolute);
324 static void undraw_cursor (GtkText* text, gint absolute);
325 static gint drawn_cursor_min (GtkText* text);
326 static gint drawn_cursor_max (GtkText* text);
327 static void expose_text (GtkText* text, GdkRectangle *area, gboolean cursor);
329 /* Search and Placement. */
330 static void find_cursor (GtkText* text,
332 static void find_cursor_at_line (GtkText* text,
333 const LineParams* start_line,
335 static void find_mouse_cursor (GtkText* text, gint x, gint y);
338 static void adjust_adj (GtkText* text, GtkAdjustment* adj);
339 static void scroll_up (GtkText* text, gint diff);
340 static void scroll_down (GtkText* text, gint diff);
341 static void scroll_int (GtkText* text, gint diff);
343 static void process_exposes (GtkText *text);
345 /* Cache Management. */
346 static void free_cache (GtkText* text);
347 static GList* remove_cache_line (GtkText* text, GList* list);
350 static void move_cursor_buffer_ver (GtkText *text, int dir);
351 static void move_cursor_page_ver (GtkText *text, int dir);
352 static void move_cursor_ver (GtkText *text, int count);
353 static void move_cursor_hor (GtkText *text, int count);
355 /* Binding actions */
356 static void gtk_text_move_cursor (GtkOldEditable *old_editable,
359 static void gtk_text_move_word (GtkOldEditable *old_editable,
361 static void gtk_text_move_page (GtkOldEditable *old_editable,
364 static void gtk_text_move_to_row (GtkOldEditable *old_editable,
366 static void gtk_text_move_to_column (GtkOldEditable *old_editable,
368 static void gtk_text_kill_char (GtkOldEditable *old_editable,
370 static void gtk_text_kill_word (GtkOldEditable *old_editable,
372 static void gtk_text_kill_line (GtkOldEditable *old_editable,
376 static void gtk_text_move_forward_character (GtkText *text);
377 static void gtk_text_move_backward_character (GtkText *text);
378 static void gtk_text_move_forward_word (GtkText *text);
379 static void gtk_text_move_backward_word (GtkText *text);
380 static void gtk_text_move_beginning_of_line (GtkText *text);
381 static void gtk_text_move_end_of_line (GtkText *text);
382 static void gtk_text_move_next_line (GtkText *text);
383 static void gtk_text_move_previous_line (GtkText *text);
385 static void gtk_text_delete_forward_character (GtkText *text);
386 static void gtk_text_delete_backward_character (GtkText *text);
387 static void gtk_text_delete_forward_word (GtkText *text);
388 static void gtk_text_delete_backward_word (GtkText *text);
389 static void gtk_text_delete_line (GtkText *text);
390 static void gtk_text_delete_to_line_end (GtkText *text);
391 static void gtk_text_select_word (GtkText *text,
393 static void gtk_text_select_line (GtkText *text,
396 static void gtk_text_set_position (GtkOldEditable *old_editable,
399 /* #define DEBUG_GTK_TEXT */
401 #if defined(DEBUG_GTK_TEXT) && defined(__GNUC__)
402 /* Debugging utilities. */
403 static void gtk_text_assert_mark (GtkText *text,
404 GtkPropertyMark *mark,
405 GtkPropertyMark *before,
406 GtkPropertyMark *after,
411 static void gtk_text_assert (GtkText *text,
414 static void gtk_text_show_cache_line (GtkText *text, GList *cache,
415 const char* what, const char* func, gint line);
416 static void gtk_text_show_cache (GtkText *text, const char* func, gint line);
417 static void gtk_text_show_adj (GtkText *text,
422 static void gtk_text_show_props (GtkText* test,
426 #define TDEBUG(args) g_message args
427 #define TEXT_ASSERT(text) gtk_text_assert (text,__PRETTY_FUNCTION__,__LINE__)
428 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_text_assert_mark (text,mark, \
429 __PRETTY_FUNCTION__,msg,__LINE__)
430 #define TEXT_SHOW(text) gtk_text_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
431 #define TEXT_SHOW_LINE(text,line,msg) gtk_text_show_cache_line (text,line,msg,\
432 __PRETTY_FUNCTION__,__LINE__)
433 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_text_show_adj (text,adj,msg, \
434 __PRETTY_FUNCTION__,__LINE__)
437 #define TEXT_ASSERT(text)
438 #define TEXT_ASSERT_MARK(text,mark,msg)
439 #define TEXT_SHOW(text)
440 #define TEXT_SHOW_LINE(text,line,msg)
441 #define TEXT_SHOW_ADJ(text,adj,msg)
444 static GtkWidgetClass *parent_class = NULL;
446 /**********************************************************************/
448 /**********************************************************************/
451 gtk_text_get_type (void)
453 static GtkType text_type = 0;
457 static const GtkTypeInfo text_info =
461 sizeof (GtkTextClass),
462 (GtkClassInitFunc) gtk_text_class_init,
463 (GtkObjectInitFunc) gtk_text_init,
464 /* reserved_1 */ NULL,
465 /* reserved_2 */ NULL,
466 (GtkClassInitFunc) NULL,
469 static const GInterfaceInfo editable_info =
471 (GInterfaceInitFunc) gtk_text_editable_init, /* interface_init */
472 NULL, /* interface_finalize */
473 NULL /* interface_data */
477 text_type = gtk_type_unique (GTK_TYPE_OLD_EDITABLE, &text_info);
478 g_type_add_interface_static (text_type,
487 gtk_text_class_init (GtkTextClass *class)
489 GObjectClass *gobject_class;
490 GtkObjectClass *object_class;
491 GtkWidgetClass *widget_class;
492 GtkOldEditableClass *old_editable_class;
494 gobject_class = G_OBJECT_CLASS (class);
495 object_class = (GtkObjectClass*) class;
496 widget_class = (GtkWidgetClass*) class;
497 old_editable_class = (GtkOldEditableClass*) class;
498 parent_class = gtk_type_class (GTK_TYPE_OLD_EDITABLE);
500 gobject_class->finalize = gtk_text_finalize;
501 gobject_class->set_property = gtk_text_set_property;
502 gobject_class->get_property = gtk_text_get_property;
504 object_class->destroy = gtk_text_destroy;
506 widget_class->realize = gtk_text_realize;
507 widget_class->unrealize = gtk_text_unrealize;
508 widget_class->style_set = gtk_text_style_set;
509 widget_class->state_changed = gtk_text_state_changed;
510 widget_class->size_request = gtk_text_size_request;
511 widget_class->size_allocate = gtk_text_size_allocate;
512 widget_class->expose_event = gtk_text_expose;
513 widget_class->button_press_event = gtk_text_button_press;
514 widget_class->button_release_event = gtk_text_button_release;
515 widget_class->motion_notify_event = gtk_text_motion_notify;
516 widget_class->key_press_event = gtk_text_key_press;
518 old_editable_class->set_editable = gtk_text_real_set_editable;
520 old_editable_class->move_cursor = gtk_text_move_cursor;
521 old_editable_class->move_word = gtk_text_move_word;
522 old_editable_class->move_page = gtk_text_move_page;
523 old_editable_class->move_to_row = gtk_text_move_to_row;
524 old_editable_class->move_to_column = gtk_text_move_to_column;
526 old_editable_class->kill_char = gtk_text_kill_char;
527 old_editable_class->kill_word = gtk_text_kill_word;
528 old_editable_class->kill_line = gtk_text_kill_line;
530 old_editable_class->update_text = gtk_text_update_text;
531 old_editable_class->get_chars = gtk_text_get_chars;
532 old_editable_class->set_selection = gtk_text_set_selection;
533 old_editable_class->set_position = gtk_text_set_position;
535 class->set_scroll_adjustments = gtk_text_set_adjustments;
537 g_object_class_install_property (gobject_class,
539 g_param_spec_object ("hadjustment",
540 P_("Horizontal Adjustment"),
541 P_("Horizontal adjustment for the text widget"),
543 GTK_PARAM_READWRITE));
545 g_object_class_install_property (gobject_class,
547 g_param_spec_object ("vadjustment",
548 P_("Vertical Adjustment"),
549 P_("Vertical adjustment for the text widget"),
551 GTK_PARAM_READWRITE));
553 g_object_class_install_property (gobject_class,
555 g_param_spec_boolean ("line-wrap",
557 P_("Whether lines are wrapped at widget edges"),
559 GTK_PARAM_READWRITE));
561 g_object_class_install_property (gobject_class,
563 g_param_spec_boolean ("word-wrap",
565 P_("Whether words are wrapped at widget edges"),
567 GTK_PARAM_READWRITE));
569 widget_class->set_scroll_adjustments_signal =
570 gtk_signal_new (I_("set_scroll_adjustments"),
572 GTK_CLASS_TYPE (object_class),
573 GTK_SIGNAL_OFFSET (GtkTextClass, set_scroll_adjustments),
574 _gtk_marshal_VOID__OBJECT_OBJECT,
575 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
579 gtk_text_set_property (GObject *object,
586 text = GTK_TEXT (object);
590 case PROP_HADJUSTMENT:
591 gtk_text_set_adjustments (text,
592 g_value_get_object (value),
595 case PROP_VADJUSTMENT:
596 gtk_text_set_adjustments (text,
598 g_value_get_object (value));
601 gtk_text_set_line_wrap (text, g_value_get_boolean (value));
604 gtk_text_set_word_wrap (text, g_value_get_boolean (value));
607 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
613 gtk_text_get_property (GObject *object,
620 text = GTK_TEXT (object);
624 case PROP_HADJUSTMENT:
625 g_value_set_object (value, text->hadj);
627 case PROP_VADJUSTMENT:
628 g_value_set_object (value, text->vadj);
631 g_value_set_boolean (value, text->line_wrap);
634 g_value_set_boolean (value, text->word_wrap);
637 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
643 gtk_text_editable_init (GtkEditableClass *iface)
645 iface->insert_text = gtk_text_insert_text;
646 iface->delete_text = gtk_text_delete_text;
650 gtk_text_init (GtkText *text)
652 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
654 text->text_area = NULL;
659 text->line_wrap_bitmap = NULL;
660 text->line_arrow_bitmap = NULL;
662 text->use_wchar = FALSE;
663 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
664 text->text_len = INITIAL_BUFFER_SIZE;
666 text->scratch_buffer.ch = NULL;
667 text->scratch_buffer_len = 0;
669 text->freeze_count = 0;
671 text->default_tab_width = 4;
672 text->tab_stops = NULL;
674 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
675 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
677 text->line_start_cache = NULL;
678 text->first_cut_pixels = 0;
680 text->line_wrap = TRUE;
681 text->word_wrap = FALSE;
686 text->current_font = NULL;
688 init_properties (text);
690 GTK_OLD_EDITABLE (text)->editable = FALSE;
692 gtk_text_set_adjustments (text, NULL, NULL);
693 gtk_editable_set_position (GTK_EDITABLE (text), 0);
697 gtk_text_new (GtkAdjustment *hadj,
703 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
705 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
707 text = gtk_widget_new (GTK_TYPE_TEXT,
716 gtk_text_set_word_wrap (GtkText *text,
719 g_return_if_fail (GTK_IS_TEXT (text));
721 text->word_wrap = (word_wrap != FALSE);
723 if (GTK_WIDGET_REALIZED (text))
725 recompute_geometry (text);
726 gtk_widget_queue_draw (GTK_WIDGET (text));
729 g_object_notify (G_OBJECT (text), "word-wrap");
733 gtk_text_set_line_wrap (GtkText *text,
736 g_return_if_fail (GTK_IS_TEXT (text));
738 text->line_wrap = (line_wrap != FALSE);
740 if (GTK_WIDGET_REALIZED (text))
742 recompute_geometry (text);
743 gtk_widget_queue_draw (GTK_WIDGET (text));
746 g_object_notify (G_OBJECT (text), "line-wrap");
750 gtk_text_set_editable (GtkText *text,
751 gboolean is_editable)
753 g_return_if_fail (GTK_IS_TEXT (text));
755 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
759 gtk_text_real_set_editable (GtkOldEditable *old_editable,
760 gboolean is_editable)
764 g_return_if_fail (GTK_IS_TEXT (old_editable));
766 text = GTK_TEXT (old_editable);
768 old_editable->editable = (is_editable != FALSE);
771 draw_cursor (text, TRUE);
773 undraw_cursor (text, TRUE);
777 gtk_text_set_adjustments (GtkText *text,
781 g_return_if_fail (GTK_IS_TEXT (text));
783 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
785 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
787 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
789 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
791 if (text->hadj && (text->hadj != hadj))
793 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
794 gtk_object_unref (GTK_OBJECT (text->hadj));
797 if (text->vadj && (text->vadj != vadj))
799 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
800 gtk_object_unref (GTK_OBJECT (text->vadj));
803 g_object_freeze_notify (G_OBJECT (text));
804 if (text->hadj != hadj)
807 g_object_ref_sink (text->hadj);
809 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
810 (GtkSignalFunc) gtk_text_adjustment,
812 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
813 (GtkSignalFunc) gtk_text_adjustment,
815 gtk_signal_connect (GTK_OBJECT (text->hadj), "destroy",
816 (GtkSignalFunc) gtk_text_adjustment_destroyed,
818 gtk_text_adjustment (hadj, text);
820 g_object_notify (G_OBJECT (text), "hadjustment");
823 if (text->vadj != vadj)
826 g_object_ref_sink (text->vadj);
828 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
829 (GtkSignalFunc) gtk_text_adjustment,
831 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
832 (GtkSignalFunc) gtk_text_adjustment,
834 gtk_signal_connect (GTK_OBJECT (text->vadj), "destroy",
835 (GtkSignalFunc) gtk_text_adjustment_destroyed,
837 gtk_text_adjustment (vadj, text);
839 g_object_notify (G_OBJECT (text), "vadjustment");
841 g_object_thaw_notify (G_OBJECT (text));
845 gtk_text_set_point (GtkText *text,
848 g_return_if_fail (GTK_IS_TEXT (text));
849 g_return_if_fail (index <= TEXT_LENGTH (text));
851 text->point = find_mark (text, index);
855 gtk_text_get_point (GtkText *text)
857 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
859 return text->point.index;
863 gtk_text_get_length (GtkText *text)
865 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
867 return TEXT_LENGTH (text);
871 gtk_text_freeze (GtkText *text)
873 g_return_if_fail (GTK_IS_TEXT (text));
875 text->freeze_count++;
879 gtk_text_thaw (GtkText *text)
881 g_return_if_fail (GTK_IS_TEXT (text));
883 if (text->freeze_count)
884 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
886 recompute_geometry (text);
887 gtk_widget_queue_draw (GTK_WIDGET (text));
892 gtk_text_insert (GtkText *text,
894 const GdkColor *fore,
895 const GdkColor *back,
899 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
900 gboolean frozen = FALSE;
902 gint new_line_count = 1;
903 guint old_height = 0;
908 g_return_if_fail (GTK_IS_TEXT (text));
911 length = strlen (chars);
918 if (!text->freeze_count && (length > FREEZE_LENGTH))
920 gtk_text_freeze (text);
924 if (!text->freeze_count && (text->line_start_cache != NULL))
926 find_line_containing_point (text, text->point.index, TRUE);
927 old_height = total_line_height (text, text->current_line, 1);
930 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
932 GtkWidget *widget = GTK_WIDGET (text);
934 gtk_widget_ensure_style (widget);
935 if ((widget->style) &&
936 (gtk_style_get_font (widget->style)->type == GDK_FONT_FONTSET))
938 text->use_wchar = TRUE;
939 g_free (text->text.ch);
940 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
941 text->text_len = INITIAL_BUFFER_SIZE;
942 g_free (text->scratch_buffer.ch);
943 text->scratch_buffer.wc = NULL;
944 text->scratch_buffer_len = 0;
948 move_gap (text, text->point.index);
949 make_forward_space (text, length);
953 char *chars_nt = (char *)chars;
956 chars_nt = g_new (char, length+1);
957 memcpy (chars_nt, chars, length);
958 chars_nt[length] = 0;
960 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
962 if (chars_nt != chars)
970 memcpy(text->text.ch + text->gap_position, chars, length);
973 if (!text->freeze_count && (text->line_start_cache != NULL))
977 for (i=0; i<numwcs; i++)
978 if (text->text.wc[text->gap_position + i] == '\n')
983 for (i=0; i<numwcs; i++)
984 if (text->text.ch[text->gap_position + i] == '\n')
991 insert_text_property (text, font, fore, back, numwcs);
993 text->gap_size -= numwcs;
994 text->gap_position += numwcs;
996 if (text->point.index < text->first_line_start_index)
997 text->first_line_start_index += numwcs;
998 if (text->point.index < old_editable->selection_start_pos)
999 old_editable->selection_start_pos += numwcs;
1000 if (text->point.index < old_editable->selection_end_pos)
1001 old_editable->selection_end_pos += numwcs;
1002 /* We'll reset the cursor later anyways if we aren't frozen */
1003 if (text->point.index < text->cursor_mark.index)
1004 text->cursor_mark.index += numwcs;
1006 advance_mark_n (&text->point, numwcs);
1008 if (!text->freeze_count && (text->line_start_cache != NULL))
1009 insert_expose (text, old_height, numwcs, new_line_count);
1013 gtk_text_thaw (text);
1017 gtk_text_backward_delete (GtkText *text,
1020 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1022 if (nchars > text->point.index || nchars <= 0)
1025 gtk_text_set_point (text, text->point.index - nchars);
1027 return gtk_text_forward_delete (text, nchars);
1031 gtk_text_forward_delete (GtkText *text,
1034 guint old_lines = 0, old_height = 0;
1035 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
1036 gboolean frozen = FALSE;
1038 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1040 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1043 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1045 gtk_text_freeze (text);
1049 if (!text->freeze_count && text->line_start_cache != NULL)
1051 /* We need to undraw the cursor here, since we may later
1052 * delete the cursor's property
1054 undraw_cursor (text, FALSE);
1055 find_line_containing_point (text, text->point.index, TRUE);
1056 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1059 /* FIXME, or resizing after deleting will be odd */
1060 if (text->point.index < text->first_line_start_index)
1062 if (text->point.index + nchars >= text->first_line_start_index)
1064 text->first_line_start_index = text->point.index;
1065 while ((text->first_line_start_index > 0) &&
1066 (GTK_TEXT_INDEX (text, text->first_line_start_index - 1)
1068 text->first_line_start_index -= 1;
1072 text->first_line_start_index -= nchars;
1075 if (text->point.index < old_editable->selection_start_pos)
1076 old_editable->selection_start_pos -=
1077 MIN(nchars, old_editable->selection_start_pos - text->point.index);
1078 if (text->point.index < old_editable->selection_end_pos)
1079 old_editable->selection_end_pos -=
1080 MIN(nchars, old_editable->selection_end_pos - text->point.index);
1081 /* We'll reset the cursor later anyways if we aren't frozen */
1082 if (text->point.index < text->cursor_mark.index)
1083 move_mark_n (&text->cursor_mark,
1084 -MIN(nchars, text->cursor_mark.index - text->point.index));
1086 move_gap (text, text->point.index);
1088 text->gap_size += nchars;
1090 delete_text_property (text, nchars);
1092 if (!text->freeze_count && (text->line_start_cache != NULL))
1094 delete_expose (text, nchars, old_lines, old_height);
1095 draw_cursor (text, FALSE);
1099 gtk_text_thaw (text);
1105 gtk_text_set_position (GtkOldEditable *old_editable,
1108 GtkText *text = (GtkText *) old_editable;
1111 position = gtk_text_get_length (text);
1113 undraw_cursor (text, FALSE);
1114 text->cursor_mark = find_mark (text, position);
1115 find_cursor (text, TRUE);
1116 draw_cursor (text, FALSE);
1117 gtk_editable_select_region (GTK_EDITABLE (old_editable), 0, 0);
1121 gtk_text_get_chars (GtkOldEditable *old_editable,
1129 g_return_val_if_fail (GTK_IS_TEXT (old_editable), NULL);
1130 text = GTK_TEXT (old_editable);
1133 end_pos = TEXT_LENGTH (text);
1135 if ((start_pos < 0) ||
1136 (end_pos > TEXT_LENGTH (text)) ||
1137 (end_pos < start_pos))
1140 move_gap (text, TEXT_LENGTH (text));
1141 make_forward_space (text, 1);
1143 if (text->use_wchar)
1146 ch = text->text.wc[end_pos];
1147 text->text.wc[end_pos] = 0;
1148 retval = gdk_wcstombs (text->text.wc + start_pos);
1149 text->text.wc[end_pos] = ch;
1154 ch = text->text.ch[end_pos];
1155 text->text.ch[end_pos] = 0;
1156 retval = g_strdup (text->text.ch + start_pos);
1157 text->text.ch[end_pos] = ch;
1165 gtk_text_destroy (GtkObject *object)
1167 GtkText *text = GTK_TEXT (object);
1171 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1172 gtk_object_unref (GTK_OBJECT (text->hadj));
1177 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1178 gtk_object_unref (GTK_OBJECT (text->vadj));
1184 g_source_remove (text->timer);
1188 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1192 gtk_text_finalize (GObject *object)
1194 GtkText *text = GTK_TEXT (object);
1197 /* Clean up the internal structures */
1198 if (text->use_wchar)
1199 g_free (text->text.wc);
1201 g_free (text->text.ch);
1203 tmp_list = text->text_properties;
1206 destroy_text_property (tmp_list->data);
1207 tmp_list = tmp_list->next;
1210 if (text->current_font)
1211 text_font_unref (text->current_font);
1213 g_list_free (text->text_properties);
1215 if (text->use_wchar)
1217 g_free (text->scratch_buffer.wc);
1221 g_free (text->scratch_buffer.ch);
1224 g_list_free (text->tab_stops);
1226 G_OBJECT_CLASS (parent_class)->finalize (object);
1230 gtk_text_realize (GtkWidget *widget)
1232 GtkText *text = GTK_TEXT (widget);
1233 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (widget);
1234 GdkWindowAttr attributes;
1235 gint attributes_mask;
1237 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1239 attributes.window_type = GDK_WINDOW_CHILD;
1240 attributes.x = widget->allocation.x;
1241 attributes.y = widget->allocation.y;
1242 attributes.width = widget->allocation.width;
1243 attributes.height = widget->allocation.height;
1244 attributes.wclass = GDK_INPUT_OUTPUT;
1245 attributes.visual = gtk_widget_get_visual (widget);
1246 attributes.colormap = gtk_widget_get_colormap (widget);
1247 attributes.event_mask = gtk_widget_get_events (widget);
1248 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1249 GDK_BUTTON_PRESS_MASK |
1250 GDK_BUTTON_RELEASE_MASK |
1251 GDK_BUTTON_MOTION_MASK |
1252 GDK_ENTER_NOTIFY_MASK |
1253 GDK_LEAVE_NOTIFY_MASK |
1254 GDK_KEY_PRESS_MASK);
1255 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1257 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1258 gdk_window_set_user_data (widget->window, text);
1260 attributes.x = (widget->style->xthickness + TEXT_BORDER_ROOM);
1261 attributes.y = (widget->style->ythickness + TEXT_BORDER_ROOM);
1262 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1263 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1265 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1266 attributes_mask |= GDK_WA_CURSOR;
1268 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1269 gdk_window_set_user_data (text->text_area, text);
1271 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1273 widget->style = gtk_style_attach (widget->style, widget->window);
1275 /* Can't call gtk_style_set_background here because it's handled specially */
1276 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1277 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1279 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1280 text->bg_gc = create_bg_gc (text);
1282 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1283 (gchar*) line_wrap_bits,
1287 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1288 (gchar*) line_arrow_bits,
1292 text->gc = gdk_gc_new (text->text_area);
1293 gdk_gc_set_exposures (text->gc, TRUE);
1294 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1296 realize_properties (text);
1297 gdk_window_show (text->text_area);
1298 init_properties (text);
1300 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1301 gtk_old_editable_claim_selection (old_editable, TRUE, GDK_CURRENT_TIME);
1303 recompute_geometry (text);
1307 gtk_text_style_set (GtkWidget *widget,
1308 GtkStyle *previous_style)
1310 GtkText *text = GTK_TEXT (widget);
1312 if (GTK_WIDGET_REALIZED (widget))
1314 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1315 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1319 gdk_gc_destroy (text->bg_gc);
1323 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1324 text->bg_gc = create_bg_gc (text);
1326 recompute_geometry (text);
1329 if (text->current_font)
1330 text_font_unref (text->current_font);
1331 text->current_font = get_text_font (gtk_style_get_font (widget->style));
1335 gtk_text_state_changed (GtkWidget *widget,
1336 GtkStateType previous_state)
1338 GtkText *text = GTK_TEXT (widget);
1340 if (GTK_WIDGET_REALIZED (widget))
1342 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1343 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1348 gtk_text_unrealize (GtkWidget *widget)
1350 GtkText *text = GTK_TEXT (widget);
1352 gdk_window_set_user_data (text->text_area, NULL);
1353 gdk_window_destroy (text->text_area);
1354 text->text_area = NULL;
1356 gdk_gc_destroy (text->gc);
1361 gdk_gc_destroy (text->bg_gc);
1365 gdk_pixmap_unref (text->line_wrap_bitmap);
1366 gdk_pixmap_unref (text->line_arrow_bitmap);
1368 unrealize_properties (text);
1372 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1373 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1377 clear_focus_area (GtkText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1379 GtkWidget *widget = GTK_WIDGET (text);
1382 gint ythick = TEXT_BORDER_ROOM + widget->style->ythickness;
1383 gint xthick = TEXT_BORDER_ROOM + widget->style->xthickness;
1387 if (area_width == 0 || area_height == 0)
1390 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1392 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1394 gdk_gc_set_ts_origin (text->bg_gc,
1395 (- text->first_onscreen_hor_pixel + xthick) % width,
1396 (- text->first_onscreen_ver_pixel + ythick) % height);
1401 gc = widget->style->base_gc[widget->state];
1403 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1404 area_x, area_y, area_width, area_height);
1408 gtk_text_draw_focus (GtkWidget *widget)
1414 g_return_if_fail (GTK_IS_TEXT (widget));
1416 text = GTK_TEXT (widget);
1418 if (GTK_WIDGET_DRAWABLE (widget))
1420 gint ythick = widget->style->ythickness;
1421 gint xthick = widget->style->xthickness;
1422 gint xextra = TEXT_BORDER_ROOM;
1423 gint yextra = TEXT_BORDER_ROOM;
1425 TDEBUG (("in gtk_text_draw_focus\n"));
1429 width = widget->allocation.width;
1430 height = widget->allocation.height;
1432 if (GTK_WIDGET_HAS_FOCUS (widget))
1441 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1442 NULL, widget, "text",
1444 widget->allocation.width,
1445 widget->allocation.height);
1448 gtk_paint_shadow (widget->style, widget->window,
1449 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1450 NULL, widget, "text",
1451 x, y, width, height);
1455 width -= 2 * xthick;
1456 height -= 2 * ythick;
1459 clear_focus_area (text, x, y, width, yextra);
1461 clear_focus_area (text, x, y + yextra,
1462 xextra, y + height - 2 * yextra);
1464 clear_focus_area (text, x + width - xextra, y + yextra,
1465 xextra, height - 2 * ythick);
1467 clear_focus_area (text, x, x + height - yextra, width, yextra);
1471 TDEBUG (("in gtk_text_draw_focus (undrawable !!!)\n"));
1476 gtk_text_size_request (GtkWidget *widget,
1477 GtkRequisition *requisition)
1485 xthickness = widget->style->xthickness + TEXT_BORDER_ROOM;
1486 ythickness = widget->style->ythickness + TEXT_BORDER_ROOM;
1488 font = gtk_style_get_font (widget->style);
1490 char_height = MIN_TEXT_HEIGHT_LINES * (font->ascent +
1493 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (font,
1494 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1498 requisition->width = char_width + xthickness * 2;
1499 requisition->height = char_height + ythickness * 2;
1503 gtk_text_size_allocate (GtkWidget *widget,
1504 GtkAllocation *allocation)
1506 GtkText *text = GTK_TEXT (widget);
1508 widget->allocation = *allocation;
1509 if (GTK_WIDGET_REALIZED (widget))
1511 gdk_window_move_resize (widget->window,
1512 allocation->x, allocation->y,
1513 allocation->width, allocation->height);
1515 gdk_window_move_resize (text->text_area,
1516 widget->style->xthickness + TEXT_BORDER_ROOM,
1517 widget->style->ythickness + TEXT_BORDER_ROOM,
1518 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->xthickness +
1519 (gint)TEXT_BORDER_ROOM) * 2),
1520 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->ythickness +
1521 (gint)TEXT_BORDER_ROOM) * 2));
1523 recompute_geometry (text);
1528 gtk_text_expose (GtkWidget *widget,
1529 GdkEventExpose *event)
1531 if (event->window == GTK_TEXT (widget)->text_area)
1533 TDEBUG (("in gtk_text_expose (expose)\n"));
1534 expose_text (GTK_TEXT (widget), &event->area, TRUE);
1536 else if (event->count == 0)
1538 TDEBUG (("in gtk_text_expose (focus)\n"));
1539 gtk_text_draw_focus (widget);
1546 gtk_text_scroll_timeout (gpointer data)
1550 GdkModifierType mask;
1552 text = GTK_TEXT (data);
1555 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1557 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1559 GdkEvent *event = gdk_event_new (GDK_MOTION_NOTIFY);
1561 event->motion.is_hint = 0;
1562 event->motion.x = x;
1563 event->motion.y = y;
1564 event->motion.state = mask;
1566 gtk_text_motion_notify (GTK_WIDGET (text), (GdkEventMotion *)event);
1568 gdk_event_free (event);
1575 gtk_text_button_press (GtkWidget *widget,
1576 GdkEventButton *event)
1578 GtkText *text = GTK_TEXT (widget);
1579 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (widget);
1581 if (text->button && (event->button != text->button))
1584 text->button = event->button;
1586 if (!GTK_WIDGET_HAS_FOCUS (widget))
1587 gtk_widget_grab_focus (widget);
1589 if (event->button == 1)
1591 switch (event->type)
1593 case GDK_BUTTON_PRESS:
1594 gtk_grab_add (widget);
1596 undraw_cursor (text, FALSE);
1597 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1598 draw_cursor (text, FALSE);
1600 /* Set it now, so we display things right. We'll unset it
1601 * later if things don't work out */
1602 old_editable->has_selection = TRUE;
1603 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1604 text->cursor_mark.index,
1605 text->cursor_mark.index);
1609 case GDK_2BUTTON_PRESS:
1610 gtk_text_select_word (text, event->time);
1613 case GDK_3BUTTON_PRESS:
1614 gtk_text_select_line (text, event->time);
1621 else if (event->type == GDK_BUTTON_PRESS)
1623 if ((event->button == 2) && old_editable->editable)
1625 if (old_editable->selection_start_pos == old_editable->selection_end_pos ||
1626 old_editable->has_selection)
1628 undraw_cursor (text, FALSE);
1629 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1630 draw_cursor (text, FALSE);
1634 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1635 gdk_atom_intern_static_string ("UTF8_STRING"),
1640 GdkDisplay *display = gtk_widget_get_display (widget);
1642 gtk_grab_add (widget);
1644 undraw_cursor (text, FALSE);
1645 find_mouse_cursor (text, event->x, event->y);
1646 draw_cursor (text, FALSE);
1648 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1649 text->cursor_mark.index,
1650 text->cursor_mark.index);
1652 old_editable->has_selection = FALSE;
1653 if (gdk_selection_owner_get_for_display (display,
1654 GDK_SELECTION_PRIMARY) == widget->window)
1655 gtk_selection_owner_set_for_display (display,
1657 GDK_SELECTION_PRIMARY,
1666 gtk_text_button_release (GtkWidget *widget,
1667 GdkEventButton *event)
1669 GtkText *text = GTK_TEXT (widget);
1670 GtkOldEditable *old_editable;
1671 GdkDisplay *display;
1673 gtk_grab_remove (widget);
1675 if (text->button != event->button)
1682 g_source_remove (text->timer);
1686 if (event->button == 1)
1688 text = GTK_TEXT (widget);
1689 old_editable = GTK_OLD_EDITABLE (widget);
1690 display = gtk_widget_get_display (widget);
1692 gtk_grab_remove (widget);
1694 old_editable->has_selection = FALSE;
1695 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1697 if (gtk_selection_owner_set_for_display (display,
1699 GDK_SELECTION_PRIMARY,
1701 old_editable->has_selection = TRUE;
1703 gtk_text_update_text (old_editable, old_editable->selection_start_pos,
1704 old_editable->selection_end_pos);
1708 if (gdk_selection_owner_get_for_display (display,
1709 GDK_SELECTION_PRIMARY) == widget->window)
1710 gtk_selection_owner_set_for_display (display,
1712 GDK_SELECTION_PRIMARY,
1716 else if (event->button == 3)
1718 gtk_grab_remove (widget);
1721 undraw_cursor (text, FALSE);
1722 find_cursor (text, TRUE);
1723 draw_cursor (text, FALSE);
1729 gtk_text_motion_notify (GtkWidget *widget,
1730 GdkEventMotion *event)
1732 GtkText *text = GTK_TEXT (widget);
1735 GdkModifierType mask;
1739 mask = event->state;
1740 if (event->is_hint || (text->text_area != event->window))
1742 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1745 if ((text->button == 0) ||
1746 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1749 gdk_window_get_size (text->text_area, NULL, &height);
1751 if ((y < 0) || (y > height))
1753 if (text->timer == 0)
1755 text->timer = gdk_threads_add_timeout (SCROLL_TIME,
1756 gtk_text_scroll_timeout,
1760 scroll_int (text, y/2);
1762 scroll_int (text, (y - height)/2);
1768 undraw_cursor (GTK_TEXT (widget), FALSE);
1769 find_mouse_cursor (GTK_TEXT (widget), x, y);
1770 draw_cursor (GTK_TEXT (widget), FALSE);
1772 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1773 GTK_OLD_EDITABLE (text)->selection_start_pos,
1774 text->cursor_mark.index);
1780 gtk_text_insert_text (GtkEditable *editable,
1781 const gchar *new_text,
1782 gint new_text_length,
1785 GtkText *text = GTK_TEXT (editable);
1787 GdkColor *fore, *back;
1789 TextProperty *property;
1791 gtk_text_set_point (text, *position);
1793 property = MARK_CURRENT_PROPERTY (&text->point);
1794 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
1795 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
1796 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
1798 gtk_text_insert (text, font, fore, back, new_text, new_text_length);
1800 *position = text->point.index;
1804 gtk_text_delete_text (GtkEditable *editable,
1808 GtkText *text = GTK_TEXT (editable);
1810 g_return_if_fail (start_pos >= 0);
1812 gtk_text_set_point (text, start_pos);
1814 end_pos = TEXT_LENGTH (text);
1816 if (end_pos > start_pos)
1817 gtk_text_forward_delete (text, end_pos - start_pos);
1821 gtk_text_key_press (GtkWidget *widget,
1824 GtkText *text = GTK_TEXT (widget);
1825 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (widget);
1830 key = event->keyval;
1833 if ((GTK_OLD_EDITABLE(text)->editable == FALSE))
1835 switch (event->keyval)
1839 if (event->state & GDK_CONTROL_MASK)
1840 scroll_int (text, -text->vadj->value);
1846 if (event->state & GDK_CONTROL_MASK)
1847 scroll_int (text, +text->vadj->upper);
1851 case GDK_KP_Page_Up:
1852 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
1853 case GDK_KP_Page_Down:
1854 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
1856 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
1858 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
1862 if (event->state & GDK_CONTROL_MASK)
1863 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
1874 gint extend_selection;
1876 guint initial_pos = old_editable->current_pos;
1878 text->point = find_mark (text, text->cursor_mark.index);
1880 extend_selection = event->state & GDK_SHIFT_MASK;
1881 extend_start = FALSE;
1883 if (extend_selection)
1885 old_editable->has_selection = TRUE;
1887 if (old_editable->selection_start_pos == old_editable->selection_end_pos)
1889 old_editable->selection_start_pos = text->point.index;
1890 old_editable->selection_end_pos = text->point.index;
1893 extend_start = (text->point.index == old_editable->selection_start_pos);
1896 switch (event->keyval)
1900 if (event->state & GDK_CONTROL_MASK)
1901 move_cursor_buffer_ver (text, -1);
1903 gtk_text_move_beginning_of_line (text);
1907 if (event->state & GDK_CONTROL_MASK)
1908 move_cursor_buffer_ver (text, +1);
1910 gtk_text_move_end_of_line (text);
1912 case GDK_KP_Page_Up:
1913 case GDK_Page_Up: move_cursor_page_ver (text, -1); break;
1914 case GDK_KP_Page_Down:
1915 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
1916 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
1918 case GDK_Up: move_cursor_ver (text, -1); break;
1920 case GDK_Down: move_cursor_ver (text, +1); break;
1923 if (event->state & GDK_CONTROL_MASK)
1924 gtk_text_move_backward_word (text);
1926 move_cursor_hor (text, -1);
1930 if (event->state & GDK_CONTROL_MASK)
1931 gtk_text_move_forward_word (text);
1933 move_cursor_hor (text, +1);
1937 if (event->state & GDK_CONTROL_MASK)
1938 gtk_text_delete_backward_word (text);
1940 gtk_text_delete_backward_character (text);
1943 gtk_text_delete_line (text);
1947 if (event->state & GDK_SHIFT_MASK)
1949 extend_selection = FALSE;
1950 gtk_editable_paste_clipboard (GTK_EDITABLE (old_editable));
1952 else if (event->state & GDK_CONTROL_MASK)
1954 gtk_editable_copy_clipboard (GTK_EDITABLE (old_editable));
1958 /* gtk_toggle_insert(text) -- IMPLEMENT */
1963 if (event->state & GDK_CONTROL_MASK)
1964 gtk_text_delete_forward_word (text);
1965 else if (event->state & GDK_SHIFT_MASK)
1967 extend_selection = FALSE;
1968 gtk_editable_cut_clipboard (GTK_EDITABLE (old_editable));
1971 gtk_text_delete_forward_character (text);
1974 case GDK_ISO_Left_Tab:
1976 position = text->point.index;
1977 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\t", 1, &position);
1982 if (event->state & GDK_CONTROL_MASK)
1983 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
1986 position = text->point.index;
1987 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\n", 1, &position);
1991 /* Don't insert literally */
1998 if (event->state & GDK_CONTROL_MASK)
2001 if ((key >= 'A') && (key <= 'Z'))
2007 gtk_text_move_beginning_of_line (text);
2010 gtk_text_move_backward_character (text);
2013 gtk_editable_copy_clipboard (GTK_EDITABLE (text));
2016 gtk_text_delete_forward_character (text);
2019 gtk_text_move_end_of_line (text);
2022 gtk_text_move_forward_character (text);
2025 gtk_text_delete_backward_character (text);
2028 gtk_text_delete_to_line_end (text);
2031 gtk_text_move_next_line (text);
2034 gtk_text_move_previous_line (text);
2037 gtk_text_delete_line (text);
2040 gtk_editable_paste_clipboard (GTK_EDITABLE (text));
2043 gtk_text_delete_backward_word (text);
2046 gtk_editable_cut_clipboard (GTK_EDITABLE (text));
2054 else if (event->state & GDK_MOD1_MASK)
2057 if ((key >= 'A') && (key <= 'Z'))
2063 gtk_text_move_backward_word (text);
2066 gtk_text_delete_forward_word (text);
2069 gtk_text_move_forward_word (text);
2077 else if (event->length > 0)
2079 extend_selection = FALSE;
2081 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
2082 position = text->point.index;
2083 gtk_editable_insert_text (GTK_EDITABLE (old_editable), event->string, event->length, &position);
2089 if (return_val && (old_editable->current_pos != initial_pos))
2091 if (extend_selection)
2093 if (old_editable->current_pos < old_editable->selection_start_pos)
2094 gtk_text_set_selection (old_editable, old_editable->current_pos,
2095 old_editable->selection_end_pos);
2096 else if (old_editable->current_pos > old_editable->selection_end_pos)
2097 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2098 old_editable->current_pos);
2102 gtk_text_set_selection (old_editable, old_editable->current_pos,
2103 old_editable->selection_end_pos);
2105 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2106 old_editable->current_pos);
2110 gtk_text_set_selection (old_editable, 0, 0);
2112 gtk_old_editable_claim_selection (old_editable,
2113 old_editable->selection_start_pos != old_editable->selection_end_pos,
2122 gtk_text_adjustment (GtkAdjustment *adjustment,
2125 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2126 g_return_if_fail (GTK_IS_TEXT (text));
2128 /* Just ignore it if we haven't been size-allocated and realized yet */
2129 if (text->line_start_cache == NULL)
2132 if (adjustment == text->hadj)
2134 g_warning ("horizontal scrolling not implemented");
2138 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2142 undraw_cursor (text, FALSE);
2145 scroll_down (text, diff);
2146 else /* if (diff < 0) */
2147 scroll_up (text, diff);
2149 draw_cursor (text, FALSE);
2151 text->last_ver_value = adjustment->value;
2157 gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
2160 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2161 g_return_if_fail (GTK_IS_TEXT (text));
2163 if (adjustment == text->hadj)
2164 gtk_text_set_adjustments (text, NULL, text->vadj);
2165 if (adjustment == text->vadj)
2166 gtk_text_set_adjustments (text, text->hadj, NULL);
2170 static GtkPropertyMark
2171 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2173 GtkPropertyMark mark;
2175 mark = find_mark_near (text, point_position, near);
2177 while (mark.index > 0 &&
2178 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2179 decrement_mark (&mark);
2185 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2187 tab_cont->pixel_offset = 0;
2188 tab_cont->tab_start.tab_stops = text->tab_stops;
2189 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2191 if (!tab_cont->tab_start.to_next_tab)
2192 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2196 line_params_iterate (GtkText* text,
2197 const GtkPropertyMark* mark0,
2198 const PrevTabCont* tab_mark0,
2201 LineIteratorFunction iter)
2202 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2203 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2204 * for each line following MARK, iteration continues unless ITER_CALL
2207 GtkPropertyMark mark = *mark0;
2208 PrevTabCont tab_conts[2];
2209 LineParams *lp, lpbuf;
2210 gint tab_cont_index = 0;
2213 tab_conts[0] = *tab_mark0;
2215 init_tab_cont (text, tab_conts);
2220 lp = g_slice_new (LineParams);
2224 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2225 tab_conts + (tab_cont_index + 1) % 2);
2227 if ((*iter) (text, lp, data))
2230 if (LAST_INDEX (text, lp->end))
2234 advance_mark (&mark);
2235 tab_cont_index = (tab_cont_index + 1) % 2;
2240 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2242 FetchLinesData *fldata = (FetchLinesData*) data;
2244 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2246 switch (fldata->fl_type)
2248 case FetchLinesCount:
2249 if (!text->line_wrap || !lp->wraps)
2252 if (fldata->data >= fldata->data_max)
2256 case FetchLinesPixels:
2258 fldata->data += LINE_HEIGHT(*lp);
2260 if (fldata->data >= fldata->data_max)
2270 fetch_lines (GtkText* text,
2271 const GtkPropertyMark* mark0,
2272 const PrevTabCont* tab_cont0,
2276 FetchLinesData fl_data;
2278 fl_data.new_lines = NULL;
2280 fl_data.data_max = data;
2281 fl_data.fl_type = fl_type;
2283 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2285 return g_list_reverse (fl_data.new_lines);
2289 fetch_lines_backward (GtkText* text)
2291 GList *new_line_start;
2292 GtkPropertyMark mark;
2294 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2297 mark = find_this_line_start_mark (text,
2298 CACHE_DATA(text->line_start_cache).start.index - 1,
2299 &CACHE_DATA(text->line_start_cache).start);
2301 new_line_start = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2303 while (new_line_start->next)
2304 new_line_start = new_line_start->next;
2306 new_line_start->next = text->line_start_cache;
2307 text->line_start_cache->prev = new_line_start;
2311 fetch_lines_forward (GtkText* text, gint line_count)
2313 GtkPropertyMark mark;
2314 GList* line = text->line_start_cache;
2319 mark = CACHE_DATA(line).end;
2321 if (LAST_INDEX (text, mark))
2324 advance_mark(&mark);
2326 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2329 line->next->prev = line;
2332 /* Compute the number of lines, and vertical pixels for n characters
2333 * starting from the point
2336 compute_lines_pixels (GtkText* text, guint char_count,
2337 guint *lines, guint *pixels)
2339 GList *line = text->current_line;
2340 gint chars_left = char_count;
2345 /* If chars_left == 0, that means we're joining two lines in a
2346 * deletion, so add in the values for the next line as well
2348 for (; line && chars_left >= 0; line = line->next)
2350 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2352 if (line == text->current_line)
2353 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2355 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2357 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2361 chars_left = 0; /* force another loop */
2364 fetch_lines_forward (text, 1);
2369 total_line_height (GtkText* text, GList* line, gint line_count)
2373 for (; line && line_count > 0; line = line->next)
2375 height += LINE_HEIGHT(CACHE_DATA(line));
2377 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2381 fetch_lines_forward (text, line_count);
2388 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2390 if (old == text->line_start_cache)
2394 for (; old_line_count > 0; old_line_count -= 1)
2396 while (text->line_start_cache &&
2398 CACHE_DATA(text->line_start_cache).wraps)
2399 remove_cache_line(text, text->line_start_cache);
2401 remove_cache_line(text, text->line_start_cache);
2404 last = g_list_last (new);
2406 last->next = text->line_start_cache;
2408 if (text->line_start_cache)
2409 text->line_start_cache->prev = last;
2411 text->line_start_cache = new;
2417 g_assert (old->prev);
2421 for (; old_line_count > 0; old_line_count -= 1)
2423 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2424 old = remove_cache_line (text, old);
2426 old = remove_cache_line (text, old);
2432 last = g_list_last (new);
2442 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2444 GList* cache = text->current_line;
2447 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2450 for (; cache; cache = cache->next)
2452 GtkPropertyMark *start = &CACHE_DATA(cache).start;
2453 GtkPropertyMark *end = &CACHE_DATA(cache).end;
2455 start->index -= nchars;
2456 end->index -= nchars;
2458 if (LAST_INDEX (text, text->point) &&
2459 start->index == text->point.index)
2460 *start = text->point;
2461 else if (start->property == text->point.property)
2462 start->offset = start->index - (text->point.index - text->point.offset);
2464 if (LAST_INDEX (text, text->point) &&
2465 end->index == text->point.index)
2467 if (end->property == text->point.property)
2468 end->offset = end->index - (text->point.index - text->point.offset);
2470 /*TEXT_ASSERT_MARK(text, start, "start");*/
2471 /*TEXT_ASSERT_MARK(text, end, "end");*/
2476 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2478 GtkWidget *widget = GTK_WIDGET (text);
2481 guint new_pixels = 0;
2483 GList* new_line = NULL;
2486 text->cursor_virtual_x = 0;
2488 correct_cache_delete (text, nchars, old_lines);
2490 pixel_height = pixel_height_of(text, text->current_line) -
2491 LINE_HEIGHT(CACHE_DATA(text->current_line));
2493 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2494 CACHE_DATA(text->current_line).start = text->point;
2496 new_line = fetch_lines (text,
2497 &CACHE_DATA(text->current_line).start,
2498 &CACHE_DATA(text->current_line).tab_cont,
2502 swap_lines (text, text->current_line, new_line, old_lines);
2504 text->current_line = new_line;
2506 new_pixels = total_line_height (text, new_line, 1);
2508 gdk_window_get_size (text->text_area, &width, &height);
2510 if (old_pixels != new_pixels)
2512 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2514 gdk_draw_pixmap (text->text_area,
2518 pixel_height + old_pixels,
2520 pixel_height + new_pixels,
2524 text->vadj->upper += new_pixels;
2525 text->vadj->upper -= old_pixels;
2526 adjust_adj (text, text->vadj);
2530 rect.y = pixel_height;
2532 rect.height = new_pixels;
2534 expose_text (text, &rect, FALSE);
2535 gtk_text_draw_focus ( (GtkWidget *) text);
2537 text->cursor_mark = text->point;
2539 find_cursor (text, TRUE);
2541 if (old_pixels != new_pixels)
2543 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2546 rect.y = pixel_height + new_pixels;
2548 rect.height = height - rect.y;
2550 expose_text (text, &rect, FALSE);
2553 process_exposes (text);
2560 /* note, the point has already been moved forward */
2562 correct_cache_insert (GtkText* text, gint nchars)
2565 GtkPropertyMark *start;
2566 GtkPropertyMark *end;
2567 gboolean was_split = FALSE;
2569 /* We need to distinguish whether the property was split in the
2570 * insert or not, so we check if the point (which points after
2571 * the insertion here), points to the same character as the
2572 * point before. Ugh.
2576 GtkPropertyMark tmp_mark = text->point;
2577 move_mark_n (&tmp_mark, -1);
2579 if (tmp_mark.property != text->point.property)
2583 /* If we inserted a property exactly at the beginning of the
2584 * line, we have to correct here, or fetch_lines will
2587 start = &CACHE_DATA(text->current_line).start;
2589 /* Check if if we split exactly at the beginning of the line:
2590 * (was_split won't be set if we are inserting at the end of the text,
2591 * so we don't check)
2593 if (start->offset == MARK_CURRENT_PROPERTY (start)->length)
2594 SET_PROPERTY_MARK (start, start->property->next, 0);
2595 /* Check if we inserted a property at the beginning of the text: */
2596 else if (was_split &&
2597 (start->property == text->point.property) &&
2598 (start->index == text->point.index - nchars))
2599 SET_PROPERTY_MARK (start, start->property->prev, 0);
2601 /* Now correct the offsets, and check for start or end marks that
2602 * are after the point, yet point to a property before the point's
2603 * property. This indicates that they are meant to point to the
2604 * second half of a property we split in insert_text_property(), so
2605 * we fix them up that way.
2607 cache = text->current_line->next;
2609 for (; cache; cache = cache->next)
2611 start = &CACHE_DATA(cache).start;
2612 end = &CACHE_DATA(cache).end;
2614 if (LAST_INDEX (text, text->point) &&
2615 start->index == text->point.index)
2616 *start = text->point;
2617 else if (start->index >= text->point.index - nchars)
2619 if (!was_split && start->property == text->point.property)
2620 move_mark_n(start, nchars);
2623 if (start->property->next &&
2624 (start->property->next->next == text->point.property))
2626 g_assert (start->offset >= MARK_CURRENT_PROPERTY (start)->length);
2627 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2628 start->property = text->point.property;
2630 start->index += nchars;
2634 if (LAST_INDEX (text, text->point) &&
2635 end->index == text->point.index)
2637 if (end->index >= text->point.index - nchars)
2639 if (!was_split && end->property == text->point.property)
2640 move_mark_n(end, nchars);
2643 if (end->property->next &&
2644 (end->property->next->next == text->point.property))
2646 g_assert (end->offset >= MARK_CURRENT_PROPERTY (end)->length);
2647 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2648 end->property = text->point.property;
2650 end->index += nchars;
2654 /*TEXT_ASSERT_MARK(text, start, "start");*/
2655 /*TEXT_ASSERT_MARK(text, end, "end");*/
2661 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2662 guint new_line_count)
2664 GtkWidget *widget = GTK_WIDGET (text);
2667 guint new_pixels = 0;
2669 GList* new_lines = NULL;
2672 text->cursor_virtual_x = 0;
2674 undraw_cursor (text, FALSE);
2676 correct_cache_insert (text, nchars);
2678 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2680 pixel_height = pixel_height_of(text, text->current_line) -
2681 LINE_HEIGHT(CACHE_DATA(text->current_line));
2683 new_lines = fetch_lines (text,
2684 &CACHE_DATA(text->current_line).start,
2685 &CACHE_DATA(text->current_line).tab_cont,
2689 swap_lines (text, text->current_line, new_lines, 1);
2691 text->current_line = new_lines;
2693 new_pixels = total_line_height (text, new_lines, new_line_count);
2695 gdk_window_get_size (text->text_area, &width, &height);
2697 if (old_pixels != new_pixels)
2699 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2701 gdk_draw_pixmap (text->text_area,
2705 pixel_height + old_pixels,
2707 pixel_height + new_pixels,
2709 height + (old_pixels - new_pixels) - pixel_height);
2712 text->vadj->upper += new_pixels;
2713 text->vadj->upper -= old_pixels;
2714 adjust_adj (text, text->vadj);
2718 rect.y = pixel_height;
2720 rect.height = new_pixels;
2722 expose_text (text, &rect, FALSE);
2723 gtk_text_draw_focus ( (GtkWidget *) text);
2725 text->cursor_mark = text->point;
2727 find_cursor (text, TRUE);
2729 draw_cursor (text, FALSE);
2731 if (old_pixels != new_pixels)
2733 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2736 rect.y = pixel_height + new_pixels;
2738 rect.height = height - rect.y;
2740 expose_text (text, &rect, FALSE);
2743 process_exposes (text);
2746 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2751 /* Text property functions */
2754 font_hash (gconstpointer font)
2756 return gdk_font_id ((const GdkFont*) font);
2759 static GHashTable *font_cache_table = NULL;
2762 get_text_font (GdkFont* gfont)
2767 if (!font_cache_table)
2768 font_cache_table = g_hash_table_new (font_hash, (GEqualFunc) gdk_font_equal);
2770 tf = g_hash_table_lookup (font_cache_table, gfont);
2778 tf = g_new (GtkTextFont, 1);
2781 tf->gdk_font = gfont;
2782 gdk_font_ref (gfont);
2784 for(i = 0; i < 256; i += 1)
2785 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2787 g_hash_table_insert (font_cache_table, gfont, tf);
2793 text_font_unref (GtkTextFont *text_font)
2795 text_font->ref_count--;
2796 if (text_font->ref_count == 0)
2798 g_hash_table_remove (font_cache_table, text_font->gdk_font);
2799 gdk_font_unref (text_font->gdk_font);
2805 text_properties_equal (TextProperty* prop, GdkFont* font, const GdkColor *fore, const GdkColor *back)
2807 if (prop->flags & PROPERTY_FONT)
2810 GtkTextFont *text_font;
2815 text_font = get_text_font (font);
2817 retval = (prop->font == text_font);
2818 text_font_unref (text_font);
2827 if (prop->flags & PROPERTY_FOREGROUND)
2829 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
2836 if (prop->flags & PROPERTY_BACKGROUND)
2838 if (!back || !gdk_color_equal (&prop->back_color, back))
2849 realize_property (GtkText *text, TextProperty *prop)
2851 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2853 if (prop->flags & PROPERTY_FOREGROUND)
2854 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
2856 if (prop->flags & PROPERTY_BACKGROUND)
2857 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
2861 realize_properties (GtkText *text)
2863 GList *tmp_list = text->text_properties;
2867 realize_property (text, tmp_list->data);
2869 tmp_list = tmp_list->next;
2874 unrealize_property (GtkText *text, TextProperty *prop)
2876 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2878 if (prop->flags & PROPERTY_FOREGROUND)
2879 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
2881 if (prop->flags & PROPERTY_BACKGROUND)
2882 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
2886 unrealize_properties (GtkText *text)
2888 GList *tmp_list = text->text_properties;
2892 unrealize_property (text, tmp_list->data);
2894 tmp_list = tmp_list->next;
2898 static TextProperty*
2899 new_text_property (GtkText *text, GdkFont *font, const GdkColor* fore,
2900 const GdkColor* back, guint length)
2904 prop = g_slice_new (TextProperty);
2909 prop->flags |= PROPERTY_FONT;
2910 prop->font = get_text_font (font);
2917 prop->flags |= PROPERTY_FOREGROUND;
2918 prop->fore_color = *fore;
2923 prop->flags |= PROPERTY_BACKGROUND;
2924 prop->back_color = *back;
2927 prop->length = length;
2929 if (GTK_WIDGET_REALIZED (text))
2930 realize_property (text, prop);
2936 destroy_text_property (TextProperty *prop)
2939 text_font_unref (prop->font);
2941 g_slice_free (TextProperty, prop);
2944 /* Flop the memory between the point and the gap around like a
2947 move_gap (GtkText* text, guint index)
2949 if (text->gap_position < index)
2951 gint diff = index - text->gap_position;
2953 if (text->use_wchar)
2954 g_memmove (text->text.wc + text->gap_position,
2955 text->text.wc + text->gap_position + text->gap_size,
2956 diff*sizeof (GdkWChar));
2958 g_memmove (text->text.ch + text->gap_position,
2959 text->text.ch + text->gap_position + text->gap_size,
2962 text->gap_position = index;
2964 else if (text->gap_position > index)
2966 gint diff = text->gap_position - index;
2968 if (text->use_wchar)
2969 g_memmove (text->text.wc + index + text->gap_size,
2970 text->text.wc + index,
2971 diff*sizeof (GdkWChar));
2973 g_memmove (text->text.ch + index + text->gap_size,
2974 text->text.ch + index,
2977 text->gap_position = index;
2981 /* Increase the gap size. */
2983 make_forward_space (GtkText* text, guint len)
2985 if (text->gap_size < len)
2987 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
2989 if (sum >= text->text_len)
2993 while (i <= sum) i <<= 1;
2995 if (text->use_wchar)
2996 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
2997 i*sizeof(GdkWChar));
2999 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3003 if (text->use_wchar)
3004 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3005 text->text.wc + text->gap_position + text->gap_size,
3006 (text->text_end - (text->gap_position + text->gap_size))
3009 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3010 text->text.ch + text->gap_position + text->gap_size,
3011 text->text_end - (text->gap_position + text->gap_size));
3013 text->text_end += len*2;
3014 text->gap_size += len*2;
3018 /* Inserts into the text property list a list element that guarantees
3019 * that for len characters following the point, text has the correct
3020 * property. does not move point. adjusts text_properties_point and
3021 * text_properties_point_offset relative to the current value of
3024 insert_text_property (GtkText* text, GdkFont* font,
3025 const GdkColor *fore, const GdkColor* back, guint len)
3027 GtkPropertyMark *mark = &text->point;
3028 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3029 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3031 if (MARK_OFFSET(mark) == 0)
3033 /* Point is on the boundary of two properties.
3034 * If it is the same as either, grow, else insert
3037 if (text_properties_equal(forward_prop, font, fore, back))
3039 /* Grow the property in front of us. */
3041 MARK_PROPERTY_LENGTH(mark) += len;
3043 else if (backward_prop &&
3044 text_properties_equal(backward_prop, font, fore, back))
3046 /* Grow property behind us, point property and offset
3049 SET_PROPERTY_MARK (&text->point,
3050 MARK_PREV_LIST_PTR (mark),
3051 backward_prop->length);
3053 backward_prop->length += len;
3055 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3056 (forward_prop->length == 1))
3058 /* Next property just has last position, take it over */
3060 if (GTK_WIDGET_REALIZED (text))
3061 unrealize_property (text, forward_prop);
3063 forward_prop->flags = 0;
3066 forward_prop->flags |= PROPERTY_FONT;
3067 forward_prop->font = get_text_font (font);
3070 forward_prop->font = NULL;
3074 forward_prop->flags |= PROPERTY_FOREGROUND;
3075 forward_prop->fore_color = *fore;
3079 forward_prop->flags |= PROPERTY_BACKGROUND;
3080 forward_prop->back_color = *back;
3082 forward_prop->length += len;
3084 if (GTK_WIDGET_REALIZED (text))
3085 realize_property (text, forward_prop);
3089 /* Splice a new property into the list. */
3091 GList* new_prop = g_list_alloc();
3093 new_prop->next = MARK_LIST_PTR(mark);
3094 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3095 new_prop->next->prev = new_prop;
3098 new_prop->prev->next = new_prop;
3100 new_prop->data = new_text_property (text, font, fore, back, len);
3102 SET_PROPERTY_MARK (mark, new_prop, 0);
3107 /* The following will screw up the line_start cache,
3108 * we'll fix it up in correct_cache_insert
3111 /* In the middle of forward_prop, if properties are equal,
3112 * just add to its length, else split it into two and splice
3114 if (text_properties_equal (forward_prop, font, fore, back))
3116 forward_prop->length += len;
3118 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3119 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3121 /* Inserting before only the last position in the text */
3124 forward_prop->length -= 1;
3126 new_prop = g_list_alloc();
3127 new_prop->data = new_text_property (text, font, fore, back, len+1);
3128 new_prop->prev = MARK_LIST_PTR(mark);
3129 new_prop->next = NULL;
3130 MARK_NEXT_LIST_PTR(mark) = new_prop;
3132 SET_PROPERTY_MARK (mark, new_prop, 0);
3136 GList* new_prop = g_list_alloc();
3137 GList* new_prop_forward = g_list_alloc();
3138 gint old_length = forward_prop->length;
3139 GList* next = MARK_NEXT_LIST_PTR(mark);
3141 /* Set the new lengths according to where they are split. Construct
3142 * two new properties. */
3143 forward_prop->length = MARK_OFFSET(mark);
3145 new_prop_forward->data =
3146 new_text_property(text,
3147 forward_prop->flags & PROPERTY_FONT ?
3148 forward_prop->font->gdk_font : NULL,
3149 forward_prop->flags & PROPERTY_FOREGROUND ?
3150 &forward_prop->fore_color : NULL,
3151 forward_prop->flags & PROPERTY_BACKGROUND ?
3152 &forward_prop->back_color : NULL,
3153 old_length - forward_prop->length);
3155 new_prop->data = new_text_property(text, font, fore, back, len);
3157 /* Now splice things in. */
3158 MARK_NEXT_LIST_PTR(mark) = new_prop;
3159 new_prop->prev = MARK_LIST_PTR(mark);
3161 new_prop->next = new_prop_forward;
3162 new_prop_forward->prev = new_prop;
3164 new_prop_forward->next = next;
3167 next->prev = new_prop_forward;
3169 SET_PROPERTY_MARK (mark, new_prop, 0);
3173 while (text->text_properties_end->next)
3174 text->text_properties_end = text->text_properties_end->next;
3176 while (text->text_properties->prev)
3177 text->text_properties = text->text_properties->prev;
3181 delete_text_property (GtkText* text, guint nchars)
3183 /* Delete nchars forward from point. */
3185 /* Deleting text properties is problematical, because we
3186 * might be storing around marks pointing to a property.
3188 * The marks in question and how we handle them are:
3190 * point: We know the new value, since it will be at the
3191 * end of the deleted text, and we move it there
3193 * cursor: We just remove the mark and set it equal to the
3194 * point after the operation.
3195 * line-start cache: We replace most affected lines.
3196 * The current line gets used to fetch the new
3197 * lines so, if necessary, (delete at the beginning
3198 * of a line) we fix it up by setting it equal to the
3206 for(; nchars; nchars -= 1)
3208 prop = MARK_CURRENT_PROPERTY(&text->point);
3212 if (prop->length == 0)
3214 tmp = MARK_LIST_PTR (&text->point);
3216 is_first = tmp == text->text_properties;
3218 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3219 text->point.offset = 0;
3221 if (GTK_WIDGET_REALIZED (text))
3222 unrealize_property (text, prop);
3224 destroy_text_property (prop);
3225 g_list_free_1 (tmp);
3227 prop = MARK_CURRENT_PROPERTY (&text->point);
3230 text->text_properties = MARK_LIST_PTR (&text->point);
3232 g_assert (prop->length != 0);
3234 else if (prop->length == text->point.offset)
3236 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3237 text->point.offset = 0;
3241 /* Check to see if we have just the single final position remaining
3242 * along in a property; if so, combine it with the previous property
3244 if (LAST_INDEX (text, text->point) &&
3245 (MARK_OFFSET (&text->point) == 0) &&
3246 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3248 tmp = MARK_LIST_PTR (&text->point);
3249 prop = MARK_CURRENT_PROPERTY(&text->point);
3251 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3252 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3253 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3255 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3257 if (GTK_WIDGET_REALIZED (text))
3258 unrealize_property (text, prop);
3260 destroy_text_property (prop);
3261 g_list_free_1 (tmp);
3266 init_properties (GtkText *text)
3268 if (!text->text_properties)
3270 text->text_properties = g_list_alloc();
3271 text->text_properties->next = NULL;
3272 text->text_properties->prev = NULL;
3273 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3274 text->text_properties_end = text->text_properties;
3276 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3278 text->point.index = 0;
3283 /**********************************************************************/
3284 /* Property Movement */
3285 /**********************************************************************/
3288 move_mark_n (GtkPropertyMark* mark, gint n)
3291 advance_mark_n(mark, n);
3293 decrement_mark_n(mark, -n);
3297 advance_mark (GtkPropertyMark* mark)
3299 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3303 if (prop->length > mark->offset + 1)
3307 mark->property = MARK_NEXT_LIST_PTR (mark);
3313 advance_mark_n (GtkPropertyMark* mark, gint n)
3320 i = 0; /* otherwise it migth not be init. */
3321 prop = MARK_CURRENT_PROPERTY(mark);
3323 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3324 /* to make it easier */
3325 n += (mark->offset);
3326 mark->index -= mark->offset;
3328 /* first we take seven-mile-leaps to get to the right text
3330 while ((n-i) > prop->length - 1) {
3332 mark->index += prop->length;
3333 mark->property = MARK_NEXT_LIST_PTR (mark);
3334 prop = MARK_CURRENT_PROPERTY (mark);
3338 /* and then the rest */
3339 mark->index += n - i;
3340 mark->offset += n - i;
3344 decrement_mark (GtkPropertyMark* mark)
3348 if (mark->offset > 0)
3352 mark->property = MARK_PREV_LIST_PTR (mark);
3353 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3358 decrement_mark_n (GtkPropertyMark* mark, gint n)
3362 while (mark->offset < n) {
3363 /* jump to end of prev */
3364 n -= mark->offset + 1;
3365 mark->index -= mark->offset + 1;
3366 mark->property = MARK_PREV_LIST_PTR (mark);
3367 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3375 static GtkPropertyMark
3376 find_mark (GtkText* text, guint mark_position)
3378 return find_mark_near (text, mark_position, &text->point);
3382 * You can also start from the end, what a drag.
3384 static GtkPropertyMark
3385 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3390 GtkPropertyMark mark;
3393 diffa = mark_position + 1;
3395 diffa = mark_position - near->index;
3397 diffb = mark_position;
3409 mark.property = text->text_properties;
3413 move_mark_n (&mark, mark_position - mark.index);
3418 /* This routine must be called with scroll == FALSE, only when
3419 * point is at least partially on screen
3423 find_line_containing_point (GtkText* text, guint point,
3429 text->current_line = NULL;
3433 /* Scroll backwards until the point is on screen
3435 while (CACHE_DATA(text->line_start_cache).start.index > point)
3436 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3438 /* Now additionally try to make sure that the point is fully on screen
3442 while (text->first_cut_pixels != 0 &&
3443 text->line_start_cache->next &&
3444 CACHE_DATA(text->line_start_cache->next).start.index > point)
3445 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3448 gdk_window_get_size (text->text_area, NULL, &height);
3450 for (cache = text->line_start_cache; cache; cache = cache->next)
3454 if (CACHE_DATA(cache).end.index >= point ||
3455 LAST_INDEX(text, CACHE_DATA(cache).end))
3457 text->current_line = cache; /* LOOK HERE, this proc has an
3458 * important side effect. */
3462 TEXT_SHOW_LINE (text, cache, "cache");
3464 if (cache->next == NULL)
3465 fetch_lines_forward (text, 1);
3469 lph = pixel_height_of (text, cache->next);
3471 /* Scroll the bottom of the line is on screen, or until
3472 * the line is the first onscreen line.
3474 while (cache->next != text->line_start_cache && lph > height)
3476 TEXT_SHOW_LINE (text, cache, "cache");
3477 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3478 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3479 lph = pixel_height_of (text, cache->next);
3484 g_assert_not_reached (); /* Must set text->current_line here */
3488 pixel_height_of (GtkText* text, GList* cache_line)
3490 gint pixels = - text->first_cut_pixels;
3491 GList *cache = text->line_start_cache;
3494 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3496 if (cache->data == cache_line->data)
3499 cache = cache->next;
3505 /**********************************************************************/
3506 /* Search and Placement */
3507 /**********************************************************************/
3510 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3513 gint16* char_widths;
3515 if (LAST_INDEX (text, *mark))
3518 ch = GTK_TEXT_INDEX (text, mark->index);
3519 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3523 return tab_mark->to_next_tab * char_widths[' '];
3527 return char_widths[ch];
3531 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3536 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, GdkWChar ch)
3538 if (tab_mark->to_next_tab == 1 || ch == '\t')
3540 if (tab_mark->tab_stops->next)
3542 tab_mark->tab_stops = tab_mark->tab_stops->next;
3543 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3547 tab_mark->to_next_tab = text->default_tab_width;
3552 tab_mark->to_next_tab -= 1;
3557 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3561 advance_tab_mark (text, tab_mark, 0);
3565 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3569 GtkPropertyMark mark = start_line->start;
3570 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3571 gint pixel_width = LINE_START_PIXEL (*start_line);
3573 while (mark.index < text->cursor_mark.index)
3575 pixel_width += find_char_width (text, &mark, &tab_mark);
3577 advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3578 advance_mark (&mark);
3581 text->cursor_pos_x = pixel_width;
3582 text->cursor_pos_y = pixel_height;
3583 text->cursor_char_offset = start_line->font_descent;
3584 text->cursor_mark = mark;
3586 ch = LAST_INDEX (text, mark) ?
3587 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3589 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3590 text->cursor_char = 0;
3592 text->cursor_char = ch;
3596 find_cursor (GtkText* text, gboolean scroll)
3598 if (GTK_WIDGET_REALIZED (text))
3600 find_line_containing_point (text, text->cursor_mark.index, scroll);
3602 if (text->current_line)
3603 find_cursor_at_line (text,
3604 &CACHE_DATA(text->current_line),
3605 pixel_height_of(text, text->current_line));
3608 GTK_OLD_EDITABLE (text)->current_pos = text->cursor_mark.index;
3612 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3613 guint line_pixel_height,
3616 GtkPropertyMark mark = lp->start;
3617 TabStopMark tab_mark = lp->tab_cont.tab_start;
3619 gint char_width = find_char_width(text, &mark, &tab_mark);
3620 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3622 text->cursor_pos_y = line_pixel_height;
3626 GdkWChar ch = LAST_INDEX (text, mark) ?
3627 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3629 if (button_x < pixel_width || mark.index == lp->end.index)
3631 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3632 text->cursor_mark = mark;
3633 text->cursor_char_offset = lp->font_descent;
3635 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3636 text->cursor_char = 0;
3638 text->cursor_char = ch;
3643 advance_tab_mark (text, &tab_mark, ch);
3644 advance_mark (&mark);
3646 pixel_width += char_width/2;
3648 char_width = find_char_width (text, &mark, &tab_mark);
3650 pixel_width += (char_width+1)/2;
3655 find_mouse_cursor (GtkText* text, gint x, gint y)
3658 GList* cache = text->line_start_cache;
3662 pixel_height = - text->first_cut_pixels;
3664 for (; cache; cache = cache->next)
3666 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3668 if (y < pixel_height || !cache->next)
3670 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3672 find_cursor (text, FALSE);
3679 /**********************************************************************/
3681 /**********************************************************************/
3684 free_cache (GtkText* text)
3686 GList* cache = text->line_start_cache;
3691 cache = cache->prev;
3693 text->line_start_cache = cache;
3696 for (; cache; cache = cache->next)
3697 g_slice_free (LineParams, cache->data);
3699 g_list_free (text->line_start_cache);
3701 text->line_start_cache = NULL;
3705 remove_cache_line (GtkText* text, GList* member)
3712 if (member == text->line_start_cache)
3713 text->line_start_cache = text->line_start_cache->next;
3716 member->prev->next = member->next;
3719 member->next->prev = member->prev;
3721 list = member->next;
3723 g_slice_free (LineParams, member->data);
3724 g_list_free_1 (member);
3729 /**********************************************************************/
3731 /**********************************************************************/
3734 move_cursor_buffer_ver (GtkText *text, int dir)
3736 undraw_cursor (text, FALSE);
3740 scroll_int (text, text->vadj->upper);
3741 text->cursor_mark = find_this_line_start_mark (text,
3743 &text->cursor_mark);
3747 scroll_int (text, - text->vadj->value);
3748 text->cursor_mark = find_this_line_start_mark (text,
3750 &text->cursor_mark);
3753 find_cursor (text, TRUE);
3754 draw_cursor (text, FALSE);
3758 move_cursor_page_ver (GtkText *text, int dir)
3760 scroll_int (text, dir * text->vadj->page_increment);
3764 move_cursor_ver (GtkText *text, int count)
3767 GtkPropertyMark mark;
3770 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3771 offset = text->cursor_mark.index - mark.index;
3773 if (offset > text->cursor_virtual_x)
3774 text->cursor_virtual_x = offset;
3778 if (mark.index == 0)
3781 decrement_mark (&mark);
3782 mark = find_this_line_start_mark (text, mark.index, &mark);
3786 mark = text->cursor_mark;
3788 while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3789 advance_mark (&mark);
3791 if (LAST_INDEX(text, mark))
3794 advance_mark (&mark);
3797 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3798 if (LAST_INDEX(text, mark) ||
3799 GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3802 undraw_cursor (text, FALSE);
3804 text->cursor_mark = mark;
3806 find_cursor (text, TRUE);
3808 draw_cursor (text, FALSE);
3812 move_cursor_hor (GtkText *text, int count)
3814 /* count should be +-1. */
3815 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3816 (count < 0 && text->cursor_mark.index < (- count)) ||
3820 text->cursor_virtual_x = 0;
3822 undraw_cursor (text, FALSE);
3824 move_mark_n (&text->cursor_mark, count);
3826 find_cursor (text, TRUE);
3828 draw_cursor (text, FALSE);
3832 gtk_text_move_cursor (GtkOldEditable *old_editable,
3839 move_cursor_hor (GTK_TEXT (old_editable), 1);
3844 move_cursor_hor (GTK_TEXT (old_editable), -1);
3850 move_cursor_ver (GTK_TEXT (old_editable), 1);
3855 move_cursor_ver (GTK_TEXT (old_editable), -1);
3860 gtk_text_move_forward_character (GtkText *text)
3862 move_cursor_hor (text, 1);
3866 gtk_text_move_backward_character (GtkText *text)
3868 move_cursor_hor (text, -1);
3872 gtk_text_move_next_line (GtkText *text)
3874 move_cursor_ver (text, 1);
3878 gtk_text_move_previous_line (GtkText *text)
3880 move_cursor_ver (text, -1);
3884 gtk_text_move_word (GtkOldEditable *old_editable,
3890 gtk_text_move_forward_word (GTK_TEXT (old_editable));
3895 gtk_text_move_backward_word (GTK_TEXT (old_editable));
3900 gtk_text_move_forward_word (GtkText *text)
3902 text->cursor_virtual_x = 0;
3904 undraw_cursor (text, FALSE);
3906 if (text->use_wchar)
3908 while (!LAST_INDEX (text, text->cursor_mark) &&
3909 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3910 advance_mark (&text->cursor_mark);
3912 while (!LAST_INDEX (text, text->cursor_mark) &&
3913 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3914 advance_mark (&text->cursor_mark);
3918 while (!LAST_INDEX (text, text->cursor_mark) &&
3919 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3920 advance_mark (&text->cursor_mark);
3922 while (!LAST_INDEX (text, text->cursor_mark) &&
3923 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3924 advance_mark (&text->cursor_mark);
3927 find_cursor (text, TRUE);
3928 draw_cursor (text, FALSE);
3932 gtk_text_move_backward_word (GtkText *text)
3934 text->cursor_virtual_x = 0;
3936 undraw_cursor (text, FALSE);
3938 if (text->use_wchar)
3940 while ((text->cursor_mark.index > 0) &&
3941 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3942 decrement_mark (&text->cursor_mark);
3944 while ((text->cursor_mark.index > 0) &&
3945 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3946 decrement_mark (&text->cursor_mark);
3950 while ((text->cursor_mark.index > 0) &&
3951 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3952 decrement_mark (&text->cursor_mark);
3954 while ((text->cursor_mark.index > 0) &&
3955 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3956 decrement_mark (&text->cursor_mark);
3959 find_cursor (text, TRUE);
3960 draw_cursor (text, FALSE);
3964 gtk_text_move_page (GtkOldEditable *old_editable,
3969 scroll_int (GTK_TEXT (old_editable),
3970 y * GTK_TEXT(old_editable)->vadj->page_increment);
3974 gtk_text_move_to_row (GtkOldEditable *old_editable,
3980 gtk_text_move_to_column (GtkOldEditable *old_editable,
3985 text = GTK_TEXT (old_editable);
3987 text->cursor_virtual_x = 0; /* FIXME */
3989 undraw_cursor (text, FALSE);
3991 /* Move to the beginning of the line */
3992 while ((text->cursor_mark.index > 0) &&
3993 (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
3994 decrement_mark (&text->cursor_mark);
3996 while (!LAST_INDEX (text, text->cursor_mark) &&
3997 (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4001 else if (column == 0)
4004 advance_mark (&text->cursor_mark);
4007 find_cursor (text, TRUE);
4008 draw_cursor (text, FALSE);
4012 gtk_text_move_beginning_of_line (GtkText *text)
4014 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4019 gtk_text_move_end_of_line (GtkText *text)
4021 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), -1);
4025 gtk_text_kill_char (GtkOldEditable *old_editable,
4030 text = GTK_TEXT (old_editable);
4032 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4033 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4038 if (text->point.index + 1 <= TEXT_LENGTH (text))
4039 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index, text->point.index + 1);
4043 if (text->point.index > 0)
4044 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index - 1, text->point.index);
4050 gtk_text_delete_forward_character (GtkText *text)
4052 gtk_text_kill_char (GTK_OLD_EDITABLE (text), 1);
4056 gtk_text_delete_backward_character (GtkText *text)
4058 gtk_text_kill_char (GTK_OLD_EDITABLE (text), -1);
4062 gtk_text_kill_word (GtkOldEditable *old_editable,
4065 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4066 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4069 gint old_pos = old_editable->current_pos;
4072 gtk_text_move_word (old_editable, 1);
4073 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4077 gtk_text_move_word (old_editable, -1);
4078 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4084 gtk_text_delete_forward_word (GtkText *text)
4086 gtk_text_kill_word (GTK_OLD_EDITABLE (text), 1);
4090 gtk_text_delete_backward_word (GtkText *text)
4092 gtk_text_kill_word (GTK_OLD_EDITABLE (text), -1);
4096 gtk_text_kill_line (GtkOldEditable *old_editable,
4099 gint old_pos = old_editable->current_pos;
4102 gtk_text_move_to_column (old_editable, -1);
4103 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4107 gtk_text_move_to_column (old_editable, 0);
4108 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4113 gtk_text_delete_line (GtkText *text)
4115 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4116 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4120 gtk_text_delete_to_line_end (GtkText *text)
4122 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4126 gtk_text_select_word (GtkText *text, guint32 time)
4131 GtkOldEditable *old_editable;
4132 old_editable = GTK_OLD_EDITABLE (text);
4134 gtk_text_move_backward_word (text);
4135 start_pos = text->cursor_mark.index;
4137 gtk_text_move_forward_word (text);
4138 end_pos = text->cursor_mark.index;
4140 old_editable->has_selection = TRUE;
4141 gtk_text_set_selection (old_editable, start_pos, end_pos);
4142 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4146 gtk_text_select_line (GtkText *text, guint32 time)
4151 GtkOldEditable *old_editable;
4152 old_editable = GTK_OLD_EDITABLE (text);
4154 gtk_text_move_beginning_of_line (text);
4155 start_pos = text->cursor_mark.index;
4157 gtk_text_move_end_of_line (text);
4158 gtk_text_move_forward_character (text);
4159 end_pos = text->cursor_mark.index;
4161 old_editable->has_selection = TRUE;
4162 gtk_text_set_selection (old_editable, start_pos, end_pos);
4163 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4166 /**********************************************************************/
4168 /**********************************************************************/
4171 adjust_adj (GtkText* text, GtkAdjustment* adj)
4175 gdk_window_get_size (text->text_area, NULL, &height);
4177 adj->step_increment = MIN (adj->upper, SCROLL_PIXELS);
4178 adj->page_increment = MIN (adj->upper, height - KEY_SCROLL_PIXELS);
4179 adj->page_size = MIN (adj->upper, height);
4180 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4181 adj->value = MAX (adj->value, 0.0);
4183 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4187 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
4189 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4191 if ((text->first_line_start_index >= lp->start.index) &&
4192 (text->first_line_start_index <= lp->end.index))
4194 svdata->mark = lp->start;
4196 if (text->first_line_start_index == lp->start.index)
4198 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4202 text->first_onscreen_ver_pixel = svdata->pixel_height;
4203 text->first_cut_pixels = 0;
4206 text->vadj->value = text->first_onscreen_ver_pixel;
4209 svdata->pixel_height += LINE_HEIGHT (*lp);
4215 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
4217 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4220 if (svdata->pixel_height <= (gint) text->vadj->value &&
4221 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4223 svdata->mark = lp->start;
4225 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4226 text->first_onscreen_ver_pixel = svdata->pixel_height;
4227 text->first_line_start_index = lp->start.index;
4233 svdata->pixel_height += LINE_HEIGHT (*lp);
4241 static GtkPropertyMark
4242 set_vertical_scroll (GtkText* text)
4244 GtkPropertyMark mark = find_mark (text, 0);
4245 SetVerticalScrollData data;
4249 data.pixel_height = 0;
4250 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4252 text->vadj->upper = data.pixel_height;
4253 orig_value = (gint) text->vadj->value;
4255 gdk_window_get_size (text->text_area, NULL, &height);
4257 text->vadj->step_increment = MIN (text->vadj->upper, SCROLL_PIXELS);
4258 text->vadj->page_increment = MIN (text->vadj->upper, height - KEY_SCROLL_PIXELS);
4259 text->vadj->page_size = MIN (text->vadj->upper, height);
4260 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4261 text->vadj->value = MAX (text->vadj->value, 0.0);
4263 text->last_ver_value = (gint)text->vadj->value;
4265 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4267 if (text->vadj->value != orig_value)
4269 /* We got clipped, and don't really know which line to put first. */
4270 data.pixel_height = 0;
4271 data.last_didnt_wrap = TRUE;
4273 line_params_iterate (text, &mark, NULL,
4275 set_vertical_scroll_find_iterator);
4282 scroll_int (GtkText* text, gint diff)
4286 text->vadj->value += diff;
4288 upper = text->vadj->upper - text->vadj->page_size;
4289 text->vadj->value = MIN (text->vadj->value, upper);
4290 text->vadj->value = MAX (text->vadj->value, 0.0);
4292 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4296 process_exposes (GtkText *text)
4300 /* Make sure graphics expose events are processed before scrolling
4303 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4305 gtk_widget_send_expose (GTK_WIDGET (text), event);
4306 if (event->expose.count == 0)
4308 gdk_event_free (event);
4311 gdk_event_free (event);
4315 static gint last_visible_line_height (GtkText* text)
4317 GList *cache = text->line_start_cache;
4320 gdk_window_get_size (text->text_area, NULL, &height);
4322 for (; cache->next; cache = cache->next)
4323 if (pixel_height_of(text, cache->next) > height)
4327 return pixel_height_of(text, cache) - 1;
4332 static gint first_visible_line_height (GtkText* text)
4334 if (text->first_cut_pixels)
4335 return pixel_height_of(text, text->line_start_cache) + 1;
4341 scroll_down (GtkText* text, gint diff0)
4347 text->first_onscreen_ver_pixel += diff0;
4351 g_assert (text->line_start_cache);
4353 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4355 text->first_cut_pixels += 1;
4359 text->first_cut_pixels = 0;
4361 text->line_start_cache = text->line_start_cache->next;
4363 text->first_line_start_index =
4364 CACHE_DATA(text->line_start_cache).start.index;
4366 if (!text->line_start_cache->next)
4367 fetch_lines_forward (text, 1);
4373 gdk_window_get_size (text->text_area, &width, &height);
4374 if (height > real_diff)
4375 gdk_draw_pixmap (text->text_area,
4383 height - real_diff);
4386 rect.y = MAX (0, height - real_diff);
4388 rect.height = MIN (height, real_diff);
4390 expose_text (text, &rect, FALSE);
4391 gtk_text_draw_focus ( (GtkWidget *) text);
4393 if (text->current_line)
4397 text->cursor_pos_y -= real_diff;
4398 cursor_min = drawn_cursor_min(text);
4401 find_mouse_cursor (text, text->cursor_pos_x,
4402 first_visible_line_height (text));
4405 if (height > real_diff)
4406 process_exposes (text);
4410 scroll_up (GtkText* text, gint diff0)
4416 text->first_onscreen_ver_pixel += diff0;
4420 g_assert (text->line_start_cache);
4422 if (text->first_cut_pixels > 0)
4424 text->first_cut_pixels -= 1;
4428 if (!text->line_start_cache->prev)
4429 fetch_lines_backward (text);
4431 text->line_start_cache = text->line_start_cache->prev;
4433 text->first_line_start_index =
4434 CACHE_DATA(text->line_start_cache).start.index;
4436 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4442 gdk_window_get_size (text->text_area, &width, &height);
4443 if (height > real_diff)
4444 gdk_draw_pixmap (text->text_area,
4452 height - real_diff);
4457 rect.height = MIN (height, real_diff);
4459 expose_text (text, &rect, FALSE);
4460 gtk_text_draw_focus ( (GtkWidget *) text);
4462 if (text->current_line)
4467 text->cursor_pos_y += real_diff;
4468 cursor_max = drawn_cursor_max(text);
4469 gdk_window_get_size (text->text_area, NULL, &height);
4471 if (cursor_max >= height)
4472 find_mouse_cursor (text, text->cursor_pos_x,
4473 last_visible_line_height (text));
4476 if (height > real_diff)
4477 process_exposes (text);
4480 /**********************************************************************/
4482 /**********************************************************************/
4484 /* Assumes mark starts a line. Calculates the height, width, and
4485 * displayable character count of a single DISPLAYABLE line. That
4486 * means that in line-wrap mode, this does may not compute the
4487 * properties of an entire line. */
4489 find_line_params (GtkText* text,
4490 const GtkPropertyMark* mark,
4491 const PrevTabCont *tab_cont,
4492 PrevTabCont *next_cont)
4495 TabStopMark tab_mark = tab_cont->tab_start;
4496 guint max_display_pixels;
4501 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4502 max_display_pixels -= LINE_WRAP_ROOM;
4505 lp.tab_cont = *tab_cont;
4508 lp.pixel_width = tab_cont->pixel_offset;
4509 lp.displayable_chars = 0;
4511 lp.font_descent = 0;
4513 init_tab_cont (text, next_cont);
4515 while (!LAST_INDEX(text, lp.end))
4517 g_assert (lp.end.property);
4519 ch = GTK_TEXT_INDEX (text, lp.end.index);
4520 font = MARK_CURRENT_FONT (text, &lp.end);
4522 if (ch == LINE_DELIM)
4524 /* Newline doesn't count in computation of line height, even
4525 * if its in a bigger font than the rest of the line. Unless,
4526 * of course, there are no other characters. */
4528 if (!lp.font_ascent && !lp.font_descent)
4530 lp.font_ascent = font->ascent;
4531 lp.font_descent = font->descent;
4534 lp.tab_cont_next = *next_cont;
4539 ch_width = find_char_width (text, &lp.end, &tab_mark);
4541 if ((ch_width + lp.pixel_width > max_display_pixels) &&
4542 (lp.end.index > lp.start.index))
4546 if (text->line_wrap)
4548 next_cont->tab_start = tab_mark;
4549 next_cont->pixel_offset = 0;
4553 /* Here's the tough case, a tab is wrapping. */
4554 gint pixels_avail = max_display_pixels - lp.pixel_width;
4555 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
4556 gint spaces_avail = pixels_avail / space_width;
4558 if (spaces_avail == 0)
4560 decrement_mark (&lp.end);
4564 advance_tab_mark (text, &next_cont->tab_start, '\t');
4565 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4567 lp.displayable_chars += 1;
4572 if (text->word_wrap)
4574 GtkPropertyMark saved_mark = lp.end;
4575 guint saved_characters = lp.displayable_chars;
4577 lp.displayable_chars += 1;
4579 if (text->use_wchar)
4581 while (!gdk_iswspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4582 (lp.end.index > lp.start.index))
4584 decrement_mark (&lp.end);
4585 lp.displayable_chars -= 1;
4590 while (!isspace(GTK_TEXT_INDEX (text, lp.end.index)) &&
4591 (lp.end.index > lp.start.index))
4593 decrement_mark (&lp.end);
4594 lp.displayable_chars -= 1;
4598 /* If whole line is one word, revert to char wrapping */
4599 if (lp.end.index == lp.start.index)
4601 lp.end = saved_mark;
4602 lp.displayable_chars = saved_characters;
4603 decrement_mark (&lp.end);
4608 /* Don't include this character, it will wrap. */
4609 decrement_mark (&lp.end);
4613 lp.tab_cont_next = *next_cont;
4620 lp.displayable_chars += 1;
4623 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4624 lp.font_descent = MAX (font->descent, lp.font_descent);
4625 lp.pixel_width += ch_width;
4627 advance_mark(&lp.end);
4628 advance_tab_mark (text, &tab_mark, ch);
4631 if (LAST_INDEX(text, lp.start))
4633 /* Special case, empty last line. */
4634 font = MARK_CURRENT_FONT (text, &lp.end);
4636 lp.font_ascent = font->ascent;
4637 lp.font_descent = font->descent;
4640 lp.tab_cont_next = *next_cont;
4646 expand_scratch_buffer (GtkText* text, guint len)
4648 if (len >= text->scratch_buffer_len)
4652 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4654 if (text->use_wchar)
4656 if (text->scratch_buffer.wc)
4657 text->scratch_buffer.wc = g_new (GdkWChar, i);
4659 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
4660 i*sizeof (GdkWChar));
4664 if (text->scratch_buffer.ch)
4665 text->scratch_buffer.ch = g_new (guchar, i);
4667 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
4670 text->scratch_buffer_len = i;
4674 /* Side effect: modifies text->gc
4677 draw_bg_rect (GtkText* text, GtkPropertyMark *mark,
4678 gint x, gint y, gint width, gint height,
4679 gboolean already_cleared)
4681 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4683 if ((mark->index >= MIN(old_editable->selection_start_pos, old_editable->selection_end_pos) &&
4684 mark->index < MAX(old_editable->selection_start_pos, old_editable->selection_end_pos)))
4686 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
4687 old_editable->has_selection ?
4688 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
4690 NULL, GTK_WIDGET(text), "text",
4691 x, y, width, height);
4693 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
4694 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
4696 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
4698 gdk_draw_rectangle (text->text_area,
4700 TRUE, x, y, width, height);
4702 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4709 rect.height = height;
4711 clear_area (text, &rect);
4713 else if (!already_cleared)
4714 gdk_window_clear_area (text->text_area, x, y, width, height);
4718 draw_line (GtkText* text,
4719 gint pixel_start_height,
4722 GdkGCValues gc_values;
4725 guint running_offset = lp->tab_cont.pixel_offset;
4726 union { GdkWChar *wc; guchar *ch; } buffer;
4729 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4731 guint selection_start_pos = MIN (old_editable->selection_start_pos, old_editable->selection_end_pos);
4732 guint selection_end_pos = MAX (old_editable->selection_start_pos, old_editable->selection_end_pos);
4734 GtkPropertyMark mark = lp->start;
4735 TabStopMark tab_mark = lp->tab_cont.tab_start;
4736 gint pixel_height = pixel_start_height + lp->font_ascent;
4737 guint chars = lp->displayable_chars;
4739 /* First provide a contiguous segment of memory. This makes reading
4740 * the code below *much* easier, and only incurs the cost of copying
4741 * when the line being displayed spans the gap. */
4742 if (mark.index <= text->gap_position &&
4743 mark.index + chars > text->gap_position)
4745 expand_scratch_buffer (text, chars);
4747 if (text->use_wchar)
4749 for (i = 0; i < chars; i += 1)
4750 text->scratch_buffer.wc[i] = GTK_TEXT_INDEX(text, mark.index + i);
4751 buffer.wc = text->scratch_buffer.wc;
4755 for (i = 0; i < chars; i += 1)
4756 text->scratch_buffer.ch[i] = GTK_TEXT_INDEX(text, mark.index + i);
4757 buffer.ch = text->scratch_buffer.ch;
4762 if (text->use_wchar)
4764 if (mark.index >= text->gap_position)
4765 buffer.wc = text->text.wc + mark.index + text->gap_size;
4767 buffer.wc = text->text.wc + mark.index;
4771 if (mark.index >= text->gap_position)
4772 buffer.ch = text->text.ch + mark.index + text->gap_size;
4774 buffer.ch = text->text.ch + mark.index;
4779 if (running_offset > 0)
4781 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
4782 LINE_HEIGHT (*lp), TRUE);
4788 if ((text->use_wchar && buffer.wc[0] != '\t') ||
4789 (!text->use_wchar && buffer.ch[0] != '\t'))
4791 union { GdkWChar *wc; guchar *ch; } next_tab;
4796 if (text->use_wchar)
4797 for (i=0; i<chars; i++)
4799 if (buffer.wc[i] == '\t')
4801 next_tab.wc = buffer.wc + i;
4806 next_tab.ch = memchr (buffer.ch, '\t', chars);
4808 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4810 if (text->use_wchar)
4813 len = MIN (len, next_tab.wc - buffer.wc);
4818 len = MIN (len, next_tab.ch - buffer.ch);
4821 if (mark.index < selection_start_pos)
4822 len = MIN (len, selection_start_pos - mark.index);
4823 else if (mark.index < selection_end_pos)
4824 len = MIN (len, selection_end_pos - mark.index);
4826 font = MARK_CURRENT_FONT (text, &mark);
4827 if (font->type == GDK_FONT_FONT)
4829 gdk_gc_set_font (text->gc, font);
4830 gdk_gc_get_values (text->gc, &gc_values);
4831 if (text->use_wchar)
4832 pixel_width = gdk_text_width_wc (gc_values.font,
4835 pixel_width = gdk_text_width (gc_values.font,
4840 if (text->use_wchar)
4841 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
4843 pixel_width = gdk_text_width (font, buffer.ch, len);
4846 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4847 pixel_width, LINE_HEIGHT (*lp), TRUE);
4849 if ((mark.index >= selection_start_pos) &&
4850 (mark.index < selection_end_pos))
4852 if (old_editable->has_selection)
4853 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_SELECTED];
4855 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_ACTIVE];
4859 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
4863 if (text->use_wchar)
4864 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
4871 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
4878 running_offset += pixel_width;
4880 advance_tab_mark_n (text, &tab_mark, len);
4884 gint pixels_remaining;
4890 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
4891 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
4893 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4895 spaces_avail = pixels_remaining / space_width;
4896 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
4898 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4899 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
4901 running_offset += tab_mark.to_next_tab *
4902 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4904 advance_tab_mark (text, &tab_mark, '\t');
4907 advance_mark_n (&mark, len);
4908 if (text->use_wchar)
4917 draw_line_wrap (GtkText* text, guint height /* baseline height */)
4924 if (text->line_wrap)
4926 bitmap = text->line_wrap_bitmap;
4927 bitmap_width = line_wrap_width;
4928 bitmap_height = line_wrap_height;
4932 bitmap = text->line_arrow_bitmap;
4933 bitmap_width = line_arrow_width;
4934 bitmap_height = line_arrow_height;
4937 gdk_window_get_size (text->text_area, &width, NULL);
4938 width -= LINE_WRAP_ROOM;
4940 gdk_gc_set_stipple (text->gc,
4943 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
4945 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
4947 gdk_gc_set_ts_origin (text->gc,
4949 height - bitmap_height - 1);
4951 gdk_draw_rectangle (text->text_area,
4955 height - bitmap_height - 1 /* one pixel above the baseline. */,
4959 gdk_gc_set_ts_origin (text->gc, 0, 0);
4961 gdk_gc_set_fill (text->gc, GDK_SOLID);
4965 undraw_cursor (GtkText* text, gint absolute)
4967 GtkOldEditable *old_editable = (GtkOldEditable *) text;
4969 TDEBUG (("in undraw_cursor\n"));
4972 text->cursor_drawn_level = 0;
4974 if ((text->cursor_drawn_level ++ == 0) &&
4975 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
4976 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
4980 g_assert(text->cursor_mark.property);
4982 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
4984 draw_bg_rect (text, &text->cursor_mark,
4986 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
4987 1, font->ascent + 1, FALSE);
4989 if (text->cursor_char)
4991 if (font->type == GDK_FONT_FONT)
4992 gdk_gc_set_font (text->gc, font);
4994 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
4996 gdk_draw_text_wc (text->text_area, font,
4999 text->cursor_pos_y - text->cursor_char_offset,
5007 drawn_cursor_min (GtkText* text)
5011 g_assert(text->cursor_mark.property);
5013 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5015 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5019 drawn_cursor_max (GtkText* text)
5021 g_assert(text->cursor_mark.property);
5023 return text->cursor_pos_y - text->cursor_char_offset;
5027 draw_cursor (GtkText* text, gint absolute)
5029 GtkOldEditable *old_editable = (GtkOldEditable *)text;
5031 TDEBUG (("in draw_cursor\n"));
5034 text->cursor_drawn_level = 1;
5036 if ((--text->cursor_drawn_level == 0) &&
5037 old_editable->editable &&
5038 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5039 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5043 g_assert (text->cursor_mark.property);
5045 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5047 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5049 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
5050 text->cursor_pos_y - text->cursor_char_offset,
5052 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5057 create_bg_gc (GtkText *text)
5061 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5062 values.fill = GDK_TILED;
5064 return gdk_gc_new_with_values (text->text_area, &values,
5065 GDK_GC_FILL | GDK_GC_TILE);
5069 clear_area (GtkText *text, GdkRectangle *area)
5071 GtkWidget *widget = GTK_WIDGET (text);
5077 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5079 gdk_gc_set_ts_origin (text->bg_gc,
5080 (- text->first_onscreen_hor_pixel) % width,
5081 (- text->first_onscreen_ver_pixel) % height);
5083 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5084 area->x, area->y, area->width, area->height);
5087 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5091 expose_text (GtkText* text, GdkRectangle *area, gboolean cursor)
5093 GList *cache = text->line_start_cache;
5094 gint pixels = - text->first_cut_pixels;
5095 gint min_y = MAX (0, area->y);
5096 gint max_y = MAX (0, area->y + area->height);
5099 gdk_window_get_size (text->text_area, NULL, &height);
5100 max_y = MIN (max_y, height);
5102 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5104 clear_area (text, area);
5106 for (; pixels < height; cache = cache->next)
5108 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5110 draw_line (text, pixels, &CACHE_DATA(cache));
5112 if (CACHE_DATA(cache).wraps)
5113 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5116 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5118 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5119 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5121 /* We undraw and draw the cursor here to get the drawn
5122 * level right ... FIXME - maybe the second parameter
5123 * of draw_cursor should work differently
5125 undraw_cursor (text, FALSE);
5126 draw_cursor (text, FALSE);
5130 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5134 fetch_lines_forward (text, 1);
5143 gtk_text_update_text (GtkOldEditable *old_editable,
5147 GtkText *text = GTK_TEXT (old_editable);
5149 GList *cache = text->line_start_cache;
5150 gint pixels = - text->first_cut_pixels;
5156 end_pos = TEXT_LENGTH (text);
5158 if (end_pos < start_pos)
5161 gdk_window_get_size (text->text_area, &width, &height);
5167 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5169 for (; pixels < height; cache = cache->next)
5171 if (CACHE_DATA(cache).start.index < end_pos)
5173 if (CACHE_DATA(cache).end.index >= start_pos)
5176 area.y = MAX(0,pixels);
5177 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5183 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5187 fetch_lines_forward (text, 1);
5195 expose_text (text, &area, TRUE);
5199 recompute_geometry (GtkText* text)
5201 GtkPropertyMark mark, start_mark;
5208 mark = start_mark = set_vertical_scroll (text);
5210 /* We need a real start of a line when calling fetch_lines().
5211 * not the start of a wrapped line.
5213 while (mark.index > 0 &&
5214 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5215 decrement_mark (&mark);
5217 gdk_window_get_size (text->text_area, &width, &height);
5219 /* Fetch an entire line, to make sure that we get all the text
5220 * we backed over above, in addition to enough text to fill up
5221 * the space vertically
5224 new_lines = fetch_lines (text,
5230 mark = CACHE_DATA (g_list_last (new_lines)).end;
5231 if (!LAST_INDEX (text, mark))
5233 advance_mark (&mark);
5235 new_lines = g_list_concat (new_lines,
5240 height + text->first_cut_pixels));
5243 /* Now work forward to the actual first onscreen line */
5245 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5246 new_lines = new_lines->next;
5248 text->line_start_cache = new_lines;
5250 find_cursor (text, TRUE);
5253 /**********************************************************************/
5255 /**********************************************************************/
5258 gtk_text_set_selection (GtkOldEditable *old_editable,
5262 GtkText *text = GTK_TEXT (old_editable);
5264 guint start1, end1, start2, end2;
5267 end = TEXT_LENGTH (text);
5269 start1 = MIN(start,end);
5270 end1 = MAX(start,end);
5271 start2 = MIN(old_editable->selection_start_pos, old_editable->selection_end_pos);
5272 end2 = MAX(old_editable->selection_start_pos, old_editable->selection_end_pos);
5274 if (start2 < start1)
5278 tmp = start1; start1 = start2; start2 = tmp;
5279 tmp = end1; end1 = end2; end2 = tmp;
5282 undraw_cursor (text, FALSE);
5283 old_editable->selection_start_pos = start;
5284 old_editable->selection_end_pos = end;
5285 draw_cursor (text, FALSE);
5287 /* Expose only what changed */
5289 if (start1 < start2)
5290 gtk_text_update_text (old_editable, start1, MIN(end1, start2));
5293 gtk_text_update_text (old_editable, MAX(end1, start2), end2);
5294 else if (end2 < end1)
5295 gtk_text_update_text (old_editable, end2, end1);
5299 /**********************************************************************/
5301 /**********************************************************************/
5303 #ifdef DEBUG_GTK_TEXT
5305 gtk_text_show_cache_line (GtkText *text, GList *cache,
5306 const char* what, const char* func, gint line)
5308 LineParams *lp = &CACHE_DATA(cache);
5311 if (cache == text->line_start_cache)
5312 g_message ("Line Start Cache: ");
5314 if (cache == text->current_line)
5315 g_message("Current Line: ");
5317 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
5325 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
5326 g_message ("%c", GTK_TEXT_INDEX (text, i));
5332 gtk_text_show_cache (GtkText *text, const char* func, gint line)
5334 GList *l = text->line_start_cache;
5340 /* back up to the absolute beginning of the line cache */
5344 g_message ("*** line cache ***\n");
5345 for (; l; l = l->next)
5346 gtk_text_show_cache_line (text, l, "all", func, line);
5350 gtk_text_assert_mark (GtkText *text,
5351 GtkPropertyMark *mark,
5352 GtkPropertyMark *before,
5353 GtkPropertyMark *after,
5358 GtkPropertyMark correct_mark = find_mark (text, mark->index);
5360 if (mark->offset != correct_mark.offset ||
5361 mark->property != correct_mark.property)
5362 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
5366 gtk_text_assert (GtkText *text,
5370 GList* cache = text->line_start_cache;
5371 GtkPropertyMark* before_mark = NULL;
5372 GtkPropertyMark* after_mark = NULL;
5374 gtk_text_show_props (text, msg, line);
5376 for (; cache->prev; cache = cache->prev)
5379 g_message ("*** line markers ***\n");
5381 for (; cache; cache = cache->next)
5383 after_mark = &CACHE_DATA(cache).end;
5384 gtk_text_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
5385 before_mark = &CACHE_DATA(cache).start;
5388 after_mark = &CACHE_DATA(cache->next).start;
5392 gtk_text_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
5393 before_mark = &CACHE_DATA(cache).end;
5398 gtk_text_show_adj (GtkText *text,
5404 g_message ("*** adjustment ***\n");
5406 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
5413 adj->step_increment,
5414 adj->page_increment,
5419 gtk_text_show_props (GtkText *text,
5423 GList* props = text->text_properties;
5426 g_message ("%s:%d: ", msg, line);
5428 for (; props; props = props->next)
5430 TextProperty *p = (TextProperty*)props->data;
5432 proplen += p->length;
5434 g_message ("[%d,%p,", p->length, p);
5435 if (p->flags & PROPERTY_FONT)
5436 g_message ("%p,", p->font);
5439 if (p->flags & PROPERTY_FOREGROUND)
5440 g_message ("%ld, ", p->fore_color.pixel);
5443 if (p->flags & PROPERTY_BACKGROUND)
5444 g_message ("%ld] ", p->back_color.pixel);
5451 if (proplen - 1 != TEXT_LENGTH(text))
5452 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);
5456 #define __GTK_TEXT_C__
5457 #include "gtkaliasdef.c"