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"
40 #define INITIAL_BUFFER_SIZE 1024
41 #define INITIAL_LINE_CACHE_SIZE 256
42 #define MIN_GAP_SIZE 256
43 #define LINE_DELIM '\n'
44 #define MIN_TEXT_WIDTH_LINES 20
45 #define MIN_TEXT_HEIGHT_LINES 10
46 #define TEXT_BORDER_ROOM 1
47 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
48 #define DEFAULT_TAB_STOP_WIDTH 4
49 #define SCROLL_PIXELS 5
50 #define KEY_SCROLL_PIXELS 10
51 #define SCROLL_TIME 100
52 #define FREEZE_LENGTH 1024
53 /* Freeze text when inserting or deleting more than this many characters */
55 #define SET_PROPERTY_MARK(m, p, o) do { \
56 (m)->property = (p); \
59 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
60 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
61 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
62 (mark)->property->prev->data \
64 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
65 #define MARK_LIST_PTR(mark) ((mark)->property)
66 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
67 #define MARK_OFFSET(mark) ((mark)->offset)
68 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
71 #define MARK_CURRENT_FONT(text, mark) \
72 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
73 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
74 GTK_WIDGET (text)->style->font)
75 #define MARK_CURRENT_FORE(text, mark) \
76 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
77 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
78 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
79 #define MARK_CURRENT_BACK(text, mark) \
80 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
81 &MARK_CURRENT_PROPERTY(mark)->back_color : \
82 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
83 #define MARK_CURRENT_TEXT_FONT(text, mark) \
84 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
85 MARK_CURRENT_PROPERTY(mark)->font : \
88 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
89 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
90 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
91 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
92 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
93 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
94 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
95 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
105 typedef struct _TextProperty TextProperty;
106 typedef struct _TabStopMark TabStopMark;
107 typedef struct _PrevTabCont PrevTabCont;
108 typedef struct _FetchLinesData FetchLinesData;
109 typedef struct _LineParams LineParams;
110 typedef struct _SetVerticalScrollData SetVerticalScrollData;
112 typedef gint (*LineIteratorFunction) (GtkText* text, LineParams* lp, void* data);
120 struct _SetVerticalScrollData {
122 gint last_didnt_wrap;
123 gint last_line_start;
124 GtkPropertyMark mark;
129 /* The actual font. */
133 gint16 char_widths[256];
137 PROPERTY_FONT = 1 << 0,
138 PROPERTY_FOREGROUND = 1 << 1,
139 PROPERTY_BACKGROUND = 1 << 2
147 /* Background Color. */
150 /* Foreground Color. */
153 /* Show which properties are set */
154 TextPropertyFlags flags;
156 /* Length of this property. */
162 GList* tab_stops; /* Index into list containing the next tab position. If
163 * NULL, using default widths. */
170 TabStopMark tab_start;
173 struct _FetchLinesData
186 guint displayable_chars;
189 PrevTabCont tab_cont;
190 PrevTabCont tab_cont_next;
192 GtkPropertyMark start;
197 static void gtk_text_class_init (GtkTextClass *klass);
198 static void gtk_text_set_arg (GtkObject *object,
201 static void gtk_text_get_arg (GtkObject *object,
204 static void gtk_text_init (GtkText *text);
205 static void gtk_text_destroy (GtkObject *object);
206 static void gtk_text_finalize (GObject *object);
207 static void gtk_text_realize (GtkWidget *widget);
208 static void gtk_text_unrealize (GtkWidget *widget);
209 static void gtk_text_style_set (GtkWidget *widget,
210 GtkStyle *previous_style);
211 static void gtk_text_state_changed (GtkWidget *widget,
212 GtkStateType previous_state);
213 static void gtk_text_draw_focus (GtkWidget *widget);
214 static void gtk_text_size_request (GtkWidget *widget,
215 GtkRequisition *requisition);
216 static void gtk_text_size_allocate (GtkWidget *widget,
217 GtkAllocation *allocation);
218 static void gtk_text_adjustment (GtkAdjustment *adjustment,
220 static void gtk_text_disconnect (GtkAdjustment *adjustment,
223 static void gtk_text_insert_text (GtkOldEditable *old_editable,
224 const gchar *new_text,
225 gint new_text_length,
227 static void gtk_text_delete_text (GtkOldEditable *old_editable,
230 static void gtk_text_update_text (GtkOldEditable *old_editable,
233 static gchar *gtk_text_get_chars (GtkOldEditable *old_editable,
236 static void gtk_text_set_selection (GtkOldEditable *old_editable,
239 static void gtk_text_real_set_editable (GtkOldEditable *old_editable,
240 gboolean is_editable);
243 static gint gtk_text_expose (GtkWidget *widget,
244 GdkEventExpose *event);
245 static gint gtk_text_button_press (GtkWidget *widget,
246 GdkEventButton *event);
247 static gint gtk_text_button_release (GtkWidget *widget,
248 GdkEventButton *event);
249 static gint gtk_text_motion_notify (GtkWidget *widget,
250 GdkEventMotion *event);
251 static gint gtk_text_key_press (GtkWidget *widget,
253 static gint gtk_text_focus_in (GtkWidget *widget,
254 GdkEventFocus *event);
255 static gint gtk_text_focus_out (GtkWidget *widget,
256 GdkEventFocus *event);
258 static void move_gap (GtkText* text, guint index);
259 static void make_forward_space (GtkText* text, guint len);
261 /* Property management */
262 static GtkTextFont* get_text_font (GdkFont* gfont);
263 static void text_font_unref (GtkTextFont *text_font);
265 static void insert_text_property (GtkText* text, GdkFont* font,
266 GdkColor *fore, GdkColor* back, guint len);
267 static TextProperty* new_text_property (GtkText *text, GdkFont* font,
268 GdkColor* fore, GdkColor* back, guint length);
269 static void destroy_text_property (TextProperty *prop);
270 static void init_properties (GtkText *text);
271 static void realize_property (GtkText *text, TextProperty *prop);
272 static void realize_properties (GtkText *text);
273 static void unrealize_property (GtkText *text, TextProperty *prop);
274 static void unrealize_properties (GtkText *text);
276 static void delete_text_property (GtkText* text, guint len);
278 static guint pixel_height_of (GtkText* text, GList* cache_line);
280 /* Property Movement and Size Computations */
281 static void advance_mark (GtkPropertyMark* mark);
282 static void decrement_mark (GtkPropertyMark* mark);
283 static void advance_mark_n (GtkPropertyMark* mark, gint n);
284 static void decrement_mark_n (GtkPropertyMark* mark, gint n);
285 static void move_mark_n (GtkPropertyMark* mark, gint n);
286 static GtkPropertyMark find_mark (GtkText* text, guint mark_position);
287 static GtkPropertyMark find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near);
288 static void find_line_containing_point (GtkText* text, guint point,
292 static void compute_lines_pixels (GtkText* text, guint char_count,
293 guint *lines, guint *pixels);
295 static gint total_line_height (GtkText* text,
298 static LineParams find_line_params (GtkText* text,
299 const GtkPropertyMark *mark,
300 const PrevTabCont *tab_cont,
301 PrevTabCont *next_cont);
302 static void recompute_geometry (GtkText* text);
303 static void insert_expose (GtkText* text, guint old_pixels, gint nchars, guint new_line_count);
304 static void delete_expose (GtkText* text,
308 static GdkGC *create_bg_gc (GtkText *text);
309 static void clear_area (GtkText *text, GdkRectangle *area);
310 static void draw_line (GtkText* text,
313 static void draw_line_wrap (GtkText* text,
315 static void draw_cursor (GtkText* text, gint absolute);
316 static void undraw_cursor (GtkText* text, gint absolute);
317 static gint drawn_cursor_min (GtkText* text);
318 static gint drawn_cursor_max (GtkText* text);
319 static void expose_text (GtkText* text, GdkRectangle *area, gboolean cursor);
321 /* Search and Placement. */
322 static void find_cursor (GtkText* text,
324 static void find_cursor_at_line (GtkText* text,
325 const LineParams* start_line,
327 static void find_mouse_cursor (GtkText* text, gint x, gint y);
330 static void adjust_adj (GtkText* text, GtkAdjustment* adj);
331 static void scroll_up (GtkText* text, gint diff);
332 static void scroll_down (GtkText* text, gint diff);
333 static void scroll_int (GtkText* text, gint diff);
335 static void process_exposes (GtkText *text);
337 /* Cache Management. */
338 static void free_cache (GtkText* text);
339 static GList* remove_cache_line (GtkText* text, GList* list);
342 static void move_cursor_buffer_ver (GtkText *text, int dir);
343 static void move_cursor_page_ver (GtkText *text, int dir);
344 static void move_cursor_ver (GtkText *text, int count);
345 static void move_cursor_hor (GtkText *text, int count);
347 /* Binding actions */
348 static void gtk_text_move_cursor (GtkOldEditable *old_editable,
351 static void gtk_text_move_word (GtkOldEditable *old_editable,
353 static void gtk_text_move_page (GtkOldEditable *old_editable,
356 static void gtk_text_move_to_row (GtkOldEditable *old_editable,
358 static void gtk_text_move_to_column (GtkOldEditable *old_editable,
360 static void gtk_text_kill_char (GtkOldEditable *old_editable,
362 static void gtk_text_kill_word (GtkOldEditable *old_editable,
364 static void gtk_text_kill_line (GtkOldEditable *old_editable,
368 static void gtk_text_move_forward_character (GtkText *text);
369 static void gtk_text_move_backward_character (GtkText *text);
370 static void gtk_text_move_forward_word (GtkText *text);
371 static void gtk_text_move_backward_word (GtkText *text);
372 static void gtk_text_move_beginning_of_line (GtkText *text);
373 static void gtk_text_move_end_of_line (GtkText *text);
374 static void gtk_text_move_next_line (GtkText *text);
375 static void gtk_text_move_previous_line (GtkText *text);
377 static void gtk_text_delete_forward_character (GtkText *text);
378 static void gtk_text_delete_backward_character (GtkText *text);
379 static void gtk_text_delete_forward_word (GtkText *text);
380 static void gtk_text_delete_backward_word (GtkText *text);
381 static void gtk_text_delete_line (GtkText *text);
382 static void gtk_text_delete_to_line_end (GtkText *text);
383 static void gtk_text_select_word (GtkText *text,
385 static void gtk_text_select_line (GtkText *text,
388 static void gtk_text_set_position (GtkOldEditable *old_editable,
391 /* #define DEBUG_GTK_TEXT */
393 #if defined(DEBUG_GTK_TEXT) && defined(__GNUC__)
394 /* Debugging utilities. */
395 static void gtk_text_assert_mark (GtkText *text,
396 GtkPropertyMark *mark,
397 GtkPropertyMark *before,
398 GtkPropertyMark *after,
403 static void gtk_text_assert (GtkText *text,
406 static void gtk_text_show_cache_line (GtkText *text, GList *cache,
407 const char* what, const char* func, gint line);
408 static void gtk_text_show_cache (GtkText *text, const char* func, gint line);
409 static void gtk_text_show_adj (GtkText *text,
414 static void gtk_text_show_props (GtkText* test,
418 #define TDEBUG(args) g_message args
419 #define TEXT_ASSERT(text) gtk_text_assert (text,__PRETTY_FUNCTION__,__LINE__)
420 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_text_assert_mark (text,mark, \
421 __PRETTY_FUNCTION__,msg,__LINE__)
422 #define TEXT_SHOW(text) gtk_text_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
423 #define TEXT_SHOW_LINE(text,line,msg) gtk_text_show_cache_line (text,line,msg,\
424 __PRETTY_FUNCTION__,__LINE__)
425 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_text_show_adj (text,adj,msg, \
426 __PRETTY_FUNCTION__,__LINE__)
429 #define TEXT_ASSERT(text)
430 #define TEXT_ASSERT_MARK(text,mark,msg)
431 #define TEXT_SHOW(text)
432 #define TEXT_SHOW_LINE(text,line,msg)
433 #define TEXT_SHOW_ADJ(text,adj,msg)
436 /* Memory Management. */
437 static GMemChunk *params_mem_chunk = NULL;
438 static GMemChunk *text_property_chunk = NULL;
440 static GtkWidgetClass *parent_class = NULL;
443 static const GtkTextFunction control_keys[26] =
445 (GtkTextFunction)gtk_text_move_beginning_of_line, /* a */
446 (GtkTextFunction)gtk_text_move_backward_character, /* b */
447 (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
448 (GtkTextFunction)gtk_text_delete_forward_character, /* d */
449 (GtkTextFunction)gtk_text_move_end_of_line, /* e */
450 (GtkTextFunction)gtk_text_move_forward_character, /* f */
452 (GtkTextFunction)gtk_text_delete_backward_character, /* h */
455 (GtkTextFunction)gtk_text_delete_to_line_end, /* k */
458 (GtkTextFunction)gtk_text_move_next_line, /* n */
460 (GtkTextFunction)gtk_text_move_previous_line, /* p */
465 (GtkTextFunction)gtk_text_delete_line, /* u */
466 (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
467 (GtkTextFunction)gtk_text_delete_backward_word, /* w */
468 (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
473 static const GtkTextFunction alt_keys[26] =
476 (GtkTextFunction)gtk_text_move_backward_word, /* b */
478 (GtkTextFunction)gtk_text_delete_forward_word, /* d */
480 (GtkTextFunction)gtk_text_move_forward_word, /* f */
504 /**********************************************************************/
506 /**********************************************************************/
509 gtk_text_get_type (void)
511 static GtkType text_type = 0;
515 static const GtkTypeInfo text_info =
519 sizeof (GtkTextClass),
520 (GtkClassInitFunc) gtk_text_class_init,
521 (GtkObjectInitFunc) gtk_text_init,
522 /* reserved_1 */ NULL,
523 /* reserved_2 */ NULL,
524 (GtkClassInitFunc) NULL,
527 text_type = gtk_type_unique (GTK_TYPE_OLD_EDITABLE, &text_info);
534 gtk_text_class_init (GtkTextClass *class)
536 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
537 GtkObjectClass *object_class;
538 GtkWidgetClass *widget_class;
539 GtkOldEditableClass *old_editable_class;
541 object_class = (GtkObjectClass*) class;
542 widget_class = (GtkWidgetClass*) class;
543 old_editable_class = (GtkOldEditableClass*) class;
544 parent_class = gtk_type_class (GTK_TYPE_OLD_EDITABLE);
546 gobject_class->finalize = gtk_text_finalize;
548 object_class->set_arg = gtk_text_set_arg;
549 object_class->get_arg = gtk_text_get_arg;
550 object_class->destroy = gtk_text_destroy;
552 widget_class->realize = gtk_text_realize;
553 widget_class->unrealize = gtk_text_unrealize;
554 widget_class->style_set = gtk_text_style_set;
555 widget_class->state_changed = gtk_text_state_changed;
556 widget_class->size_request = gtk_text_size_request;
557 widget_class->size_allocate = gtk_text_size_allocate;
558 widget_class->expose_event = gtk_text_expose;
559 widget_class->button_press_event = gtk_text_button_press;
560 widget_class->button_release_event = gtk_text_button_release;
561 widget_class->motion_notify_event = gtk_text_motion_notify;
562 widget_class->key_press_event = gtk_text_key_press;
563 widget_class->focus_in_event = gtk_text_focus_in;
564 widget_class->focus_out_event = gtk_text_focus_out;
566 old_editable_class->set_editable = gtk_text_real_set_editable;
567 old_editable_class->insert_text = gtk_text_insert_text;
568 old_editable_class->delete_text = gtk_text_delete_text;
570 old_editable_class->move_cursor = gtk_text_move_cursor;
571 old_editable_class->move_word = gtk_text_move_word;
572 old_editable_class->move_page = gtk_text_move_page;
573 old_editable_class->move_to_row = gtk_text_move_to_row;
574 old_editable_class->move_to_column = gtk_text_move_to_column;
576 old_editable_class->kill_char = gtk_text_kill_char;
577 old_editable_class->kill_word = gtk_text_kill_word;
578 old_editable_class->kill_line = gtk_text_kill_line;
580 old_editable_class->update_text = gtk_text_update_text;
581 old_editable_class->get_chars = gtk_text_get_chars;
582 old_editable_class->set_selection = gtk_text_set_selection;
583 old_editable_class->set_position = gtk_text_set_position;
585 class->set_scroll_adjustments = gtk_text_set_adjustments;
587 gtk_object_add_arg_type ("GtkText::hadjustment",
591 gtk_object_add_arg_type ("GtkText::vadjustment",
595 gtk_object_add_arg_type ("GtkText::line_wrap",
599 gtk_object_add_arg_type ("GtkText::word_wrap",
604 widget_class->set_scroll_adjustments_signal =
605 gtk_signal_new ("set_scroll_adjustments",
607 GTK_CLASS_TYPE (object_class),
608 GTK_SIGNAL_OFFSET (GtkTextClass, set_scroll_adjustments),
609 gtk_marshal_VOID__OBJECT_OBJECT,
610 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
614 gtk_text_set_arg (GtkObject *object,
620 text = GTK_TEXT (object);
624 case ARG_HADJUSTMENT:
625 gtk_text_set_adjustments (text,
626 GTK_VALUE_POINTER (*arg),
629 case ARG_VADJUSTMENT:
630 gtk_text_set_adjustments (text,
632 GTK_VALUE_POINTER (*arg));
635 gtk_text_set_line_wrap (text, GTK_VALUE_BOOL (*arg));
638 gtk_text_set_word_wrap (text, GTK_VALUE_BOOL (*arg));
646 gtk_text_get_arg (GtkObject *object,
652 text = GTK_TEXT (object);
656 case ARG_HADJUSTMENT:
657 GTK_VALUE_POINTER (*arg) = text->hadj;
659 case ARG_VADJUSTMENT:
660 GTK_VALUE_POINTER (*arg) = text->vadj;
663 GTK_VALUE_BOOL (*arg) = text->line_wrap;
666 GTK_VALUE_BOOL (*arg) = text->word_wrap;
669 arg->type = GTK_TYPE_INVALID;
675 gtk_text_init (GtkText *text)
677 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
679 text->text_area = NULL;
684 text->line_wrap_bitmap = NULL;
685 text->line_arrow_bitmap = NULL;
687 text->use_wchar = FALSE;
688 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
689 text->text_len = INITIAL_BUFFER_SIZE;
691 text->scratch_buffer.ch = NULL;
692 text->scratch_buffer_len = 0;
694 text->freeze_count = 0;
696 if (!params_mem_chunk)
697 params_mem_chunk = g_mem_chunk_new ("LineParams",
699 256 * sizeof (LineParams),
702 text->default_tab_width = 4;
703 text->tab_stops = NULL;
705 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
706 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
708 text->line_start_cache = NULL;
709 text->first_cut_pixels = 0;
711 text->line_wrap = TRUE;
712 text->word_wrap = FALSE;
717 text->current_font = NULL;
719 init_properties (text);
721 GTK_OLD_EDITABLE (text)->editable = FALSE;
723 gtk_text_set_adjustments (text, NULL, NULL);
724 gtk_editable_set_position (GTK_EDITABLE (text), 0);
728 gtk_text_new (GtkAdjustment *hadj,
734 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
736 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
738 text = gtk_widget_new (GTK_TYPE_TEXT,
747 gtk_text_set_word_wrap (GtkText *text,
750 g_return_if_fail (text != NULL);
751 g_return_if_fail (GTK_IS_TEXT (text));
753 text->word_wrap = (word_wrap != FALSE);
755 if (GTK_WIDGET_REALIZED (text))
757 recompute_geometry (text);
758 gtk_widget_queue_draw (GTK_WIDGET (text));
763 gtk_text_set_line_wrap (GtkText *text,
766 g_return_if_fail (text != NULL);
767 g_return_if_fail (GTK_IS_TEXT (text));
769 text->line_wrap = (line_wrap != FALSE);
771 if (GTK_WIDGET_REALIZED (text))
773 recompute_geometry (text);
774 gtk_widget_queue_draw (GTK_WIDGET (text));
779 gtk_text_set_editable (GtkText *text,
780 gboolean is_editable)
782 g_return_if_fail (text != NULL);
783 g_return_if_fail (GTK_IS_TEXT (text));
785 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
789 gtk_text_real_set_editable (GtkOldEditable *old_editable,
790 gboolean is_editable)
794 g_return_if_fail (old_editable != NULL);
795 g_return_if_fail (GTK_IS_TEXT (old_editable));
797 text = GTK_TEXT (old_editable);
799 old_editable->editable = (is_editable != FALSE);
802 draw_cursor (text, TRUE);
804 undraw_cursor (text, TRUE);
808 gtk_text_set_adjustments (GtkText *text,
812 g_return_if_fail (text != NULL);
813 g_return_if_fail (GTK_IS_TEXT (text));
815 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
817 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
819 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
821 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
823 if (text->hadj && (text->hadj != hadj))
825 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
826 gtk_object_unref (GTK_OBJECT (text->hadj));
829 if (text->vadj && (text->vadj != vadj))
831 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
832 gtk_object_unref (GTK_OBJECT (text->vadj));
835 if (text->hadj != hadj)
838 gtk_object_ref (GTK_OBJECT (text->hadj));
839 gtk_object_sink (GTK_OBJECT (text->hadj));
841 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
842 (GtkSignalFunc) gtk_text_adjustment,
844 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
845 (GtkSignalFunc) gtk_text_adjustment,
847 gtk_signal_connect (GTK_OBJECT (text->hadj), "disconnect",
848 (GtkSignalFunc) gtk_text_disconnect,
850 gtk_text_adjustment (hadj, text);
853 if (text->vadj != vadj)
856 gtk_object_ref (GTK_OBJECT (text->vadj));
857 gtk_object_sink (GTK_OBJECT (text->vadj));
859 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
860 (GtkSignalFunc) gtk_text_adjustment,
862 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
863 (GtkSignalFunc) gtk_text_adjustment,
865 gtk_signal_connect (GTK_OBJECT (text->vadj), "disconnect",
866 (GtkSignalFunc) gtk_text_disconnect,
868 gtk_text_adjustment (vadj, text);
873 gtk_text_set_point (GtkText *text,
876 g_return_if_fail (text != NULL);
877 g_return_if_fail (GTK_IS_TEXT (text));
878 g_return_if_fail (index <= TEXT_LENGTH (text));
880 text->point = find_mark (text, index);
884 gtk_text_get_point (GtkText *text)
886 g_return_val_if_fail (text != NULL, 0);
887 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
889 return text->point.index;
893 gtk_text_get_length (GtkText *text)
895 g_return_val_if_fail (text != NULL, 0);
896 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
898 return TEXT_LENGTH (text);
902 gtk_text_freeze (GtkText *text)
904 g_return_if_fail (text != NULL);
905 g_return_if_fail (GTK_IS_TEXT (text));
907 text->freeze_count++;
911 gtk_text_thaw (GtkText *text)
913 g_return_if_fail (text != NULL);
914 g_return_if_fail (GTK_IS_TEXT (text));
916 if (text->freeze_count)
917 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
919 recompute_geometry (text);
920 gtk_widget_queue_draw (GTK_WIDGET (text));
925 gtk_text_insert (GtkText *text,
932 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
933 gboolean frozen = FALSE;
935 gint new_line_count = 1;
936 guint old_height = 0;
941 g_return_if_fail (text != NULL);
942 g_return_if_fail (GTK_IS_TEXT (text));
945 length = strlen (chars);
952 if (!text->freeze_count && (length > FREEZE_LENGTH))
954 gtk_text_freeze (text);
958 if (!text->freeze_count && (text->line_start_cache != NULL))
960 find_line_containing_point (text, text->point.index, TRUE);
961 old_height = total_line_height (text, text->current_line, 1);
964 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
967 widget = GTK_WIDGET (text);
968 gtk_widget_ensure_style (widget);
969 if ((widget->style) && (widget->style->font->type == GDK_FONT_FONTSET))
971 text->use_wchar = TRUE;
972 g_free (text->text.ch);
973 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
974 text->text_len = INITIAL_BUFFER_SIZE;
975 if (text->scratch_buffer.ch)
976 g_free (text->scratch_buffer.ch);
977 text->scratch_buffer.wc = NULL;
978 text->scratch_buffer_len = 0;
982 move_gap (text, text->point.index);
983 make_forward_space (text, length);
987 char *chars_nt = (char *)chars;
990 chars_nt = g_new (char, length+1);
991 memcpy (chars_nt, chars, length);
992 chars_nt[length] = 0;
994 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
996 if (chars_nt != chars)
1004 memcpy(text->text.ch + text->gap_position, chars, length);
1007 if (!text->freeze_count && (text->line_start_cache != NULL))
1009 if (text->use_wchar)
1011 for (i=0; i<numwcs; i++)
1012 if (text->text.wc[text->gap_position + i] == '\n')
1017 for (i=0; i<numwcs; i++)
1018 if (text->text.ch[text->gap_position + i] == '\n')
1025 insert_text_property (text, font, fore, back, numwcs);
1027 text->gap_size -= numwcs;
1028 text->gap_position += numwcs;
1030 if (text->point.index < text->first_line_start_index)
1031 text->first_line_start_index += numwcs;
1032 if (text->point.index < old_editable->selection_start_pos)
1033 old_editable->selection_start_pos += numwcs;
1034 if (text->point.index < old_editable->selection_end_pos)
1035 old_editable->selection_end_pos += numwcs;
1036 /* We'll reset the cursor later anyways if we aren't frozen */
1037 if (text->point.index < text->cursor_mark.index)
1038 text->cursor_mark.index += numwcs;
1040 advance_mark_n (&text->point, numwcs);
1042 if (!text->freeze_count && (text->line_start_cache != NULL))
1043 insert_expose (text, old_height, numwcs, new_line_count);
1047 gtk_text_thaw (text);
1051 gtk_text_backward_delete (GtkText *text,
1054 g_return_val_if_fail (text != NULL, 0);
1055 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
1057 if (nchars > text->point.index || nchars <= 0)
1060 gtk_text_set_point (text, text->point.index - nchars);
1062 return gtk_text_forward_delete (text, nchars);
1066 gtk_text_forward_delete (GtkText *text,
1069 guint old_lines, old_height;
1070 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
1071 gboolean frozen = FALSE;
1073 g_return_val_if_fail (text != NULL, 0);
1074 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
1076 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1079 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1081 gtk_text_freeze (text);
1085 if (!text->freeze_count && text->line_start_cache != NULL)
1087 /* We need to undraw the cursor here, since we may later
1088 * delete the cursor's property
1090 undraw_cursor (text, FALSE);
1091 find_line_containing_point (text, text->point.index, TRUE);
1092 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1095 /* FIXME, or resizing after deleting will be odd */
1096 if (text->point.index < text->first_line_start_index)
1098 if (text->point.index + nchars >= text->first_line_start_index)
1100 text->first_line_start_index = text->point.index;
1101 while ((text->first_line_start_index > 0) &&
1102 (GTK_TEXT_INDEX (text, text->first_line_start_index - 1)
1104 text->first_line_start_index -= 1;
1108 text->first_line_start_index -= nchars;
1111 if (text->point.index < old_editable->selection_start_pos)
1112 old_editable->selection_start_pos -=
1113 MIN(nchars, old_editable->selection_start_pos - text->point.index);
1114 if (text->point.index < old_editable->selection_end_pos)
1115 old_editable->selection_end_pos -=
1116 MIN(nchars, old_editable->selection_end_pos - text->point.index);
1117 /* We'll reset the cursor later anyways if we aren't frozen */
1118 if (text->point.index < text->cursor_mark.index)
1119 move_mark_n (&text->cursor_mark,
1120 -MIN(nchars, text->cursor_mark.index - text->point.index));
1122 move_gap (text, text->point.index);
1124 text->gap_size += nchars;
1126 delete_text_property (text, nchars);
1128 if (!text->freeze_count && (text->line_start_cache != NULL))
1130 delete_expose (text, nchars, old_lines, old_height);
1131 draw_cursor (text, FALSE);
1135 gtk_text_thaw (text);
1141 gtk_text_set_position (GtkOldEditable *old_editable,
1144 GtkText *text = (GtkText *) old_editable;
1146 undraw_cursor (text, FALSE);
1147 text->cursor_mark = find_mark (text, position);
1148 find_cursor (text, TRUE);
1149 draw_cursor (text, FALSE);
1150 gtk_editable_select_region (GTK_EDITABLE (old_editable), 0, 0);
1154 gtk_text_get_chars (GtkOldEditable *old_editable,
1162 g_return_val_if_fail (old_editable != NULL, NULL);
1163 g_return_val_if_fail (GTK_IS_TEXT (old_editable), NULL);
1164 text = GTK_TEXT (old_editable);
1167 end_pos = TEXT_LENGTH (text);
1169 if ((start_pos < 0) ||
1170 (end_pos > TEXT_LENGTH (text)) ||
1171 (end_pos < start_pos))
1174 move_gap (text, TEXT_LENGTH (text));
1175 make_forward_space (text, 1);
1177 if (text->use_wchar)
1180 ch = text->text.wc[end_pos];
1181 text->text.wc[end_pos] = 0;
1182 retval = gdk_wcstombs (text->text.wc + start_pos);
1183 text->text.wc[end_pos] = ch;
1188 ch = text->text.ch[end_pos];
1189 text->text.ch[end_pos] = 0;
1190 retval = g_strdup (text->text.ch + start_pos);
1191 text->text.ch[end_pos] = ch;
1199 gtk_text_destroy (GtkObject *object)
1203 g_return_if_fail (GTK_IS_TEXT (object));
1205 text = GTK_TEXT (object);
1209 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1210 gtk_object_unref (GTK_OBJECT (text->hadj));
1215 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1216 gtk_object_unref (GTK_OBJECT (text->vadj));
1222 gtk_timeout_remove (text->timer);
1226 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1230 gtk_text_finalize (GObject *object)
1235 g_return_if_fail (GTK_IS_TEXT (object));
1237 text = GTK_TEXT (object);
1239 /* Clean up the internal structures */
1240 if (text->use_wchar)
1241 g_free (text->text.wc);
1243 g_free (text->text.ch);
1245 tmp_list = text->text_properties;
1248 destroy_text_property (tmp_list->data);
1249 tmp_list = tmp_list->next;
1252 if (text->current_font)
1253 text_font_unref (text->current_font);
1255 g_list_free (text->text_properties);
1257 if (text->use_wchar)
1259 if (text->scratch_buffer.wc)
1260 g_free (text->scratch_buffer.wc);
1264 if (text->scratch_buffer.ch)
1265 g_free (text->scratch_buffer.ch);
1268 g_list_free (text->tab_stops);
1270 G_OBJECT_CLASS (parent_class)->finalize (object);
1274 gtk_text_realize (GtkWidget *widget)
1277 GtkOldEditable *old_editable;
1278 GdkWindowAttr attributes;
1279 gint attributes_mask;
1281 g_return_if_fail (widget != NULL);
1282 g_return_if_fail (GTK_IS_TEXT (widget));
1284 text = GTK_TEXT (widget);
1285 old_editable = GTK_OLD_EDITABLE (widget);
1286 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1288 attributes.window_type = GDK_WINDOW_CHILD;
1289 attributes.x = widget->allocation.x;
1290 attributes.y = widget->allocation.y;
1291 attributes.width = widget->allocation.width;
1292 attributes.height = widget->allocation.height;
1293 attributes.wclass = GDK_INPUT_OUTPUT;
1294 attributes.visual = gtk_widget_get_visual (widget);
1295 attributes.colormap = gtk_widget_get_colormap (widget);
1296 attributes.event_mask = gtk_widget_get_events (widget);
1297 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1298 GDK_BUTTON_PRESS_MASK |
1299 GDK_BUTTON_RELEASE_MASK |
1300 GDK_BUTTON_MOTION_MASK |
1301 GDK_ENTER_NOTIFY_MASK |
1302 GDK_LEAVE_NOTIFY_MASK |
1303 GDK_KEY_PRESS_MASK);
1304 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1306 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1307 gdk_window_set_user_data (widget->window, text);
1309 attributes.x = (widget->style->xthickness + TEXT_BORDER_ROOM);
1310 attributes.y = (widget->style->ythickness + TEXT_BORDER_ROOM);
1311 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1312 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1314 attributes.cursor = gdk_cursor_new (GDK_XTERM);
1315 attributes_mask |= GDK_WA_CURSOR;
1317 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1318 gdk_window_set_user_data (text->text_area, text);
1320 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1322 widget->style = gtk_style_attach (widget->style, widget->window);
1324 /* Can't call gtk_style_set_background here because it's handled specially */
1325 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1326 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1328 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1329 text->bg_gc = create_bg_gc (text);
1331 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1332 (gchar*) line_wrap_bits,
1336 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1337 (gchar*) line_arrow_bits,
1341 text->gc = gdk_gc_new (text->text_area);
1342 gdk_gc_set_exposures (text->gc, TRUE);
1343 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1345 realize_properties (text);
1346 gdk_window_show (text->text_area);
1347 init_properties (text);
1349 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1350 gtk_old_editable_claim_selection (old_editable, TRUE, GDK_CURRENT_TIME);
1352 recompute_geometry (text);
1356 gtk_text_style_set (GtkWidget *widget,
1357 GtkStyle *previous_style)
1359 GtkText *text = GTK_TEXT (widget);
1361 if (GTK_WIDGET_REALIZED (widget))
1363 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1364 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1368 gdk_gc_destroy (text->bg_gc);
1372 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1373 text->bg_gc = create_bg_gc (text);
1375 recompute_geometry (text);
1378 if (text->current_font)
1379 text_font_unref (text->current_font);
1380 text->current_font = get_text_font (widget->style->font);
1384 gtk_text_state_changed (GtkWidget *widget,
1385 GtkStateType previous_state)
1387 GtkText *text = GTK_TEXT (widget);
1389 if (GTK_WIDGET_REALIZED (widget))
1391 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1392 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1397 gtk_text_unrealize (GtkWidget *widget)
1401 g_return_if_fail (widget != NULL);
1402 g_return_if_fail (GTK_IS_TEXT (widget));
1404 text = GTK_TEXT (widget);
1406 gdk_window_set_user_data (text->text_area, NULL);
1407 gdk_window_destroy (text->text_area);
1408 text->text_area = NULL;
1410 gdk_gc_destroy (text->gc);
1415 gdk_gc_destroy (text->bg_gc);
1419 gdk_pixmap_unref (text->line_wrap_bitmap);
1420 gdk_pixmap_unref (text->line_arrow_bitmap);
1422 unrealize_properties (text);
1426 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1427 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1431 clear_focus_area (GtkText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1433 GtkWidget *widget = GTK_WIDGET (text);
1436 gint ythick = TEXT_BORDER_ROOM + widget->style->ythickness;
1437 gint xthick = TEXT_BORDER_ROOM + widget->style->xthickness;
1441 if (area_width == 0 || area_height == 0)
1444 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1446 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1448 gdk_gc_set_ts_origin (text->bg_gc,
1449 (- text->first_onscreen_hor_pixel + xthick) % width,
1450 (- text->first_onscreen_ver_pixel + ythick) % height);
1455 gc = widget->style->bg_gc[widget->state];
1457 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1458 area_x, area_y, area_width, area_height);
1462 gtk_text_draw_focus (GtkWidget *widget)
1468 g_return_if_fail (widget != NULL);
1469 g_return_if_fail (GTK_IS_TEXT (widget));
1471 text = GTK_TEXT (widget);
1473 if (GTK_WIDGET_DRAWABLE (widget))
1475 gint ythick = widget->style->ythickness;
1476 gint xthick = widget->style->xthickness;
1477 gint xextra = TEXT_BORDER_ROOM;
1478 gint yextra = TEXT_BORDER_ROOM;
1480 TDEBUG (("in gtk_text_draw_focus\n"));
1484 width = widget->allocation.width;
1485 height = widget->allocation.height;
1487 if (GTK_WIDGET_HAS_FOCUS (widget))
1496 gtk_paint_focus (widget->style, widget->window,
1497 NULL, widget, "text",
1499 widget->allocation.width - 1,
1500 widget->allocation.height - 1);
1503 gtk_paint_shadow (widget->style, widget->window,
1504 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1505 NULL, widget, "text",
1506 x, y, width, height);
1510 width -= 2 * xthick;
1511 height -= 2 * ythick;
1514 clear_focus_area (text, x, y, width, yextra);
1516 clear_focus_area (text, x, y + yextra,
1517 xextra, y + height - 2 * yextra);
1519 clear_focus_area (text, x + width - xextra, y + yextra,
1520 xextra, height - 2 * ythick);
1522 clear_focus_area (text, x, x + height - yextra, width, yextra);
1526 TDEBUG (("in gtk_text_draw_focus (undrawable !!!)\n"));
1531 gtk_text_size_request (GtkWidget *widget,
1532 GtkRequisition *requisition)
1539 g_return_if_fail (widget != NULL);
1540 g_return_if_fail (GTK_IS_TEXT (widget));
1541 g_return_if_fail (requisition != NULL);
1543 xthickness = widget->style->xthickness + TEXT_BORDER_ROOM;
1544 ythickness = widget->style->ythickness + TEXT_BORDER_ROOM;
1546 char_height = MIN_TEXT_HEIGHT_LINES * (widget->style->font->ascent +
1547 widget->style->font->descent);
1549 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (widget->style->font,
1550 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1554 requisition->width = char_width + xthickness * 2;
1555 requisition->height = char_height + ythickness * 2;
1559 gtk_text_size_allocate (GtkWidget *widget,
1560 GtkAllocation *allocation)
1563 GtkOldEditable *old_editable;
1565 g_return_if_fail (widget != NULL);
1566 g_return_if_fail (GTK_IS_TEXT (widget));
1567 g_return_if_fail (allocation != NULL);
1569 text = GTK_TEXT (widget);
1570 old_editable = GTK_OLD_EDITABLE (widget);
1572 widget->allocation = *allocation;
1573 if (GTK_WIDGET_REALIZED (widget))
1575 gdk_window_move_resize (widget->window,
1576 allocation->x, allocation->y,
1577 allocation->width, allocation->height);
1579 gdk_window_move_resize (text->text_area,
1580 widget->style->xthickness + TEXT_BORDER_ROOM,
1581 widget->style->ythickness + TEXT_BORDER_ROOM,
1582 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->xthickness +
1583 (gint)TEXT_BORDER_ROOM) * 2),
1584 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->ythickness +
1585 (gint)TEXT_BORDER_ROOM) * 2));
1587 recompute_geometry (text);
1592 gtk_text_expose (GtkWidget *widget,
1593 GdkEventExpose *event)
1595 g_return_val_if_fail (widget != NULL, FALSE);
1596 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1597 g_return_val_if_fail (event != NULL, FALSE);
1599 if (event->window == GTK_TEXT (widget)->text_area)
1601 TDEBUG (("in gtk_text_expose (expose)\n"));
1602 expose_text (GTK_TEXT (widget), &event->area, TRUE);
1604 else if (event->count == 0)
1606 TDEBUG (("in gtk_text_expose (focus)\n"));
1607 gtk_text_draw_focus (widget);
1614 gtk_text_scroll_timeout (gpointer data)
1617 GdkEventMotion event;
1619 GdkModifierType mask;
1621 GDK_THREADS_ENTER ();
1623 text = GTK_TEXT (data);
1626 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1628 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1635 gtk_text_motion_notify (GTK_WIDGET (text), &event);
1638 GDK_THREADS_LEAVE ();
1644 gtk_text_button_press (GtkWidget *widget,
1645 GdkEventButton *event)
1648 GtkOldEditable *old_editable;
1650 g_return_val_if_fail (widget != NULL, FALSE);
1651 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1652 g_return_val_if_fail (event != NULL, FALSE);
1654 text = GTK_TEXT (widget);
1655 old_editable = GTK_OLD_EDITABLE (widget);
1657 if (text->button && (event->button != text->button))
1660 text->button = event->button;
1662 if (!GTK_WIDGET_HAS_FOCUS (widget))
1663 gtk_widget_grab_focus (widget);
1665 if (event->button == 1)
1667 switch (event->type)
1669 case GDK_BUTTON_PRESS:
1670 gtk_grab_add (widget);
1672 undraw_cursor (text, FALSE);
1673 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1674 draw_cursor (text, FALSE);
1676 /* Set it now, so we display things right. We'll unset it
1677 * later if things don't work out */
1678 old_editable->has_selection = TRUE;
1679 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1680 text->cursor_mark.index,
1681 text->cursor_mark.index);
1685 case GDK_2BUTTON_PRESS:
1686 gtk_text_select_word (text, event->time);
1689 case GDK_3BUTTON_PRESS:
1690 gtk_text_select_line (text, event->time);
1697 else if (event->type == GDK_BUTTON_PRESS)
1699 if ((event->button == 2) && old_editable->editable)
1701 if (old_editable->selection_start_pos == old_editable->selection_end_pos ||
1702 old_editable->has_selection)
1704 undraw_cursor (text, FALSE);
1705 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1706 draw_cursor (text, FALSE);
1710 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1711 gdk_atom_intern ("UTF8_STRING", FALSE),
1716 gtk_grab_add (widget);
1718 undraw_cursor (text, FALSE);
1719 find_mouse_cursor (text, event->x, event->y);
1720 draw_cursor (text, FALSE);
1722 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1723 text->cursor_mark.index,
1724 text->cursor_mark.index);
1726 old_editable->has_selection = FALSE;
1727 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1728 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1736 gtk_text_button_release (GtkWidget *widget,
1737 GdkEventButton *event)
1740 GtkOldEditable *old_editable;
1741 g_return_val_if_fail (widget != NULL, FALSE);
1742 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1743 g_return_val_if_fail (event != NULL, FALSE);
1745 text = GTK_TEXT (widget);
1747 gtk_grab_remove (widget);
1749 if (text->button != event->button)
1756 gtk_timeout_remove (text->timer);
1760 if (event->button == 1)
1762 text = GTK_TEXT (widget);
1763 old_editable = GTK_OLD_EDITABLE (widget);
1765 gtk_grab_remove (widget);
1767 old_editable->has_selection = FALSE;
1768 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1770 if (gtk_selection_owner_set (widget,
1771 GDK_SELECTION_PRIMARY,
1773 old_editable->has_selection = TRUE;
1775 gtk_text_update_text (old_editable, old_editable->selection_start_pos,
1776 old_editable->selection_end_pos);
1780 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1781 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1784 else if (event->button == 3)
1786 gtk_grab_remove (widget);
1789 undraw_cursor (text, FALSE);
1790 find_cursor (text, TRUE);
1791 draw_cursor (text, FALSE);
1797 gtk_text_motion_notify (GtkWidget *widget,
1798 GdkEventMotion *event)
1803 GdkModifierType mask;
1805 g_return_val_if_fail (widget != NULL, FALSE);
1806 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1807 g_return_val_if_fail (event != NULL, FALSE);
1809 text = GTK_TEXT (widget);
1813 mask = event->state;
1814 if (event->is_hint || (text->text_area != event->window))
1816 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1819 if ((text->button == 0) ||
1820 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1823 gdk_window_get_size (text->text_area, NULL, &height);
1825 if ((y < 0) || (y > height))
1827 if (text->timer == 0)
1829 text->timer = gtk_timeout_add (SCROLL_TIME,
1830 gtk_text_scroll_timeout,
1834 scroll_int (text, y/2);
1836 scroll_int (text, (y - height)/2);
1842 undraw_cursor (GTK_TEXT (widget), FALSE);
1843 find_mouse_cursor (GTK_TEXT (widget), x, y);
1844 draw_cursor (GTK_TEXT (widget), FALSE);
1846 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1847 GTK_OLD_EDITABLE (text)->selection_start_pos,
1848 text->cursor_mark.index);
1854 gtk_text_insert_text (GtkOldEditable *old_editable,
1855 const gchar *new_text,
1856 gint new_text_length,
1859 GtkText *text = GTK_TEXT (old_editable);
1861 GdkColor *fore, *back;
1863 TextProperty *property;
1865 gtk_text_set_point (text, *position);
1867 property = MARK_CURRENT_PROPERTY (&text->point);
1868 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
1869 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
1870 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
1872 gtk_text_insert (text, font, fore, back, new_text, new_text_length);
1874 *position = text->point.index;
1878 gtk_text_delete_text (GtkOldEditable *old_editable,
1884 g_return_if_fail (start_pos >= 0);
1886 text = GTK_TEXT (old_editable);
1888 gtk_text_set_point (text, start_pos);
1890 end_pos = TEXT_LENGTH (text);
1892 if (end_pos > start_pos)
1893 gtk_text_forward_delete (text, end_pos - start_pos);
1897 gtk_text_key_press (GtkWidget *widget,
1901 GtkOldEditable *old_editable;
1906 g_return_val_if_fail (widget != NULL, FALSE);
1907 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1908 g_return_val_if_fail (event != NULL, FALSE);
1912 text = GTK_TEXT (widget);
1913 old_editable = GTK_OLD_EDITABLE (widget);
1915 key = event->keyval;
1918 if ((GTK_OLD_EDITABLE(text)->editable == FALSE))
1920 switch (event->keyval)
1923 if (event->state & GDK_CONTROL_MASK)
1924 scroll_int (text, -text->vadj->value);
1929 if (event->state & GDK_CONTROL_MASK)
1930 scroll_int (text, +text->vadj->upper);
1934 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
1935 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
1936 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
1937 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
1939 if (event->state & GDK_CONTROL_MASK)
1940 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
1951 gint extend_selection;
1953 guint initial_pos = old_editable->current_pos;
1955 text->point = find_mark (text, text->cursor_mark.index);
1957 extend_selection = event->state & GDK_SHIFT_MASK;
1958 extend_start = FALSE;
1960 if (extend_selection)
1962 old_editable->has_selection = TRUE;
1964 if (old_editable->selection_start_pos == old_editable->selection_end_pos)
1966 old_editable->selection_start_pos = text->point.index;
1967 old_editable->selection_end_pos = text->point.index;
1970 extend_start = (text->point.index == old_editable->selection_start_pos);
1973 switch (event->keyval)
1976 if (event->state & GDK_CONTROL_MASK)
1977 move_cursor_buffer_ver (text, -1);
1979 gtk_text_move_beginning_of_line (text);
1982 if (event->state & GDK_CONTROL_MASK)
1983 move_cursor_buffer_ver (text, +1);
1985 gtk_text_move_end_of_line (text);
1987 case GDK_Page_Up: move_cursor_page_ver (text, -1); break;
1988 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
1989 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
1990 case GDK_Up: move_cursor_ver (text, -1); break;
1991 case GDK_Down: move_cursor_ver (text, +1); break;
1993 if (event->state & GDK_CONTROL_MASK)
1994 gtk_text_move_backward_word (text);
1996 move_cursor_hor (text, -1);
1999 if (event->state & GDK_CONTROL_MASK)
2000 gtk_text_move_forward_word (text);
2002 move_cursor_hor (text, +1);
2006 if (event->state & GDK_CONTROL_MASK)
2007 gtk_text_delete_backward_word (text);
2009 gtk_text_delete_backward_character (text);
2012 gtk_text_delete_line (text);
2015 if (event->state & GDK_SHIFT_MASK)
2017 extend_selection = FALSE;
2018 gtk_editable_paste_clipboard (GTK_EDITABLE (old_editable));
2020 else if (event->state & GDK_CONTROL_MASK)
2022 gtk_editable_copy_clipboard (GTK_EDITABLE (old_editable));
2026 /* gtk_toggle_insert(text) -- IMPLEMENT */
2030 if (event->state & GDK_CONTROL_MASK)
2031 gtk_text_delete_forward_word (text);
2032 else if (event->state & GDK_SHIFT_MASK)
2034 extend_selection = FALSE;
2035 gtk_editable_cut_clipboard (GTK_EDITABLE (old_editable));
2038 gtk_text_delete_forward_character (text);
2041 position = text->point.index;
2042 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\t", 1, &position);
2045 if (event->state & GDK_CONTROL_MASK)
2046 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2049 position = text->point.index;
2050 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\n", 1, &position);
2054 /* Don't insert literally */
2061 if (event->state & GDK_CONTROL_MASK)
2063 if ((key >= 'A') && (key <= 'Z'))
2066 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2068 (* control_keys[(int) (key - 'a')]) (old_editable, event->time);
2074 else if (event->state & GDK_MOD1_MASK)
2076 if ((key >= 'A') && (key <= 'Z'))
2079 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2081 (* alt_keys[(int) (key - 'a')]) (old_editable, event->time);
2087 else if (event->length > 0)
2089 extend_selection = FALSE;
2091 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
2092 position = text->point.index;
2093 gtk_editable_insert_text (GTK_EDITABLE (old_editable), event->string, event->length, &position);
2101 if (return_val && (old_editable->current_pos != initial_pos))
2103 if (extend_selection)
2105 if (old_editable->current_pos < old_editable->selection_start_pos)
2106 gtk_text_set_selection (old_editable, old_editable->current_pos,
2107 old_editable->selection_end_pos);
2108 else if (old_editable->current_pos > old_editable->selection_end_pos)
2109 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2110 old_editable->current_pos);
2114 gtk_text_set_selection (old_editable, old_editable->current_pos,
2115 old_editable->selection_end_pos);
2117 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2118 old_editable->current_pos);
2122 gtk_text_set_selection (old_editable, 0, 0);
2124 gtk_old_editable_claim_selection (old_editable,
2125 old_editable->selection_start_pos != old_editable->selection_end_pos,
2134 gtk_text_focus_in (GtkWidget *widget,
2135 GdkEventFocus *event)
2137 g_return_val_if_fail (widget != NULL, FALSE);
2138 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
2139 g_return_val_if_fail (event != NULL, FALSE);
2141 TDEBUG (("in gtk_text_focus_in\n"));
2143 return (* GTK_WIDGET_CLASS (parent_class)->focus_in_event) (widget, event);
2147 gtk_text_focus_out (GtkWidget *widget,
2148 GdkEventFocus *event)
2150 g_return_val_if_fail (widget != NULL, FALSE);
2151 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
2152 g_return_val_if_fail (event != NULL, FALSE);
2154 TDEBUG (("in gtk_text_focus_out\n"));
2156 return (* GTK_WIDGET_CLASS (parent_class)->focus_out_event) (widget, event);
2160 gtk_text_adjustment (GtkAdjustment *adjustment,
2163 g_return_if_fail (adjustment != NULL);
2164 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2165 g_return_if_fail (text != NULL);
2166 g_return_if_fail (GTK_IS_TEXT (text));
2168 /* Just ignore it if we haven't been size-allocated and realized yet */
2169 if (text->line_start_cache == NULL)
2172 if (adjustment == text->hadj)
2174 g_warning ("horizontal scrolling not implemented");
2178 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2182 undraw_cursor (text, FALSE);
2185 scroll_down (text, diff);
2186 else /* if (diff < 0) */
2187 scroll_up (text, diff);
2189 draw_cursor (text, FALSE);
2191 text->last_ver_value = adjustment->value;
2197 gtk_text_disconnect (GtkAdjustment *adjustment,
2200 g_return_if_fail (adjustment != NULL);
2201 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2202 g_return_if_fail (text != NULL);
2203 g_return_if_fail (GTK_IS_TEXT (text));
2205 if (adjustment == text->hadj)
2206 gtk_text_set_adjustments (text, NULL, text->vadj);
2207 if (adjustment == text->vadj)
2208 gtk_text_set_adjustments (text, text->hadj, NULL);
2212 static GtkPropertyMark
2213 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2215 GtkPropertyMark mark;
2217 mark = find_mark_near (text, point_position, near);
2219 while (mark.index > 0 &&
2220 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2221 decrement_mark (&mark);
2227 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2229 tab_cont->pixel_offset = 0;
2230 tab_cont->tab_start.tab_stops = text->tab_stops;
2231 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2233 if (!tab_cont->tab_start.to_next_tab)
2234 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2238 line_params_iterate (GtkText* text,
2239 const GtkPropertyMark* mark0,
2240 const PrevTabCont* tab_mark0,
2243 LineIteratorFunction iter)
2244 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2245 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2246 * for each line following MARK, iteration continues unless ITER_CALL
2249 GtkPropertyMark mark = *mark0;
2250 PrevTabCont tab_conts[2];
2251 LineParams *lp, lpbuf;
2252 gint tab_cont_index = 0;
2255 tab_conts[0] = *tab_mark0;
2257 init_tab_cont (text, tab_conts);
2262 lp = g_chunk_new (LineParams, params_mem_chunk);
2266 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2267 tab_conts + (tab_cont_index + 1) % 2);
2269 if ((*iter) (text, lp, data))
2272 if (LAST_INDEX (text, lp->end))
2276 advance_mark (&mark);
2277 tab_cont_index = (tab_cont_index + 1) % 2;
2282 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2284 FetchLinesData *fldata = (FetchLinesData*) data;
2286 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2288 switch (fldata->fl_type)
2290 case FetchLinesCount:
2291 if (!text->line_wrap || !lp->wraps)
2294 if (fldata->data >= fldata->data_max)
2298 case FetchLinesPixels:
2300 fldata->data += LINE_HEIGHT(*lp);
2302 if (fldata->data >= fldata->data_max)
2312 fetch_lines (GtkText* text,
2313 const GtkPropertyMark* mark0,
2314 const PrevTabCont* tab_cont0,
2318 FetchLinesData fl_data;
2320 fl_data.new_lines = NULL;
2322 fl_data.data_max = data;
2323 fl_data.fl_type = fl_type;
2325 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2327 return g_list_reverse (fl_data.new_lines);
2331 fetch_lines_backward (GtkText* text)
2333 GList* new_lines = NULL, *new_line_start;
2334 GtkPropertyMark mark;
2336 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2339 mark = find_this_line_start_mark (text,
2340 CACHE_DATA(text->line_start_cache).start.index - 1,
2341 &CACHE_DATA(text->line_start_cache).start);
2343 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2345 while (new_line_start->next)
2346 new_line_start = new_line_start->next;
2348 new_line_start->next = text->line_start_cache;
2349 text->line_start_cache->prev = new_line_start;
2353 fetch_lines_forward (GtkText* text, gint line_count)
2355 GtkPropertyMark mark;
2356 GList* line = text->line_start_cache;
2361 mark = CACHE_DATA(line).end;
2363 if (LAST_INDEX (text, mark))
2366 advance_mark(&mark);
2368 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2371 line->next->prev = line;
2374 /* Compute the number of lines, and vertical pixels for n characters
2375 * starting from the point
2378 compute_lines_pixels (GtkText* text, guint char_count,
2379 guint *lines, guint *pixels)
2381 GList *line = text->current_line;
2382 gint chars_left = char_count;
2387 /* If chars_left == 0, that means we're joining two lines in a
2388 * deletion, so add in the values for the next line as well
2390 for (; line && chars_left >= 0; line = line->next)
2392 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2394 if (line == text->current_line)
2395 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2397 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2399 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2403 chars_left = 0; /* force another loop */
2406 fetch_lines_forward (text, 1);
2411 total_line_height (GtkText* text, GList* line, gint line_count)
2415 for (; line && line_count > 0; line = line->next)
2417 height += LINE_HEIGHT(CACHE_DATA(line));
2419 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2423 fetch_lines_forward (text, line_count);
2430 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2432 if (old == text->line_start_cache)
2436 for (; old_line_count > 0; old_line_count -= 1)
2438 while (text->line_start_cache &&
2440 CACHE_DATA(text->line_start_cache).wraps)
2441 remove_cache_line(text, text->line_start_cache);
2443 remove_cache_line(text, text->line_start_cache);
2446 last = g_list_last (new);
2448 last->next = text->line_start_cache;
2450 if (text->line_start_cache)
2451 text->line_start_cache->prev = last;
2453 text->line_start_cache = new;
2459 g_assert (old->prev);
2463 for (; old_line_count > 0; old_line_count -= 1)
2465 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2466 old = remove_cache_line (text, old);
2468 old = remove_cache_line (text, old);
2474 last = g_list_last (new);
2484 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2486 GList* cache = text->current_line;
2489 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2492 for (; cache; cache = cache->next)
2494 GtkPropertyMark *start = &CACHE_DATA(cache).start;
2495 GtkPropertyMark *end = &CACHE_DATA(cache).end;
2497 start->index -= nchars;
2498 end->index -= nchars;
2500 if (LAST_INDEX (text, text->point) &&
2501 start->index == text->point.index)
2502 *start = text->point;
2503 else if (start->property == text->point.property)
2504 start->offset = start->index - (text->point.index - text->point.offset);
2506 if (LAST_INDEX (text, text->point) &&
2507 end->index == text->point.index)
2509 if (end->property == text->point.property)
2510 end->offset = end->index - (text->point.index - text->point.offset);
2512 /*TEXT_ASSERT_MARK(text, start, "start");*/
2513 /*TEXT_ASSERT_MARK(text, end, "end");*/
2518 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2520 GtkWidget *widget = GTK_WIDGET (text);
2523 guint new_pixels = 0;
2525 GList* new_line = NULL;
2528 text->cursor_virtual_x = 0;
2530 correct_cache_delete (text, nchars, old_lines);
2532 pixel_height = pixel_height_of(text, text->current_line) -
2533 LINE_HEIGHT(CACHE_DATA(text->current_line));
2535 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2536 CACHE_DATA(text->current_line).start = text->point;
2538 new_line = fetch_lines (text,
2539 &CACHE_DATA(text->current_line).start,
2540 &CACHE_DATA(text->current_line).tab_cont,
2544 swap_lines (text, text->current_line, new_line, old_lines);
2546 text->current_line = new_line;
2548 new_pixels = total_line_height (text, new_line, 1);
2550 gdk_window_get_size (text->text_area, &width, &height);
2552 if (old_pixels != new_pixels)
2554 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2556 gdk_draw_pixmap (text->text_area,
2560 pixel_height + old_pixels,
2562 pixel_height + new_pixels,
2566 text->vadj->upper += new_pixels;
2567 text->vadj->upper -= old_pixels;
2568 adjust_adj (text, text->vadj);
2572 rect.y = pixel_height;
2574 rect.height = new_pixels;
2576 expose_text (text, &rect, FALSE);
2577 gtk_text_draw_focus ( (GtkWidget *) text);
2579 text->cursor_mark = text->point;
2581 find_cursor (text, TRUE);
2583 if (old_pixels != new_pixels)
2585 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2588 rect.y = pixel_height + new_pixels;
2590 rect.height = height - rect.y;
2592 expose_text (text, &rect, FALSE);
2595 process_exposes (text);
2602 /* note, the point has already been moved forward */
2604 correct_cache_insert (GtkText* text, gint nchars)
2607 GtkPropertyMark *start;
2608 GtkPropertyMark *end;
2609 gboolean was_split = FALSE;
2611 /* We need to distinguish whether the property was split in the
2612 * insert or not, so we check if the point (which points after
2613 * the insertion here), points to the same character as the
2614 * point before. Ugh.
2618 GtkPropertyMark tmp_mark = text->point;
2619 move_mark_n (&tmp_mark, -1);
2621 if (tmp_mark.property != text->point.property)
2625 /* If we inserted a property exactly at the beginning of the
2626 * line, we have to correct here, or fetch_lines will
2629 start = &CACHE_DATA(text->current_line).start;
2631 /* Check if if we split exactly at the beginning of the line:
2632 * (was_split won't be set if we are inserting at the end of the text,
2633 * so we don't check)
2635 if (start->offset == MARK_CURRENT_PROPERTY (start)->length)
2636 SET_PROPERTY_MARK (start, start->property->next, 0);
2637 /* Check if we inserted a property at the beginning of the text: */
2638 else if (was_split &&
2639 (start->property == text->point.property) &&
2640 (start->index == text->point.index - nchars))
2641 SET_PROPERTY_MARK (start, start->property->prev, 0);
2643 /* Now correct the offsets, and check for start or end marks that
2644 * are after the point, yet point to a property before the point's
2645 * property. This indicates that they are meant to point to the
2646 * second half of a property we split in insert_text_property(), so
2647 * we fix them up that way.
2649 cache = text->current_line->next;
2651 for (; cache; cache = cache->next)
2653 start = &CACHE_DATA(cache).start;
2654 end = &CACHE_DATA(cache).end;
2656 if (LAST_INDEX (text, text->point) &&
2657 start->index == text->point.index)
2658 *start = text->point;
2659 else if (start->index >= text->point.index - nchars)
2661 if (!was_split && start->property == text->point.property)
2662 move_mark_n(start, nchars);
2665 if (start->property->next &&
2666 (start->property->next->next == text->point.property))
2668 g_assert (start->offset >= MARK_CURRENT_PROPERTY (start)->length);
2669 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2670 start->property = text->point.property;
2672 start->index += nchars;
2676 if (LAST_INDEX (text, text->point) &&
2677 end->index == text->point.index)
2679 if (end->index >= text->point.index - nchars)
2681 if (!was_split && end->property == text->point.property)
2682 move_mark_n(end, nchars);
2685 if (end->property->next &&
2686 (end->property->next->next == text->point.property))
2688 g_assert (end->offset >= MARK_CURRENT_PROPERTY (end)->length);
2689 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2690 end->property = text->point.property;
2692 end->index += nchars;
2696 /*TEXT_ASSERT_MARK(text, start, "start");*/
2697 /*TEXT_ASSERT_MARK(text, end, "end");*/
2703 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2704 guint new_line_count)
2706 GtkWidget *widget = GTK_WIDGET (text);
2709 guint new_pixels = 0;
2711 GList* new_lines = NULL;
2714 text->cursor_virtual_x = 0;
2716 undraw_cursor (text, FALSE);
2718 correct_cache_insert (text, nchars);
2720 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2722 pixel_height = pixel_height_of(text, text->current_line) -
2723 LINE_HEIGHT(CACHE_DATA(text->current_line));
2725 new_lines = fetch_lines (text,
2726 &CACHE_DATA(text->current_line).start,
2727 &CACHE_DATA(text->current_line).tab_cont,
2731 swap_lines (text, text->current_line, new_lines, 1);
2733 text->current_line = new_lines;
2735 new_pixels = total_line_height (text, new_lines, new_line_count);
2737 gdk_window_get_size (text->text_area, &width, &height);
2739 if (old_pixels != new_pixels)
2741 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2743 gdk_draw_pixmap (text->text_area,
2747 pixel_height + old_pixels,
2749 pixel_height + new_pixels,
2751 height + (old_pixels - new_pixels) - pixel_height);
2754 text->vadj->upper += new_pixels;
2755 text->vadj->upper -= old_pixels;
2756 adjust_adj (text, text->vadj);
2760 rect.y = pixel_height;
2762 rect.height = new_pixels;
2764 expose_text (text, &rect, FALSE);
2765 gtk_text_draw_focus ( (GtkWidget *) text);
2767 text->cursor_mark = text->point;
2769 find_cursor (text, TRUE);
2771 draw_cursor (text, FALSE);
2773 if (old_pixels != new_pixels)
2775 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2778 rect.y = pixel_height + new_pixels;
2780 rect.height = height - rect.y;
2782 expose_text (text, &rect, FALSE);
2785 process_exposes (text);
2788 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2793 /* Text property functions */
2796 font_hash (gconstpointer font)
2798 return gdk_font_id ((const GdkFont*) font);
2801 static GHashTable *font_cache_table = NULL;
2804 get_text_font (GdkFont* gfont)
2809 if (!font_cache_table)
2810 font_cache_table = g_hash_table_new (font_hash, (GEqualFunc) gdk_font_equal);
2812 tf = g_hash_table_lookup (font_cache_table, gfont);
2820 tf = g_new (GtkTextFont, 1);
2823 tf->gdk_font = gfont;
2824 gdk_font_ref (gfont);
2826 for(i = 0; i < 256; i += 1)
2827 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2829 g_hash_table_insert (font_cache_table, gfont, tf);
2835 text_font_unref (GtkTextFont *text_font)
2837 text_font->ref_count--;
2838 if (text_font->ref_count == 0)
2840 g_hash_table_remove (font_cache_table, text_font->gdk_font);
2841 gdk_font_unref (text_font->gdk_font);
2847 text_properties_equal (TextProperty* prop, GdkFont* font, GdkColor *fore, GdkColor *back)
2849 if (prop->flags & PROPERTY_FONT)
2852 GtkTextFont *text_font;
2857 text_font = get_text_font (font);
2859 retval = (prop->font == text_font);
2860 text_font_unref (text_font);
2869 if (prop->flags & PROPERTY_FOREGROUND)
2871 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
2878 if (prop->flags & PROPERTY_BACKGROUND)
2880 if (!back || !gdk_color_equal (&prop->back_color, back))
2891 realize_property (GtkText *text, TextProperty *prop)
2893 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2895 if (prop->flags & PROPERTY_FOREGROUND)
2896 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
2898 if (prop->flags & PROPERTY_BACKGROUND)
2899 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
2903 realize_properties (GtkText *text)
2905 GList *tmp_list = text->text_properties;
2909 realize_property (text, tmp_list->data);
2911 tmp_list = tmp_list->next;
2916 unrealize_property (GtkText *text, TextProperty *prop)
2918 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2920 if (prop->flags & PROPERTY_FOREGROUND)
2921 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
2923 if (prop->flags & PROPERTY_BACKGROUND)
2924 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
2928 unrealize_properties (GtkText *text)
2930 GList *tmp_list = text->text_properties;
2934 unrealize_property (text, tmp_list->data);
2936 tmp_list = tmp_list->next;
2940 static TextProperty*
2941 new_text_property (GtkText *text, GdkFont *font, GdkColor* fore,
2942 GdkColor* back, guint length)
2946 if (text_property_chunk == NULL)
2948 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
2949 sizeof(TextProperty),
2950 1024*sizeof(TextProperty),
2954 prop = g_chunk_new(TextProperty, text_property_chunk);
2959 prop->flags |= PROPERTY_FONT;
2960 prop->font = get_text_font (font);
2967 prop->flags |= PROPERTY_FOREGROUND;
2968 prop->fore_color = *fore;
2973 prop->flags |= PROPERTY_BACKGROUND;
2974 prop->back_color = *back;
2977 prop->length = length;
2979 if (GTK_WIDGET_REALIZED (text))
2980 realize_property (text, prop);
2986 destroy_text_property (TextProperty *prop)
2989 text_font_unref (prop->font);
2991 g_mem_chunk_free (text_property_chunk, prop);
2994 /* Flop the memory between the point and the gap around like a
2997 move_gap (GtkText* text, guint index)
2999 if (text->gap_position < index)
3001 gint diff = index - text->gap_position;
3003 if (text->use_wchar)
3004 g_memmove (text->text.wc + text->gap_position,
3005 text->text.wc + text->gap_position + text->gap_size,
3006 diff*sizeof (GdkWChar));
3008 g_memmove (text->text.ch + text->gap_position,
3009 text->text.ch + text->gap_position + text->gap_size,
3012 text->gap_position = index;
3014 else if (text->gap_position > index)
3016 gint diff = text->gap_position - index;
3018 if (text->use_wchar)
3019 g_memmove (text->text.wc + index + text->gap_size,
3020 text->text.wc + index,
3021 diff*sizeof (GdkWChar));
3023 g_memmove (text->text.ch + index + text->gap_size,
3024 text->text.ch + index,
3027 text->gap_position = index;
3031 /* Increase the gap size. */
3033 make_forward_space (GtkText* text, guint len)
3035 if (text->gap_size < len)
3037 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3039 if (sum >= text->text_len)
3043 while (i <= sum) i <<= 1;
3045 if (text->use_wchar)
3046 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3047 i*sizeof(GdkWChar));
3049 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3053 if (text->use_wchar)
3054 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3055 text->text.wc + text->gap_position + text->gap_size,
3056 (text->text_end - (text->gap_position + text->gap_size))
3059 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3060 text->text.ch + text->gap_position + text->gap_size,
3061 text->text_end - (text->gap_position + text->gap_size));
3063 text->text_end += len*2;
3064 text->gap_size += len*2;
3068 /* Inserts into the text property list a list element that guarantees
3069 * that for len characters following the point, text has the correct
3070 * property. does not move point. adjusts text_properties_point and
3071 * text_properties_point_offset relative to the current value of
3074 insert_text_property (GtkText* text, GdkFont* font,
3075 GdkColor *fore, GdkColor* back, guint len)
3077 GtkPropertyMark *mark = &text->point;
3078 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3079 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3081 if (MARK_OFFSET(mark) == 0)
3083 /* Point is on the boundary of two properties.
3084 * If it is the same as either, grow, else insert
3087 if (text_properties_equal(forward_prop, font, fore, back))
3089 /* Grow the property in front of us. */
3091 MARK_PROPERTY_LENGTH(mark) += len;
3093 else if (backward_prop &&
3094 text_properties_equal(backward_prop, font, fore, back))
3096 /* Grow property behind us, point property and offset
3099 SET_PROPERTY_MARK (&text->point,
3100 MARK_PREV_LIST_PTR (mark),
3101 backward_prop->length);
3103 backward_prop->length += len;
3105 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3106 (forward_prop->length == 1))
3108 /* Next property just has last position, take it over */
3110 if (GTK_WIDGET_REALIZED (text))
3111 unrealize_property (text, forward_prop);
3113 forward_prop->flags = 0;
3116 forward_prop->flags |= PROPERTY_FONT;
3117 forward_prop->font = get_text_font (font);
3120 forward_prop->font = NULL;
3124 forward_prop->flags |= PROPERTY_FOREGROUND;
3125 forward_prop->fore_color = *fore;
3129 forward_prop->flags |= PROPERTY_BACKGROUND;
3130 forward_prop->back_color = *back;
3132 forward_prop->length += len;
3134 if (GTK_WIDGET_REALIZED (text))
3135 realize_property (text, forward_prop);
3139 /* Splice a new property into the list. */
3141 GList* new_prop = g_list_alloc();
3143 new_prop->next = MARK_LIST_PTR(mark);
3144 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3145 new_prop->next->prev = new_prop;
3148 new_prop->prev->next = new_prop;
3150 new_prop->data = new_text_property (text, font, fore, back, len);
3152 SET_PROPERTY_MARK (mark, new_prop, 0);
3157 /* The following will screw up the line_start cache,
3158 * we'll fix it up in correct_cache_insert
3161 /* In the middle of forward_prop, if properties are equal,
3162 * just add to its length, else split it into two and splice
3164 if (text_properties_equal (forward_prop, font, fore, back))
3166 forward_prop->length += len;
3168 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3169 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3171 /* Inserting before only the last position in the text */
3174 forward_prop->length -= 1;
3176 new_prop = g_list_alloc();
3177 new_prop->data = new_text_property (text, font, fore, back, len+1);
3178 new_prop->prev = MARK_LIST_PTR(mark);
3179 new_prop->next = NULL;
3180 MARK_NEXT_LIST_PTR(mark) = new_prop;
3182 SET_PROPERTY_MARK (mark, new_prop, 0);
3186 GList* new_prop = g_list_alloc();
3187 GList* new_prop_forward = g_list_alloc();
3188 gint old_length = forward_prop->length;
3189 GList* next = MARK_NEXT_LIST_PTR(mark);
3191 /* Set the new lengths according to where they are split. Construct
3192 * two new properties. */
3193 forward_prop->length = MARK_OFFSET(mark);
3195 new_prop_forward->data =
3196 new_text_property(text,
3197 forward_prop->flags & PROPERTY_FONT ?
3198 forward_prop->font->gdk_font : NULL,
3199 forward_prop->flags & PROPERTY_FOREGROUND ?
3200 &forward_prop->fore_color : NULL,
3201 forward_prop->flags & PROPERTY_BACKGROUND ?
3202 &forward_prop->back_color : NULL,
3203 old_length - forward_prop->length);
3205 new_prop->data = new_text_property(text, font, fore, back, len);
3207 /* Now splice things in. */
3208 MARK_NEXT_LIST_PTR(mark) = new_prop;
3209 new_prop->prev = MARK_LIST_PTR(mark);
3211 new_prop->next = new_prop_forward;
3212 new_prop_forward->prev = new_prop;
3214 new_prop_forward->next = next;
3217 next->prev = new_prop_forward;
3219 SET_PROPERTY_MARK (mark, new_prop, 0);
3223 while (text->text_properties_end->next)
3224 text->text_properties_end = text->text_properties_end->next;
3226 while (text->text_properties->prev)
3227 text->text_properties = text->text_properties->prev;
3231 delete_text_property (GtkText* text, guint nchars)
3233 /* Delete nchars forward from point. */
3235 /* Deleting text properties is problematical, because we
3236 * might be storing around marks pointing to a property.
3238 * The marks in question and how we handle them are:
3240 * point: We know the new value, since it will be at the
3241 * end of the deleted text, and we move it there
3243 * cursor: We just remove the mark and set it equal to the
3244 * point after the operation.
3245 * line-start cache: We replace most affected lines.
3246 * The current line gets used to fetch the new
3247 * lines so, if necessary, (delete at the beginning
3248 * of a line) we fix it up by setting it equal to the
3256 for(; nchars; nchars -= 1)
3258 prop = MARK_CURRENT_PROPERTY(&text->point);
3262 if (prop->length == 0)
3264 tmp = MARK_LIST_PTR (&text->point);
3266 is_first = tmp == text->text_properties;
3268 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3269 text->point.offset = 0;
3271 if (GTK_WIDGET_REALIZED (text))
3272 unrealize_property (text, prop);
3274 destroy_text_property (prop);
3275 g_list_free_1 (tmp);
3277 prop = MARK_CURRENT_PROPERTY (&text->point);
3280 text->text_properties = MARK_LIST_PTR (&text->point);
3282 g_assert (prop->length != 0);
3284 else if (prop->length == text->point.offset)
3286 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3287 text->point.offset = 0;
3291 /* Check to see if we have just the single final position remaining
3292 * along in a property; if so, combine it with the previous property
3294 if (LAST_INDEX (text, text->point) &&
3295 (MARK_OFFSET (&text->point) == 0) &&
3296 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3298 tmp = MARK_LIST_PTR (&text->point);
3299 prop = MARK_CURRENT_PROPERTY(&text->point);
3301 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3302 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3303 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3305 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3307 if (GTK_WIDGET_REALIZED (text))
3308 unrealize_property (text, prop);
3310 destroy_text_property (prop);
3311 g_list_free_1 (tmp);
3316 init_properties (GtkText *text)
3318 if (!text->text_properties)
3320 text->text_properties = g_list_alloc();
3321 text->text_properties->next = NULL;
3322 text->text_properties->prev = NULL;
3323 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3324 text->text_properties_end = text->text_properties;
3326 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3328 text->point.index = 0;
3333 /**********************************************************************/
3334 /* Property Movement */
3335 /**********************************************************************/
3338 move_mark_n (GtkPropertyMark* mark, gint n)
3341 advance_mark_n(mark, n);
3343 decrement_mark_n(mark, -n);
3347 advance_mark (GtkPropertyMark* mark)
3349 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3353 if (prop->length > mark->offset + 1)
3357 mark->property = MARK_NEXT_LIST_PTR (mark);
3363 advance_mark_n (GtkPropertyMark* mark, gint n)
3370 i = 0; /* otherwise it migth not be init. */
3371 prop = MARK_CURRENT_PROPERTY(mark);
3373 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3374 /* to make it easier */
3375 n += (mark->offset);
3376 mark->index -= mark->offset;
3378 /* first we take seven-mile-leaps to get to the right text
3380 while ((n-i) > prop->length - 1) {
3382 mark->index += prop->length;
3383 mark->property = MARK_NEXT_LIST_PTR (mark);
3384 prop = MARK_CURRENT_PROPERTY (mark);
3388 /* and then the rest */
3389 mark->index += n - i;
3390 mark->offset += n - i;
3394 decrement_mark (GtkPropertyMark* mark)
3398 if (mark->offset > 0)
3402 mark->property = MARK_PREV_LIST_PTR (mark);
3403 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3408 decrement_mark_n (GtkPropertyMark* mark, gint n)
3412 while (mark->offset < n) {
3413 /* jump to end of prev */
3414 n -= mark->offset + 1;
3415 mark->index -= mark->offset + 1;
3416 mark->property = MARK_PREV_LIST_PTR (mark);
3417 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3425 static GtkPropertyMark
3426 find_mark (GtkText* text, guint mark_position)
3428 return find_mark_near (text, mark_position, &text->point);
3432 * You can also start from the end, what a drag.
3434 static GtkPropertyMark
3435 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3440 GtkPropertyMark mark;
3443 diffa = mark_position + 1;
3445 diffa = mark_position - near->index;
3447 diffb = mark_position;
3459 mark.property = text->text_properties;
3463 move_mark_n (&mark, mark_position - mark.index);
3468 /* This routine must be called with scroll == FALSE, only when
3469 * point is at least partially on screen
3473 find_line_containing_point (GtkText* text, guint point,
3479 text->current_line = NULL;
3483 /* Scroll backwards until the point is on screen
3485 while (CACHE_DATA(text->line_start_cache).start.index > point)
3486 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3488 /* Now additionally try to make sure that the point is fully on screen
3492 while (text->first_cut_pixels != 0 &&
3493 text->line_start_cache->next &&
3494 CACHE_DATA(text->line_start_cache->next).start.index > point)
3495 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3498 gdk_window_get_size (text->text_area, NULL, &height);
3500 for (cache = text->line_start_cache; cache; cache = cache->next)
3504 if (CACHE_DATA(cache).end.index >= point ||
3505 LAST_INDEX(text, CACHE_DATA(cache).end))
3507 text->current_line = cache; /* LOOK HERE, this proc has an
3508 * important side effect. */
3512 TEXT_SHOW_LINE (text, cache, "cache");
3514 if (cache->next == NULL)
3515 fetch_lines_forward (text, 1);
3519 lph = pixel_height_of (text, cache->next);
3521 /* Scroll the bottom of the line is on screen, or until
3522 * the line is the first onscreen line.
3524 while (cache->next != text->line_start_cache && lph > height)
3526 TEXT_SHOW_LINE (text, cache, "cache");
3527 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3528 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3529 lph = pixel_height_of (text, cache->next);
3534 g_assert_not_reached (); /* Must set text->current_line here */
3538 pixel_height_of (GtkText* text, GList* cache_line)
3540 gint pixels = - text->first_cut_pixels;
3541 GList *cache = text->line_start_cache;
3544 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3546 if (cache->data == cache_line->data)
3549 cache = cache->next;
3555 /**********************************************************************/
3556 /* Search and Placement */
3557 /**********************************************************************/
3560 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3563 gint16* char_widths;
3565 if (LAST_INDEX (text, *mark))
3568 ch = GTK_TEXT_INDEX (text, mark->index);
3569 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3573 return tab_mark->to_next_tab * char_widths[' '];
3577 return char_widths[ch];
3581 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3586 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, GdkWChar ch)
3588 if (tab_mark->to_next_tab == 1 || ch == '\t')
3590 if (tab_mark->tab_stops->next)
3592 tab_mark->tab_stops = tab_mark->tab_stops->next;
3593 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3597 tab_mark->to_next_tab = text->default_tab_width;
3602 tab_mark->to_next_tab -= 1;
3607 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3611 advance_tab_mark (text, tab_mark, 0);
3615 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3618 GtkOldEditable *old_editable = (GtkOldEditable *)text;
3620 GtkPropertyMark mark = start_line->start;
3621 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3622 gint pixel_width = LINE_START_PIXEL (*start_line);
3624 while (mark.index < text->cursor_mark.index)
3626 pixel_width += find_char_width (text, &mark, &tab_mark);
3628 advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3629 advance_mark (&mark);
3632 text->cursor_pos_x = pixel_width;
3633 text->cursor_pos_y = pixel_height;
3634 text->cursor_char_offset = start_line->font_descent;
3635 text->cursor_mark = mark;
3637 ch = LAST_INDEX (text, mark) ?
3638 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3640 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3641 text->cursor_char = 0;
3643 text->cursor_char = ch;
3647 find_cursor (GtkText* text, gboolean scroll)
3649 if (GTK_WIDGET_REALIZED (text))
3651 find_line_containing_point (text, text->cursor_mark.index, scroll);
3653 if (text->current_line)
3654 find_cursor_at_line (text,
3655 &CACHE_DATA(text->current_line),
3656 pixel_height_of(text, text->current_line));
3659 GTK_OLD_EDITABLE (text)->current_pos = text->cursor_mark.index;
3663 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3664 guint line_pixel_height,
3667 GtkPropertyMark mark = lp->start;
3668 TabStopMark tab_mark = lp->tab_cont.tab_start;
3670 gint char_width = find_char_width(text, &mark, &tab_mark);
3671 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3673 text->cursor_pos_y = line_pixel_height;
3677 GdkWChar ch = LAST_INDEX (text, mark) ?
3678 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3680 if (button_x < pixel_width || mark.index == lp->end.index)
3682 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3683 text->cursor_mark = mark;
3684 text->cursor_char_offset = lp->font_descent;
3686 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3687 text->cursor_char = 0;
3689 text->cursor_char = ch;
3694 advance_tab_mark (text, &tab_mark, ch);
3695 advance_mark (&mark);
3697 pixel_width += char_width/2;
3699 char_width = find_char_width (text, &mark, &tab_mark);
3701 pixel_width += (char_width+1)/2;
3706 find_mouse_cursor (GtkText* text, gint x, gint y)
3709 GList* cache = text->line_start_cache;
3713 pixel_height = - text->first_cut_pixels;
3715 for (; cache; cache = cache->next)
3717 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3719 if (y < pixel_height || !cache->next)
3721 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3723 find_cursor (text, FALSE);
3730 /**********************************************************************/
3732 /**********************************************************************/
3735 free_cache (GtkText* text)
3737 GList* cache = text->line_start_cache;
3742 cache = cache->prev;
3744 text->line_start_cache = cache;
3747 for (; cache; cache = cache->next)
3748 g_mem_chunk_free (params_mem_chunk, cache->data);
3750 g_list_free (text->line_start_cache);
3752 text->line_start_cache = NULL;
3756 remove_cache_line (GtkText* text, GList* member)
3763 if (member == text->line_start_cache)
3764 text->line_start_cache = text->line_start_cache->next;
3767 member->prev->next = member->next;
3770 member->next->prev = member->prev;
3772 list = member->next;
3774 g_mem_chunk_free (params_mem_chunk, member->data);
3775 g_list_free_1 (member);
3780 /**********************************************************************/
3782 /**********************************************************************/
3785 move_cursor_buffer_ver (GtkText *text, int dir)
3787 undraw_cursor (text, FALSE);
3791 scroll_int (text, text->vadj->upper);
3792 text->cursor_mark = find_this_line_start_mark (text,
3794 &text->cursor_mark);
3798 scroll_int (text, - text->vadj->value);
3799 text->cursor_mark = find_this_line_start_mark (text,
3801 &text->cursor_mark);
3804 find_cursor (text, TRUE);
3805 draw_cursor (text, FALSE);
3809 move_cursor_page_ver (GtkText *text, int dir)
3811 scroll_int (text, dir * text->vadj->page_increment);
3815 move_cursor_ver (GtkText *text, int count)
3818 GtkPropertyMark mark;
3821 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3822 offset = text->cursor_mark.index - mark.index;
3824 if (offset > text->cursor_virtual_x)
3825 text->cursor_virtual_x = offset;
3829 if (mark.index == 0)
3832 decrement_mark (&mark);
3833 mark = find_this_line_start_mark (text, mark.index, &mark);
3837 mark = text->cursor_mark;
3839 while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3840 advance_mark (&mark);
3842 if (LAST_INDEX(text, mark))
3845 advance_mark (&mark);
3848 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3849 if (LAST_INDEX(text, mark) ||
3850 GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3853 undraw_cursor (text, FALSE);
3855 text->cursor_mark = mark;
3857 find_cursor (text, TRUE);
3859 draw_cursor (text, FALSE);
3863 move_cursor_hor (GtkText *text, int count)
3865 /* count should be +-1. */
3866 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3867 (count < 0 && text->cursor_mark.index < (- count)) ||
3871 text->cursor_virtual_x = 0;
3873 undraw_cursor (text, FALSE);
3875 move_mark_n (&text->cursor_mark, count);
3877 find_cursor (text, TRUE);
3879 draw_cursor (text, FALSE);
3883 gtk_text_move_cursor (GtkOldEditable *old_editable,
3890 move_cursor_hor (GTK_TEXT (old_editable), 1);
3895 move_cursor_hor (GTK_TEXT (old_editable), -1);
3901 move_cursor_ver (GTK_TEXT (old_editable), 1);
3906 move_cursor_ver (GTK_TEXT (old_editable), -1);
3911 gtk_text_move_forward_character (GtkText *text)
3913 move_cursor_hor (text, 1);
3917 gtk_text_move_backward_character (GtkText *text)
3919 move_cursor_hor (text, -1);
3923 gtk_text_move_next_line (GtkText *text)
3925 move_cursor_ver (text, 1);
3929 gtk_text_move_previous_line (GtkText *text)
3931 move_cursor_ver (text, -1);
3935 gtk_text_move_word (GtkOldEditable *old_editable,
3941 gtk_text_move_forward_word (GTK_TEXT (old_editable));
3946 gtk_text_move_backward_word (GTK_TEXT (old_editable));
3951 gtk_text_move_forward_word (GtkText *text)
3953 text->cursor_virtual_x = 0;
3955 undraw_cursor (text, FALSE);
3957 if (text->use_wchar)
3959 while (!LAST_INDEX (text, text->cursor_mark) &&
3960 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3961 advance_mark (&text->cursor_mark);
3963 while (!LAST_INDEX (text, text->cursor_mark) &&
3964 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3965 advance_mark (&text->cursor_mark);
3969 while (!LAST_INDEX (text, text->cursor_mark) &&
3970 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3971 advance_mark (&text->cursor_mark);
3973 while (!LAST_INDEX (text, text->cursor_mark) &&
3974 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3975 advance_mark (&text->cursor_mark);
3978 find_cursor (text, TRUE);
3979 draw_cursor (text, FALSE);
3983 gtk_text_move_backward_word (GtkText *text)
3985 text->cursor_virtual_x = 0;
3987 undraw_cursor (text, FALSE);
3989 if (text->use_wchar)
3991 while ((text->cursor_mark.index > 0) &&
3992 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3993 decrement_mark (&text->cursor_mark);
3995 while ((text->cursor_mark.index > 0) &&
3996 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3997 decrement_mark (&text->cursor_mark);
4001 while ((text->cursor_mark.index > 0) &&
4002 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4003 decrement_mark (&text->cursor_mark);
4005 while ((text->cursor_mark.index > 0) &&
4006 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4007 decrement_mark (&text->cursor_mark);
4010 find_cursor (text, TRUE);
4011 draw_cursor (text, FALSE);
4015 gtk_text_move_page (GtkOldEditable *old_editable,
4020 scroll_int (GTK_TEXT (old_editable),
4021 y * GTK_TEXT(old_editable)->vadj->page_increment);
4025 gtk_text_move_to_row (GtkOldEditable *old_editable,
4031 gtk_text_move_to_column (GtkOldEditable *old_editable,
4036 text = GTK_TEXT (old_editable);
4038 text->cursor_virtual_x = 0; /* FIXME */
4040 undraw_cursor (text, FALSE);
4042 /* Move to the beginning of the line */
4043 while ((text->cursor_mark.index > 0) &&
4044 (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4045 decrement_mark (&text->cursor_mark);
4047 while (!LAST_INDEX (text, text->cursor_mark) &&
4048 (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4052 else if (column == 0)
4055 advance_mark (&text->cursor_mark);
4058 find_cursor (text, TRUE);
4059 draw_cursor (text, FALSE);
4063 gtk_text_move_beginning_of_line (GtkText *text)
4065 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4070 gtk_text_move_end_of_line (GtkText *text)
4072 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), -1);
4076 gtk_text_kill_char (GtkOldEditable *old_editable,
4081 text = GTK_TEXT (old_editable);
4083 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4084 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4089 if (text->point.index + 1 <= TEXT_LENGTH (text))
4090 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index, text->point.index + 1);
4094 if (text->point.index > 0)
4095 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index - 1, text->point.index);
4101 gtk_text_delete_forward_character (GtkText *text)
4103 gtk_text_kill_char (GTK_OLD_EDITABLE (text), 1);
4107 gtk_text_delete_backward_character (GtkText *text)
4109 gtk_text_kill_char (GTK_OLD_EDITABLE (text), -1);
4113 gtk_text_kill_word (GtkOldEditable *old_editable,
4116 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4117 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4120 gint old_pos = old_editable->current_pos;
4123 gtk_text_move_word (old_editable, 1);
4124 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4128 gtk_text_move_word (old_editable, -1);
4129 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4135 gtk_text_delete_forward_word (GtkText *text)
4137 gtk_text_kill_word (GTK_OLD_EDITABLE (text), 1);
4141 gtk_text_delete_backward_word (GtkText *text)
4143 gtk_text_kill_word (GTK_OLD_EDITABLE (text), -1);
4147 gtk_text_kill_line (GtkOldEditable *old_editable,
4150 gint old_pos = old_editable->current_pos;
4153 gtk_text_move_to_column (old_editable, -1);
4154 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4158 gtk_text_move_to_column (old_editable, 0);
4159 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4164 gtk_text_delete_line (GtkText *text)
4166 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4167 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4171 gtk_text_delete_to_line_end (GtkText *text)
4173 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4177 gtk_text_select_word (GtkText *text, guint32 time)
4182 GtkOldEditable *old_editable;
4183 old_editable = GTK_OLD_EDITABLE (text);
4185 gtk_text_move_backward_word (text);
4186 start_pos = text->cursor_mark.index;
4188 gtk_text_move_forward_word (text);
4189 end_pos = text->cursor_mark.index;
4191 old_editable->has_selection = TRUE;
4192 gtk_text_set_selection (old_editable, start_pos, end_pos);
4193 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4197 gtk_text_select_line (GtkText *text, guint32 time)
4202 GtkOldEditable *old_editable;
4203 old_editable = GTK_OLD_EDITABLE (text);
4205 gtk_text_move_beginning_of_line (text);
4206 start_pos = text->cursor_mark.index;
4208 gtk_text_move_end_of_line (text);
4209 gtk_text_move_forward_character (text);
4210 end_pos = text->cursor_mark.index;
4212 old_editable->has_selection = TRUE;
4213 gtk_text_set_selection (old_editable, start_pos, end_pos);
4214 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4217 /**********************************************************************/
4219 /**********************************************************************/
4222 adjust_adj (GtkText* text, GtkAdjustment* adj)
4226 gdk_window_get_size (text->text_area, NULL, &height);
4228 adj->step_increment = MIN (adj->upper, (float) SCROLL_PIXELS);
4229 adj->page_increment = MIN (adj->upper, height - (float) KEY_SCROLL_PIXELS);
4230 adj->page_size = MIN (adj->upper, height);
4231 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4232 adj->value = MAX (adj->value, 0.0);
4234 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4238 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
4240 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4242 if ((text->first_line_start_index >= lp->start.index) &&
4243 (text->first_line_start_index <= lp->end.index))
4245 svdata->mark = lp->start;
4247 if (text->first_line_start_index == lp->start.index)
4249 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4253 text->first_onscreen_ver_pixel = svdata->pixel_height;
4254 text->first_cut_pixels = 0;
4257 text->vadj->value = (float) text->first_onscreen_ver_pixel;
4260 svdata->pixel_height += LINE_HEIGHT (*lp);
4266 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
4268 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4271 if (svdata->pixel_height <= (gint) text->vadj->value &&
4272 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4274 svdata->mark = lp->start;
4276 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4277 text->first_onscreen_ver_pixel = svdata->pixel_height;
4278 text->first_line_start_index = lp->start.index;
4284 svdata->pixel_height += LINE_HEIGHT (*lp);
4292 static GtkPropertyMark
4293 set_vertical_scroll (GtkText* text)
4295 GtkPropertyMark mark = find_mark (text, 0);
4296 SetVerticalScrollData data;
4300 data.pixel_height = 0;
4301 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4303 text->vadj->upper = (float) data.pixel_height;
4304 orig_value = (gint) text->vadj->value;
4306 gdk_window_get_size (text->text_area, NULL, &height);
4308 text->vadj->step_increment = MIN (text->vadj->upper, (float) SCROLL_PIXELS);
4309 text->vadj->page_increment = MIN (text->vadj->upper, height - (float) KEY_SCROLL_PIXELS);
4310 text->vadj->page_size = MIN (text->vadj->upper, height);
4311 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4312 text->vadj->value = MAX (text->vadj->value, 0.0);
4314 text->last_ver_value = (gint)text->vadj->value;
4316 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4318 if (text->vadj->value != orig_value)
4320 /* We got clipped, and don't really know which line to put first. */
4321 data.pixel_height = 0;
4322 data.last_didnt_wrap = TRUE;
4324 line_params_iterate (text, &mark, NULL,
4326 set_vertical_scroll_find_iterator);
4333 scroll_int (GtkText* text, gint diff)
4337 text->vadj->value += diff;
4339 upper = text->vadj->upper - text->vadj->page_size;
4340 text->vadj->value = MIN (text->vadj->value, upper);
4341 text->vadj->value = MAX (text->vadj->value, 0.0);
4343 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4347 process_exposes (GtkText *text)
4351 /* Make sure graphics expose events are processed before scrolling
4354 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4356 gtk_widget_send_expose (GTK_WIDGET (text), event);
4357 if (event->expose.count == 0)
4359 gdk_event_free (event);
4362 gdk_event_free (event);
4366 static gint last_visible_line_height (GtkText* text)
4368 GList *cache = text->line_start_cache;
4371 gdk_window_get_size (text->text_area, NULL, &height);
4373 for (; cache->next; cache = cache->next)
4374 if (pixel_height_of(text, cache->next) > height)
4378 return pixel_height_of(text, cache) - 1;
4383 static gint first_visible_line_height (GtkText* text)
4385 if (text->first_cut_pixels)
4386 return pixel_height_of(text, text->line_start_cache) + 1;
4392 scroll_down (GtkText* text, gint diff0)
4398 text->first_onscreen_ver_pixel += diff0;
4402 g_assert (text->line_start_cache);
4404 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4406 text->first_cut_pixels += 1;
4410 text->first_cut_pixels = 0;
4412 text->line_start_cache = text->line_start_cache->next;
4414 text->first_line_start_index =
4415 CACHE_DATA(text->line_start_cache).start.index;
4417 if (!text->line_start_cache->next)
4418 fetch_lines_forward (text, 1);
4424 gdk_window_get_size (text->text_area, &width, &height);
4425 if (height > real_diff)
4426 gdk_draw_pixmap (text->text_area,
4434 height - real_diff);
4437 rect.y = MAX (0, height - real_diff);
4439 rect.height = MIN (height, real_diff);
4441 expose_text (text, &rect, FALSE);
4442 gtk_text_draw_focus ( (GtkWidget *) text);
4444 if (text->current_line)
4448 text->cursor_pos_y -= real_diff;
4449 cursor_min = drawn_cursor_min(text);
4452 find_mouse_cursor (text, text->cursor_pos_x,
4453 first_visible_line_height (text));
4456 if (height > real_diff)
4457 process_exposes (text);
4461 scroll_up (GtkText* text, gint diff0)
4467 text->first_onscreen_ver_pixel += diff0;
4471 g_assert (text->line_start_cache);
4473 if (text->first_cut_pixels > 0)
4475 text->first_cut_pixels -= 1;
4479 if (!text->line_start_cache->prev)
4480 fetch_lines_backward (text);
4482 text->line_start_cache = text->line_start_cache->prev;
4484 text->first_line_start_index =
4485 CACHE_DATA(text->line_start_cache).start.index;
4487 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4493 gdk_window_get_size (text->text_area, &width, &height);
4494 if (height > real_diff)
4495 gdk_draw_pixmap (text->text_area,
4503 height - real_diff);
4508 rect.height = MIN (height, real_diff);
4510 expose_text (text, &rect, FALSE);
4511 gtk_text_draw_focus ( (GtkWidget *) text);
4513 if (text->current_line)
4518 text->cursor_pos_y += real_diff;
4519 cursor_max = drawn_cursor_max(text);
4520 gdk_window_get_size (text->text_area, NULL, &height);
4522 if (cursor_max >= height)
4523 find_mouse_cursor (text, text->cursor_pos_x,
4524 last_visible_line_height (text));
4527 if (height > real_diff)
4528 process_exposes (text);
4531 /**********************************************************************/
4533 /**********************************************************************/
4535 /* Assumes mark starts a line. Calculates the height, width, and
4536 * displayable character count of a single DISPLAYABLE line. That
4537 * means that in line-wrap mode, this does may not compute the
4538 * properties of an entire line. */
4540 find_line_params (GtkText* text,
4541 const GtkPropertyMark* mark,
4542 const PrevTabCont *tab_cont,
4543 PrevTabCont *next_cont)
4546 TabStopMark tab_mark = tab_cont->tab_start;
4547 guint max_display_pixels;
4552 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4553 max_display_pixels -= LINE_WRAP_ROOM;
4556 lp.tab_cont = *tab_cont;
4559 lp.pixel_width = tab_cont->pixel_offset;
4560 lp.displayable_chars = 0;
4562 lp.font_descent = 0;
4564 init_tab_cont (text, next_cont);
4566 while (!LAST_INDEX(text, lp.end))
4568 g_assert (lp.end.property);
4570 ch = GTK_TEXT_INDEX (text, lp.end.index);
4571 font = MARK_CURRENT_FONT (text, &lp.end);
4573 if (ch == LINE_DELIM)
4575 /* Newline doesn't count in computation of line height, even
4576 * if its in a bigger font than the rest of the line. Unless,
4577 * of course, there are no other characters. */
4579 if (!lp.font_ascent && !lp.font_descent)
4581 lp.font_ascent = font->ascent;
4582 lp.font_descent = font->descent;
4585 lp.tab_cont_next = *next_cont;
4590 ch_width = find_char_width (text, &lp.end, &tab_mark);
4592 if ((ch_width + lp.pixel_width > max_display_pixels) &&
4593 (lp.end.index > lp.start.index))
4597 if (text->line_wrap)
4599 next_cont->tab_start = tab_mark;
4600 next_cont->pixel_offset = 0;
4604 /* Here's the tough case, a tab is wrapping. */
4605 gint pixels_avail = max_display_pixels - lp.pixel_width;
4606 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
4607 gint spaces_avail = pixels_avail / space_width;
4609 if (spaces_avail == 0)
4611 decrement_mark (&lp.end);
4615 advance_tab_mark (text, &next_cont->tab_start, '\t');
4616 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4618 lp.displayable_chars += 1;
4623 if (text->word_wrap)
4625 GtkPropertyMark saved_mark = lp.end;
4626 guint saved_characters = lp.displayable_chars;
4628 lp.displayable_chars += 1;
4630 if (text->use_wchar)
4632 while (!gdk_iswspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4633 (lp.end.index > lp.start.index))
4635 decrement_mark (&lp.end);
4636 lp.displayable_chars -= 1;
4641 while (!isspace(GTK_TEXT_INDEX (text, lp.end.index)) &&
4642 (lp.end.index > lp.start.index))
4644 decrement_mark (&lp.end);
4645 lp.displayable_chars -= 1;
4649 /* If whole line is one word, revert to char wrapping */
4650 if (lp.end.index == lp.start.index)
4652 lp.end = saved_mark;
4653 lp.displayable_chars = saved_characters;
4654 decrement_mark (&lp.end);
4659 /* Don't include this character, it will wrap. */
4660 decrement_mark (&lp.end);
4664 lp.tab_cont_next = *next_cont;
4671 lp.displayable_chars += 1;
4674 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4675 lp.font_descent = MAX (font->descent, lp.font_descent);
4676 lp.pixel_width += ch_width;
4678 advance_mark(&lp.end);
4679 advance_tab_mark (text, &tab_mark, ch);
4682 if (LAST_INDEX(text, lp.start))
4684 /* Special case, empty last line. */
4685 font = MARK_CURRENT_FONT (text, &lp.end);
4687 lp.font_ascent = font->ascent;
4688 lp.font_descent = font->descent;
4691 lp.tab_cont_next = *next_cont;
4697 expand_scratch_buffer (GtkText* text, guint len)
4699 if (len >= text->scratch_buffer_len)
4703 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4705 if (text->use_wchar)
4707 if (text->scratch_buffer.wc)
4708 text->scratch_buffer.wc = g_new (GdkWChar, i);
4710 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
4711 i*sizeof (GdkWChar));
4715 if (text->scratch_buffer.ch)
4716 text->scratch_buffer.ch = g_new (guchar, i);
4718 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
4721 text->scratch_buffer_len = i;
4725 /* Side effect: modifies text->gc
4728 draw_bg_rect (GtkText* text, GtkPropertyMark *mark,
4729 gint x, gint y, gint width, gint height,
4730 gboolean already_cleared)
4732 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4734 if ((mark->index >= MIN(old_editable->selection_start_pos, old_editable->selection_end_pos) &&
4735 mark->index < MAX(old_editable->selection_start_pos, old_editable->selection_end_pos)))
4737 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
4738 old_editable->has_selection ?
4739 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
4741 NULL, GTK_WIDGET(text), "text",
4742 x, y, width, height);
4744 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
4745 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
4747 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
4749 gdk_draw_rectangle (text->text_area,
4751 TRUE, x, y, width, height);
4753 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4760 rect.height = height;
4762 clear_area (text, &rect);
4764 else if (!already_cleared)
4765 gdk_window_clear_area (text->text_area, x, y, width, height);
4769 draw_line (GtkText* text,
4770 gint pixel_start_height,
4773 GdkGCValues gc_values;
4776 guint running_offset = lp->tab_cont.pixel_offset;
4777 union { GdkWChar *wc; guchar *ch; } buffer;
4780 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4782 guint selection_start_pos = MIN (old_editable->selection_start_pos, old_editable->selection_end_pos);
4783 guint selection_end_pos = MAX (old_editable->selection_start_pos, old_editable->selection_end_pos);
4785 GtkPropertyMark mark = lp->start;
4786 TabStopMark tab_mark = lp->tab_cont.tab_start;
4787 gint pixel_height = pixel_start_height + lp->font_ascent;
4788 guint chars = lp->displayable_chars;
4790 /* First provide a contiguous segment of memory. This makes reading
4791 * the code below *much* easier, and only incurs the cost of copying
4792 * when the line being displayed spans the gap. */
4793 if (mark.index <= text->gap_position &&
4794 mark.index + chars > text->gap_position)
4796 expand_scratch_buffer (text, chars);
4798 if (text->use_wchar)
4800 for (i = 0; i < chars; i += 1)
4801 text->scratch_buffer.wc[i] = GTK_TEXT_INDEX(text, mark.index + i);
4802 buffer.wc = text->scratch_buffer.wc;
4806 for (i = 0; i < chars; i += 1)
4807 text->scratch_buffer.ch[i] = GTK_TEXT_INDEX(text, mark.index + i);
4808 buffer.ch = text->scratch_buffer.ch;
4813 if (text->use_wchar)
4815 if (mark.index >= text->gap_position)
4816 buffer.wc = text->text.wc + mark.index + text->gap_size;
4818 buffer.wc = text->text.wc + mark.index;
4822 if (mark.index >= text->gap_position)
4823 buffer.ch = text->text.ch + mark.index + text->gap_size;
4825 buffer.ch = text->text.ch + mark.index;
4830 if (running_offset > 0)
4832 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
4833 LINE_HEIGHT (*lp), TRUE);
4839 if ((text->use_wchar && buffer.wc[0] != '\t') ||
4840 (!text->use_wchar && buffer.ch[0] != '\t'))
4842 union { GdkWChar *wc; guchar *ch; } next_tab;
4847 if (text->use_wchar)
4848 for (i=0; i<chars; i++)
4850 if (buffer.wc[i] == '\t')
4852 next_tab.wc = buffer.wc + i;
4857 next_tab.ch = memchr (buffer.ch, '\t', chars);
4859 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4861 if (text->use_wchar)
4864 len = MIN (len, next_tab.wc - buffer.wc);
4869 len = MIN (len, next_tab.ch - buffer.ch);
4872 if (mark.index < selection_start_pos)
4873 len = MIN (len, selection_start_pos - mark.index);
4874 else if (mark.index < selection_end_pos)
4875 len = MIN (len, selection_end_pos - mark.index);
4877 font = MARK_CURRENT_FONT (text, &mark);
4878 if (font->type == GDK_FONT_FONT)
4880 gdk_gc_set_font (text->gc, font);
4881 gdk_gc_get_values (text->gc, &gc_values);
4882 if (text->use_wchar)
4883 pixel_width = gdk_text_width_wc (gc_values.font,
4886 pixel_width = gdk_text_width (gc_values.font,
4891 if (text->use_wchar)
4892 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
4894 pixel_width = gdk_text_width (font, buffer.ch, len);
4897 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4898 pixel_width, LINE_HEIGHT (*lp), TRUE);
4900 if ((mark.index >= selection_start_pos) &&
4901 (mark.index < selection_end_pos))
4903 if (old_editable->has_selection)
4904 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_SELECTED];
4906 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_ACTIVE];
4910 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
4914 if (text->use_wchar)
4915 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
4922 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
4929 running_offset += pixel_width;
4931 advance_tab_mark_n (text, &tab_mark, len);
4935 gint pixels_remaining;
4941 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
4942 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
4944 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4946 spaces_avail = pixels_remaining / space_width;
4947 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
4949 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4950 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
4952 running_offset += tab_mark.to_next_tab *
4953 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4955 advance_tab_mark (text, &tab_mark, '\t');
4958 advance_mark_n (&mark, len);
4959 if (text->use_wchar)
4968 draw_line_wrap (GtkText* text, guint height /* baseline height */)
4975 if (text->line_wrap)
4977 bitmap = text->line_wrap_bitmap;
4978 bitmap_width = line_wrap_width;
4979 bitmap_height = line_wrap_height;
4983 bitmap = text->line_arrow_bitmap;
4984 bitmap_width = line_arrow_width;
4985 bitmap_height = line_arrow_height;
4988 gdk_window_get_size (text->text_area, &width, NULL);
4989 width -= LINE_WRAP_ROOM;
4991 gdk_gc_set_stipple (text->gc,
4994 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
4996 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
4998 gdk_gc_set_ts_origin (text->gc,
5000 height - bitmap_height - 1);
5002 gdk_draw_rectangle (text->text_area,
5006 height - bitmap_height - 1 /* one pixel above the baseline. */,
5010 gdk_gc_set_ts_origin (text->gc, 0, 0);
5012 gdk_gc_set_fill (text->gc, GDK_SOLID);
5016 undraw_cursor (GtkText* text, gint absolute)
5018 GtkOldEditable *old_editable = (GtkOldEditable *) text;
5020 TDEBUG (("in undraw_cursor\n"));
5023 text->cursor_drawn_level = 0;
5025 if ((text->cursor_drawn_level ++ == 0) &&
5026 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5027 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5031 g_assert(text->cursor_mark.property);
5033 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5035 draw_bg_rect (text, &text->cursor_mark,
5037 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5038 1, font->ascent + 1, FALSE);
5040 if (text->cursor_char)
5042 if (font->type == GDK_FONT_FONT)
5043 gdk_gc_set_font (text->gc, font);
5045 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5047 gdk_draw_text_wc (text->text_area, font,
5050 text->cursor_pos_y - text->cursor_char_offset,
5058 drawn_cursor_min (GtkText* text)
5062 g_assert(text->cursor_mark.property);
5064 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5066 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5070 drawn_cursor_max (GtkText* text)
5074 g_assert(text->cursor_mark.property);
5076 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5078 return text->cursor_pos_y - text->cursor_char_offset;
5082 draw_cursor (GtkText* text, gint absolute)
5084 GtkOldEditable *old_editable = (GtkOldEditable *)text;
5086 TDEBUG (("in draw_cursor\n"));
5089 text->cursor_drawn_level = 1;
5091 if ((--text->cursor_drawn_level == 0) &&
5092 old_editable->editable &&
5093 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5094 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5098 g_assert (text->cursor_mark.property);
5100 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5102 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5104 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
5105 text->cursor_pos_y - text->cursor_char_offset,
5107 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5112 create_bg_gc (GtkText *text)
5116 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5117 values.fill = GDK_TILED;
5119 return gdk_gc_new_with_values (text->text_area, &values,
5120 GDK_GC_FILL | GDK_GC_TILE);
5124 clear_area (GtkText *text, GdkRectangle *area)
5126 GtkWidget *widget = GTK_WIDGET (text);
5132 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5134 gdk_gc_set_ts_origin (text->bg_gc,
5135 (- text->first_onscreen_hor_pixel) % width,
5136 (- text->first_onscreen_ver_pixel) % height);
5138 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5139 area->x, area->y, area->width, area->height);
5142 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5146 expose_text (GtkText* text, GdkRectangle *area, gboolean cursor)
5148 GList *cache = text->line_start_cache;
5149 gint pixels = - text->first_cut_pixels;
5150 gint min_y = MAX (0, area->y);
5151 gint max_y = MAX (0, area->y + area->height);
5154 gdk_window_get_size (text->text_area, NULL, &height);
5155 max_y = MIN (max_y, height);
5157 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5159 clear_area (text, area);
5161 for (; pixels < height; cache = cache->next)
5163 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5165 draw_line (text, pixels, &CACHE_DATA(cache));
5167 if (CACHE_DATA(cache).wraps)
5168 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5171 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5173 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5174 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5176 /* We undraw and draw the cursor here to get the drawn
5177 * level right ... FIXME - maybe the second parameter
5178 * of draw_cursor should work differently
5180 undraw_cursor (text, FALSE);
5181 draw_cursor (text, FALSE);
5185 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5189 fetch_lines_forward (text, 1);
5198 gtk_text_update_text (GtkOldEditable *old_editable,
5202 GtkText *text = GTK_TEXT (old_editable);
5204 GList *cache = text->line_start_cache;
5205 gint pixels = - text->first_cut_pixels;
5211 end_pos = TEXT_LENGTH (text);
5213 if (end_pos < start_pos)
5216 gdk_window_get_size (text->text_area, &width, &height);
5222 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5224 for (; pixels < height; cache = cache->next)
5226 if (CACHE_DATA(cache).start.index < end_pos)
5228 if (CACHE_DATA(cache).end.index >= start_pos)
5231 area.y = MAX(0,pixels);
5232 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5238 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5242 fetch_lines_forward (text, 1);
5250 expose_text (text, &area, TRUE);
5254 recompute_geometry (GtkText* text)
5256 GtkPropertyMark mark, start_mark;
5263 mark = start_mark = set_vertical_scroll (text);
5265 /* We need a real start of a line when calling fetch_lines().
5266 * not the start of a wrapped line.
5268 while (mark.index > 0 &&
5269 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5270 decrement_mark (&mark);
5272 gdk_window_get_size (text->text_area, &width, &height);
5274 /* Fetch an entire line, to make sure that we get all the text
5275 * we backed over above, in addition to enough text to fill up
5276 * the space vertically
5279 new_lines = fetch_lines (text,
5285 mark = CACHE_DATA (g_list_last (new_lines)).end;
5286 if (!LAST_INDEX (text, mark))
5288 advance_mark (&mark);
5290 new_lines = g_list_concat (new_lines,
5295 height + text->first_cut_pixels));
5298 /* Now work forward to the actual first onscreen line */
5300 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5301 new_lines = new_lines->next;
5303 text->line_start_cache = new_lines;
5305 find_cursor (text, TRUE);
5308 /**********************************************************************/
5310 /**********************************************************************/
5313 gtk_text_set_selection (GtkOldEditable *old_editable,
5317 GtkText *text = GTK_TEXT (old_editable);
5319 guint start1, end1, start2, end2;
5322 end = TEXT_LENGTH (text);
5324 start1 = MIN(start,end);
5325 end1 = MAX(start,end);
5326 start2 = MIN(old_editable->selection_start_pos, old_editable->selection_end_pos);
5327 end2 = MAX(old_editable->selection_start_pos, old_editable->selection_end_pos);
5329 if (start2 < start1)
5333 tmp = start1; start1 = start2; start2 = tmp;
5334 tmp = end1; end1 = end2; end2 = tmp;
5337 undraw_cursor (text, FALSE);
5338 old_editable->selection_start_pos = start;
5339 old_editable->selection_end_pos = end;
5340 draw_cursor (text, FALSE);
5342 /* Expose only what changed */
5344 if (start1 < start2)
5345 gtk_text_update_text (old_editable, start1, MIN(end1, start2));
5348 gtk_text_update_text (old_editable, MAX(end1, start2), end2);
5349 else if (end2 < end1)
5350 gtk_text_update_text (old_editable, end2, end1);
5354 /**********************************************************************/
5356 /**********************************************************************/
5358 #ifdef DEBUG_GTK_TEXT
5360 gtk_text_show_cache_line (GtkText *text, GList *cache,
5361 const char* what, const char* func, gint line)
5363 LineParams *lp = &CACHE_DATA(cache);
5366 if (cache == text->line_start_cache)
5367 g_message ("Line Start Cache: ");
5369 if (cache == text->current_line)
5370 g_message("Current Line: ");
5372 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
5380 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
5381 g_message ("%c", GTK_TEXT_INDEX (text, i));
5387 gtk_text_show_cache (GtkText *text, const char* func, gint line)
5389 GList *l = text->line_start_cache;
5395 /* back up to the absolute beginning of the line cache */
5399 g_message ("*** line cache ***\n");
5400 for (; l; l = l->next)
5401 gtk_text_show_cache_line (text, l, "all", func, line);
5405 gtk_text_assert_mark (GtkText *text,
5406 GtkPropertyMark *mark,
5407 GtkPropertyMark *before,
5408 GtkPropertyMark *after,
5413 GtkPropertyMark correct_mark = find_mark (text, mark->index);
5415 if (mark->offset != correct_mark.offset ||
5416 mark->property != correct_mark.property)
5417 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
5421 gtk_text_assert (GtkText *text,
5425 GList* cache = text->line_start_cache;
5426 GtkPropertyMark* before_mark = NULL;
5427 GtkPropertyMark* after_mark = NULL;
5429 gtk_text_show_props (text, msg, line);
5431 for (; cache->prev; cache = cache->prev)
5434 g_message ("*** line markers ***\n");
5436 for (; cache; cache = cache->next)
5438 after_mark = &CACHE_DATA(cache).end;
5439 gtk_text_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
5440 before_mark = &CACHE_DATA(cache).start;
5443 after_mark = &CACHE_DATA(cache->next).start;
5447 gtk_text_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
5448 before_mark = &CACHE_DATA(cache).end;
5453 gtk_text_show_adj (GtkText *text,
5459 g_message ("*** adjustment ***\n");
5461 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
5468 adj->step_increment,
5469 adj->page_increment,
5474 gtk_text_show_props (GtkText *text,
5478 GList* props = text->text_properties;
5481 g_message ("%s:%d: ", msg, line);
5483 for (; props; props = props->next)
5485 TextProperty *p = (TextProperty*)props->data;
5487 proplen += p->length;
5489 g_message ("[%d,%p,", p->length, p);
5490 if (p->flags & PROPERTY_FONT)
5491 g_message ("%p,", p->font);
5494 if (p->flags & PROPERTY_FOREGROUND)
5495 g_message ("%ld, ", p->fore_color.pixel);
5498 if (p->flags & PROPERTY_BACKGROUND)
5499 g_message ("%ld] ", p->back_color.pixel);
5506 if (proplen - 1 != TEXT_LENGTH(text))
5507 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);