1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdk/gdkkeysyms.h"
30 #include "gdk/gdki18n.h"
32 #include "gtkselection.h"
33 #include "gtksignal.h"
34 #define GTK_ENABLE_BROKEN
36 #include "line-wrap.xbm"
37 #include "line-arrow.xbm"
41 #define INITIAL_BUFFER_SIZE 1024
42 #define INITIAL_LINE_CACHE_SIZE 256
43 #define MIN_GAP_SIZE 256
44 #define LINE_DELIM '\n'
45 #define MIN_TEXT_WIDTH_LINES 20
46 #define MIN_TEXT_HEIGHT_LINES 10
47 #define TEXT_BORDER_ROOM 1
48 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
49 #define DEFAULT_TAB_STOP_WIDTH 4
50 #define SCROLL_PIXELS 5
51 #define KEY_SCROLL_PIXELS 10
52 #define SCROLL_TIME 100
53 #define FREEZE_LENGTH 1024
54 /* Freeze text when inserting or deleting more than this many characters */
56 #define SET_PROPERTY_MARK(m, p, o) do { \
57 (m)->property = (p); \
60 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
61 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
62 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
63 (mark)->property->prev->data \
65 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
66 #define MARK_LIST_PTR(mark) ((mark)->property)
67 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
68 #define MARK_OFFSET(mark) ((mark)->offset)
69 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
72 #define MARK_CURRENT_FONT(text, mark) \
73 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
74 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
75 gtk_style_get_font (GTK_WIDGET (text)->style))
76 #define MARK_CURRENT_FORE(text, mark) \
77 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
78 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
79 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
80 #define MARK_CURRENT_BACK(text, mark) \
81 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
82 &MARK_CURRENT_PROPERTY(mark)->back_color : \
83 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
84 #define MARK_CURRENT_TEXT_FONT(text, mark) \
85 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
86 MARK_CURRENT_PROPERTY(mark)->font : \
89 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
90 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
91 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
92 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
93 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
94 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
95 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
96 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
106 typedef struct _TextProperty TextProperty;
107 typedef struct _TabStopMark TabStopMark;
108 typedef struct _PrevTabCont PrevTabCont;
109 typedef struct _FetchLinesData FetchLinesData;
110 typedef struct _LineParams LineParams;
111 typedef struct _SetVerticalScrollData SetVerticalScrollData;
113 typedef gint (*LineIteratorFunction) (GtkText* text, LineParams* lp, void* data);
121 struct _SetVerticalScrollData {
123 gint last_didnt_wrap;
124 gint last_line_start;
125 GtkPropertyMark mark;
130 /* The actual font. */
134 gint16 char_widths[256];
138 PROPERTY_FONT = 1 << 0,
139 PROPERTY_FOREGROUND = 1 << 1,
140 PROPERTY_BACKGROUND = 1 << 2
148 /* Background Color. */
151 /* Foreground Color. */
154 /* Show which properties are set */
155 TextPropertyFlags flags;
157 /* Length of this property. */
163 GList* tab_stops; /* Index into list containing the next tab position. If
164 * NULL, using default widths. */
171 TabStopMark tab_start;
174 struct _FetchLinesData
187 guint displayable_chars;
190 PrevTabCont tab_cont;
191 PrevTabCont tab_cont_next;
193 GtkPropertyMark start;
198 static void gtk_text_class_init (GtkTextClass *klass);
199 static void gtk_text_set_property (GObject *object,
203 static void gtk_text_get_property (GObject *object,
207 static void gtk_text_editable_init (GtkEditableClass *iface);
208 static void gtk_text_init (GtkText *text);
209 static void gtk_text_destroy (GtkObject *object);
210 static void gtk_text_finalize (GObject *object);
211 static void gtk_text_realize (GtkWidget *widget);
212 static void gtk_text_unrealize (GtkWidget *widget);
213 static void gtk_text_style_set (GtkWidget *widget,
214 GtkStyle *previous_style);
215 static void gtk_text_state_changed (GtkWidget *widget,
216 GtkStateType previous_state);
217 static void gtk_text_draw_focus (GtkWidget *widget);
218 static void gtk_text_size_request (GtkWidget *widget,
219 GtkRequisition *requisition);
220 static void gtk_text_size_allocate (GtkWidget *widget,
221 GtkAllocation *allocation);
222 static void gtk_text_adjustment (GtkAdjustment *adjustment,
224 static void gtk_text_insert_text (GtkEditable *editable,
225 const gchar *new_text,
226 gint new_text_length,
228 static void gtk_text_delete_text (GtkEditable *editable,
231 static void gtk_text_update_text (GtkOldEditable *old_editable,
234 static gchar *gtk_text_get_chars (GtkOldEditable *old_editable,
237 static void gtk_text_set_selection (GtkOldEditable *old_editable,
240 static void gtk_text_real_set_editable (GtkOldEditable *old_editable,
241 gboolean is_editable);
243 static void gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
247 static gint gtk_text_expose (GtkWidget *widget,
248 GdkEventExpose *event);
249 static gint gtk_text_button_press (GtkWidget *widget,
250 GdkEventButton *event);
251 static gint gtk_text_button_release (GtkWidget *widget,
252 GdkEventButton *event);
253 static gint gtk_text_motion_notify (GtkWidget *widget,
254 GdkEventMotion *event);
255 static gint gtk_text_key_press (GtkWidget *widget,
257 static gint gtk_text_focus_in (GtkWidget *widget,
258 GdkEventFocus *event);
259 static gint gtk_text_focus_out (GtkWidget *widget,
260 GdkEventFocus *event);
262 static void move_gap (GtkText* text, guint index);
263 static void make_forward_space (GtkText* text, guint len);
265 /* Property management */
266 static GtkTextFont* get_text_font (GdkFont* gfont);
267 static void text_font_unref (GtkTextFont *text_font);
269 static void insert_text_property (GtkText* text, GdkFont* font,
270 GdkColor *fore, GdkColor* back, guint len);
271 static TextProperty* new_text_property (GtkText *text, GdkFont* font,
272 GdkColor* fore, GdkColor* back, guint length);
273 static void destroy_text_property (TextProperty *prop);
274 static void init_properties (GtkText *text);
275 static void realize_property (GtkText *text, TextProperty *prop);
276 static void realize_properties (GtkText *text);
277 static void unrealize_property (GtkText *text, TextProperty *prop);
278 static void unrealize_properties (GtkText *text);
280 static void delete_text_property (GtkText* text, guint len);
282 static guint pixel_height_of (GtkText* text, GList* cache_line);
284 /* Property Movement and Size Computations */
285 static void advance_mark (GtkPropertyMark* mark);
286 static void decrement_mark (GtkPropertyMark* mark);
287 static void advance_mark_n (GtkPropertyMark* mark, gint n);
288 static void decrement_mark_n (GtkPropertyMark* mark, gint n);
289 static void move_mark_n (GtkPropertyMark* mark, gint n);
290 static GtkPropertyMark find_mark (GtkText* text, guint mark_position);
291 static GtkPropertyMark find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near);
292 static void find_line_containing_point (GtkText* text, guint point,
296 static void compute_lines_pixels (GtkText* text, guint char_count,
297 guint *lines, guint *pixels);
299 static gint total_line_height (GtkText* text,
302 static LineParams find_line_params (GtkText* text,
303 const GtkPropertyMark *mark,
304 const PrevTabCont *tab_cont,
305 PrevTabCont *next_cont);
306 static void recompute_geometry (GtkText* text);
307 static void insert_expose (GtkText* text, guint old_pixels, gint nchars, guint new_line_count);
308 static void delete_expose (GtkText* text,
312 static GdkGC *create_bg_gc (GtkText *text);
313 static void clear_area (GtkText *text, GdkRectangle *area);
314 static void draw_line (GtkText* text,
317 static void draw_line_wrap (GtkText* text,
319 static void draw_cursor (GtkText* text, gint absolute);
320 static void undraw_cursor (GtkText* text, gint absolute);
321 static gint drawn_cursor_min (GtkText* text);
322 static gint drawn_cursor_max (GtkText* text);
323 static void expose_text (GtkText* text, GdkRectangle *area, gboolean cursor);
325 /* Search and Placement. */
326 static void find_cursor (GtkText* text,
328 static void find_cursor_at_line (GtkText* text,
329 const LineParams* start_line,
331 static void find_mouse_cursor (GtkText* text, gint x, gint y);
334 static void adjust_adj (GtkText* text, GtkAdjustment* adj);
335 static void scroll_up (GtkText* text, gint diff);
336 static void scroll_down (GtkText* text, gint diff);
337 static void scroll_int (GtkText* text, gint diff);
339 static void process_exposes (GtkText *text);
341 /* Cache Management. */
342 static void free_cache (GtkText* text);
343 static GList* remove_cache_line (GtkText* text, GList* list);
346 static void move_cursor_buffer_ver (GtkText *text, int dir);
347 static void move_cursor_page_ver (GtkText *text, int dir);
348 static void move_cursor_ver (GtkText *text, int count);
349 static void move_cursor_hor (GtkText *text, int count);
351 /* Binding actions */
352 static void gtk_text_move_cursor (GtkOldEditable *old_editable,
355 static void gtk_text_move_word (GtkOldEditable *old_editable,
357 static void gtk_text_move_page (GtkOldEditable *old_editable,
360 static void gtk_text_move_to_row (GtkOldEditable *old_editable,
362 static void gtk_text_move_to_column (GtkOldEditable *old_editable,
364 static void gtk_text_kill_char (GtkOldEditable *old_editable,
366 static void gtk_text_kill_word (GtkOldEditable *old_editable,
368 static void gtk_text_kill_line (GtkOldEditable *old_editable,
372 static void gtk_text_move_forward_character (GtkText *text);
373 static void gtk_text_move_backward_character (GtkText *text);
374 static void gtk_text_move_forward_word (GtkText *text);
375 static void gtk_text_move_backward_word (GtkText *text);
376 static void gtk_text_move_beginning_of_line (GtkText *text);
377 static void gtk_text_move_end_of_line (GtkText *text);
378 static void gtk_text_move_next_line (GtkText *text);
379 static void gtk_text_move_previous_line (GtkText *text);
381 static void gtk_text_delete_forward_character (GtkText *text);
382 static void gtk_text_delete_backward_character (GtkText *text);
383 static void gtk_text_delete_forward_word (GtkText *text);
384 static void gtk_text_delete_backward_word (GtkText *text);
385 static void gtk_text_delete_line (GtkText *text);
386 static void gtk_text_delete_to_line_end (GtkText *text);
387 static void gtk_text_select_word (GtkText *text,
389 static void gtk_text_select_line (GtkText *text,
392 static void gtk_text_set_position (GtkOldEditable *old_editable,
395 /* #define DEBUG_GTK_TEXT */
397 #if defined(DEBUG_GTK_TEXT) && defined(__GNUC__)
398 /* Debugging utilities. */
399 static void gtk_text_assert_mark (GtkText *text,
400 GtkPropertyMark *mark,
401 GtkPropertyMark *before,
402 GtkPropertyMark *after,
407 static void gtk_text_assert (GtkText *text,
410 static void gtk_text_show_cache_line (GtkText *text, GList *cache,
411 const char* what, const char* func, gint line);
412 static void gtk_text_show_cache (GtkText *text, const char* func, gint line);
413 static void gtk_text_show_adj (GtkText *text,
418 static void gtk_text_show_props (GtkText* test,
422 #define TDEBUG(args) g_message args
423 #define TEXT_ASSERT(text) gtk_text_assert (text,__PRETTY_FUNCTION__,__LINE__)
424 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_text_assert_mark (text,mark, \
425 __PRETTY_FUNCTION__,msg,__LINE__)
426 #define TEXT_SHOW(text) gtk_text_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
427 #define TEXT_SHOW_LINE(text,line,msg) gtk_text_show_cache_line (text,line,msg,\
428 __PRETTY_FUNCTION__,__LINE__)
429 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_text_show_adj (text,adj,msg, \
430 __PRETTY_FUNCTION__,__LINE__)
433 #define TEXT_ASSERT(text)
434 #define TEXT_ASSERT_MARK(text,mark,msg)
435 #define TEXT_SHOW(text)
436 #define TEXT_SHOW_LINE(text,line,msg)
437 #define TEXT_SHOW_ADJ(text,adj,msg)
440 /* Memory Management. */
441 static GMemChunk *params_mem_chunk = NULL;
442 static GMemChunk *text_property_chunk = NULL;
444 static GtkWidgetClass *parent_class = NULL;
447 static const GtkTextFunction control_keys[26] =
449 (GtkTextFunction)gtk_text_move_beginning_of_line, /* a */
450 (GtkTextFunction)gtk_text_move_backward_character, /* b */
451 (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
452 (GtkTextFunction)gtk_text_delete_forward_character, /* d */
453 (GtkTextFunction)gtk_text_move_end_of_line, /* e */
454 (GtkTextFunction)gtk_text_move_forward_character, /* f */
456 (GtkTextFunction)gtk_text_delete_backward_character, /* h */
459 (GtkTextFunction)gtk_text_delete_to_line_end, /* k */
462 (GtkTextFunction)gtk_text_move_next_line, /* n */
464 (GtkTextFunction)gtk_text_move_previous_line, /* p */
469 (GtkTextFunction)gtk_text_delete_line, /* u */
470 (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
471 (GtkTextFunction)gtk_text_delete_backward_word, /* w */
472 (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
477 static const GtkTextFunction alt_keys[26] =
480 (GtkTextFunction)gtk_text_move_backward_word, /* b */
482 (GtkTextFunction)gtk_text_delete_forward_word, /* d */
484 (GtkTextFunction)gtk_text_move_forward_word, /* f */
508 /**********************************************************************/
510 /**********************************************************************/
513 gtk_text_get_type (void)
515 static GtkType text_type = 0;
519 static const GtkTypeInfo text_info =
523 sizeof (GtkTextClass),
524 (GtkClassInitFunc) gtk_text_class_init,
525 (GtkObjectInitFunc) gtk_text_init,
526 /* reserved_1 */ NULL,
527 /* reserved_2 */ NULL,
528 (GtkClassInitFunc) NULL,
531 static const GInterfaceInfo editable_info =
533 (GInterfaceInitFunc) gtk_text_editable_init, /* interface_init */
534 NULL, /* interface_finalize */
535 NULL /* interface_data */
538 text_type = gtk_type_unique (GTK_TYPE_OLD_EDITABLE, &text_info);
539 g_type_add_interface_static (text_type,
548 gtk_text_class_init (GtkTextClass *class)
550 GObjectClass *gobject_class;
551 GtkObjectClass *object_class;
552 GtkWidgetClass *widget_class;
553 GtkOldEditableClass *old_editable_class;
555 gobject_class = G_OBJECT_CLASS (class);
556 object_class = (GtkObjectClass*) class;
557 widget_class = (GtkWidgetClass*) class;
558 old_editable_class = (GtkOldEditableClass*) class;
559 parent_class = gtk_type_class (GTK_TYPE_OLD_EDITABLE);
561 gobject_class->finalize = gtk_text_finalize;
562 gobject_class->set_property = gtk_text_set_property;
563 gobject_class->get_property = gtk_text_get_property;
565 object_class->destroy = gtk_text_destroy;
567 widget_class->realize = gtk_text_realize;
568 widget_class->unrealize = gtk_text_unrealize;
569 widget_class->style_set = gtk_text_style_set;
570 widget_class->state_changed = gtk_text_state_changed;
571 widget_class->size_request = gtk_text_size_request;
572 widget_class->size_allocate = gtk_text_size_allocate;
573 widget_class->expose_event = gtk_text_expose;
574 widget_class->button_press_event = gtk_text_button_press;
575 widget_class->button_release_event = gtk_text_button_release;
576 widget_class->motion_notify_event = gtk_text_motion_notify;
577 widget_class->key_press_event = gtk_text_key_press;
578 widget_class->focus_in_event = gtk_text_focus_in;
579 widget_class->focus_out_event = gtk_text_focus_out;
581 old_editable_class->set_editable = gtk_text_real_set_editable;
583 old_editable_class->move_cursor = gtk_text_move_cursor;
584 old_editable_class->move_word = gtk_text_move_word;
585 old_editable_class->move_page = gtk_text_move_page;
586 old_editable_class->move_to_row = gtk_text_move_to_row;
587 old_editable_class->move_to_column = gtk_text_move_to_column;
589 old_editable_class->kill_char = gtk_text_kill_char;
590 old_editable_class->kill_word = gtk_text_kill_word;
591 old_editable_class->kill_line = gtk_text_kill_line;
593 old_editable_class->update_text = gtk_text_update_text;
594 old_editable_class->get_chars = gtk_text_get_chars;
595 old_editable_class->set_selection = gtk_text_set_selection;
596 old_editable_class->set_position = gtk_text_set_position;
598 class->set_scroll_adjustments = gtk_text_set_adjustments;
600 g_object_class_install_property (gobject_class,
602 g_param_spec_object ("hadjustment",
603 _("Horizontal Adjustment"),
604 _("Horizontal adjustment for the text widget"),
608 g_object_class_install_property (gobject_class,
610 g_param_spec_object ("vadjustment",
611 _("Vertical Adjustment"),
612 _("Vertical adjustment for the text widget"),
616 g_object_class_install_property (gobject_class,
618 g_param_spec_boolean ("line_wrap",
620 _("Whether lines are wrapped at widget edges"),
624 g_object_class_install_property (gobject_class,
626 g_param_spec_boolean ("word_wrap",
628 _("Whether words are wrapped at widget edges"),
632 widget_class->set_scroll_adjustments_signal =
633 gtk_signal_new ("set_scroll_adjustments",
635 GTK_CLASS_TYPE (object_class),
636 GTK_SIGNAL_OFFSET (GtkTextClass, set_scroll_adjustments),
637 gtk_marshal_VOID__OBJECT_OBJECT,
638 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
642 gtk_text_set_property (GObject *object,
649 text = GTK_TEXT (object);
653 case PROP_HADJUSTMENT:
654 gtk_text_set_adjustments (text,
655 g_value_get_object (value),
658 case PROP_VADJUSTMENT:
659 gtk_text_set_adjustments (text,
661 g_value_get_object (value));
664 gtk_text_set_line_wrap (text, g_value_get_boolean (value));
667 gtk_text_set_word_wrap (text, g_value_get_boolean (value));
670 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
676 gtk_text_get_property (GObject *object,
683 text = GTK_TEXT (object);
687 case PROP_HADJUSTMENT:
688 g_value_set_object (value, text->hadj);
690 case PROP_VADJUSTMENT:
691 g_value_set_object (value, text->vadj);
694 g_value_set_boolean (value, text->line_wrap);
697 g_value_set_boolean (value, text->word_wrap);
700 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
706 gtk_text_editable_init (GtkEditableClass *iface)
708 iface->insert_text = gtk_text_insert_text;
709 iface->delete_text = gtk_text_delete_text;
713 gtk_text_init (GtkText *text)
715 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
717 text->text_area = NULL;
722 text->line_wrap_bitmap = NULL;
723 text->line_arrow_bitmap = NULL;
725 text->use_wchar = FALSE;
726 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
727 text->text_len = INITIAL_BUFFER_SIZE;
729 text->scratch_buffer.ch = NULL;
730 text->scratch_buffer_len = 0;
732 text->freeze_count = 0;
734 if (!params_mem_chunk)
735 params_mem_chunk = g_mem_chunk_new ("LineParams",
737 256 * sizeof (LineParams),
740 text->default_tab_width = 4;
741 text->tab_stops = NULL;
743 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
744 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
746 text->line_start_cache = NULL;
747 text->first_cut_pixels = 0;
749 text->line_wrap = TRUE;
750 text->word_wrap = FALSE;
755 text->current_font = NULL;
757 init_properties (text);
759 GTK_OLD_EDITABLE (text)->editable = FALSE;
761 gtk_text_set_adjustments (text, NULL, NULL);
762 gtk_editable_set_position (GTK_EDITABLE (text), 0);
766 gtk_text_new (GtkAdjustment *hadj,
772 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
774 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
776 text = gtk_widget_new (GTK_TYPE_TEXT,
785 gtk_text_set_word_wrap (GtkText *text,
788 g_return_if_fail (GTK_IS_TEXT (text));
790 text->word_wrap = (word_wrap != FALSE);
792 if (GTK_WIDGET_REALIZED (text))
794 recompute_geometry (text);
795 gtk_widget_queue_draw (GTK_WIDGET (text));
798 g_object_notify (G_OBJECT (text), "word_wrap");
802 gtk_text_set_line_wrap (GtkText *text,
805 g_return_if_fail (GTK_IS_TEXT (text));
807 text->line_wrap = (line_wrap != FALSE);
809 if (GTK_WIDGET_REALIZED (text))
811 recompute_geometry (text);
812 gtk_widget_queue_draw (GTK_WIDGET (text));
815 g_object_notify (G_OBJECT (text), "line_wrap");
819 gtk_text_set_editable (GtkText *text,
820 gboolean is_editable)
822 g_return_if_fail (GTK_IS_TEXT (text));
824 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
828 gtk_text_real_set_editable (GtkOldEditable *old_editable,
829 gboolean is_editable)
833 g_return_if_fail (GTK_IS_TEXT (old_editable));
835 text = GTK_TEXT (old_editable);
837 old_editable->editable = (is_editable != FALSE);
840 draw_cursor (text, TRUE);
842 undraw_cursor (text, TRUE);
846 gtk_text_set_adjustments (GtkText *text,
850 g_return_if_fail (GTK_IS_TEXT (text));
852 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
854 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
856 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
858 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
860 if (text->hadj && (text->hadj != hadj))
862 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
863 gtk_object_unref (GTK_OBJECT (text->hadj));
866 if (text->vadj && (text->vadj != vadj))
868 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
869 gtk_object_unref (GTK_OBJECT (text->vadj));
872 if (text->hadj != hadj)
875 gtk_object_ref (GTK_OBJECT (text->hadj));
876 gtk_object_sink (GTK_OBJECT (text->hadj));
878 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
879 (GtkSignalFunc) gtk_text_adjustment,
881 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
882 (GtkSignalFunc) gtk_text_adjustment,
884 gtk_signal_connect (GTK_OBJECT (text->hadj), "destroy",
885 (GtkSignalFunc) gtk_text_adjustment_destroyed,
887 gtk_text_adjustment (hadj, text);
889 g_object_notify (G_OBJECT (text), "hadjustment");
892 if (text->vadj != vadj)
895 gtk_object_ref (GTK_OBJECT (text->vadj));
896 gtk_object_sink (GTK_OBJECT (text->vadj));
898 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
899 (GtkSignalFunc) gtk_text_adjustment,
901 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
902 (GtkSignalFunc) gtk_text_adjustment,
904 gtk_signal_connect (GTK_OBJECT (text->vadj), "destroy",
905 (GtkSignalFunc) gtk_text_adjustment_destroyed,
907 gtk_text_adjustment (vadj, text);
909 g_object_notify (G_OBJECT (text), "vadjustment");
914 gtk_text_set_point (GtkText *text,
917 g_return_if_fail (GTK_IS_TEXT (text));
918 g_return_if_fail (index <= TEXT_LENGTH (text));
920 text->point = find_mark (text, index);
924 gtk_text_get_point (GtkText *text)
926 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
928 return text->point.index;
932 gtk_text_get_length (GtkText *text)
934 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
936 return TEXT_LENGTH (text);
940 gtk_text_freeze (GtkText *text)
942 g_return_if_fail (GTK_IS_TEXT (text));
944 text->freeze_count++;
948 gtk_text_thaw (GtkText *text)
950 g_return_if_fail (GTK_IS_TEXT (text));
952 if (text->freeze_count)
953 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
955 recompute_geometry (text);
956 gtk_widget_queue_draw (GTK_WIDGET (text));
961 gtk_text_insert (GtkText *text,
968 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
969 gboolean frozen = FALSE;
971 gint new_line_count = 1;
972 guint old_height = 0;
977 g_return_if_fail (GTK_IS_TEXT (text));
980 length = strlen (chars);
987 if (!text->freeze_count && (length > FREEZE_LENGTH))
989 gtk_text_freeze (text);
993 if (!text->freeze_count && (text->line_start_cache != NULL))
995 find_line_containing_point (text, text->point.index, TRUE);
996 old_height = total_line_height (text, text->current_line, 1);
999 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
1002 widget = GTK_WIDGET (text);
1003 gtk_widget_ensure_style (widget);
1004 if ((widget->style) && (gtk_style_get_font (widget->style)->type == GDK_FONT_FONTSET))
1006 text->use_wchar = TRUE;
1007 g_free (text->text.ch);
1008 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
1009 text->text_len = INITIAL_BUFFER_SIZE;
1010 if (text->scratch_buffer.ch)
1011 g_free (text->scratch_buffer.ch);
1012 text->scratch_buffer.wc = NULL;
1013 text->scratch_buffer_len = 0;
1017 move_gap (text, text->point.index);
1018 make_forward_space (text, length);
1020 if (text->use_wchar)
1022 char *chars_nt = (char *)chars;
1025 chars_nt = g_new (char, length+1);
1026 memcpy (chars_nt, chars, length);
1027 chars_nt[length] = 0;
1029 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
1031 if (chars_nt != chars)
1039 memcpy(text->text.ch + text->gap_position, chars, length);
1042 if (!text->freeze_count && (text->line_start_cache != NULL))
1044 if (text->use_wchar)
1046 for (i=0; i<numwcs; i++)
1047 if (text->text.wc[text->gap_position + i] == '\n')
1052 for (i=0; i<numwcs; i++)
1053 if (text->text.ch[text->gap_position + i] == '\n')
1060 insert_text_property (text, font, fore, back, numwcs);
1062 text->gap_size -= numwcs;
1063 text->gap_position += numwcs;
1065 if (text->point.index < text->first_line_start_index)
1066 text->first_line_start_index += numwcs;
1067 if (text->point.index < old_editable->selection_start_pos)
1068 old_editable->selection_start_pos += numwcs;
1069 if (text->point.index < old_editable->selection_end_pos)
1070 old_editable->selection_end_pos += numwcs;
1071 /* We'll reset the cursor later anyways if we aren't frozen */
1072 if (text->point.index < text->cursor_mark.index)
1073 text->cursor_mark.index += numwcs;
1075 advance_mark_n (&text->point, numwcs);
1077 if (!text->freeze_count && (text->line_start_cache != NULL))
1078 insert_expose (text, old_height, numwcs, new_line_count);
1082 gtk_text_thaw (text);
1086 gtk_text_backward_delete (GtkText *text,
1089 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1091 if (nchars > text->point.index || nchars <= 0)
1094 gtk_text_set_point (text, text->point.index - nchars);
1096 return gtk_text_forward_delete (text, nchars);
1100 gtk_text_forward_delete (GtkText *text,
1103 guint old_lines, old_height;
1104 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
1105 gboolean frozen = FALSE;
1107 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1109 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1112 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1114 gtk_text_freeze (text);
1118 if (!text->freeze_count && text->line_start_cache != NULL)
1120 /* We need to undraw the cursor here, since we may later
1121 * delete the cursor's property
1123 undraw_cursor (text, FALSE);
1124 find_line_containing_point (text, text->point.index, TRUE);
1125 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1128 /* FIXME, or resizing after deleting will be odd */
1129 if (text->point.index < text->first_line_start_index)
1131 if (text->point.index + nchars >= text->first_line_start_index)
1133 text->first_line_start_index = text->point.index;
1134 while ((text->first_line_start_index > 0) &&
1135 (GTK_TEXT_INDEX (text, text->first_line_start_index - 1)
1137 text->first_line_start_index -= 1;
1141 text->first_line_start_index -= nchars;
1144 if (text->point.index < old_editable->selection_start_pos)
1145 old_editable->selection_start_pos -=
1146 MIN(nchars, old_editable->selection_start_pos - text->point.index);
1147 if (text->point.index < old_editable->selection_end_pos)
1148 old_editable->selection_end_pos -=
1149 MIN(nchars, old_editable->selection_end_pos - text->point.index);
1150 /* We'll reset the cursor later anyways if we aren't frozen */
1151 if (text->point.index < text->cursor_mark.index)
1152 move_mark_n (&text->cursor_mark,
1153 -MIN(nchars, text->cursor_mark.index - text->point.index));
1155 move_gap (text, text->point.index);
1157 text->gap_size += nchars;
1159 delete_text_property (text, nchars);
1161 if (!text->freeze_count && (text->line_start_cache != NULL))
1163 delete_expose (text, nchars, old_lines, old_height);
1164 draw_cursor (text, FALSE);
1168 gtk_text_thaw (text);
1174 gtk_text_set_position (GtkOldEditable *old_editable,
1177 GtkText *text = (GtkText *) old_editable;
1180 position = gtk_text_get_length (text);
1182 undraw_cursor (text, FALSE);
1183 text->cursor_mark = find_mark (text, position);
1184 find_cursor (text, TRUE);
1185 draw_cursor (text, FALSE);
1186 gtk_editable_select_region (GTK_EDITABLE (old_editable), 0, 0);
1190 gtk_text_get_chars (GtkOldEditable *old_editable,
1198 g_return_val_if_fail (GTK_IS_TEXT (old_editable), NULL);
1199 text = GTK_TEXT (old_editable);
1202 end_pos = TEXT_LENGTH (text);
1204 if ((start_pos < 0) ||
1205 (end_pos > TEXT_LENGTH (text)) ||
1206 (end_pos < start_pos))
1209 move_gap (text, TEXT_LENGTH (text));
1210 make_forward_space (text, 1);
1212 if (text->use_wchar)
1215 ch = text->text.wc[end_pos];
1216 text->text.wc[end_pos] = 0;
1217 retval = gdk_wcstombs (text->text.wc + start_pos);
1218 text->text.wc[end_pos] = ch;
1223 ch = text->text.ch[end_pos];
1224 text->text.ch[end_pos] = 0;
1225 retval = g_strdup (text->text.ch + start_pos);
1226 text->text.ch[end_pos] = ch;
1234 gtk_text_destroy (GtkObject *object)
1238 g_return_if_fail (GTK_IS_TEXT (object));
1240 text = GTK_TEXT (object);
1244 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1245 gtk_object_unref (GTK_OBJECT (text->hadj));
1250 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1251 gtk_object_unref (GTK_OBJECT (text->vadj));
1257 gtk_timeout_remove (text->timer);
1261 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1265 gtk_text_finalize (GObject *object)
1270 g_return_if_fail (GTK_IS_TEXT (object));
1272 text = GTK_TEXT (object);
1274 /* Clean up the internal structures */
1275 if (text->use_wchar)
1276 g_free (text->text.wc);
1278 g_free (text->text.ch);
1280 tmp_list = text->text_properties;
1283 destroy_text_property (tmp_list->data);
1284 tmp_list = tmp_list->next;
1287 if (text->current_font)
1288 text_font_unref (text->current_font);
1290 g_list_free (text->text_properties);
1292 if (text->use_wchar)
1294 if (text->scratch_buffer.wc)
1295 g_free (text->scratch_buffer.wc);
1299 if (text->scratch_buffer.ch)
1300 g_free (text->scratch_buffer.ch);
1303 g_list_free (text->tab_stops);
1305 G_OBJECT_CLASS (parent_class)->finalize (object);
1309 gtk_text_realize (GtkWidget *widget)
1312 GtkOldEditable *old_editable;
1313 GdkWindowAttr attributes;
1314 gint attributes_mask;
1316 g_return_if_fail (GTK_IS_TEXT (widget));
1318 text = GTK_TEXT (widget);
1319 old_editable = GTK_OLD_EDITABLE (widget);
1320 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1322 attributes.window_type = GDK_WINDOW_CHILD;
1323 attributes.x = widget->allocation.x;
1324 attributes.y = widget->allocation.y;
1325 attributes.width = widget->allocation.width;
1326 attributes.height = widget->allocation.height;
1327 attributes.wclass = GDK_INPUT_OUTPUT;
1328 attributes.visual = gtk_widget_get_visual (widget);
1329 attributes.colormap = gtk_widget_get_colormap (widget);
1330 attributes.event_mask = gtk_widget_get_events (widget);
1331 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1332 GDK_BUTTON_PRESS_MASK |
1333 GDK_BUTTON_RELEASE_MASK |
1334 GDK_BUTTON_MOTION_MASK |
1335 GDK_ENTER_NOTIFY_MASK |
1336 GDK_LEAVE_NOTIFY_MASK |
1337 GDK_KEY_PRESS_MASK);
1338 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1340 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1341 gdk_window_set_user_data (widget->window, text);
1343 attributes.x = (widget->style->xthickness + TEXT_BORDER_ROOM);
1344 attributes.y = (widget->style->ythickness + TEXT_BORDER_ROOM);
1345 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1346 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1348 attributes.cursor = gdk_cursor_new (GDK_XTERM);
1349 attributes_mask |= GDK_WA_CURSOR;
1351 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1352 gdk_window_set_user_data (text->text_area, text);
1354 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1356 widget->style = gtk_style_attach (widget->style, widget->window);
1358 /* Can't call gtk_style_set_background here because it's handled specially */
1359 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1360 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1362 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1363 text->bg_gc = create_bg_gc (text);
1365 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1366 (gchar*) line_wrap_bits,
1370 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1371 (gchar*) line_arrow_bits,
1375 text->gc = gdk_gc_new (text->text_area);
1376 gdk_gc_set_exposures (text->gc, TRUE);
1377 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1379 realize_properties (text);
1380 gdk_window_show (text->text_area);
1381 init_properties (text);
1383 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1384 gtk_old_editable_claim_selection (old_editable, TRUE, GDK_CURRENT_TIME);
1386 recompute_geometry (text);
1390 gtk_text_style_set (GtkWidget *widget,
1391 GtkStyle *previous_style)
1393 GtkText *text = GTK_TEXT (widget);
1395 if (GTK_WIDGET_REALIZED (widget))
1397 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1398 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1402 gdk_gc_destroy (text->bg_gc);
1406 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1407 text->bg_gc = create_bg_gc (text);
1409 recompute_geometry (text);
1412 if (text->current_font)
1413 text_font_unref (text->current_font);
1414 text->current_font = get_text_font (gtk_style_get_font (widget->style));
1418 gtk_text_state_changed (GtkWidget *widget,
1419 GtkStateType previous_state)
1421 GtkText *text = GTK_TEXT (widget);
1423 if (GTK_WIDGET_REALIZED (widget))
1425 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1426 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1431 gtk_text_unrealize (GtkWidget *widget)
1435 g_return_if_fail (GTK_IS_TEXT (widget));
1437 text = GTK_TEXT (widget);
1439 gdk_window_set_user_data (text->text_area, NULL);
1440 gdk_window_destroy (text->text_area);
1441 text->text_area = NULL;
1443 gdk_gc_destroy (text->gc);
1448 gdk_gc_destroy (text->bg_gc);
1452 gdk_pixmap_unref (text->line_wrap_bitmap);
1453 gdk_pixmap_unref (text->line_arrow_bitmap);
1455 unrealize_properties (text);
1459 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1460 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1464 clear_focus_area (GtkText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1466 GtkWidget *widget = GTK_WIDGET (text);
1469 gint ythick = TEXT_BORDER_ROOM + widget->style->ythickness;
1470 gint xthick = TEXT_BORDER_ROOM + widget->style->xthickness;
1474 if (area_width == 0 || area_height == 0)
1477 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1479 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1481 gdk_gc_set_ts_origin (text->bg_gc,
1482 (- text->first_onscreen_hor_pixel + xthick) % width,
1483 (- text->first_onscreen_ver_pixel + ythick) % height);
1488 gc = widget->style->base_gc[widget->state];
1490 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1491 area_x, area_y, area_width, area_height);
1495 gtk_text_draw_focus (GtkWidget *widget)
1501 g_return_if_fail (GTK_IS_TEXT (widget));
1503 text = GTK_TEXT (widget);
1505 if (GTK_WIDGET_DRAWABLE (widget))
1507 gint ythick = widget->style->ythickness;
1508 gint xthick = widget->style->xthickness;
1509 gint xextra = TEXT_BORDER_ROOM;
1510 gint yextra = TEXT_BORDER_ROOM;
1512 TDEBUG (("in gtk_text_draw_focus\n"));
1516 width = widget->allocation.width;
1517 height = widget->allocation.height;
1519 if (GTK_WIDGET_HAS_FOCUS (widget))
1528 gtk_paint_focus (widget->style, widget->window,
1529 NULL, widget, "text",
1531 widget->allocation.width - 1,
1532 widget->allocation.height - 1);
1535 gtk_paint_shadow (widget->style, widget->window,
1536 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1537 NULL, widget, "text",
1538 x, y, width, height);
1542 width -= 2 * xthick;
1543 height -= 2 * ythick;
1546 clear_focus_area (text, x, y, width, yextra);
1548 clear_focus_area (text, x, y + yextra,
1549 xextra, y + height - 2 * yextra);
1551 clear_focus_area (text, x + width - xextra, y + yextra,
1552 xextra, height - 2 * ythick);
1554 clear_focus_area (text, x, x + height - yextra, width, yextra);
1558 TDEBUG (("in gtk_text_draw_focus (undrawable !!!)\n"));
1563 gtk_text_size_request (GtkWidget *widget,
1564 GtkRequisition *requisition)
1572 g_return_if_fail (GTK_IS_TEXT (widget));
1573 g_return_if_fail (requisition != NULL);
1575 xthickness = widget->style->xthickness + TEXT_BORDER_ROOM;
1576 ythickness = widget->style->ythickness + TEXT_BORDER_ROOM;
1578 font = gtk_style_get_font (widget->style);
1580 char_height = MIN_TEXT_HEIGHT_LINES * (font->ascent +
1583 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (font,
1584 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1588 requisition->width = char_width + xthickness * 2;
1589 requisition->height = char_height + ythickness * 2;
1593 gtk_text_size_allocate (GtkWidget *widget,
1594 GtkAllocation *allocation)
1597 GtkOldEditable *old_editable;
1599 g_return_if_fail (GTK_IS_TEXT (widget));
1600 g_return_if_fail (allocation != NULL);
1602 text = GTK_TEXT (widget);
1603 old_editable = GTK_OLD_EDITABLE (widget);
1605 widget->allocation = *allocation;
1606 if (GTK_WIDGET_REALIZED (widget))
1608 gdk_window_move_resize (widget->window,
1609 allocation->x, allocation->y,
1610 allocation->width, allocation->height);
1612 gdk_window_move_resize (text->text_area,
1613 widget->style->xthickness + TEXT_BORDER_ROOM,
1614 widget->style->ythickness + TEXT_BORDER_ROOM,
1615 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->xthickness +
1616 (gint)TEXT_BORDER_ROOM) * 2),
1617 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->ythickness +
1618 (gint)TEXT_BORDER_ROOM) * 2));
1620 recompute_geometry (text);
1625 gtk_text_expose (GtkWidget *widget,
1626 GdkEventExpose *event)
1628 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1629 g_return_val_if_fail (event != NULL, FALSE);
1631 if (event->window == GTK_TEXT (widget)->text_area)
1633 TDEBUG (("in gtk_text_expose (expose)\n"));
1634 expose_text (GTK_TEXT (widget), &event->area, TRUE);
1636 else if (event->count == 0)
1638 TDEBUG (("in gtk_text_expose (focus)\n"));
1639 gtk_text_draw_focus (widget);
1646 gtk_text_scroll_timeout (gpointer data)
1649 GdkEventMotion event;
1651 GdkModifierType mask;
1653 GDK_THREADS_ENTER ();
1655 text = GTK_TEXT (data);
1658 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1660 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1667 gtk_text_motion_notify (GTK_WIDGET (text), &event);
1670 GDK_THREADS_LEAVE ();
1676 gtk_text_button_press (GtkWidget *widget,
1677 GdkEventButton *event)
1680 GtkOldEditable *old_editable;
1682 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1683 g_return_val_if_fail (event != NULL, FALSE);
1685 text = GTK_TEXT (widget);
1686 old_editable = GTK_OLD_EDITABLE (widget);
1688 if (text->button && (event->button != text->button))
1691 text->button = event->button;
1693 if (!GTK_WIDGET_HAS_FOCUS (widget))
1694 gtk_widget_grab_focus (widget);
1696 if (event->button == 1)
1698 switch (event->type)
1700 case GDK_BUTTON_PRESS:
1701 gtk_grab_add (widget);
1703 undraw_cursor (text, FALSE);
1704 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1705 draw_cursor (text, FALSE);
1707 /* Set it now, so we display things right. We'll unset it
1708 * later if things don't work out */
1709 old_editable->has_selection = TRUE;
1710 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1711 text->cursor_mark.index,
1712 text->cursor_mark.index);
1716 case GDK_2BUTTON_PRESS:
1717 gtk_text_select_word (text, event->time);
1720 case GDK_3BUTTON_PRESS:
1721 gtk_text_select_line (text, event->time);
1728 else if (event->type == GDK_BUTTON_PRESS)
1730 if ((event->button == 2) && old_editable->editable)
1732 if (old_editable->selection_start_pos == old_editable->selection_end_pos ||
1733 old_editable->has_selection)
1735 undraw_cursor (text, FALSE);
1736 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1737 draw_cursor (text, FALSE);
1741 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1742 gdk_atom_intern ("UTF8_STRING", FALSE),
1747 gtk_grab_add (widget);
1749 undraw_cursor (text, FALSE);
1750 find_mouse_cursor (text, event->x, event->y);
1751 draw_cursor (text, FALSE);
1753 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1754 text->cursor_mark.index,
1755 text->cursor_mark.index);
1757 old_editable->has_selection = FALSE;
1758 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1759 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1767 gtk_text_button_release (GtkWidget *widget,
1768 GdkEventButton *event)
1771 GtkOldEditable *old_editable;
1772 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1773 g_return_val_if_fail (event != NULL, FALSE);
1775 text = GTK_TEXT (widget);
1777 gtk_grab_remove (widget);
1779 if (text->button != event->button)
1786 gtk_timeout_remove (text->timer);
1790 if (event->button == 1)
1792 text = GTK_TEXT (widget);
1793 old_editable = GTK_OLD_EDITABLE (widget);
1795 gtk_grab_remove (widget);
1797 old_editable->has_selection = FALSE;
1798 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1800 if (gtk_selection_owner_set (widget,
1801 GDK_SELECTION_PRIMARY,
1803 old_editable->has_selection = TRUE;
1805 gtk_text_update_text (old_editable, old_editable->selection_start_pos,
1806 old_editable->selection_end_pos);
1810 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1811 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1814 else if (event->button == 3)
1816 gtk_grab_remove (widget);
1819 undraw_cursor (text, FALSE);
1820 find_cursor (text, TRUE);
1821 draw_cursor (text, FALSE);
1827 gtk_text_motion_notify (GtkWidget *widget,
1828 GdkEventMotion *event)
1833 GdkModifierType mask;
1835 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1836 g_return_val_if_fail (event != NULL, FALSE);
1838 text = GTK_TEXT (widget);
1842 mask = event->state;
1843 if (event->is_hint || (text->text_area != event->window))
1845 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1848 if ((text->button == 0) ||
1849 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1852 gdk_window_get_size (text->text_area, NULL, &height);
1854 if ((y < 0) || (y > height))
1856 if (text->timer == 0)
1858 text->timer = gtk_timeout_add (SCROLL_TIME,
1859 gtk_text_scroll_timeout,
1863 scroll_int (text, y/2);
1865 scroll_int (text, (y - height)/2);
1871 undraw_cursor (GTK_TEXT (widget), FALSE);
1872 find_mouse_cursor (GTK_TEXT (widget), x, y);
1873 draw_cursor (GTK_TEXT (widget), FALSE);
1875 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1876 GTK_OLD_EDITABLE (text)->selection_start_pos,
1877 text->cursor_mark.index);
1883 gtk_text_insert_text (GtkEditable *editable,
1884 const gchar *new_text,
1885 gint new_text_length,
1888 GtkText *text = GTK_TEXT (editable);
1890 GdkColor *fore, *back;
1892 TextProperty *property;
1894 gtk_text_set_point (text, *position);
1896 property = MARK_CURRENT_PROPERTY (&text->point);
1897 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
1898 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
1899 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
1901 gtk_text_insert (text, font, fore, back, new_text, new_text_length);
1903 *position = text->point.index;
1907 gtk_text_delete_text (GtkEditable *editable,
1913 g_return_if_fail (start_pos >= 0);
1915 text = GTK_TEXT (editable);
1917 gtk_text_set_point (text, start_pos);
1919 end_pos = TEXT_LENGTH (text);
1921 if (end_pos > start_pos)
1922 gtk_text_forward_delete (text, end_pos - start_pos);
1926 gtk_text_key_press (GtkWidget *widget,
1930 GtkOldEditable *old_editable;
1935 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1936 g_return_val_if_fail (event != NULL, FALSE);
1938 text = GTK_TEXT (widget);
1939 old_editable = GTK_OLD_EDITABLE (widget);
1941 key = event->keyval;
1944 if ((GTK_OLD_EDITABLE(text)->editable == FALSE))
1946 switch (event->keyval)
1950 if (event->state & GDK_CONTROL_MASK)
1951 scroll_int (text, -text->vadj->value);
1957 if (event->state & GDK_CONTROL_MASK)
1958 scroll_int (text, +text->vadj->upper);
1962 case GDK_KP_Page_Up:
1963 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
1964 case GDK_KP_Page_Down:
1965 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
1967 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
1969 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
1972 if (event->state & GDK_CONTROL_MASK)
1973 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
1984 gint extend_selection;
1986 guint initial_pos = old_editable->current_pos;
1988 text->point = find_mark (text, text->cursor_mark.index);
1990 extend_selection = event->state & GDK_SHIFT_MASK;
1991 extend_start = FALSE;
1993 if (extend_selection)
1995 old_editable->has_selection = TRUE;
1997 if (old_editable->selection_start_pos == old_editable->selection_end_pos)
1999 old_editable->selection_start_pos = text->point.index;
2000 old_editable->selection_end_pos = text->point.index;
2003 extend_start = (text->point.index == old_editable->selection_start_pos);
2006 switch (event->keyval)
2010 if (event->state & GDK_CONTROL_MASK)
2011 move_cursor_buffer_ver (text, -1);
2013 gtk_text_move_beginning_of_line (text);
2017 if (event->state & GDK_CONTROL_MASK)
2018 move_cursor_buffer_ver (text, +1);
2020 gtk_text_move_end_of_line (text);
2022 case GDK_KP_Page_Up:
2023 case GDK_Page_Up: move_cursor_page_ver (text, -1); break;
2024 case GDK_KP_Page_Down:
2025 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
2026 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
2028 case GDK_Up: move_cursor_ver (text, -1); break;
2030 case GDK_Down: move_cursor_ver (text, +1); break;
2033 if (event->state & GDK_CONTROL_MASK)
2034 gtk_text_move_backward_word (text);
2036 move_cursor_hor (text, -1);
2040 if (event->state & GDK_CONTROL_MASK)
2041 gtk_text_move_forward_word (text);
2043 move_cursor_hor (text, +1);
2047 if (event->state & GDK_CONTROL_MASK)
2048 gtk_text_delete_backward_word (text);
2050 gtk_text_delete_backward_character (text);
2053 gtk_text_delete_line (text);
2057 if (event->state & GDK_SHIFT_MASK)
2059 extend_selection = FALSE;
2060 gtk_editable_paste_clipboard (GTK_EDITABLE (old_editable));
2062 else if (event->state & GDK_CONTROL_MASK)
2064 gtk_editable_copy_clipboard (GTK_EDITABLE (old_editable));
2068 /* gtk_toggle_insert(text) -- IMPLEMENT */
2073 if (event->state & GDK_CONTROL_MASK)
2074 gtk_text_delete_forward_word (text);
2075 else if (event->state & GDK_SHIFT_MASK)
2077 extend_selection = FALSE;
2078 gtk_editable_cut_clipboard (GTK_EDITABLE (old_editable));
2081 gtk_text_delete_forward_character (text);
2084 case GDK_ISO_Left_Tab:
2086 position = text->point.index;
2087 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\t", 1, &position);
2091 if (event->state & GDK_CONTROL_MASK)
2092 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2095 position = text->point.index;
2096 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\n", 1, &position);
2100 /* Don't insert literally */
2107 if (event->state & GDK_CONTROL_MASK)
2109 if ((key >= 'A') && (key <= 'Z'))
2112 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2114 (* control_keys[(int) (key - 'a')]) (old_editable, event->time);
2120 else if (event->state & GDK_MOD1_MASK)
2122 if ((key >= 'A') && (key <= 'Z'))
2125 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2127 (* alt_keys[(int) (key - 'a')]) (old_editable, event->time);
2133 else if (event->length > 0)
2135 extend_selection = FALSE;
2137 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
2138 position = text->point.index;
2139 gtk_editable_insert_text (GTK_EDITABLE (old_editable), event->string, event->length, &position);
2145 if (return_val && (old_editable->current_pos != initial_pos))
2147 if (extend_selection)
2149 if (old_editable->current_pos < old_editable->selection_start_pos)
2150 gtk_text_set_selection (old_editable, old_editable->current_pos,
2151 old_editable->selection_end_pos);
2152 else if (old_editable->current_pos > old_editable->selection_end_pos)
2153 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2154 old_editable->current_pos);
2158 gtk_text_set_selection (old_editable, old_editable->current_pos,
2159 old_editable->selection_end_pos);
2161 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2162 old_editable->current_pos);
2166 gtk_text_set_selection (old_editable, 0, 0);
2168 gtk_old_editable_claim_selection (old_editable,
2169 old_editable->selection_start_pos != old_editable->selection_end_pos,
2178 gtk_text_focus_in (GtkWidget *widget,
2179 GdkEventFocus *event)
2181 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
2182 g_return_val_if_fail (event != NULL, FALSE);
2184 TDEBUG (("in gtk_text_focus_in\n"));
2186 return (* GTK_WIDGET_CLASS (parent_class)->focus_in_event) (widget, event);
2190 gtk_text_focus_out (GtkWidget *widget,
2191 GdkEventFocus *event)
2193 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
2194 g_return_val_if_fail (event != NULL, FALSE);
2196 TDEBUG (("in gtk_text_focus_out\n"));
2198 return (* GTK_WIDGET_CLASS (parent_class)->focus_out_event) (widget, event);
2202 gtk_text_adjustment (GtkAdjustment *adjustment,
2205 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2206 g_return_if_fail (GTK_IS_TEXT (text));
2208 /* Just ignore it if we haven't been size-allocated and realized yet */
2209 if (text->line_start_cache == NULL)
2212 if (adjustment == text->hadj)
2214 g_warning ("horizontal scrolling not implemented");
2218 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2222 undraw_cursor (text, FALSE);
2225 scroll_down (text, diff);
2226 else /* if (diff < 0) */
2227 scroll_up (text, diff);
2229 draw_cursor (text, FALSE);
2231 text->last_ver_value = adjustment->value;
2237 gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
2240 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2241 g_return_if_fail (GTK_IS_TEXT (text));
2243 if (adjustment == text->hadj)
2244 gtk_text_set_adjustments (text, NULL, text->vadj);
2245 if (adjustment == text->vadj)
2246 gtk_text_set_adjustments (text, text->hadj, NULL);
2250 static GtkPropertyMark
2251 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2253 GtkPropertyMark mark;
2255 mark = find_mark_near (text, point_position, near);
2257 while (mark.index > 0 &&
2258 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2259 decrement_mark (&mark);
2265 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2267 tab_cont->pixel_offset = 0;
2268 tab_cont->tab_start.tab_stops = text->tab_stops;
2269 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2271 if (!tab_cont->tab_start.to_next_tab)
2272 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2276 line_params_iterate (GtkText* text,
2277 const GtkPropertyMark* mark0,
2278 const PrevTabCont* tab_mark0,
2281 LineIteratorFunction iter)
2282 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2283 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2284 * for each line following MARK, iteration continues unless ITER_CALL
2287 GtkPropertyMark mark = *mark0;
2288 PrevTabCont tab_conts[2];
2289 LineParams *lp, lpbuf;
2290 gint tab_cont_index = 0;
2293 tab_conts[0] = *tab_mark0;
2295 init_tab_cont (text, tab_conts);
2300 lp = g_chunk_new (LineParams, params_mem_chunk);
2304 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2305 tab_conts + (tab_cont_index + 1) % 2);
2307 if ((*iter) (text, lp, data))
2310 if (LAST_INDEX (text, lp->end))
2314 advance_mark (&mark);
2315 tab_cont_index = (tab_cont_index + 1) % 2;
2320 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2322 FetchLinesData *fldata = (FetchLinesData*) data;
2324 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2326 switch (fldata->fl_type)
2328 case FetchLinesCount:
2329 if (!text->line_wrap || !lp->wraps)
2332 if (fldata->data >= fldata->data_max)
2336 case FetchLinesPixels:
2338 fldata->data += LINE_HEIGHT(*lp);
2340 if (fldata->data >= fldata->data_max)
2350 fetch_lines (GtkText* text,
2351 const GtkPropertyMark* mark0,
2352 const PrevTabCont* tab_cont0,
2356 FetchLinesData fl_data;
2358 fl_data.new_lines = NULL;
2360 fl_data.data_max = data;
2361 fl_data.fl_type = fl_type;
2363 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2365 return g_list_reverse (fl_data.new_lines);
2369 fetch_lines_backward (GtkText* text)
2371 GList* new_lines = NULL, *new_line_start;
2372 GtkPropertyMark mark;
2374 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2377 mark = find_this_line_start_mark (text,
2378 CACHE_DATA(text->line_start_cache).start.index - 1,
2379 &CACHE_DATA(text->line_start_cache).start);
2381 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2383 while (new_line_start->next)
2384 new_line_start = new_line_start->next;
2386 new_line_start->next = text->line_start_cache;
2387 text->line_start_cache->prev = new_line_start;
2391 fetch_lines_forward (GtkText* text, gint line_count)
2393 GtkPropertyMark mark;
2394 GList* line = text->line_start_cache;
2399 mark = CACHE_DATA(line).end;
2401 if (LAST_INDEX (text, mark))
2404 advance_mark(&mark);
2406 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2409 line->next->prev = line;
2412 /* Compute the number of lines, and vertical pixels for n characters
2413 * starting from the point
2416 compute_lines_pixels (GtkText* text, guint char_count,
2417 guint *lines, guint *pixels)
2419 GList *line = text->current_line;
2420 gint chars_left = char_count;
2425 /* If chars_left == 0, that means we're joining two lines in a
2426 * deletion, so add in the values for the next line as well
2428 for (; line && chars_left >= 0; line = line->next)
2430 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2432 if (line == text->current_line)
2433 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2435 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2437 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2441 chars_left = 0; /* force another loop */
2444 fetch_lines_forward (text, 1);
2449 total_line_height (GtkText* text, GList* line, gint line_count)
2453 for (; line && line_count > 0; line = line->next)
2455 height += LINE_HEIGHT(CACHE_DATA(line));
2457 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2461 fetch_lines_forward (text, line_count);
2468 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2470 if (old == text->line_start_cache)
2474 for (; old_line_count > 0; old_line_count -= 1)
2476 while (text->line_start_cache &&
2478 CACHE_DATA(text->line_start_cache).wraps)
2479 remove_cache_line(text, text->line_start_cache);
2481 remove_cache_line(text, text->line_start_cache);
2484 last = g_list_last (new);
2486 last->next = text->line_start_cache;
2488 if (text->line_start_cache)
2489 text->line_start_cache->prev = last;
2491 text->line_start_cache = new;
2497 g_assert (old->prev);
2501 for (; old_line_count > 0; old_line_count -= 1)
2503 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2504 old = remove_cache_line (text, old);
2506 old = remove_cache_line (text, old);
2512 last = g_list_last (new);
2522 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2524 GList* cache = text->current_line;
2527 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2530 for (; cache; cache = cache->next)
2532 GtkPropertyMark *start = &CACHE_DATA(cache).start;
2533 GtkPropertyMark *end = &CACHE_DATA(cache).end;
2535 start->index -= nchars;
2536 end->index -= nchars;
2538 if (LAST_INDEX (text, text->point) &&
2539 start->index == text->point.index)
2540 *start = text->point;
2541 else if (start->property == text->point.property)
2542 start->offset = start->index - (text->point.index - text->point.offset);
2544 if (LAST_INDEX (text, text->point) &&
2545 end->index == text->point.index)
2547 if (end->property == text->point.property)
2548 end->offset = end->index - (text->point.index - text->point.offset);
2550 /*TEXT_ASSERT_MARK(text, start, "start");*/
2551 /*TEXT_ASSERT_MARK(text, end, "end");*/
2556 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2558 GtkWidget *widget = GTK_WIDGET (text);
2561 guint new_pixels = 0;
2563 GList* new_line = NULL;
2566 text->cursor_virtual_x = 0;
2568 correct_cache_delete (text, nchars, old_lines);
2570 pixel_height = pixel_height_of(text, text->current_line) -
2571 LINE_HEIGHT(CACHE_DATA(text->current_line));
2573 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2574 CACHE_DATA(text->current_line).start = text->point;
2576 new_line = fetch_lines (text,
2577 &CACHE_DATA(text->current_line).start,
2578 &CACHE_DATA(text->current_line).tab_cont,
2582 swap_lines (text, text->current_line, new_line, old_lines);
2584 text->current_line = new_line;
2586 new_pixels = total_line_height (text, new_line, 1);
2588 gdk_window_get_size (text->text_area, &width, &height);
2590 if (old_pixels != new_pixels)
2592 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2594 gdk_draw_pixmap (text->text_area,
2598 pixel_height + old_pixels,
2600 pixel_height + new_pixels,
2604 text->vadj->upper += new_pixels;
2605 text->vadj->upper -= old_pixels;
2606 adjust_adj (text, text->vadj);
2610 rect.y = pixel_height;
2612 rect.height = new_pixels;
2614 expose_text (text, &rect, FALSE);
2615 gtk_text_draw_focus ( (GtkWidget *) text);
2617 text->cursor_mark = text->point;
2619 find_cursor (text, TRUE);
2621 if (old_pixels != new_pixels)
2623 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2626 rect.y = pixel_height + new_pixels;
2628 rect.height = height - rect.y;
2630 expose_text (text, &rect, FALSE);
2633 process_exposes (text);
2640 /* note, the point has already been moved forward */
2642 correct_cache_insert (GtkText* text, gint nchars)
2645 GtkPropertyMark *start;
2646 GtkPropertyMark *end;
2647 gboolean was_split = FALSE;
2649 /* We need to distinguish whether the property was split in the
2650 * insert or not, so we check if the point (which points after
2651 * the insertion here), points to the same character as the
2652 * point before. Ugh.
2656 GtkPropertyMark tmp_mark = text->point;
2657 move_mark_n (&tmp_mark, -1);
2659 if (tmp_mark.property != text->point.property)
2663 /* If we inserted a property exactly at the beginning of the
2664 * line, we have to correct here, or fetch_lines will
2667 start = &CACHE_DATA(text->current_line).start;
2669 /* Check if if we split exactly at the beginning of the line:
2670 * (was_split won't be set if we are inserting at the end of the text,
2671 * so we don't check)
2673 if (start->offset == MARK_CURRENT_PROPERTY (start)->length)
2674 SET_PROPERTY_MARK (start, start->property->next, 0);
2675 /* Check if we inserted a property at the beginning of the text: */
2676 else if (was_split &&
2677 (start->property == text->point.property) &&
2678 (start->index == text->point.index - nchars))
2679 SET_PROPERTY_MARK (start, start->property->prev, 0);
2681 /* Now correct the offsets, and check for start or end marks that
2682 * are after the point, yet point to a property before the point's
2683 * property. This indicates that they are meant to point to the
2684 * second half of a property we split in insert_text_property(), so
2685 * we fix them up that way.
2687 cache = text->current_line->next;
2689 for (; cache; cache = cache->next)
2691 start = &CACHE_DATA(cache).start;
2692 end = &CACHE_DATA(cache).end;
2694 if (LAST_INDEX (text, text->point) &&
2695 start->index == text->point.index)
2696 *start = text->point;
2697 else if (start->index >= text->point.index - nchars)
2699 if (!was_split && start->property == text->point.property)
2700 move_mark_n(start, nchars);
2703 if (start->property->next &&
2704 (start->property->next->next == text->point.property))
2706 g_assert (start->offset >= MARK_CURRENT_PROPERTY (start)->length);
2707 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2708 start->property = text->point.property;
2710 start->index += nchars;
2714 if (LAST_INDEX (text, text->point) &&
2715 end->index == text->point.index)
2717 if (end->index >= text->point.index - nchars)
2719 if (!was_split && end->property == text->point.property)
2720 move_mark_n(end, nchars);
2723 if (end->property->next &&
2724 (end->property->next->next == text->point.property))
2726 g_assert (end->offset >= MARK_CURRENT_PROPERTY (end)->length);
2727 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2728 end->property = text->point.property;
2730 end->index += nchars;
2734 /*TEXT_ASSERT_MARK(text, start, "start");*/
2735 /*TEXT_ASSERT_MARK(text, end, "end");*/
2741 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2742 guint new_line_count)
2744 GtkWidget *widget = GTK_WIDGET (text);
2747 guint new_pixels = 0;
2749 GList* new_lines = NULL;
2752 text->cursor_virtual_x = 0;
2754 undraw_cursor (text, FALSE);
2756 correct_cache_insert (text, nchars);
2758 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2760 pixel_height = pixel_height_of(text, text->current_line) -
2761 LINE_HEIGHT(CACHE_DATA(text->current_line));
2763 new_lines = fetch_lines (text,
2764 &CACHE_DATA(text->current_line).start,
2765 &CACHE_DATA(text->current_line).tab_cont,
2769 swap_lines (text, text->current_line, new_lines, 1);
2771 text->current_line = new_lines;
2773 new_pixels = total_line_height (text, new_lines, new_line_count);
2775 gdk_window_get_size (text->text_area, &width, &height);
2777 if (old_pixels != new_pixels)
2779 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2781 gdk_draw_pixmap (text->text_area,
2785 pixel_height + old_pixels,
2787 pixel_height + new_pixels,
2789 height + (old_pixels - new_pixels) - pixel_height);
2792 text->vadj->upper += new_pixels;
2793 text->vadj->upper -= old_pixels;
2794 adjust_adj (text, text->vadj);
2798 rect.y = pixel_height;
2800 rect.height = new_pixels;
2802 expose_text (text, &rect, FALSE);
2803 gtk_text_draw_focus ( (GtkWidget *) text);
2805 text->cursor_mark = text->point;
2807 find_cursor (text, TRUE);
2809 draw_cursor (text, FALSE);
2811 if (old_pixels != new_pixels)
2813 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2816 rect.y = pixel_height + new_pixels;
2818 rect.height = height - rect.y;
2820 expose_text (text, &rect, FALSE);
2823 process_exposes (text);
2826 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2831 /* Text property functions */
2834 font_hash (gconstpointer font)
2836 return gdk_font_id ((const GdkFont*) font);
2839 static GHashTable *font_cache_table = NULL;
2842 get_text_font (GdkFont* gfont)
2847 if (!font_cache_table)
2848 font_cache_table = g_hash_table_new (font_hash, (GEqualFunc) gdk_font_equal);
2850 tf = g_hash_table_lookup (font_cache_table, gfont);
2858 tf = g_new (GtkTextFont, 1);
2861 tf->gdk_font = gfont;
2862 gdk_font_ref (gfont);
2864 for(i = 0; i < 256; i += 1)
2865 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2867 g_hash_table_insert (font_cache_table, gfont, tf);
2873 text_font_unref (GtkTextFont *text_font)
2875 text_font->ref_count--;
2876 if (text_font->ref_count == 0)
2878 g_hash_table_remove (font_cache_table, text_font->gdk_font);
2879 gdk_font_unref (text_font->gdk_font);
2885 text_properties_equal (TextProperty* prop, GdkFont* font, GdkColor *fore, GdkColor *back)
2887 if (prop->flags & PROPERTY_FONT)
2890 GtkTextFont *text_font;
2895 text_font = get_text_font (font);
2897 retval = (prop->font == text_font);
2898 text_font_unref (text_font);
2907 if (prop->flags & PROPERTY_FOREGROUND)
2909 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
2916 if (prop->flags & PROPERTY_BACKGROUND)
2918 if (!back || !gdk_color_equal (&prop->back_color, back))
2929 realize_property (GtkText *text, TextProperty *prop)
2931 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2933 if (prop->flags & PROPERTY_FOREGROUND)
2934 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
2936 if (prop->flags & PROPERTY_BACKGROUND)
2937 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
2941 realize_properties (GtkText *text)
2943 GList *tmp_list = text->text_properties;
2947 realize_property (text, tmp_list->data);
2949 tmp_list = tmp_list->next;
2954 unrealize_property (GtkText *text, TextProperty *prop)
2956 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2958 if (prop->flags & PROPERTY_FOREGROUND)
2959 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
2961 if (prop->flags & PROPERTY_BACKGROUND)
2962 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
2966 unrealize_properties (GtkText *text)
2968 GList *tmp_list = text->text_properties;
2972 unrealize_property (text, tmp_list->data);
2974 tmp_list = tmp_list->next;
2978 static TextProperty*
2979 new_text_property (GtkText *text, GdkFont *font, GdkColor* fore,
2980 GdkColor* back, guint length)
2984 if (text_property_chunk == NULL)
2986 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
2987 sizeof(TextProperty),
2988 1024*sizeof(TextProperty),
2992 prop = g_chunk_new(TextProperty, text_property_chunk);
2997 prop->flags |= PROPERTY_FONT;
2998 prop->font = get_text_font (font);
3005 prop->flags |= PROPERTY_FOREGROUND;
3006 prop->fore_color = *fore;
3011 prop->flags |= PROPERTY_BACKGROUND;
3012 prop->back_color = *back;
3015 prop->length = length;
3017 if (GTK_WIDGET_REALIZED (text))
3018 realize_property (text, prop);
3024 destroy_text_property (TextProperty *prop)
3027 text_font_unref (prop->font);
3029 g_mem_chunk_free (text_property_chunk, prop);
3032 /* Flop the memory between the point and the gap around like a
3035 move_gap (GtkText* text, guint index)
3037 if (text->gap_position < index)
3039 gint diff = index - text->gap_position;
3041 if (text->use_wchar)
3042 g_memmove (text->text.wc + text->gap_position,
3043 text->text.wc + text->gap_position + text->gap_size,
3044 diff*sizeof (GdkWChar));
3046 g_memmove (text->text.ch + text->gap_position,
3047 text->text.ch + text->gap_position + text->gap_size,
3050 text->gap_position = index;
3052 else if (text->gap_position > index)
3054 gint diff = text->gap_position - index;
3056 if (text->use_wchar)
3057 g_memmove (text->text.wc + index + text->gap_size,
3058 text->text.wc + index,
3059 diff*sizeof (GdkWChar));
3061 g_memmove (text->text.ch + index + text->gap_size,
3062 text->text.ch + index,
3065 text->gap_position = index;
3069 /* Increase the gap size. */
3071 make_forward_space (GtkText* text, guint len)
3073 if (text->gap_size < len)
3075 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3077 if (sum >= text->text_len)
3081 while (i <= sum) i <<= 1;
3083 if (text->use_wchar)
3084 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3085 i*sizeof(GdkWChar));
3087 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3091 if (text->use_wchar)
3092 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3093 text->text.wc + text->gap_position + text->gap_size,
3094 (text->text_end - (text->gap_position + text->gap_size))
3097 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3098 text->text.ch + text->gap_position + text->gap_size,
3099 text->text_end - (text->gap_position + text->gap_size));
3101 text->text_end += len*2;
3102 text->gap_size += len*2;
3106 /* Inserts into the text property list a list element that guarantees
3107 * that for len characters following the point, text has the correct
3108 * property. does not move point. adjusts text_properties_point and
3109 * text_properties_point_offset relative to the current value of
3112 insert_text_property (GtkText* text, GdkFont* font,
3113 GdkColor *fore, GdkColor* back, guint len)
3115 GtkPropertyMark *mark = &text->point;
3116 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3117 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3119 if (MARK_OFFSET(mark) == 0)
3121 /* Point is on the boundary of two properties.
3122 * If it is the same as either, grow, else insert
3125 if (text_properties_equal(forward_prop, font, fore, back))
3127 /* Grow the property in front of us. */
3129 MARK_PROPERTY_LENGTH(mark) += len;
3131 else if (backward_prop &&
3132 text_properties_equal(backward_prop, font, fore, back))
3134 /* Grow property behind us, point property and offset
3137 SET_PROPERTY_MARK (&text->point,
3138 MARK_PREV_LIST_PTR (mark),
3139 backward_prop->length);
3141 backward_prop->length += len;
3143 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3144 (forward_prop->length == 1))
3146 /* Next property just has last position, take it over */
3148 if (GTK_WIDGET_REALIZED (text))
3149 unrealize_property (text, forward_prop);
3151 forward_prop->flags = 0;
3154 forward_prop->flags |= PROPERTY_FONT;
3155 forward_prop->font = get_text_font (font);
3158 forward_prop->font = NULL;
3162 forward_prop->flags |= PROPERTY_FOREGROUND;
3163 forward_prop->fore_color = *fore;
3167 forward_prop->flags |= PROPERTY_BACKGROUND;
3168 forward_prop->back_color = *back;
3170 forward_prop->length += len;
3172 if (GTK_WIDGET_REALIZED (text))
3173 realize_property (text, forward_prop);
3177 /* Splice a new property into the list. */
3179 GList* new_prop = g_list_alloc();
3181 new_prop->next = MARK_LIST_PTR(mark);
3182 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3183 new_prop->next->prev = new_prop;
3186 new_prop->prev->next = new_prop;
3188 new_prop->data = new_text_property (text, font, fore, back, len);
3190 SET_PROPERTY_MARK (mark, new_prop, 0);
3195 /* The following will screw up the line_start cache,
3196 * we'll fix it up in correct_cache_insert
3199 /* In the middle of forward_prop, if properties are equal,
3200 * just add to its length, else split it into two and splice
3202 if (text_properties_equal (forward_prop, font, fore, back))
3204 forward_prop->length += len;
3206 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3207 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3209 /* Inserting before only the last position in the text */
3212 forward_prop->length -= 1;
3214 new_prop = g_list_alloc();
3215 new_prop->data = new_text_property (text, font, fore, back, len+1);
3216 new_prop->prev = MARK_LIST_PTR(mark);
3217 new_prop->next = NULL;
3218 MARK_NEXT_LIST_PTR(mark) = new_prop;
3220 SET_PROPERTY_MARK (mark, new_prop, 0);
3224 GList* new_prop = g_list_alloc();
3225 GList* new_prop_forward = g_list_alloc();
3226 gint old_length = forward_prop->length;
3227 GList* next = MARK_NEXT_LIST_PTR(mark);
3229 /* Set the new lengths according to where they are split. Construct
3230 * two new properties. */
3231 forward_prop->length = MARK_OFFSET(mark);
3233 new_prop_forward->data =
3234 new_text_property(text,
3235 forward_prop->flags & PROPERTY_FONT ?
3236 forward_prop->font->gdk_font : NULL,
3237 forward_prop->flags & PROPERTY_FOREGROUND ?
3238 &forward_prop->fore_color : NULL,
3239 forward_prop->flags & PROPERTY_BACKGROUND ?
3240 &forward_prop->back_color : NULL,
3241 old_length - forward_prop->length);
3243 new_prop->data = new_text_property(text, font, fore, back, len);
3245 /* Now splice things in. */
3246 MARK_NEXT_LIST_PTR(mark) = new_prop;
3247 new_prop->prev = MARK_LIST_PTR(mark);
3249 new_prop->next = new_prop_forward;
3250 new_prop_forward->prev = new_prop;
3252 new_prop_forward->next = next;
3255 next->prev = new_prop_forward;
3257 SET_PROPERTY_MARK (mark, new_prop, 0);
3261 while (text->text_properties_end->next)
3262 text->text_properties_end = text->text_properties_end->next;
3264 while (text->text_properties->prev)
3265 text->text_properties = text->text_properties->prev;
3269 delete_text_property (GtkText* text, guint nchars)
3271 /* Delete nchars forward from point. */
3273 /* Deleting text properties is problematical, because we
3274 * might be storing around marks pointing to a property.
3276 * The marks in question and how we handle them are:
3278 * point: We know the new value, since it will be at the
3279 * end of the deleted text, and we move it there
3281 * cursor: We just remove the mark and set it equal to the
3282 * point after the operation.
3283 * line-start cache: We replace most affected lines.
3284 * The current line gets used to fetch the new
3285 * lines so, if necessary, (delete at the beginning
3286 * of a line) we fix it up by setting it equal to the
3294 for(; nchars; nchars -= 1)
3296 prop = MARK_CURRENT_PROPERTY(&text->point);
3300 if (prop->length == 0)
3302 tmp = MARK_LIST_PTR (&text->point);
3304 is_first = tmp == text->text_properties;
3306 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3307 text->point.offset = 0;
3309 if (GTK_WIDGET_REALIZED (text))
3310 unrealize_property (text, prop);
3312 destroy_text_property (prop);
3313 g_list_free_1 (tmp);
3315 prop = MARK_CURRENT_PROPERTY (&text->point);
3318 text->text_properties = MARK_LIST_PTR (&text->point);
3320 g_assert (prop->length != 0);
3322 else if (prop->length == text->point.offset)
3324 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3325 text->point.offset = 0;
3329 /* Check to see if we have just the single final position remaining
3330 * along in a property; if so, combine it with the previous property
3332 if (LAST_INDEX (text, text->point) &&
3333 (MARK_OFFSET (&text->point) == 0) &&
3334 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3336 tmp = MARK_LIST_PTR (&text->point);
3337 prop = MARK_CURRENT_PROPERTY(&text->point);
3339 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3340 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3341 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3343 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3345 if (GTK_WIDGET_REALIZED (text))
3346 unrealize_property (text, prop);
3348 destroy_text_property (prop);
3349 g_list_free_1 (tmp);
3354 init_properties (GtkText *text)
3356 if (!text->text_properties)
3358 text->text_properties = g_list_alloc();
3359 text->text_properties->next = NULL;
3360 text->text_properties->prev = NULL;
3361 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3362 text->text_properties_end = text->text_properties;
3364 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3366 text->point.index = 0;
3371 /**********************************************************************/
3372 /* Property Movement */
3373 /**********************************************************************/
3376 move_mark_n (GtkPropertyMark* mark, gint n)
3379 advance_mark_n(mark, n);
3381 decrement_mark_n(mark, -n);
3385 advance_mark (GtkPropertyMark* mark)
3387 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3391 if (prop->length > mark->offset + 1)
3395 mark->property = MARK_NEXT_LIST_PTR (mark);
3401 advance_mark_n (GtkPropertyMark* mark, gint n)
3408 i = 0; /* otherwise it migth not be init. */
3409 prop = MARK_CURRENT_PROPERTY(mark);
3411 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3412 /* to make it easier */
3413 n += (mark->offset);
3414 mark->index -= mark->offset;
3416 /* first we take seven-mile-leaps to get to the right text
3418 while ((n-i) > prop->length - 1) {
3420 mark->index += prop->length;
3421 mark->property = MARK_NEXT_LIST_PTR (mark);
3422 prop = MARK_CURRENT_PROPERTY (mark);
3426 /* and then the rest */
3427 mark->index += n - i;
3428 mark->offset += n - i;
3432 decrement_mark (GtkPropertyMark* mark)
3436 if (mark->offset > 0)
3440 mark->property = MARK_PREV_LIST_PTR (mark);
3441 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3446 decrement_mark_n (GtkPropertyMark* mark, gint n)
3450 while (mark->offset < n) {
3451 /* jump to end of prev */
3452 n -= mark->offset + 1;
3453 mark->index -= mark->offset + 1;
3454 mark->property = MARK_PREV_LIST_PTR (mark);
3455 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3463 static GtkPropertyMark
3464 find_mark (GtkText* text, guint mark_position)
3466 return find_mark_near (text, mark_position, &text->point);
3470 * You can also start from the end, what a drag.
3472 static GtkPropertyMark
3473 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3478 GtkPropertyMark mark;
3481 diffa = mark_position + 1;
3483 diffa = mark_position - near->index;
3485 diffb = mark_position;
3497 mark.property = text->text_properties;
3501 move_mark_n (&mark, mark_position - mark.index);
3506 /* This routine must be called with scroll == FALSE, only when
3507 * point is at least partially on screen
3511 find_line_containing_point (GtkText* text, guint point,
3517 text->current_line = NULL;
3521 /* Scroll backwards until the point is on screen
3523 while (CACHE_DATA(text->line_start_cache).start.index > point)
3524 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3526 /* Now additionally try to make sure that the point is fully on screen
3530 while (text->first_cut_pixels != 0 &&
3531 text->line_start_cache->next &&
3532 CACHE_DATA(text->line_start_cache->next).start.index > point)
3533 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3536 gdk_window_get_size (text->text_area, NULL, &height);
3538 for (cache = text->line_start_cache; cache; cache = cache->next)
3542 if (CACHE_DATA(cache).end.index >= point ||
3543 LAST_INDEX(text, CACHE_DATA(cache).end))
3545 text->current_line = cache; /* LOOK HERE, this proc has an
3546 * important side effect. */
3550 TEXT_SHOW_LINE (text, cache, "cache");
3552 if (cache->next == NULL)
3553 fetch_lines_forward (text, 1);
3557 lph = pixel_height_of (text, cache->next);
3559 /* Scroll the bottom of the line is on screen, or until
3560 * the line is the first onscreen line.
3562 while (cache->next != text->line_start_cache && lph > height)
3564 TEXT_SHOW_LINE (text, cache, "cache");
3565 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3566 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3567 lph = pixel_height_of (text, cache->next);
3572 g_assert_not_reached (); /* Must set text->current_line here */
3576 pixel_height_of (GtkText* text, GList* cache_line)
3578 gint pixels = - text->first_cut_pixels;
3579 GList *cache = text->line_start_cache;
3582 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3584 if (cache->data == cache_line->data)
3587 cache = cache->next;
3593 /**********************************************************************/
3594 /* Search and Placement */
3595 /**********************************************************************/
3598 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3601 gint16* char_widths;
3603 if (LAST_INDEX (text, *mark))
3606 ch = GTK_TEXT_INDEX (text, mark->index);
3607 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3611 return tab_mark->to_next_tab * char_widths[' '];
3615 return char_widths[ch];
3619 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3624 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, GdkWChar ch)
3626 if (tab_mark->to_next_tab == 1 || ch == '\t')
3628 if (tab_mark->tab_stops->next)
3630 tab_mark->tab_stops = tab_mark->tab_stops->next;
3631 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3635 tab_mark->to_next_tab = text->default_tab_width;
3640 tab_mark->to_next_tab -= 1;
3645 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3649 advance_tab_mark (text, tab_mark, 0);
3653 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3657 GtkPropertyMark mark = start_line->start;
3658 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3659 gint pixel_width = LINE_START_PIXEL (*start_line);
3661 while (mark.index < text->cursor_mark.index)
3663 pixel_width += find_char_width (text, &mark, &tab_mark);
3665 advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3666 advance_mark (&mark);
3669 text->cursor_pos_x = pixel_width;
3670 text->cursor_pos_y = pixel_height;
3671 text->cursor_char_offset = start_line->font_descent;
3672 text->cursor_mark = mark;
3674 ch = LAST_INDEX (text, mark) ?
3675 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3677 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3678 text->cursor_char = 0;
3680 text->cursor_char = ch;
3684 find_cursor (GtkText* text, gboolean scroll)
3686 if (GTK_WIDGET_REALIZED (text))
3688 find_line_containing_point (text, text->cursor_mark.index, scroll);
3690 if (text->current_line)
3691 find_cursor_at_line (text,
3692 &CACHE_DATA(text->current_line),
3693 pixel_height_of(text, text->current_line));
3696 GTK_OLD_EDITABLE (text)->current_pos = text->cursor_mark.index;
3700 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3701 guint line_pixel_height,
3704 GtkPropertyMark mark = lp->start;
3705 TabStopMark tab_mark = lp->tab_cont.tab_start;
3707 gint char_width = find_char_width(text, &mark, &tab_mark);
3708 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3710 text->cursor_pos_y = line_pixel_height;
3714 GdkWChar ch = LAST_INDEX (text, mark) ?
3715 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3717 if (button_x < pixel_width || mark.index == lp->end.index)
3719 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3720 text->cursor_mark = mark;
3721 text->cursor_char_offset = lp->font_descent;
3723 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3724 text->cursor_char = 0;
3726 text->cursor_char = ch;
3731 advance_tab_mark (text, &tab_mark, ch);
3732 advance_mark (&mark);
3734 pixel_width += char_width/2;
3736 char_width = find_char_width (text, &mark, &tab_mark);
3738 pixel_width += (char_width+1)/2;
3743 find_mouse_cursor (GtkText* text, gint x, gint y)
3746 GList* cache = text->line_start_cache;
3750 pixel_height = - text->first_cut_pixels;
3752 for (; cache; cache = cache->next)
3754 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3756 if (y < pixel_height || !cache->next)
3758 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3760 find_cursor (text, FALSE);
3767 /**********************************************************************/
3769 /**********************************************************************/
3772 free_cache (GtkText* text)
3774 GList* cache = text->line_start_cache;
3779 cache = cache->prev;
3781 text->line_start_cache = cache;
3784 for (; cache; cache = cache->next)
3785 g_mem_chunk_free (params_mem_chunk, cache->data);
3787 g_list_free (text->line_start_cache);
3789 text->line_start_cache = NULL;
3793 remove_cache_line (GtkText* text, GList* member)
3800 if (member == text->line_start_cache)
3801 text->line_start_cache = text->line_start_cache->next;
3804 member->prev->next = member->next;
3807 member->next->prev = member->prev;
3809 list = member->next;
3811 g_mem_chunk_free (params_mem_chunk, member->data);
3812 g_list_free_1 (member);
3817 /**********************************************************************/
3819 /**********************************************************************/
3822 move_cursor_buffer_ver (GtkText *text, int dir)
3824 undraw_cursor (text, FALSE);
3828 scroll_int (text, text->vadj->upper);
3829 text->cursor_mark = find_this_line_start_mark (text,
3831 &text->cursor_mark);
3835 scroll_int (text, - text->vadj->value);
3836 text->cursor_mark = find_this_line_start_mark (text,
3838 &text->cursor_mark);
3841 find_cursor (text, TRUE);
3842 draw_cursor (text, FALSE);
3846 move_cursor_page_ver (GtkText *text, int dir)
3848 scroll_int (text, dir * text->vadj->page_increment);
3852 move_cursor_ver (GtkText *text, int count)
3855 GtkPropertyMark mark;
3858 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3859 offset = text->cursor_mark.index - mark.index;
3861 if (offset > text->cursor_virtual_x)
3862 text->cursor_virtual_x = offset;
3866 if (mark.index == 0)
3869 decrement_mark (&mark);
3870 mark = find_this_line_start_mark (text, mark.index, &mark);
3874 mark = text->cursor_mark;
3876 while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3877 advance_mark (&mark);
3879 if (LAST_INDEX(text, mark))
3882 advance_mark (&mark);
3885 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3886 if (LAST_INDEX(text, mark) ||
3887 GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3890 undraw_cursor (text, FALSE);
3892 text->cursor_mark = mark;
3894 find_cursor (text, TRUE);
3896 draw_cursor (text, FALSE);
3900 move_cursor_hor (GtkText *text, int count)
3902 /* count should be +-1. */
3903 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3904 (count < 0 && text->cursor_mark.index < (- count)) ||
3908 text->cursor_virtual_x = 0;
3910 undraw_cursor (text, FALSE);
3912 move_mark_n (&text->cursor_mark, count);
3914 find_cursor (text, TRUE);
3916 draw_cursor (text, FALSE);
3920 gtk_text_move_cursor (GtkOldEditable *old_editable,
3927 move_cursor_hor (GTK_TEXT (old_editable), 1);
3932 move_cursor_hor (GTK_TEXT (old_editable), -1);
3938 move_cursor_ver (GTK_TEXT (old_editable), 1);
3943 move_cursor_ver (GTK_TEXT (old_editable), -1);
3948 gtk_text_move_forward_character (GtkText *text)
3950 move_cursor_hor (text, 1);
3954 gtk_text_move_backward_character (GtkText *text)
3956 move_cursor_hor (text, -1);
3960 gtk_text_move_next_line (GtkText *text)
3962 move_cursor_ver (text, 1);
3966 gtk_text_move_previous_line (GtkText *text)
3968 move_cursor_ver (text, -1);
3972 gtk_text_move_word (GtkOldEditable *old_editable,
3978 gtk_text_move_forward_word (GTK_TEXT (old_editable));
3983 gtk_text_move_backward_word (GTK_TEXT (old_editable));
3988 gtk_text_move_forward_word (GtkText *text)
3990 text->cursor_virtual_x = 0;
3992 undraw_cursor (text, FALSE);
3994 if (text->use_wchar)
3996 while (!LAST_INDEX (text, text->cursor_mark) &&
3997 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3998 advance_mark (&text->cursor_mark);
4000 while (!LAST_INDEX (text, text->cursor_mark) &&
4001 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4002 advance_mark (&text->cursor_mark);
4006 while (!LAST_INDEX (text, text->cursor_mark) &&
4007 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4008 advance_mark (&text->cursor_mark);
4010 while (!LAST_INDEX (text, text->cursor_mark) &&
4011 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4012 advance_mark (&text->cursor_mark);
4015 find_cursor (text, TRUE);
4016 draw_cursor (text, FALSE);
4020 gtk_text_move_backward_word (GtkText *text)
4022 text->cursor_virtual_x = 0;
4024 undraw_cursor (text, FALSE);
4026 if (text->use_wchar)
4028 while ((text->cursor_mark.index > 0) &&
4029 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4030 decrement_mark (&text->cursor_mark);
4032 while ((text->cursor_mark.index > 0) &&
4033 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4034 decrement_mark (&text->cursor_mark);
4038 while ((text->cursor_mark.index > 0) &&
4039 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4040 decrement_mark (&text->cursor_mark);
4042 while ((text->cursor_mark.index > 0) &&
4043 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4044 decrement_mark (&text->cursor_mark);
4047 find_cursor (text, TRUE);
4048 draw_cursor (text, FALSE);
4052 gtk_text_move_page (GtkOldEditable *old_editable,
4057 scroll_int (GTK_TEXT (old_editable),
4058 y * GTK_TEXT(old_editable)->vadj->page_increment);
4062 gtk_text_move_to_row (GtkOldEditable *old_editable,
4068 gtk_text_move_to_column (GtkOldEditable *old_editable,
4073 text = GTK_TEXT (old_editable);
4075 text->cursor_virtual_x = 0; /* FIXME */
4077 undraw_cursor (text, FALSE);
4079 /* Move to the beginning of the line */
4080 while ((text->cursor_mark.index > 0) &&
4081 (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4082 decrement_mark (&text->cursor_mark);
4084 while (!LAST_INDEX (text, text->cursor_mark) &&
4085 (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4089 else if (column == 0)
4092 advance_mark (&text->cursor_mark);
4095 find_cursor (text, TRUE);
4096 draw_cursor (text, FALSE);
4100 gtk_text_move_beginning_of_line (GtkText *text)
4102 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4107 gtk_text_move_end_of_line (GtkText *text)
4109 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), -1);
4113 gtk_text_kill_char (GtkOldEditable *old_editable,
4118 text = GTK_TEXT (old_editable);
4120 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4121 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4126 if (text->point.index + 1 <= TEXT_LENGTH (text))
4127 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index, text->point.index + 1);
4131 if (text->point.index > 0)
4132 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index - 1, text->point.index);
4138 gtk_text_delete_forward_character (GtkText *text)
4140 gtk_text_kill_char (GTK_OLD_EDITABLE (text), 1);
4144 gtk_text_delete_backward_character (GtkText *text)
4146 gtk_text_kill_char (GTK_OLD_EDITABLE (text), -1);
4150 gtk_text_kill_word (GtkOldEditable *old_editable,
4153 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4154 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4157 gint old_pos = old_editable->current_pos;
4160 gtk_text_move_word (old_editable, 1);
4161 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4165 gtk_text_move_word (old_editable, -1);
4166 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4172 gtk_text_delete_forward_word (GtkText *text)
4174 gtk_text_kill_word (GTK_OLD_EDITABLE (text), 1);
4178 gtk_text_delete_backward_word (GtkText *text)
4180 gtk_text_kill_word (GTK_OLD_EDITABLE (text), -1);
4184 gtk_text_kill_line (GtkOldEditable *old_editable,
4187 gint old_pos = old_editable->current_pos;
4190 gtk_text_move_to_column (old_editable, -1);
4191 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4195 gtk_text_move_to_column (old_editable, 0);
4196 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4201 gtk_text_delete_line (GtkText *text)
4203 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4204 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4208 gtk_text_delete_to_line_end (GtkText *text)
4210 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4214 gtk_text_select_word (GtkText *text, guint32 time)
4219 GtkOldEditable *old_editable;
4220 old_editable = GTK_OLD_EDITABLE (text);
4222 gtk_text_move_backward_word (text);
4223 start_pos = text->cursor_mark.index;
4225 gtk_text_move_forward_word (text);
4226 end_pos = text->cursor_mark.index;
4228 old_editable->has_selection = TRUE;
4229 gtk_text_set_selection (old_editable, start_pos, end_pos);
4230 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4234 gtk_text_select_line (GtkText *text, guint32 time)
4239 GtkOldEditable *old_editable;
4240 old_editable = GTK_OLD_EDITABLE (text);
4242 gtk_text_move_beginning_of_line (text);
4243 start_pos = text->cursor_mark.index;
4245 gtk_text_move_end_of_line (text);
4246 gtk_text_move_forward_character (text);
4247 end_pos = text->cursor_mark.index;
4249 old_editable->has_selection = TRUE;
4250 gtk_text_set_selection (old_editable, start_pos, end_pos);
4251 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4254 /**********************************************************************/
4256 /**********************************************************************/
4259 adjust_adj (GtkText* text, GtkAdjustment* adj)
4263 gdk_window_get_size (text->text_area, NULL, &height);
4265 adj->step_increment = MIN (adj->upper, SCROLL_PIXELS);
4266 adj->page_increment = MIN (adj->upper, height - KEY_SCROLL_PIXELS);
4267 adj->page_size = MIN (adj->upper, height);
4268 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4269 adj->value = MAX (adj->value, 0.0);
4271 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4275 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
4277 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4279 if ((text->first_line_start_index >= lp->start.index) &&
4280 (text->first_line_start_index <= lp->end.index))
4282 svdata->mark = lp->start;
4284 if (text->first_line_start_index == lp->start.index)
4286 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4290 text->first_onscreen_ver_pixel = svdata->pixel_height;
4291 text->first_cut_pixels = 0;
4294 text->vadj->value = text->first_onscreen_ver_pixel;
4297 svdata->pixel_height += LINE_HEIGHT (*lp);
4303 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
4305 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4308 if (svdata->pixel_height <= (gint) text->vadj->value &&
4309 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4311 svdata->mark = lp->start;
4313 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4314 text->first_onscreen_ver_pixel = svdata->pixel_height;
4315 text->first_line_start_index = lp->start.index;
4321 svdata->pixel_height += LINE_HEIGHT (*lp);
4329 static GtkPropertyMark
4330 set_vertical_scroll (GtkText* text)
4332 GtkPropertyMark mark = find_mark (text, 0);
4333 SetVerticalScrollData data;
4337 data.pixel_height = 0;
4338 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4340 text->vadj->upper = data.pixel_height;
4341 orig_value = (gint) text->vadj->value;
4343 gdk_window_get_size (text->text_area, NULL, &height);
4345 text->vadj->step_increment = MIN (text->vadj->upper, SCROLL_PIXELS);
4346 text->vadj->page_increment = MIN (text->vadj->upper, height - KEY_SCROLL_PIXELS);
4347 text->vadj->page_size = MIN (text->vadj->upper, height);
4348 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4349 text->vadj->value = MAX (text->vadj->value, 0.0);
4351 text->last_ver_value = (gint)text->vadj->value;
4353 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4355 if (text->vadj->value != orig_value)
4357 /* We got clipped, and don't really know which line to put first. */
4358 data.pixel_height = 0;
4359 data.last_didnt_wrap = TRUE;
4361 line_params_iterate (text, &mark, NULL,
4363 set_vertical_scroll_find_iterator);
4370 scroll_int (GtkText* text, gint diff)
4374 text->vadj->value += diff;
4376 upper = text->vadj->upper - text->vadj->page_size;
4377 text->vadj->value = MIN (text->vadj->value, upper);
4378 text->vadj->value = MAX (text->vadj->value, 0.0);
4380 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4384 process_exposes (GtkText *text)
4388 /* Make sure graphics expose events are processed before scrolling
4391 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4393 gtk_widget_send_expose (GTK_WIDGET (text), event);
4394 if (event->expose.count == 0)
4396 gdk_event_free (event);
4399 gdk_event_free (event);
4403 static gint last_visible_line_height (GtkText* text)
4405 GList *cache = text->line_start_cache;
4408 gdk_window_get_size (text->text_area, NULL, &height);
4410 for (; cache->next; cache = cache->next)
4411 if (pixel_height_of(text, cache->next) > height)
4415 return pixel_height_of(text, cache) - 1;
4420 static gint first_visible_line_height (GtkText* text)
4422 if (text->first_cut_pixels)
4423 return pixel_height_of(text, text->line_start_cache) + 1;
4429 scroll_down (GtkText* text, gint diff0)
4435 text->first_onscreen_ver_pixel += diff0;
4439 g_assert (text->line_start_cache);
4441 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4443 text->first_cut_pixels += 1;
4447 text->first_cut_pixels = 0;
4449 text->line_start_cache = text->line_start_cache->next;
4451 text->first_line_start_index =
4452 CACHE_DATA(text->line_start_cache).start.index;
4454 if (!text->line_start_cache->next)
4455 fetch_lines_forward (text, 1);
4461 gdk_window_get_size (text->text_area, &width, &height);
4462 if (height > real_diff)
4463 gdk_draw_pixmap (text->text_area,
4471 height - real_diff);
4474 rect.y = MAX (0, height - real_diff);
4476 rect.height = MIN (height, real_diff);
4478 expose_text (text, &rect, FALSE);
4479 gtk_text_draw_focus ( (GtkWidget *) text);
4481 if (text->current_line)
4485 text->cursor_pos_y -= real_diff;
4486 cursor_min = drawn_cursor_min(text);
4489 find_mouse_cursor (text, text->cursor_pos_x,
4490 first_visible_line_height (text));
4493 if (height > real_diff)
4494 process_exposes (text);
4498 scroll_up (GtkText* text, gint diff0)
4504 text->first_onscreen_ver_pixel += diff0;
4508 g_assert (text->line_start_cache);
4510 if (text->first_cut_pixels > 0)
4512 text->first_cut_pixels -= 1;
4516 if (!text->line_start_cache->prev)
4517 fetch_lines_backward (text);
4519 text->line_start_cache = text->line_start_cache->prev;
4521 text->first_line_start_index =
4522 CACHE_DATA(text->line_start_cache).start.index;
4524 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4530 gdk_window_get_size (text->text_area, &width, &height);
4531 if (height > real_diff)
4532 gdk_draw_pixmap (text->text_area,
4540 height - real_diff);
4545 rect.height = MIN (height, real_diff);
4547 expose_text (text, &rect, FALSE);
4548 gtk_text_draw_focus ( (GtkWidget *) text);
4550 if (text->current_line)
4555 text->cursor_pos_y += real_diff;
4556 cursor_max = drawn_cursor_max(text);
4557 gdk_window_get_size (text->text_area, NULL, &height);
4559 if (cursor_max >= height)
4560 find_mouse_cursor (text, text->cursor_pos_x,
4561 last_visible_line_height (text));
4564 if (height > real_diff)
4565 process_exposes (text);
4568 /**********************************************************************/
4570 /**********************************************************************/
4572 /* Assumes mark starts a line. Calculates the height, width, and
4573 * displayable character count of a single DISPLAYABLE line. That
4574 * means that in line-wrap mode, this does may not compute the
4575 * properties of an entire line. */
4577 find_line_params (GtkText* text,
4578 const GtkPropertyMark* mark,
4579 const PrevTabCont *tab_cont,
4580 PrevTabCont *next_cont)
4583 TabStopMark tab_mark = tab_cont->tab_start;
4584 guint max_display_pixels;
4589 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4590 max_display_pixels -= LINE_WRAP_ROOM;
4593 lp.tab_cont = *tab_cont;
4596 lp.pixel_width = tab_cont->pixel_offset;
4597 lp.displayable_chars = 0;
4599 lp.font_descent = 0;
4601 init_tab_cont (text, next_cont);
4603 while (!LAST_INDEX(text, lp.end))
4605 g_assert (lp.end.property);
4607 ch = GTK_TEXT_INDEX (text, lp.end.index);
4608 font = MARK_CURRENT_FONT (text, &lp.end);
4610 if (ch == LINE_DELIM)
4612 /* Newline doesn't count in computation of line height, even
4613 * if its in a bigger font than the rest of the line. Unless,
4614 * of course, there are no other characters. */
4616 if (!lp.font_ascent && !lp.font_descent)
4618 lp.font_ascent = font->ascent;
4619 lp.font_descent = font->descent;
4622 lp.tab_cont_next = *next_cont;
4627 ch_width = find_char_width (text, &lp.end, &tab_mark);
4629 if ((ch_width + lp.pixel_width > max_display_pixels) &&
4630 (lp.end.index > lp.start.index))
4634 if (text->line_wrap)
4636 next_cont->tab_start = tab_mark;
4637 next_cont->pixel_offset = 0;
4641 /* Here's the tough case, a tab is wrapping. */
4642 gint pixels_avail = max_display_pixels - lp.pixel_width;
4643 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
4644 gint spaces_avail = pixels_avail / space_width;
4646 if (spaces_avail == 0)
4648 decrement_mark (&lp.end);
4652 advance_tab_mark (text, &next_cont->tab_start, '\t');
4653 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4655 lp.displayable_chars += 1;
4660 if (text->word_wrap)
4662 GtkPropertyMark saved_mark = lp.end;
4663 guint saved_characters = lp.displayable_chars;
4665 lp.displayable_chars += 1;
4667 if (text->use_wchar)
4669 while (!gdk_iswspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4670 (lp.end.index > lp.start.index))
4672 decrement_mark (&lp.end);
4673 lp.displayable_chars -= 1;
4678 while (!isspace(GTK_TEXT_INDEX (text, lp.end.index)) &&
4679 (lp.end.index > lp.start.index))
4681 decrement_mark (&lp.end);
4682 lp.displayable_chars -= 1;
4686 /* If whole line is one word, revert to char wrapping */
4687 if (lp.end.index == lp.start.index)
4689 lp.end = saved_mark;
4690 lp.displayable_chars = saved_characters;
4691 decrement_mark (&lp.end);
4696 /* Don't include this character, it will wrap. */
4697 decrement_mark (&lp.end);
4701 lp.tab_cont_next = *next_cont;
4708 lp.displayable_chars += 1;
4711 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4712 lp.font_descent = MAX (font->descent, lp.font_descent);
4713 lp.pixel_width += ch_width;
4715 advance_mark(&lp.end);
4716 advance_tab_mark (text, &tab_mark, ch);
4719 if (LAST_INDEX(text, lp.start))
4721 /* Special case, empty last line. */
4722 font = MARK_CURRENT_FONT (text, &lp.end);
4724 lp.font_ascent = font->ascent;
4725 lp.font_descent = font->descent;
4728 lp.tab_cont_next = *next_cont;
4734 expand_scratch_buffer (GtkText* text, guint len)
4736 if (len >= text->scratch_buffer_len)
4740 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4742 if (text->use_wchar)
4744 if (text->scratch_buffer.wc)
4745 text->scratch_buffer.wc = g_new (GdkWChar, i);
4747 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
4748 i*sizeof (GdkWChar));
4752 if (text->scratch_buffer.ch)
4753 text->scratch_buffer.ch = g_new (guchar, i);
4755 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
4758 text->scratch_buffer_len = i;
4762 /* Side effect: modifies text->gc
4765 draw_bg_rect (GtkText* text, GtkPropertyMark *mark,
4766 gint x, gint y, gint width, gint height,
4767 gboolean already_cleared)
4769 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4771 if ((mark->index >= MIN(old_editable->selection_start_pos, old_editable->selection_end_pos) &&
4772 mark->index < MAX(old_editable->selection_start_pos, old_editable->selection_end_pos)))
4774 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
4775 old_editable->has_selection ?
4776 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
4778 NULL, GTK_WIDGET(text), "text",
4779 x, y, width, height);
4781 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
4782 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
4784 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
4786 gdk_draw_rectangle (text->text_area,
4788 TRUE, x, y, width, height);
4790 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4797 rect.height = height;
4799 clear_area (text, &rect);
4801 else if (!already_cleared)
4802 gdk_window_clear_area (text->text_area, x, y, width, height);
4806 draw_line (GtkText* text,
4807 gint pixel_start_height,
4810 GdkGCValues gc_values;
4813 guint running_offset = lp->tab_cont.pixel_offset;
4814 union { GdkWChar *wc; guchar *ch; } buffer;
4817 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4819 guint selection_start_pos = MIN (old_editable->selection_start_pos, old_editable->selection_end_pos);
4820 guint selection_end_pos = MAX (old_editable->selection_start_pos, old_editable->selection_end_pos);
4822 GtkPropertyMark mark = lp->start;
4823 TabStopMark tab_mark = lp->tab_cont.tab_start;
4824 gint pixel_height = pixel_start_height + lp->font_ascent;
4825 guint chars = lp->displayable_chars;
4827 /* First provide a contiguous segment of memory. This makes reading
4828 * the code below *much* easier, and only incurs the cost of copying
4829 * when the line being displayed spans the gap. */
4830 if (mark.index <= text->gap_position &&
4831 mark.index + chars > text->gap_position)
4833 expand_scratch_buffer (text, chars);
4835 if (text->use_wchar)
4837 for (i = 0; i < chars; i += 1)
4838 text->scratch_buffer.wc[i] = GTK_TEXT_INDEX(text, mark.index + i);
4839 buffer.wc = text->scratch_buffer.wc;
4843 for (i = 0; i < chars; i += 1)
4844 text->scratch_buffer.ch[i] = GTK_TEXT_INDEX(text, mark.index + i);
4845 buffer.ch = text->scratch_buffer.ch;
4850 if (text->use_wchar)
4852 if (mark.index >= text->gap_position)
4853 buffer.wc = text->text.wc + mark.index + text->gap_size;
4855 buffer.wc = text->text.wc + mark.index;
4859 if (mark.index >= text->gap_position)
4860 buffer.ch = text->text.ch + mark.index + text->gap_size;
4862 buffer.ch = text->text.ch + mark.index;
4867 if (running_offset > 0)
4869 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
4870 LINE_HEIGHT (*lp), TRUE);
4876 if ((text->use_wchar && buffer.wc[0] != '\t') ||
4877 (!text->use_wchar && buffer.ch[0] != '\t'))
4879 union { GdkWChar *wc; guchar *ch; } next_tab;
4884 if (text->use_wchar)
4885 for (i=0; i<chars; i++)
4887 if (buffer.wc[i] == '\t')
4889 next_tab.wc = buffer.wc + i;
4894 next_tab.ch = memchr (buffer.ch, '\t', chars);
4896 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4898 if (text->use_wchar)
4901 len = MIN (len, next_tab.wc - buffer.wc);
4906 len = MIN (len, next_tab.ch - buffer.ch);
4909 if (mark.index < selection_start_pos)
4910 len = MIN (len, selection_start_pos - mark.index);
4911 else if (mark.index < selection_end_pos)
4912 len = MIN (len, selection_end_pos - mark.index);
4914 font = MARK_CURRENT_FONT (text, &mark);
4915 if (font->type == GDK_FONT_FONT)
4917 gdk_gc_set_font (text->gc, font);
4918 gdk_gc_get_values (text->gc, &gc_values);
4919 if (text->use_wchar)
4920 pixel_width = gdk_text_width_wc (gc_values.font,
4923 pixel_width = gdk_text_width (gc_values.font,
4928 if (text->use_wchar)
4929 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
4931 pixel_width = gdk_text_width (font, buffer.ch, len);
4934 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4935 pixel_width, LINE_HEIGHT (*lp), TRUE);
4937 if ((mark.index >= selection_start_pos) &&
4938 (mark.index < selection_end_pos))
4940 if (old_editable->has_selection)
4941 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_SELECTED];
4943 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_ACTIVE];
4947 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
4951 if (text->use_wchar)
4952 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
4959 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
4966 running_offset += pixel_width;
4968 advance_tab_mark_n (text, &tab_mark, len);
4972 gint pixels_remaining;
4978 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
4979 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
4981 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4983 spaces_avail = pixels_remaining / space_width;
4984 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
4986 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4987 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
4989 running_offset += tab_mark.to_next_tab *
4990 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4992 advance_tab_mark (text, &tab_mark, '\t');
4995 advance_mark_n (&mark, len);
4996 if (text->use_wchar)
5005 draw_line_wrap (GtkText* text, guint height /* baseline height */)
5012 if (text->line_wrap)
5014 bitmap = text->line_wrap_bitmap;
5015 bitmap_width = line_wrap_width;
5016 bitmap_height = line_wrap_height;
5020 bitmap = text->line_arrow_bitmap;
5021 bitmap_width = line_arrow_width;
5022 bitmap_height = line_arrow_height;
5025 gdk_window_get_size (text->text_area, &width, NULL);
5026 width -= LINE_WRAP_ROOM;
5028 gdk_gc_set_stipple (text->gc,
5031 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
5033 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5035 gdk_gc_set_ts_origin (text->gc,
5037 height - bitmap_height - 1);
5039 gdk_draw_rectangle (text->text_area,
5043 height - bitmap_height - 1 /* one pixel above the baseline. */,
5047 gdk_gc_set_ts_origin (text->gc, 0, 0);
5049 gdk_gc_set_fill (text->gc, GDK_SOLID);
5053 undraw_cursor (GtkText* text, gint absolute)
5055 GtkOldEditable *old_editable = (GtkOldEditable *) text;
5057 TDEBUG (("in undraw_cursor\n"));
5060 text->cursor_drawn_level = 0;
5062 if ((text->cursor_drawn_level ++ == 0) &&
5063 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5064 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5068 g_assert(text->cursor_mark.property);
5070 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5072 draw_bg_rect (text, &text->cursor_mark,
5074 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5075 1, font->ascent + 1, FALSE);
5077 if (text->cursor_char)
5079 if (font->type == GDK_FONT_FONT)
5080 gdk_gc_set_font (text->gc, font);
5082 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5084 gdk_draw_text_wc (text->text_area, font,
5087 text->cursor_pos_y - text->cursor_char_offset,
5095 drawn_cursor_min (GtkText* text)
5099 g_assert(text->cursor_mark.property);
5101 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5103 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5107 drawn_cursor_max (GtkText* text)
5111 g_assert(text->cursor_mark.property);
5113 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5115 return text->cursor_pos_y - text->cursor_char_offset;
5119 draw_cursor (GtkText* text, gint absolute)
5121 GtkOldEditable *old_editable = (GtkOldEditable *)text;
5123 TDEBUG (("in draw_cursor\n"));
5126 text->cursor_drawn_level = 1;
5128 if ((--text->cursor_drawn_level == 0) &&
5129 old_editable->editable &&
5130 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5131 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5135 g_assert (text->cursor_mark.property);
5137 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5139 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5141 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
5142 text->cursor_pos_y - text->cursor_char_offset,
5144 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5149 create_bg_gc (GtkText *text)
5153 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5154 values.fill = GDK_TILED;
5156 return gdk_gc_new_with_values (text->text_area, &values,
5157 GDK_GC_FILL | GDK_GC_TILE);
5161 clear_area (GtkText *text, GdkRectangle *area)
5163 GtkWidget *widget = GTK_WIDGET (text);
5169 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5171 gdk_gc_set_ts_origin (text->bg_gc,
5172 (- text->first_onscreen_hor_pixel) % width,
5173 (- text->first_onscreen_ver_pixel) % height);
5175 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5176 area->x, area->y, area->width, area->height);
5179 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5183 expose_text (GtkText* text, GdkRectangle *area, gboolean cursor)
5185 GList *cache = text->line_start_cache;
5186 gint pixels = - text->first_cut_pixels;
5187 gint min_y = MAX (0, area->y);
5188 gint max_y = MAX (0, area->y + area->height);
5191 gdk_window_get_size (text->text_area, NULL, &height);
5192 max_y = MIN (max_y, height);
5194 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5196 clear_area (text, area);
5198 for (; pixels < height; cache = cache->next)
5200 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5202 draw_line (text, pixels, &CACHE_DATA(cache));
5204 if (CACHE_DATA(cache).wraps)
5205 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5208 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5210 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5211 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5213 /* We undraw and draw the cursor here to get the drawn
5214 * level right ... FIXME - maybe the second parameter
5215 * of draw_cursor should work differently
5217 undraw_cursor (text, FALSE);
5218 draw_cursor (text, FALSE);
5222 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5226 fetch_lines_forward (text, 1);
5235 gtk_text_update_text (GtkOldEditable *old_editable,
5239 GtkText *text = GTK_TEXT (old_editable);
5241 GList *cache = text->line_start_cache;
5242 gint pixels = - text->first_cut_pixels;
5248 end_pos = TEXT_LENGTH (text);
5250 if (end_pos < start_pos)
5253 gdk_window_get_size (text->text_area, &width, &height);
5259 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5261 for (; pixels < height; cache = cache->next)
5263 if (CACHE_DATA(cache).start.index < end_pos)
5265 if (CACHE_DATA(cache).end.index >= start_pos)
5268 area.y = MAX(0,pixels);
5269 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5275 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5279 fetch_lines_forward (text, 1);
5287 expose_text (text, &area, TRUE);
5291 recompute_geometry (GtkText* text)
5293 GtkPropertyMark mark, start_mark;
5300 mark = start_mark = set_vertical_scroll (text);
5302 /* We need a real start of a line when calling fetch_lines().
5303 * not the start of a wrapped line.
5305 while (mark.index > 0 &&
5306 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5307 decrement_mark (&mark);
5309 gdk_window_get_size (text->text_area, &width, &height);
5311 /* Fetch an entire line, to make sure that we get all the text
5312 * we backed over above, in addition to enough text to fill up
5313 * the space vertically
5316 new_lines = fetch_lines (text,
5322 mark = CACHE_DATA (g_list_last (new_lines)).end;
5323 if (!LAST_INDEX (text, mark))
5325 advance_mark (&mark);
5327 new_lines = g_list_concat (new_lines,
5332 height + text->first_cut_pixels));
5335 /* Now work forward to the actual first onscreen line */
5337 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5338 new_lines = new_lines->next;
5340 text->line_start_cache = new_lines;
5342 find_cursor (text, TRUE);
5345 /**********************************************************************/
5347 /**********************************************************************/
5350 gtk_text_set_selection (GtkOldEditable *old_editable,
5354 GtkText *text = GTK_TEXT (old_editable);
5356 guint start1, end1, start2, end2;
5359 end = TEXT_LENGTH (text);
5361 start1 = MIN(start,end);
5362 end1 = MAX(start,end);
5363 start2 = MIN(old_editable->selection_start_pos, old_editable->selection_end_pos);
5364 end2 = MAX(old_editable->selection_start_pos, old_editable->selection_end_pos);
5366 if (start2 < start1)
5370 tmp = start1; start1 = start2; start2 = tmp;
5371 tmp = end1; end1 = end2; end2 = tmp;
5374 undraw_cursor (text, FALSE);
5375 old_editable->selection_start_pos = start;
5376 old_editable->selection_end_pos = end;
5377 draw_cursor (text, FALSE);
5379 /* Expose only what changed */
5381 if (start1 < start2)
5382 gtk_text_update_text (old_editable, start1, MIN(end1, start2));
5385 gtk_text_update_text (old_editable, MAX(end1, start2), end2);
5386 else if (end2 < end1)
5387 gtk_text_update_text (old_editable, end2, end1);
5391 /**********************************************************************/
5393 /**********************************************************************/
5395 #ifdef DEBUG_GTK_TEXT
5397 gtk_text_show_cache_line (GtkText *text, GList *cache,
5398 const char* what, const char* func, gint line)
5400 LineParams *lp = &CACHE_DATA(cache);
5403 if (cache == text->line_start_cache)
5404 g_message ("Line Start Cache: ");
5406 if (cache == text->current_line)
5407 g_message("Current Line: ");
5409 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
5417 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
5418 g_message ("%c", GTK_TEXT_INDEX (text, i));
5424 gtk_text_show_cache (GtkText *text, const char* func, gint line)
5426 GList *l = text->line_start_cache;
5432 /* back up to the absolute beginning of the line cache */
5436 g_message ("*** line cache ***\n");
5437 for (; l; l = l->next)
5438 gtk_text_show_cache_line (text, l, "all", func, line);
5442 gtk_text_assert_mark (GtkText *text,
5443 GtkPropertyMark *mark,
5444 GtkPropertyMark *before,
5445 GtkPropertyMark *after,
5450 GtkPropertyMark correct_mark = find_mark (text, mark->index);
5452 if (mark->offset != correct_mark.offset ||
5453 mark->property != correct_mark.property)
5454 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
5458 gtk_text_assert (GtkText *text,
5462 GList* cache = text->line_start_cache;
5463 GtkPropertyMark* before_mark = NULL;
5464 GtkPropertyMark* after_mark = NULL;
5466 gtk_text_show_props (text, msg, line);
5468 for (; cache->prev; cache = cache->prev)
5471 g_message ("*** line markers ***\n");
5473 for (; cache; cache = cache->next)
5475 after_mark = &CACHE_DATA(cache).end;
5476 gtk_text_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
5477 before_mark = &CACHE_DATA(cache).start;
5480 after_mark = &CACHE_DATA(cache->next).start;
5484 gtk_text_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
5485 before_mark = &CACHE_DATA(cache).end;
5490 gtk_text_show_adj (GtkText *text,
5496 g_message ("*** adjustment ***\n");
5498 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
5505 adj->step_increment,
5506 adj->page_increment,
5511 gtk_text_show_props (GtkText *text,
5515 GList* props = text->text_properties;
5518 g_message ("%s:%d: ", msg, line);
5520 for (; props; props = props->next)
5522 TextProperty *p = (TextProperty*)props->data;
5524 proplen += p->length;
5526 g_message ("[%d,%p,", p->length, p);
5527 if (p->flags & PROPERTY_FONT)
5528 g_message ("%p,", p->font);
5531 if (p->flags & PROPERTY_FOREGROUND)
5532 g_message ("%ld, ", p->fore_color.pixel);
5535 if (p->flags & PROPERTY_BACKGROUND)
5536 g_message ("%ld] ", p->back_color.pixel);
5543 if (proplen - 1 != TEXT_LENGTH(text))
5544 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);