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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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"
35 #include "line-wrap.xbm"
36 #include "line-arrow.xbm"
39 #define INITIAL_BUFFER_SIZE 1024
40 #define INITIAL_LINE_CACHE_SIZE 256
41 #define MIN_GAP_SIZE 256
42 #define LINE_DELIM '\n'
43 #define MIN_TEXT_WIDTH_LINES 20
44 #define MIN_TEXT_HEIGHT_LINES 10
45 #define TEXT_BORDER_ROOM 1
46 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
47 #define DEFAULT_TAB_STOP_WIDTH 4
48 #define SCROLL_PIXELS 5
49 #define KEY_SCROLL_PIXELS 10
50 #define SCROLL_TIME 100
51 #define FREEZE_LENGTH 1024
52 /* Freeze text when inserting or deleting more than this many characters */
54 #define SET_PROPERTY_MARK(m, p, o) do { \
55 (m)->property = (p); \
58 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
59 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
60 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
61 (mark)->property->prev->data \
63 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
64 #define MARK_LIST_PTR(mark) ((mark)->property)
65 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
66 #define MARK_OFFSET(mark) ((mark)->offset)
67 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
70 #define MARK_CURRENT_FONT(text, mark) \
71 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
72 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
73 GTK_WIDGET (text)->style->font)
74 #define MARK_CURRENT_FORE(text, mark) \
75 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
76 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
77 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
78 #define MARK_CURRENT_BACK(text, mark) \
79 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
80 &MARK_CURRENT_PROPERTY(mark)->back_color : \
81 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
82 #define MARK_CURRENT_TEXT_FONT(text, mark) \
83 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
84 MARK_CURRENT_PROPERTY(mark)->font : \
87 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
88 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
89 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
90 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
91 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
92 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
93 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
94 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
104 typedef struct _TextProperty TextProperty;
105 typedef struct _TabStopMark TabStopMark;
106 typedef struct _PrevTabCont PrevTabCont;
107 typedef struct _FetchLinesData FetchLinesData;
108 typedef struct _LineParams LineParams;
109 typedef struct _SetVerticalScrollData SetVerticalScrollData;
111 typedef gint (*LineIteratorFunction) (GtkText* text, LineParams* lp, void* data);
119 struct _SetVerticalScrollData {
121 gint last_didnt_wrap;
122 gint last_line_start;
123 GtkPropertyMark mark;
128 /* The actual font. */
132 gint16 char_widths[256];
136 PROPERTY_FONT = 1 << 0,
137 PROPERTY_FOREGROUND = 1 << 1,
138 PROPERTY_BACKGROUND = 1 << 2
146 /* Background Color. */
149 /* Foreground Color. */
152 /* Show which properties are set */
153 TextPropertyFlags flags;
155 /* Length of this property. */
161 GList* tab_stops; /* Index into list containing the next tab position. If
162 * NULL, using default widths. */
169 TabStopMark tab_start;
172 struct _FetchLinesData
185 guint displayable_chars;
188 PrevTabCont tab_cont;
189 PrevTabCont tab_cont_next;
191 GtkPropertyMark start;
196 static void gtk_text_class_init (GtkTextClass *klass);
197 static void gtk_text_set_arg (GtkObject *object,
200 static void gtk_text_get_arg (GtkObject *object,
203 static void gtk_text_init (GtkText *text);
204 static void gtk_text_destroy (GtkObject *object);
205 static void gtk_text_finalize (GtkObject *object);
206 static void gtk_text_realize (GtkWidget *widget);
207 static void gtk_text_unrealize (GtkWidget *widget);
208 static void gtk_text_style_set (GtkWidget *widget,
209 GtkStyle *previous_style);
210 static void gtk_text_draw_focus (GtkWidget *widget);
211 static void gtk_text_size_request (GtkWidget *widget,
212 GtkRequisition *requisition);
213 static void gtk_text_size_allocate (GtkWidget *widget,
214 GtkAllocation *allocation);
215 static void gtk_text_adjustment (GtkAdjustment *adjustment,
217 static void gtk_text_disconnect (GtkAdjustment *adjustment,
220 static void gtk_text_insert_text (GtkEditable *editable,
221 const gchar *new_text,
222 gint new_text_length,
224 static void gtk_text_delete_text (GtkEditable *editable,
227 static void gtk_text_update_text (GtkEditable *editable,
230 static gchar *gtk_text_get_chars (GtkEditable *editable,
233 static void gtk_text_set_selection (GtkEditable *editable,
236 static void gtk_text_real_set_editable (GtkEditable *editable,
237 gboolean is_editable);
240 static void gtk_text_draw (GtkWidget *widget,
242 static gint gtk_text_expose (GtkWidget *widget,
243 GdkEventExpose *event);
244 static gint gtk_text_button_press (GtkWidget *widget,
245 GdkEventButton *event);
246 static gint gtk_text_button_release (GtkWidget *widget,
247 GdkEventButton *event);
248 static gint gtk_text_motion_notify (GtkWidget *widget,
249 GdkEventMotion *event);
250 static gint gtk_text_key_press (GtkWidget *widget,
252 static gint gtk_text_focus_in (GtkWidget *widget,
253 GdkEventFocus *event);
254 static gint gtk_text_focus_out (GtkWidget *widget,
255 GdkEventFocus *event);
257 static void move_gap (GtkText* text, guint index);
258 static void make_forward_space (GtkText* text, guint len);
260 /* Property management */
261 static GtkTextFont* get_text_font (GdkFont* gfont);
262 static void text_font_unref (GtkTextFont *text_font);
264 static void insert_text_property (GtkText* text, GdkFont* font,
265 GdkColor *fore, GdkColor* back, guint len);
266 static TextProperty* new_text_property (GtkText *text, GdkFont* font,
267 GdkColor* fore, GdkColor* back, guint length);
268 static void destroy_text_property (TextProperty *prop);
269 static void init_properties (GtkText *text);
270 static void realize_property (GtkText *text, TextProperty *prop);
271 static void realize_properties (GtkText *text);
272 static void unrealize_property (GtkText *text, TextProperty *prop);
273 static void unrealize_properties (GtkText *text);
275 static void delete_text_property (GtkText* text, guint len);
277 static guint pixel_height_of (GtkText* text, GList* cache_line);
279 /* Property Movement and Size Computations */
280 static void advance_mark (GtkPropertyMark* mark);
281 static void decrement_mark (GtkPropertyMark* mark);
282 static void advance_mark_n (GtkPropertyMark* mark, gint n);
283 static void decrement_mark_n (GtkPropertyMark* mark, gint n);
284 static void move_mark_n (GtkPropertyMark* mark, gint n);
285 static GtkPropertyMark find_mark (GtkText* text, guint mark_position);
286 static GtkPropertyMark find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near);
287 static void find_line_containing_point (GtkText* text, guint point,
291 static void compute_lines_pixels (GtkText* text, guint char_count,
292 guint *lines, guint *pixels);
294 static gint total_line_height (GtkText* text,
297 static LineParams find_line_params (GtkText* text,
298 const GtkPropertyMark *mark,
299 const PrevTabCont *tab_cont,
300 PrevTabCont *next_cont);
301 static void recompute_geometry (GtkText* text);
302 static void insert_expose (GtkText* text, guint old_pixels, gint nchars, guint new_line_count);
303 static void delete_expose (GtkText* text,
307 static GdkGC *create_bg_gc (GtkText *text);
308 static void clear_area (GtkText *text, GdkRectangle *area);
309 static void draw_line (GtkText* text,
312 static void draw_line_wrap (GtkText* text,
314 static void draw_cursor (GtkText* text, gint absolute);
315 static void undraw_cursor (GtkText* text, gint absolute);
316 static gint drawn_cursor_min (GtkText* text);
317 static gint drawn_cursor_max (GtkText* text);
318 static void expose_text (GtkText* text, GdkRectangle *area, gboolean cursor);
320 /* Search and Placement. */
321 static void find_cursor (GtkText* text,
323 static void find_cursor_at_line (GtkText* text,
324 const LineParams* start_line,
326 static void find_mouse_cursor (GtkText* text, gint x, gint y);
329 static void adjust_adj (GtkText* text, GtkAdjustment* adj);
330 static void scroll_up (GtkText* text, gint diff);
331 static void scroll_down (GtkText* text, gint diff);
332 static void scroll_int (GtkText* text, gint diff);
334 static void process_exposes (GtkText *text);
336 /* Cache Management. */
337 static void free_cache (GtkText* text);
338 static GList* remove_cache_line (GtkText* text, GList* list);
341 static void move_cursor_buffer_ver (GtkText *text, int dir);
342 static void move_cursor_page_ver (GtkText *text, int dir);
343 static void move_cursor_ver (GtkText *text, int count);
344 static void move_cursor_hor (GtkText *text, int count);
346 /* Binding actions */
347 static void gtk_text_move_cursor (GtkEditable *editable,
350 static void gtk_text_move_word (GtkEditable *editable,
352 static void gtk_text_move_page (GtkEditable *editable,
355 static void gtk_text_move_to_row (GtkEditable *editable,
357 static void gtk_text_move_to_column (GtkEditable *editable,
359 static void gtk_text_kill_char (GtkEditable *editable,
361 static void gtk_text_kill_word (GtkEditable *editable,
363 static void gtk_text_kill_line (GtkEditable *editable,
367 static void gtk_text_move_forward_character (GtkText *text);
368 static void gtk_text_move_backward_character (GtkText *text);
369 static void gtk_text_move_forward_word (GtkText *text);
370 static void gtk_text_move_backward_word (GtkText *text);
371 static void gtk_text_move_beginning_of_line (GtkText *text);
372 static void gtk_text_move_end_of_line (GtkText *text);
373 static void gtk_text_move_next_line (GtkText *text);
374 static void gtk_text_move_previous_line (GtkText *text);
376 static void gtk_text_delete_forward_character (GtkText *text);
377 static void gtk_text_delete_backward_character (GtkText *text);
378 static void gtk_text_delete_forward_word (GtkText *text);
379 static void gtk_text_delete_backward_word (GtkText *text);
380 static void gtk_text_delete_line (GtkText *text);
381 static void gtk_text_delete_to_line_end (GtkText *text);
382 static void gtk_text_select_word (GtkText *text,
384 static void gtk_text_select_line (GtkText *text,
387 static void gtk_text_set_position (GtkEditable *editable,
390 /* #define DEBUG_GTK_TEXT */
392 #if defined(DEBUG_GTK_TEXT) && defined(__GNUC__)
393 /* Debugging utilities. */
394 static void gtk_text_assert_mark (GtkText *text,
395 GtkPropertyMark *mark,
396 GtkPropertyMark *before,
397 GtkPropertyMark *after,
402 static void gtk_text_assert (GtkText *text,
405 static void gtk_text_show_cache_line (GtkText *text, GList *cache,
406 const char* what, const char* func, gint line);
407 static void gtk_text_show_cache (GtkText *text, const char* func, gint line);
408 static void gtk_text_show_adj (GtkText *text,
413 static void gtk_text_show_props (GtkText* test,
417 #define TDEBUG(args) g_message args
418 #define TEXT_ASSERT(text) gtk_text_assert (text,__PRETTY_FUNCTION__,__LINE__)
419 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_text_assert_mark (text,mark, \
420 __PRETTY_FUNCTION__,msg,__LINE__)
421 #define TEXT_SHOW(text) gtk_text_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
422 #define TEXT_SHOW_LINE(text,line,msg) gtk_text_show_cache_line (text,line,msg,\
423 __PRETTY_FUNCTION__,__LINE__)
424 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_text_show_adj (text,adj,msg, \
425 __PRETTY_FUNCTION__,__LINE__)
428 #define TEXT_ASSERT(text)
429 #define TEXT_ASSERT_MARK(text,mark,msg)
430 #define TEXT_SHOW(text)
431 #define TEXT_SHOW_LINE(text,line,msg)
432 #define TEXT_SHOW_ADJ(text,adj,msg)
435 /* Memory Management. */
436 static GMemChunk *params_mem_chunk = NULL;
437 static GMemChunk *text_property_chunk = NULL;
439 static GtkWidgetClass *parent_class = NULL;
442 static const GtkTextFunction control_keys[26] =
444 (GtkTextFunction)gtk_text_move_beginning_of_line, /* a */
445 (GtkTextFunction)gtk_text_move_backward_character, /* b */
446 (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
447 (GtkTextFunction)gtk_text_delete_forward_character, /* d */
448 (GtkTextFunction)gtk_text_move_end_of_line, /* e */
449 (GtkTextFunction)gtk_text_move_forward_character, /* f */
451 (GtkTextFunction)gtk_text_delete_backward_character, /* h */
454 (GtkTextFunction)gtk_text_delete_to_line_end, /* k */
457 (GtkTextFunction)gtk_text_move_next_line, /* n */
459 (GtkTextFunction)gtk_text_move_previous_line, /* p */
464 (GtkTextFunction)gtk_text_delete_line, /* u */
465 (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
466 (GtkTextFunction)gtk_text_delete_backward_word, /* w */
467 (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
472 static const GtkTextFunction alt_keys[26] =
475 (GtkTextFunction)gtk_text_move_backward_word, /* b */
477 (GtkTextFunction)gtk_text_delete_forward_word, /* d */
479 (GtkTextFunction)gtk_text_move_forward_word, /* f */
503 /**********************************************************************/
505 /**********************************************************************/
508 gtk_text_get_type (void)
510 static GtkType text_type = 0;
514 static const GtkTypeInfo text_info =
518 sizeof (GtkTextClass),
519 (GtkClassInitFunc) gtk_text_class_init,
520 (GtkObjectInitFunc) gtk_text_init,
521 /* reserved_1 */ NULL,
522 /* reserved_2 */ NULL,
523 (GtkClassInitFunc) NULL,
526 text_type = gtk_type_unique (GTK_TYPE_EDITABLE, &text_info);
533 gtk_text_class_init (GtkTextClass *class)
535 GtkObjectClass *object_class;
536 GtkWidgetClass *widget_class;
537 GtkEditableClass *editable_class;
539 object_class = (GtkObjectClass*) class;
540 widget_class = (GtkWidgetClass*) class;
541 editable_class = (GtkEditableClass*) class;
542 parent_class = gtk_type_class (GTK_TYPE_EDITABLE);
544 gtk_object_add_arg_type ("GtkText::hadjustment",
546 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
548 gtk_object_add_arg_type ("GtkText::vadjustment",
550 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
552 gtk_object_add_arg_type ("GtkText::line_wrap",
556 gtk_object_add_arg_type ("GtkText::word_wrap",
561 object_class->set_arg = gtk_text_set_arg;
562 object_class->get_arg = gtk_text_get_arg;
563 object_class->destroy = gtk_text_destroy;
564 object_class->finalize = gtk_text_finalize;
566 widget_class->realize = gtk_text_realize;
567 widget_class->unrealize = gtk_text_unrealize;
568 widget_class->style_set = gtk_text_style_set;
569 widget_class->draw_focus = gtk_text_draw_focus;
570 widget_class->size_request = gtk_text_size_request;
571 widget_class->size_allocate = gtk_text_size_allocate;
572 widget_class->draw = gtk_text_draw;
573 widget_class->expose_event = gtk_text_expose;
574 widget_class->button_press_event = gtk_text_button_press;
575 widget_class->button_release_event = gtk_text_button_release;
576 widget_class->motion_notify_event = gtk_text_motion_notify;
577 widget_class->key_press_event = gtk_text_key_press;
578 widget_class->focus_in_event = gtk_text_focus_in;
579 widget_class->focus_out_event = gtk_text_focus_out;
581 widget_class->set_scroll_adjustments_signal =
582 gtk_signal_new ("set_scroll_adjustments",
585 GTK_SIGNAL_OFFSET (GtkTextClass, set_scroll_adjustments),
586 gtk_marshal_NONE__POINTER_POINTER,
587 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
589 editable_class->set_editable = gtk_text_real_set_editable;
590 editable_class->insert_text = gtk_text_insert_text;
591 editable_class->delete_text = gtk_text_delete_text;
593 editable_class->move_cursor = gtk_text_move_cursor;
594 editable_class->move_word = gtk_text_move_word;
595 editable_class->move_page = gtk_text_move_page;
596 editable_class->move_to_row = gtk_text_move_to_row;
597 editable_class->move_to_column = gtk_text_move_to_column;
599 editable_class->kill_char = gtk_text_kill_char;
600 editable_class->kill_word = gtk_text_kill_word;
601 editable_class->kill_line = gtk_text_kill_line;
603 editable_class->update_text = gtk_text_update_text;
604 editable_class->get_chars = gtk_text_get_chars;
605 editable_class->set_selection = gtk_text_set_selection;
606 editable_class->set_position = gtk_text_set_position;
608 class->set_scroll_adjustments = gtk_text_set_adjustments;
612 gtk_text_set_arg (GtkObject *object,
618 text = GTK_TEXT (object);
622 case ARG_HADJUSTMENT:
623 gtk_text_set_adjustments (text,
624 GTK_VALUE_POINTER (*arg),
627 case ARG_VADJUSTMENT:
628 gtk_text_set_adjustments (text,
630 GTK_VALUE_POINTER (*arg));
633 gtk_text_set_line_wrap (text, GTK_VALUE_BOOL (*arg));
636 gtk_text_set_word_wrap (text, GTK_VALUE_BOOL (*arg));
644 gtk_text_get_arg (GtkObject *object,
650 text = GTK_TEXT (object);
654 case ARG_HADJUSTMENT:
655 GTK_VALUE_POINTER (*arg) = text->hadj;
657 case ARG_VADJUSTMENT:
658 GTK_VALUE_POINTER (*arg) = text->vadj;
661 GTK_VALUE_BOOL (*arg) = text->line_wrap;
664 GTK_VALUE_BOOL (*arg) = text->word_wrap;
667 arg->type = GTK_TYPE_INVALID;
673 gtk_text_init (GtkText *text)
675 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
677 text->text_area = NULL;
682 text->line_wrap_bitmap = NULL;
683 text->line_arrow_bitmap = NULL;
685 text->use_wchar = FALSE;
686 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
687 text->text_len = INITIAL_BUFFER_SIZE;
689 text->scratch_buffer.ch = NULL;
690 text->scratch_buffer_len = 0;
692 text->freeze_count = 0;
694 if (!params_mem_chunk)
695 params_mem_chunk = g_mem_chunk_new ("LineParams",
697 256 * sizeof (LineParams),
700 text->default_tab_width = 4;
701 text->tab_stops = NULL;
703 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
704 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
706 text->line_start_cache = NULL;
707 text->first_cut_pixels = 0;
709 text->line_wrap = TRUE;
710 text->word_wrap = FALSE;
715 text->current_font = NULL;
717 init_properties (text);
719 GTK_EDITABLE (text)->editable = FALSE;
721 gtk_editable_set_position (GTK_EDITABLE (text), 0);
725 gtk_text_new (GtkAdjustment *hadj,
731 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
733 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
735 text = gtk_widget_new (GTK_TYPE_TEXT,
744 gtk_text_set_word_wrap (GtkText *text,
747 g_return_if_fail (text != NULL);
748 g_return_if_fail (GTK_IS_TEXT (text));
750 text->word_wrap = (word_wrap != FALSE);
752 if (GTK_WIDGET_REALIZED (text))
754 recompute_geometry (text);
755 gtk_widget_queue_draw (GTK_WIDGET (text));
760 gtk_text_set_line_wrap (GtkText *text,
763 g_return_if_fail (text != NULL);
764 g_return_if_fail (GTK_IS_TEXT (text));
766 text->line_wrap = (line_wrap != FALSE);
768 if (GTK_WIDGET_REALIZED (text))
770 recompute_geometry (text);
771 gtk_widget_queue_draw (GTK_WIDGET (text));
776 gtk_text_set_editable (GtkText *text,
777 gboolean is_editable)
779 g_return_if_fail (text != NULL);
780 g_return_if_fail (GTK_IS_TEXT (text));
782 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
786 gtk_text_real_set_editable (GtkEditable *editable,
787 gboolean is_editable)
791 g_return_if_fail (editable != NULL);
792 g_return_if_fail (GTK_IS_TEXT (editable));
794 text = GTK_TEXT (editable);
796 editable->editable = (is_editable != FALSE);
799 draw_cursor (text, TRUE);
801 undraw_cursor (text, TRUE);
805 gtk_text_set_adjustments (GtkText *text,
809 g_return_if_fail (text != NULL);
810 g_return_if_fail (GTK_IS_TEXT (text));
812 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
814 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
816 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
818 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
820 if (text->hadj && (text->hadj != hadj))
822 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
823 gtk_object_unref (GTK_OBJECT (text->hadj));
826 if (text->vadj && (text->vadj != vadj))
828 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
829 gtk_object_unref (GTK_OBJECT (text->vadj));
832 if (text->hadj != hadj)
835 gtk_object_ref (GTK_OBJECT (text->hadj));
836 gtk_object_sink (GTK_OBJECT (text->hadj));
838 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
839 (GtkSignalFunc) gtk_text_adjustment,
841 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
842 (GtkSignalFunc) gtk_text_adjustment,
844 gtk_signal_connect (GTK_OBJECT (text->hadj), "disconnect",
845 (GtkSignalFunc) gtk_text_disconnect,
847 gtk_text_adjustment (hadj, text);
850 if (text->vadj != vadj)
853 gtk_object_ref (GTK_OBJECT (text->vadj));
854 gtk_object_sink (GTK_OBJECT (text->vadj));
856 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
857 (GtkSignalFunc) gtk_text_adjustment,
859 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
860 (GtkSignalFunc) gtk_text_adjustment,
862 gtk_signal_connect (GTK_OBJECT (text->vadj), "disconnect",
863 (GtkSignalFunc) gtk_text_disconnect,
865 gtk_text_adjustment (vadj, text);
870 gtk_text_set_point (GtkText *text,
873 g_return_if_fail (text != NULL);
874 g_return_if_fail (GTK_IS_TEXT (text));
875 g_return_if_fail (index <= TEXT_LENGTH (text));
877 text->point = find_mark (text, index);
881 gtk_text_get_point (GtkText *text)
883 g_return_val_if_fail (text != NULL, 0);
884 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
886 return text->point.index;
890 gtk_text_get_length (GtkText *text)
892 g_return_val_if_fail (text != NULL, 0);
893 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
895 return TEXT_LENGTH (text);
899 gtk_text_freeze (GtkText *text)
901 g_return_if_fail (text != NULL);
902 g_return_if_fail (GTK_IS_TEXT (text));
904 text->freeze_count++;
908 gtk_text_thaw (GtkText *text)
910 g_return_if_fail (text != NULL);
911 g_return_if_fail (GTK_IS_TEXT (text));
913 if (text->freeze_count)
914 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
916 recompute_geometry (text);
917 gtk_widget_queue_draw (GTK_WIDGET (text));
922 gtk_text_insert (GtkText *text,
929 GtkEditable *editable = GTK_EDITABLE (text);
930 gboolean frozen = FALSE;
932 gint new_line_count = 1;
933 guint old_height = 0;
938 g_return_if_fail (text != NULL);
939 g_return_if_fail (GTK_IS_TEXT (text));
942 length = strlen (chars);
949 if (!text->freeze_count && (length > FREEZE_LENGTH))
951 gtk_text_freeze (text);
955 if (!text->freeze_count && (text->line_start_cache != NULL))
957 find_line_containing_point (text, text->point.index, TRUE);
958 old_height = total_line_height (text, text->current_line, 1);
961 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
964 widget = GTK_WIDGET (text);
965 gtk_widget_ensure_style (widget);
966 if ((widget->style) && (widget->style->font->type == GDK_FONT_FONTSET))
968 text->use_wchar = TRUE;
969 g_free (text->text.ch);
970 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
971 text->text_len = INITIAL_BUFFER_SIZE;
972 if (text->scratch_buffer.ch)
973 g_free (text->scratch_buffer.ch);
974 text->scratch_buffer.wc = NULL;
975 text->scratch_buffer_len = 0;
979 move_gap (text, text->point.index);
980 make_forward_space (text, length);
984 char *chars_nt = (char *)chars;
987 chars_nt = g_new (char, length+1);
988 memcpy (chars_nt, chars, length);
989 chars_nt[length] = 0;
991 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
993 if (chars_nt != chars)
1001 memcpy(text->text.ch + text->gap_position, chars, length);
1004 if (!text->freeze_count && (text->line_start_cache != NULL))
1006 if (text->use_wchar)
1008 for (i=0; i<numwcs; i++)
1009 if (text->text.wc[text->gap_position + i] == '\n')
1014 for (i=0; i<numwcs; i++)
1015 if (text->text.ch[text->gap_position + i] == '\n')
1022 insert_text_property (text, font, fore, back, numwcs);
1024 text->gap_size -= numwcs;
1025 text->gap_position += numwcs;
1027 if (text->point.index < text->first_line_start_index)
1028 text->first_line_start_index += numwcs;
1029 if (text->point.index < editable->selection_start_pos)
1030 editable->selection_start_pos += numwcs;
1031 if (text->point.index < editable->selection_end_pos)
1032 editable->selection_end_pos += numwcs;
1033 /* We'll reset the cursor later anyways if we aren't frozen */
1034 if (text->point.index < text->cursor_mark.index)
1035 text->cursor_mark.index += numwcs;
1037 advance_mark_n (&text->point, numwcs);
1039 if (!text->freeze_count && (text->line_start_cache != NULL))
1040 insert_expose (text, old_height, numwcs, new_line_count);
1044 gtk_text_thaw (text);
1048 gtk_text_backward_delete (GtkText *text,
1051 g_return_val_if_fail (text != NULL, 0);
1052 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
1054 if (nchars > text->point.index || nchars <= 0)
1057 gtk_text_set_point (text, text->point.index - nchars);
1059 return gtk_text_forward_delete (text, nchars);
1063 gtk_text_forward_delete (GtkText *text,
1066 guint old_lines, old_height;
1067 GtkEditable *editable = GTK_EDITABLE (text);
1068 gboolean frozen = FALSE;
1070 g_return_val_if_fail (text != NULL, 0);
1071 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
1073 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1076 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1078 gtk_text_freeze (text);
1082 if (!text->freeze_count && text->line_start_cache != NULL)
1084 /* We need to undraw the cursor here, since we may later
1085 * delete the cursor's property
1087 undraw_cursor (text, FALSE);
1088 find_line_containing_point (text, text->point.index, TRUE);
1089 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1092 /* FIXME, or resizing after deleting will be odd */
1093 if (text->point.index < text->first_line_start_index)
1095 if (text->point.index + nchars >= text->first_line_start_index)
1097 text->first_line_start_index = text->point.index;
1098 while ((text->first_line_start_index > 0) &&
1099 (GTK_TEXT_INDEX (text, text->first_line_start_index - 1)
1101 text->first_line_start_index -= 1;
1105 text->first_line_start_index -= nchars;
1108 if (text->point.index < editable->selection_start_pos)
1109 editable->selection_start_pos -=
1110 MIN(nchars, editable->selection_start_pos - text->point.index);
1111 if (text->point.index < editable->selection_end_pos)
1112 editable->selection_end_pos -=
1113 MIN(nchars, editable->selection_end_pos - text->point.index);
1114 /* We'll reset the cursor later anyways if we aren't frozen */
1115 if (text->point.index < text->cursor_mark.index)
1116 move_mark_n (&text->cursor_mark,
1117 -MIN(nchars, text->cursor_mark.index - text->point.index));
1119 move_gap (text, text->point.index);
1121 text->gap_size += nchars;
1123 delete_text_property (text, nchars);
1125 if (!text->freeze_count && (text->line_start_cache != NULL))
1127 delete_expose (text, nchars, old_lines, old_height);
1128 draw_cursor (text, FALSE);
1132 gtk_text_thaw (text);
1138 gtk_text_set_position (GtkEditable *editable,
1141 GtkText *text = (GtkText *) editable;
1143 undraw_cursor (text, FALSE);
1144 text->cursor_mark = find_mark (text, position);
1145 find_cursor (text, TRUE);
1146 draw_cursor (text, FALSE);
1147 gtk_editable_select_region (editable, 0, 0);
1151 gtk_text_get_chars (GtkEditable *editable,
1159 g_return_val_if_fail (editable != NULL, NULL);
1160 g_return_val_if_fail (GTK_IS_TEXT (editable), NULL);
1161 text = GTK_TEXT (editable);
1164 end_pos = TEXT_LENGTH (text);
1166 if ((start_pos < 0) ||
1167 (end_pos > TEXT_LENGTH (text)) ||
1168 (end_pos < start_pos))
1171 move_gap (text, TEXT_LENGTH (text));
1172 make_forward_space (text, 1);
1174 if (text->use_wchar)
1177 ch = text->text.wc[end_pos];
1178 text->text.wc[end_pos] = 0;
1179 retval = gdk_wcstombs (text->text.wc + start_pos);
1180 text->text.wc[end_pos] = ch;
1185 ch = text->text.ch[end_pos];
1186 text->text.ch[end_pos] = 0;
1187 retval = g_strdup (text->text.ch + start_pos);
1188 text->text.ch[end_pos] = ch;
1196 gtk_text_destroy (GtkObject *object)
1200 g_return_if_fail (object != NULL);
1201 g_return_if_fail (GTK_IS_TEXT (object));
1203 text = (GtkText*) object;
1205 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1206 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1210 gtk_timeout_remove (text->timer);
1214 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1218 gtk_text_finalize (GtkObject *object)
1223 g_return_if_fail (object != NULL);
1224 g_return_if_fail (GTK_IS_TEXT (object));
1226 text = (GtkText *)object;
1228 gtk_object_unref (GTK_OBJECT (text->hadj));
1229 gtk_object_unref (GTK_OBJECT (text->vadj));
1231 /* Clean up the internal structures */
1232 if (text->use_wchar)
1233 g_free (text->text.wc);
1235 g_free (text->text.ch);
1237 tmp_list = text->text_properties;
1240 destroy_text_property (tmp_list->data);
1241 tmp_list = tmp_list->next;
1244 if (text->current_font)
1245 text_font_unref (text->current_font);
1247 g_list_free (text->text_properties);
1249 if (text->use_wchar)
1251 if (text->scratch_buffer.wc)
1252 g_free (text->scratch_buffer.wc);
1256 if (text->scratch_buffer.ch)
1257 g_free (text->scratch_buffer.ch);
1260 g_list_free (text->tab_stops);
1262 GTK_OBJECT_CLASS(parent_class)->finalize (object);
1266 gtk_text_realize (GtkWidget *widget)
1269 GtkEditable *editable;
1270 GdkWindowAttr attributes;
1271 gint attributes_mask;
1273 g_return_if_fail (widget != NULL);
1274 g_return_if_fail (GTK_IS_TEXT (widget));
1276 text = GTK_TEXT (widget);
1277 editable = GTK_EDITABLE (widget);
1278 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1280 attributes.window_type = GDK_WINDOW_CHILD;
1281 attributes.x = widget->allocation.x;
1282 attributes.y = widget->allocation.y;
1283 attributes.width = widget->allocation.width;
1284 attributes.height = widget->allocation.height;
1285 attributes.wclass = GDK_INPUT_OUTPUT;
1286 attributes.visual = gtk_widget_get_visual (widget);
1287 attributes.colormap = gtk_widget_get_colormap (widget);
1288 attributes.event_mask = gtk_widget_get_events (widget);
1289 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1290 GDK_BUTTON_PRESS_MASK |
1291 GDK_BUTTON_RELEASE_MASK |
1292 GDK_BUTTON_MOTION_MASK |
1293 GDK_ENTER_NOTIFY_MASK |
1294 GDK_LEAVE_NOTIFY_MASK |
1295 GDK_KEY_PRESS_MASK);
1296 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1298 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1299 gdk_window_set_user_data (widget->window, text);
1301 attributes.x = (widget->style->klass->xthickness + TEXT_BORDER_ROOM);
1302 attributes.y = (widget->style->klass->ythickness + TEXT_BORDER_ROOM);
1303 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1304 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1306 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1307 gdk_window_set_user_data (text->text_area, text);
1309 widget->style = gtk_style_attach (widget->style, widget->window);
1311 /* Can't call gtk_style_set_background here because it's handled specially */
1312 gdk_window_set_background (widget->window, &widget->style->base[GTK_STATE_NORMAL]);
1313 gdk_window_set_background (text->text_area, &widget->style->base[GTK_STATE_NORMAL]);
1315 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1316 text->bg_gc = create_bg_gc (text);
1318 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1319 (gchar*) line_wrap_bits,
1323 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1324 (gchar*) line_arrow_bits,
1328 text->gc = gdk_gc_new (text->text_area);
1329 gdk_gc_set_exposures (text->gc, TRUE);
1330 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1333 if (gdk_im_ready () && (editable->ic_attr = gdk_ic_attr_new ()) != NULL)
1336 GdkColormap *colormap;
1338 GdkICAttr *attr = editable->ic_attr;
1339 GdkICAttributesType attrmask = GDK_IC_ALL_REQ;
1341 GdkIMStyle supported_style = GDK_IM_PREEDIT_NONE |
1342 GDK_IM_PREEDIT_NOTHING |
1343 GDK_IM_PREEDIT_POSITION |
1344 GDK_IM_STATUS_NONE |
1345 GDK_IM_STATUS_NOTHING;
1347 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1348 supported_style &= ~GDK_IM_PREEDIT_POSITION;
1350 attr->style = style = gdk_im_decide_style (supported_style);
1351 attr->client_window = text->text_area;
1353 if ((colormap = gtk_widget_get_colormap (widget)) !=
1354 gtk_widget_get_default_colormap ())
1356 attrmask |= GDK_IC_PREEDIT_COLORMAP;
1357 attr->preedit_colormap = colormap;
1360 switch (style & GDK_IM_PREEDIT_MASK)
1362 case GDK_IM_PREEDIT_POSITION:
1363 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1365 g_warning ("over-the-spot style requires fontset");
1369 attrmask |= GDK_IC_PREEDIT_POSITION_REQ;
1370 gdk_window_get_size (text->text_area, &width, &height);
1371 attr->spot_location.x = 0;
1372 attr->spot_location.y = height;
1373 attr->preedit_area.x = 0;
1374 attr->preedit_area.y = 0;
1375 attr->preedit_area.width = width;
1376 attr->preedit_area.height = height;
1377 attr->preedit_fontset = widget->style->font;
1381 editable->ic = gdk_ic_new (attr, attrmask);
1383 if (editable->ic == NULL)
1384 g_warning ("Can't create input context.");
1387 mask = gdk_window_get_events (text->text_area);
1388 mask |= gdk_ic_get_events (editable->ic);
1389 gdk_window_set_events (text->text_area, mask);
1391 if (GTK_WIDGET_HAS_FOCUS (widget))
1392 gdk_im_begin (editable->ic, text->text_area);
1397 realize_properties (text);
1398 gdk_window_show (text->text_area);
1399 init_properties (text);
1401 if (editable->selection_start_pos != editable->selection_end_pos)
1402 gtk_editable_claim_selection (editable, TRUE, GDK_CURRENT_TIME);
1404 recompute_geometry (text);
1408 gtk_text_style_set (GtkWidget *widget,
1409 GtkStyle *previous_style)
1413 g_return_if_fail (widget != NULL);
1414 g_return_if_fail (GTK_IS_TEXT (widget));
1416 text = GTK_TEXT (widget);
1417 if (GTK_WIDGET_REALIZED (widget))
1419 gdk_window_set_background (widget->window, &widget->style->base[GTK_STATE_NORMAL]);
1420 gdk_window_set_background (text->text_area, &widget->style->base[GTK_STATE_NORMAL]);
1424 gdk_gc_destroy (text->bg_gc);
1428 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1429 text->bg_gc = create_bg_gc (text);
1431 recompute_geometry (text);
1434 if (text->current_font)
1435 text_font_unref (text->current_font);
1436 text->current_font = get_text_font (widget->style->font);
1440 gtk_text_unrealize (GtkWidget *widget)
1444 g_return_if_fail (widget != NULL);
1445 g_return_if_fail (GTK_IS_TEXT (widget));
1447 text = GTK_TEXT (widget);
1450 if (GTK_EDITABLE (widget)->ic)
1452 gdk_ic_destroy (GTK_EDITABLE (widget)->ic);
1453 GTK_EDITABLE (widget)->ic = NULL;
1455 if (GTK_EDITABLE (widget)->ic_attr)
1457 gdk_ic_attr_destroy (GTK_EDITABLE (widget)->ic_attr);
1458 GTK_EDITABLE (widget)->ic_attr = NULL;
1462 gdk_window_set_user_data (text->text_area, NULL);
1463 gdk_window_destroy (text->text_area);
1464 text->text_area = NULL;
1466 gdk_gc_destroy (text->gc);
1471 gdk_gc_destroy (text->bg_gc);
1475 gdk_pixmap_unref (text->line_wrap_bitmap);
1476 gdk_pixmap_unref (text->line_arrow_bitmap);
1478 unrealize_properties (text);
1482 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1483 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1487 clear_focus_area (GtkText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1489 GtkWidget *widget = GTK_WIDGET (text);
1491 gint ythick = TEXT_BORDER_ROOM + widget->style->klass->ythickness;
1492 gint xthick = TEXT_BORDER_ROOM + widget->style->klass->xthickness;
1496 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1498 gdk_gc_set_ts_origin (text->bg_gc,
1499 (- text->first_onscreen_hor_pixel + xthick) % width,
1500 (- text->first_onscreen_ver_pixel + ythick) % height);
1502 gdk_draw_rectangle (GTK_WIDGET (text)->window, text->bg_gc, TRUE,
1503 area_x, area_y, area_width, area_height);
1507 gtk_text_draw_focus (GtkWidget *widget)
1513 g_return_if_fail (widget != NULL);
1514 g_return_if_fail (GTK_IS_TEXT (widget));
1516 text = GTK_TEXT (widget);
1518 if (GTK_WIDGET_DRAWABLE (widget))
1520 gint ythick = widget->style->klass->ythickness;
1521 gint xthick = widget->style->klass->xthickness;
1522 gint xextra = TEXT_BORDER_ROOM;
1523 gint yextra = TEXT_BORDER_ROOM;
1525 TDEBUG (("in gtk_text_draw_focus\n"));
1529 width = widget->allocation.width;
1530 height = widget->allocation.height;
1532 if (GTK_WIDGET_HAS_FOCUS (widget))
1541 gtk_paint_focus (widget->style, widget->window,
1542 NULL, widget, "text",
1544 widget->allocation.width - 1,
1545 widget->allocation.height - 1);
1548 gtk_paint_shadow (widget->style, widget->window,
1549 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1550 NULL, widget, "text",
1551 x, y, width, height);
1555 width -= 2 * xthick;
1556 height -= 2 * ythick;
1558 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1561 clear_focus_area (text, x, y, width, yextra);
1563 clear_focus_area (text, x, y + yextra,
1564 xextra, y + height - 2 * yextra);
1566 clear_focus_area (text, x + width - xextra, y + yextra,
1567 xextra, height - 2 * ythick);
1569 clear_focus_area (text, x, x + height - yextra, width, yextra);
1574 TDEBUG (("in gtk_text_draw_focus (undrawable !!!)\n"));
1579 gtk_text_size_request (GtkWidget *widget,
1580 GtkRequisition *requisition)
1587 g_return_if_fail (widget != NULL);
1588 g_return_if_fail (GTK_IS_TEXT (widget));
1589 g_return_if_fail (requisition != NULL);
1591 xthickness = widget->style->klass->xthickness + TEXT_BORDER_ROOM;
1592 ythickness = widget->style->klass->ythickness + TEXT_BORDER_ROOM;
1594 char_height = MIN_TEXT_HEIGHT_LINES * (widget->style->font->ascent +
1595 widget->style->font->descent);
1597 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (widget->style->font,
1598 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1602 requisition->width = char_width + xthickness * 2;
1603 requisition->height = char_height + ythickness * 2;
1607 gtk_text_size_allocate (GtkWidget *widget,
1608 GtkAllocation *allocation)
1611 GtkEditable *editable;
1613 g_return_if_fail (widget != NULL);
1614 g_return_if_fail (GTK_IS_TEXT (widget));
1615 g_return_if_fail (allocation != NULL);
1617 text = GTK_TEXT (widget);
1618 editable = GTK_EDITABLE (widget);
1620 widget->allocation = *allocation;
1621 if (GTK_WIDGET_REALIZED (widget))
1623 gdk_window_move_resize (widget->window,
1624 allocation->x, allocation->y,
1625 allocation->width, allocation->height);
1627 gdk_window_move_resize (text->text_area,
1628 widget->style->klass->xthickness + TEXT_BORDER_ROOM,
1629 widget->style->klass->ythickness + TEXT_BORDER_ROOM,
1630 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->klass->xthickness +
1631 (gint)TEXT_BORDER_ROOM) * 2),
1632 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->klass->ythickness +
1633 (gint)TEXT_BORDER_ROOM) * 2));
1636 if (editable->ic && (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
1640 gdk_window_get_size (text->text_area, &width, &height);
1641 editable->ic_attr->preedit_area.width = width;
1642 editable->ic_attr->preedit_area.height = height;
1644 gdk_ic_set_attr (editable->ic,
1645 editable->ic_attr, GDK_IC_PREEDIT_AREA);
1649 recompute_geometry (text);
1654 gtk_text_draw (GtkWidget *widget,
1657 g_return_if_fail (widget != NULL);
1658 g_return_if_fail (GTK_IS_TEXT (widget));
1659 g_return_if_fail (area != NULL);
1661 if (GTK_WIDGET_DRAWABLE (widget))
1663 expose_text (GTK_TEXT (widget), area, TRUE);
1664 gtk_widget_draw_focus (widget);
1669 gtk_text_expose (GtkWidget *widget,
1670 GdkEventExpose *event)
1672 g_return_val_if_fail (widget != NULL, FALSE);
1673 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1674 g_return_val_if_fail (event != NULL, FALSE);
1676 if (event->window == GTK_TEXT (widget)->text_area)
1678 TDEBUG (("in gtk_text_expose (expose)\n"));
1679 expose_text (GTK_TEXT (widget), &event->area, TRUE);
1681 else if (event->count == 0)
1683 TDEBUG (("in gtk_text_expose (focus)\n"));
1684 gtk_widget_draw_focus (widget);
1691 gtk_text_scroll_timeout (gpointer data)
1694 GdkEventMotion event;
1696 GdkModifierType mask;
1698 GDK_THREADS_ENTER ();
1700 text = GTK_TEXT (data);
1703 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1705 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1712 gtk_text_motion_notify (GTK_WIDGET (text), &event);
1715 GDK_THREADS_LEAVE ();
1721 gtk_text_button_press (GtkWidget *widget,
1722 GdkEventButton *event)
1725 GtkEditable *editable;
1726 static GdkAtom ctext_atom = GDK_NONE;
1728 g_return_val_if_fail (widget != NULL, FALSE);
1729 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1730 g_return_val_if_fail (event != NULL, FALSE);
1732 if (ctext_atom == GDK_NONE)
1733 ctext_atom = gdk_atom_intern ("COMPOUND_TEXT", FALSE);
1735 text = GTK_TEXT (widget);
1736 editable = GTK_EDITABLE (widget);
1738 if (text->button && (event->button != text->button))
1741 text->button = event->button;
1743 if (!GTK_WIDGET_HAS_FOCUS (widget))
1744 gtk_widget_grab_focus (widget);
1746 if (event->button == 1)
1748 switch (event->type)
1750 case GDK_BUTTON_PRESS:
1751 gtk_grab_add (widget);
1753 undraw_cursor (text, FALSE);
1754 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1755 draw_cursor (text, FALSE);
1757 /* Set it now, so we display things right. We'll unset it
1758 * later if things don't work out */
1759 editable->has_selection = TRUE;
1760 gtk_text_set_selection (GTK_EDITABLE(text),
1761 text->cursor_mark.index,
1762 text->cursor_mark.index);
1766 case GDK_2BUTTON_PRESS:
1767 gtk_text_select_word (text, event->time);
1770 case GDK_3BUTTON_PRESS:
1771 gtk_text_select_line (text, event->time);
1778 else if (event->type == GDK_BUTTON_PRESS)
1780 if ((event->button == 2) && editable->editable)
1782 if (editable->selection_start_pos == editable->selection_end_pos ||
1783 editable->has_selection)
1785 undraw_cursor (text, FALSE);
1786 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1787 draw_cursor (text, FALSE);
1791 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1792 ctext_atom, event->time);
1796 gtk_grab_add (widget);
1798 undraw_cursor (text, FALSE);
1799 find_mouse_cursor (text, event->x, event->y);
1800 draw_cursor (text, FALSE);
1802 gtk_text_set_selection (GTK_EDITABLE(text),
1803 text->cursor_mark.index,
1804 text->cursor_mark.index);
1806 editable->has_selection = FALSE;
1807 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1808 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1816 gtk_text_button_release (GtkWidget *widget,
1817 GdkEventButton *event)
1820 GtkEditable *editable;
1821 g_return_val_if_fail (widget != NULL, FALSE);
1822 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1823 g_return_val_if_fail (event != NULL, FALSE);
1825 text = GTK_TEXT (widget);
1827 gtk_grab_remove (widget);
1829 if (text->button != event->button)
1836 gtk_timeout_remove (text->timer);
1840 if (event->button == 1)
1842 text = GTK_TEXT (widget);
1843 editable = GTK_EDITABLE (widget);
1845 gtk_grab_remove (widget);
1847 editable->has_selection = FALSE;
1848 if (editable->selection_start_pos != editable->selection_end_pos)
1850 if (gtk_selection_owner_set (widget,
1851 GDK_SELECTION_PRIMARY,
1853 editable->has_selection = TRUE;
1855 gtk_text_update_text (editable, editable->selection_start_pos,
1856 editable->selection_end_pos);
1860 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1861 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1864 else if (event->button == 3)
1866 gtk_grab_remove (widget);
1869 undraw_cursor (text, FALSE);
1870 find_cursor (text, TRUE);
1871 draw_cursor (text, FALSE);
1877 gtk_text_motion_notify (GtkWidget *widget,
1878 GdkEventMotion *event)
1883 GdkModifierType mask;
1885 g_return_val_if_fail (widget != NULL, FALSE);
1886 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1887 g_return_val_if_fail (event != NULL, FALSE);
1889 text = GTK_TEXT (widget);
1893 mask = event->state;
1894 if (event->is_hint || (text->text_area != event->window))
1896 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1899 if ((text->button == 0) ||
1900 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1903 gdk_window_get_size (text->text_area, NULL, &height);
1905 if ((y < 0) || (y > height))
1907 if (text->timer == 0)
1909 text->timer = gtk_timeout_add (SCROLL_TIME,
1910 gtk_text_scroll_timeout,
1914 scroll_int (text, y/2);
1916 scroll_int (text, (y - height)/2);
1922 undraw_cursor (GTK_TEXT (widget), FALSE);
1923 find_mouse_cursor (GTK_TEXT (widget), x, y);
1924 draw_cursor (GTK_TEXT (widget), FALSE);
1926 gtk_text_set_selection (GTK_EDITABLE(text),
1927 GTK_EDITABLE(text)->selection_start_pos,
1928 text->cursor_mark.index);
1934 gtk_text_insert_text (GtkEditable *editable,
1935 const gchar *new_text,
1936 gint new_text_length,
1939 GtkText *text = GTK_TEXT (editable);
1941 GdkColor *fore, *back;
1943 TextProperty *property;
1945 gtk_text_set_point (text, *position);
1947 property = MARK_CURRENT_PROPERTY (&text->point);
1948 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
1949 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
1950 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
1952 gtk_text_insert (text, font, fore, back, new_text, new_text_length);
1954 *position = text->point.index;
1958 gtk_text_delete_text (GtkEditable *editable,
1964 g_return_if_fail (start_pos >= 0);
1966 text = GTK_TEXT (editable);
1968 gtk_text_set_point (text, start_pos);
1970 end_pos = TEXT_LENGTH (text);
1972 if (end_pos > start_pos)
1973 gtk_text_forward_delete (text, end_pos - start_pos);
1977 gtk_text_key_press (GtkWidget *widget,
1981 GtkEditable *editable;
1986 g_return_val_if_fail (widget != NULL, FALSE);
1987 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1988 g_return_val_if_fail (event != NULL, FALSE);
1992 text = GTK_TEXT (widget);
1993 editable = GTK_EDITABLE (widget);
1995 key = event->keyval;
1998 if ((GTK_EDITABLE(text)->editable == FALSE))
2000 switch (event->keyval)
2003 if (event->state & GDK_CONTROL_MASK)
2004 scroll_int (text, -text->vadj->value);
2007 if (event->state & GDK_CONTROL_MASK)
2008 scroll_int (text, +text->vadj->upper);
2010 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
2011 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
2012 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
2013 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
2015 if (event->state & GDK_CONTROL_MASK)
2016 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2025 gint extend_selection;
2027 guint initial_pos = editable->current_pos;
2029 text->point = find_mark (text, text->cursor_mark.index);
2031 extend_selection = event->state & GDK_SHIFT_MASK;
2032 extend_start = FALSE;
2034 if (extend_selection)
2036 editable->has_selection = TRUE;
2038 if (editable->selection_start_pos == editable->selection_end_pos)
2040 editable->selection_start_pos = text->point.index;
2041 editable->selection_end_pos = text->point.index;
2044 extend_start = (text->point.index == editable->selection_start_pos);
2047 switch (event->keyval)
2050 if (event->state & GDK_CONTROL_MASK)
2051 move_cursor_buffer_ver (text, -1);
2053 gtk_text_move_beginning_of_line (text);
2056 if (event->state & GDK_CONTROL_MASK)
2057 move_cursor_buffer_ver (text, +1);
2059 gtk_text_move_end_of_line (text);
2061 case GDK_Page_Up: move_cursor_page_ver (text, -1); break;
2062 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
2063 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
2064 case GDK_Up: move_cursor_ver (text, -1); break;
2065 case GDK_Down: move_cursor_ver (text, +1); break;
2067 if (event->state & GDK_CONTROL_MASK)
2068 gtk_text_move_backward_word (text);
2070 move_cursor_hor (text, -1);
2073 if (event->state & GDK_CONTROL_MASK)
2074 gtk_text_move_forward_word (text);
2076 move_cursor_hor (text, +1);
2080 if (event->state & GDK_CONTROL_MASK)
2081 gtk_text_delete_backward_word (text);
2083 gtk_text_delete_backward_character (text);
2086 gtk_text_delete_line (text);
2089 if (event->state & GDK_SHIFT_MASK)
2091 extend_selection = FALSE;
2092 gtk_editable_paste_clipboard (editable);
2094 else if (event->state & GDK_CONTROL_MASK)
2096 gtk_editable_copy_clipboard (editable);
2100 /* gtk_toggle_insert(text) -- IMPLEMENT */
2104 if (event->state & GDK_CONTROL_MASK)
2105 gtk_text_delete_forward_word (text);
2106 else if (event->state & GDK_SHIFT_MASK)
2108 extend_selection = FALSE;
2109 gtk_editable_cut_clipboard (editable);
2112 gtk_text_delete_forward_character (text);
2115 position = text->point.index;
2116 gtk_editable_insert_text (editable, "\t", 1, &position);
2119 if (event->state & GDK_CONTROL_MASK)
2120 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2123 position = text->point.index;
2124 gtk_editable_insert_text (editable, "\n", 1, &position);
2128 /* Don't insert literally */
2135 if (event->state & GDK_CONTROL_MASK)
2137 if ((key >= 'A') && (key <= 'Z'))
2140 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2142 (* control_keys[(int) (key - 'a')]) (editable, event->time);
2148 else if (event->state & GDK_MOD1_MASK)
2150 if ((key >= 'A') && (key <= 'Z'))
2153 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2155 (* alt_keys[(int) (key - 'a')]) (editable, event->time);
2161 else if (event->length > 0)
2163 extend_selection = FALSE;
2165 gtk_editable_delete_selection (editable);
2166 position = text->point.index;
2167 gtk_editable_insert_text (editable, event->string, event->length, &position);
2175 if (return_val && (editable->current_pos != initial_pos))
2177 if (extend_selection)
2179 if (editable->current_pos < editable->selection_start_pos)
2180 gtk_text_set_selection (editable, editable->current_pos,
2181 editable->selection_end_pos);
2182 else if (editable->current_pos > editable->selection_end_pos)
2183 gtk_text_set_selection (editable, editable->selection_start_pos,
2184 editable->current_pos);
2188 gtk_text_set_selection (editable, editable->current_pos,
2189 editable->selection_end_pos);
2191 gtk_text_set_selection (editable, editable->selection_start_pos,
2192 editable->current_pos);
2196 gtk_text_set_selection (editable, 0, 0);
2198 gtk_editable_claim_selection (editable,
2199 editable->selection_start_pos != editable->selection_end_pos,
2208 gtk_text_focus_in (GtkWidget *widget,
2209 GdkEventFocus *event)
2211 g_return_val_if_fail (widget != NULL, FALSE);
2212 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
2213 g_return_val_if_fail (event != NULL, FALSE);
2215 TDEBUG (("in gtk_text_focus_in\n"));
2217 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
2218 gtk_widget_draw_focus (widget);
2221 if (GTK_EDITABLE(widget)->ic)
2222 gdk_im_begin (GTK_EDITABLE(widget)->ic, GTK_TEXT(widget)->text_area);
2225 draw_cursor (GTK_TEXT(widget), TRUE);
2231 gtk_text_focus_out (GtkWidget *widget,
2232 GdkEventFocus *event)
2234 g_return_val_if_fail (widget != NULL, FALSE);
2235 g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
2236 g_return_val_if_fail (event != NULL, FALSE);
2238 TDEBUG (("in gtk_text_focus_out\n"));
2240 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
2241 gtk_widget_draw_focus (widget);
2243 undraw_cursor (GTK_TEXT(widget), TRUE);
2253 gtk_text_adjustment (GtkAdjustment *adjustment,
2256 g_return_if_fail (adjustment != NULL);
2257 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2258 g_return_if_fail (text != NULL);
2259 g_return_if_fail (GTK_IS_TEXT (text));
2261 /* Just ignore it if we haven't been size-allocated and realized yet */
2262 if (text->line_start_cache == NULL)
2265 if (adjustment == text->hadj)
2267 g_warning ("horizontal scrolling not implemented");
2271 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2275 undraw_cursor (text, FALSE);
2278 scroll_down (text, diff);
2279 else /* if (diff < 0) */
2280 scroll_up (text, diff);
2282 draw_cursor (text, FALSE);
2284 text->last_ver_value = adjustment->value;
2290 gtk_text_disconnect (GtkAdjustment *adjustment,
2293 g_return_if_fail (adjustment != NULL);
2294 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2295 g_return_if_fail (text != NULL);
2296 g_return_if_fail (GTK_IS_TEXT (text));
2298 if (adjustment == text->hadj)
2299 gtk_text_set_adjustments (text, NULL, text->vadj);
2300 if (adjustment == text->vadj)
2301 gtk_text_set_adjustments (text, text->hadj, NULL);
2305 static GtkPropertyMark
2306 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2308 GtkPropertyMark mark;
2310 mark = find_mark_near (text, point_position, near);
2312 while (mark.index > 0 &&
2313 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2314 decrement_mark (&mark);
2320 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2322 tab_cont->pixel_offset = 0;
2323 tab_cont->tab_start.tab_stops = text->tab_stops;
2324 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2326 if (!tab_cont->tab_start.to_next_tab)
2327 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2331 line_params_iterate (GtkText* text,
2332 const GtkPropertyMark* mark0,
2333 const PrevTabCont* tab_mark0,
2336 LineIteratorFunction iter)
2337 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2338 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2339 * for each line following MARK, iteration continues unless ITER_CALL
2342 GtkPropertyMark mark = *mark0;
2343 PrevTabCont tab_conts[2];
2344 LineParams *lp, lpbuf;
2345 gint tab_cont_index = 0;
2348 tab_conts[0] = *tab_mark0;
2350 init_tab_cont (text, tab_conts);
2355 lp = g_chunk_new (LineParams, params_mem_chunk);
2359 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2360 tab_conts + (tab_cont_index + 1) % 2);
2362 if ((*iter) (text, lp, data))
2365 if (LAST_INDEX (text, lp->end))
2369 advance_mark (&mark);
2370 tab_cont_index = (tab_cont_index + 1) % 2;
2375 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2377 FetchLinesData *fldata = (FetchLinesData*) data;
2379 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2381 switch (fldata->fl_type)
2383 case FetchLinesCount:
2384 if (!text->line_wrap || !lp->wraps)
2387 if (fldata->data >= fldata->data_max)
2391 case FetchLinesPixels:
2393 fldata->data += LINE_HEIGHT(*lp);
2395 if (fldata->data >= fldata->data_max)
2405 fetch_lines (GtkText* text,
2406 const GtkPropertyMark* mark0,
2407 const PrevTabCont* tab_cont0,
2411 FetchLinesData fl_data;
2413 fl_data.new_lines = NULL;
2415 fl_data.data_max = data;
2416 fl_data.fl_type = fl_type;
2418 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2420 return g_list_reverse (fl_data.new_lines);
2424 fetch_lines_backward (GtkText* text)
2426 GList* new_lines = NULL, *new_line_start;
2427 GtkPropertyMark mark;
2429 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2432 mark = find_this_line_start_mark (text,
2433 CACHE_DATA(text->line_start_cache).start.index - 1,
2434 &CACHE_DATA(text->line_start_cache).start);
2436 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2438 while (new_line_start->next)
2439 new_line_start = new_line_start->next;
2441 new_line_start->next = text->line_start_cache;
2442 text->line_start_cache->prev = new_line_start;
2446 fetch_lines_forward (GtkText* text, gint line_count)
2448 GtkPropertyMark mark;
2449 GList* line = text->line_start_cache;
2454 mark = CACHE_DATA(line).end;
2456 if (LAST_INDEX (text, mark))
2459 advance_mark(&mark);
2461 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2464 line->next->prev = line;
2467 /* Compute the number of lines, and vertical pixels for n characters
2468 * starting from the point
2471 compute_lines_pixels (GtkText* text, guint char_count,
2472 guint *lines, guint *pixels)
2474 GList *line = text->current_line;
2475 gint chars_left = char_count;
2480 /* If chars_left == 0, that means we're joining two lines in a
2481 * deletion, so add in the values for the next line as well
2483 for (; line && chars_left >= 0; line = line->next)
2485 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2487 if (line == text->current_line)
2488 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2490 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2492 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2496 chars_left = 0; /* force another loop */
2499 fetch_lines_forward (text, 1);
2504 total_line_height (GtkText* text, GList* line, gint line_count)
2508 for (; line && line_count > 0; line = line->next)
2510 height += LINE_HEIGHT(CACHE_DATA(line));
2512 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2516 fetch_lines_forward (text, line_count);
2523 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2525 if (old == text->line_start_cache)
2529 for (; old_line_count > 0; old_line_count -= 1)
2531 while (text->line_start_cache &&
2533 CACHE_DATA(text->line_start_cache).wraps)
2534 remove_cache_line(text, text->line_start_cache);
2536 remove_cache_line(text, text->line_start_cache);
2539 last = g_list_last (new);
2541 last->next = text->line_start_cache;
2543 if (text->line_start_cache)
2544 text->line_start_cache->prev = last;
2546 text->line_start_cache = new;
2552 g_assert (old->prev);
2556 for (; old_line_count > 0; old_line_count -= 1)
2558 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2559 old = remove_cache_line (text, old);
2561 old = remove_cache_line (text, old);
2567 last = g_list_last (new);
2577 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2579 GList* cache = text->current_line;
2582 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2585 for (; cache; cache = cache->next)
2587 GtkPropertyMark *start = &CACHE_DATA(cache).start;
2588 GtkPropertyMark *end = &CACHE_DATA(cache).end;
2590 start->index -= nchars;
2591 end->index -= nchars;
2593 if (LAST_INDEX (text, text->point) &&
2594 start->index == text->point.index)
2595 *start = text->point;
2596 else if (start->property == text->point.property)
2597 start->offset = start->index - (text->point.index - text->point.offset);
2599 if (LAST_INDEX (text, text->point) &&
2600 end->index == text->point.index)
2602 if (end->property == text->point.property)
2603 end->offset = end->index - (text->point.index - text->point.offset);
2605 /*TEXT_ASSERT_MARK(text, start, "start");*/
2606 /*TEXT_ASSERT_MARK(text, end, "end");*/
2611 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2613 GtkWidget *widget = GTK_WIDGET (text);
2616 guint new_pixels = 0;
2618 GList* new_line = NULL;
2621 text->cursor_virtual_x = 0;
2623 correct_cache_delete (text, nchars, old_lines);
2625 pixel_height = pixel_height_of(text, text->current_line) -
2626 LINE_HEIGHT(CACHE_DATA(text->current_line));
2628 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2629 CACHE_DATA(text->current_line).start = text->point;
2631 new_line = fetch_lines (text,
2632 &CACHE_DATA(text->current_line).start,
2633 &CACHE_DATA(text->current_line).tab_cont,
2637 swap_lines (text, text->current_line, new_line, old_lines);
2639 text->current_line = new_line;
2641 new_pixels = total_line_height (text, new_line, 1);
2643 gdk_window_get_size (text->text_area, &width, &height);
2645 if (old_pixels != new_pixels)
2647 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2649 gdk_draw_pixmap (text->text_area,
2653 pixel_height + old_pixels,
2655 pixel_height + new_pixels,
2659 text->vadj->upper += new_pixels;
2660 text->vadj->upper -= old_pixels;
2661 adjust_adj (text, text->vadj);
2665 rect.y = pixel_height;
2667 rect.height = new_pixels;
2669 expose_text (text, &rect, FALSE);
2670 gtk_text_draw_focus ( (GtkWidget *) text);
2672 text->cursor_mark = text->point;
2674 find_cursor (text, TRUE);
2676 if (old_pixels != new_pixels)
2678 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2681 rect.y = pixel_height + new_pixels;
2683 rect.height = height - rect.y;
2685 expose_text (text, &rect, FALSE);
2688 process_exposes (text);
2695 /* note, the point has already been moved forward */
2697 correct_cache_insert (GtkText* text, gint nchars)
2700 GtkPropertyMark *start;
2701 GtkPropertyMark *end;
2702 gboolean was_split = FALSE;
2704 /* We need to distinguish whether the property was split in the
2705 * insert or not, so we check if the point (which points after
2706 * the insertion here), points to the same character as the
2707 * point before. Ugh.
2711 GtkPropertyMark tmp_mark = text->point;
2712 move_mark_n (&tmp_mark, -1);
2714 if (tmp_mark.property != text->point.property)
2718 /* If we inserted a property exactly at the beginning of the
2719 * line, we have to correct here, or fetch_lines will
2722 start = &CACHE_DATA(text->current_line).start;
2724 /* Check if if we split exactly at the beginning of the line:
2725 * (was_split won't be set if we are inserting at the end of the text,
2726 * so we don't check)
2728 if (start->offset == MARK_CURRENT_PROPERTY (start)->length)
2729 SET_PROPERTY_MARK (start, start->property->next, 0);
2730 /* Check if we inserted a property at the beginning of the text: */
2731 else if (was_split &&
2732 (start->property == text->point.property) &&
2733 (start->index == text->point.index - nchars))
2734 SET_PROPERTY_MARK (start, start->property->prev, 0);
2736 /* Now correct the offsets, and check for start or end marks that
2737 * are after the point, yet point to a property before the point's
2738 * property. This indicates that they are meant to point to the
2739 * second half of a property we split in insert_text_property(), so
2740 * we fix them up that way.
2742 cache = text->current_line->next;
2744 for (; cache; cache = cache->next)
2746 start = &CACHE_DATA(cache).start;
2747 end = &CACHE_DATA(cache).end;
2749 if (LAST_INDEX (text, text->point) &&
2750 start->index == text->point.index)
2751 *start = text->point;
2752 else if (start->index >= text->point.index - nchars)
2754 if (!was_split && start->property == text->point.property)
2755 move_mark_n(start, nchars);
2758 if (start->property->next &&
2759 (start->property->next->next == text->point.property))
2761 g_assert (start->offset >= MARK_CURRENT_PROPERTY (start)->length);
2762 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2763 start->property = text->point.property;
2765 start->index += nchars;
2769 if (LAST_INDEX (text, text->point) &&
2770 end->index == text->point.index)
2772 if (end->index >= text->point.index - nchars)
2774 if (!was_split && end->property == text->point.property)
2775 move_mark_n(end, nchars);
2778 if (end->property->next &&
2779 (end->property->next->next == text->point.property))
2781 g_assert (end->offset >= MARK_CURRENT_PROPERTY (end)->length);
2782 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2783 end->property = text->point.property;
2785 end->index += nchars;
2789 /*TEXT_ASSERT_MARK(text, start, "start");*/
2790 /*TEXT_ASSERT_MARK(text, end, "end");*/
2796 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2797 guint new_line_count)
2799 GtkWidget *widget = GTK_WIDGET (text);
2802 guint new_pixels = 0;
2804 GList* new_lines = NULL;
2807 text->cursor_virtual_x = 0;
2809 undraw_cursor (text, FALSE);
2811 correct_cache_insert (text, nchars);
2813 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2815 pixel_height = pixel_height_of(text, text->current_line) -
2816 LINE_HEIGHT(CACHE_DATA(text->current_line));
2818 new_lines = fetch_lines (text,
2819 &CACHE_DATA(text->current_line).start,
2820 &CACHE_DATA(text->current_line).tab_cont,
2824 swap_lines (text, text->current_line, new_lines, 1);
2826 text->current_line = new_lines;
2828 new_pixels = total_line_height (text, new_lines, new_line_count);
2830 gdk_window_get_size (text->text_area, &width, &height);
2832 if (old_pixels != new_pixels)
2834 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2836 gdk_draw_pixmap (text->text_area,
2840 pixel_height + old_pixels,
2842 pixel_height + new_pixels,
2844 height + (old_pixels - new_pixels) - pixel_height);
2847 text->vadj->upper += new_pixels;
2848 text->vadj->upper -= old_pixels;
2849 adjust_adj (text, text->vadj);
2853 rect.y = pixel_height;
2855 rect.height = new_pixels;
2857 expose_text (text, &rect, FALSE);
2858 gtk_text_draw_focus ( (GtkWidget *) text);
2860 text->cursor_mark = text->point;
2862 find_cursor (text, TRUE);
2864 draw_cursor (text, FALSE);
2866 if (old_pixels != new_pixels)
2868 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2871 rect.y = pixel_height + new_pixels;
2873 rect.height = height - rect.y;
2875 expose_text (text, &rect, FALSE);
2878 process_exposes (text);
2881 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2886 /* Text property functions */
2889 font_hash (gconstpointer font)
2891 return gdk_font_id ((const GdkFont*) font);
2894 static GHashTable *font_cache_table = NULL;
2897 get_text_font (GdkFont* gfont)
2902 if (!font_cache_table)
2903 font_cache_table = g_hash_table_new (font_hash, (GCompareFunc) gdk_font_equal);
2905 tf = g_hash_table_lookup (font_cache_table, gfont);
2913 tf = g_new (GtkTextFont, 1);
2916 tf->gdk_font = gfont;
2917 gdk_font_ref (gfont);
2919 for(i = 0; i < 256; i += 1)
2920 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2922 g_hash_table_insert (font_cache_table, gfont, tf);
2928 text_font_unref (GtkTextFont *text_font)
2930 text_font->ref_count--;
2931 if (text_font->ref_count == 0)
2933 g_hash_table_remove (font_cache_table, text_font->gdk_font);
2934 gdk_font_unref (text_font->gdk_font);
2940 text_properties_equal (TextProperty* prop, GdkFont* font, GdkColor *fore, GdkColor *back)
2942 if (prop->flags & PROPERTY_FONT)
2945 GtkTextFont *text_font;
2950 text_font = get_text_font (font);
2952 retval = (prop->font == text_font);
2953 text_font_unref (text_font);
2962 if (prop->flags & PROPERTY_FOREGROUND)
2964 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
2971 if (prop->flags & PROPERTY_BACKGROUND)
2973 if (!back || !gdk_color_equal (&prop->back_color, back))
2984 realize_property (GtkText *text, TextProperty *prop)
2986 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2988 if (prop->flags & PROPERTY_FOREGROUND)
2989 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
2991 if (prop->flags & PROPERTY_BACKGROUND)
2992 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
2996 realize_properties (GtkText *text)
2998 GList *tmp_list = text->text_properties;
3002 realize_property (text, tmp_list->data);
3004 tmp_list = tmp_list->next;
3009 unrealize_property (GtkText *text, TextProperty *prop)
3011 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3013 if (prop->flags & PROPERTY_FOREGROUND)
3014 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
3016 if (prop->flags & PROPERTY_BACKGROUND)
3017 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
3021 unrealize_properties (GtkText *text)
3023 GList *tmp_list = text->text_properties;
3027 unrealize_property (text, tmp_list->data);
3029 tmp_list = tmp_list->next;
3033 static TextProperty*
3034 new_text_property (GtkText *text, GdkFont *font, GdkColor* fore,
3035 GdkColor* back, guint length)
3039 if (text_property_chunk == NULL)
3041 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
3042 sizeof(TextProperty),
3043 1024*sizeof(TextProperty),
3047 prop = g_chunk_new(TextProperty, text_property_chunk);
3052 prop->flags |= PROPERTY_FONT;
3053 prop->font = get_text_font (font);
3060 prop->flags |= PROPERTY_FOREGROUND;
3061 prop->fore_color = *fore;
3066 prop->flags |= PROPERTY_BACKGROUND;
3067 prop->back_color = *back;
3070 prop->length = length;
3072 if (GTK_WIDGET_REALIZED (text))
3073 realize_property (text, prop);
3079 destroy_text_property (TextProperty *prop)
3082 text_font_unref (prop->font);
3084 g_mem_chunk_free (text_property_chunk, prop);
3087 /* Flop the memory between the point and the gap around like a
3090 move_gap (GtkText* text, guint index)
3092 if (text->gap_position < index)
3094 gint diff = index - text->gap_position;
3096 if (text->use_wchar)
3097 g_memmove (text->text.wc + text->gap_position,
3098 text->text.wc + text->gap_position + text->gap_size,
3099 diff*sizeof (GdkWChar));
3101 g_memmove (text->text.ch + text->gap_position,
3102 text->text.ch + text->gap_position + text->gap_size,
3105 text->gap_position = index;
3107 else if (text->gap_position > index)
3109 gint diff = text->gap_position - index;
3111 if (text->use_wchar)
3112 g_memmove (text->text.wc + index + text->gap_size,
3113 text->text.wc + index,
3114 diff*sizeof (GdkWChar));
3116 g_memmove (text->text.ch + index + text->gap_size,
3117 text->text.ch + index,
3120 text->gap_position = index;
3124 /* Increase the gap size. */
3126 make_forward_space (GtkText* text, guint len)
3128 if (text->gap_size < len)
3130 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3132 if (sum >= text->text_len)
3136 while (i <= sum) i <<= 1;
3138 if (text->use_wchar)
3139 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3140 i*sizeof(GdkWChar));
3142 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3146 if (text->use_wchar)
3147 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3148 text->text.wc + text->gap_position + text->gap_size,
3149 (text->text_end - (text->gap_position + text->gap_size))
3152 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3153 text->text.ch + text->gap_position + text->gap_size,
3154 text->text_end - (text->gap_position + text->gap_size));
3156 text->text_end += len*2;
3157 text->gap_size += len*2;
3161 /* Inserts into the text property list a list element that guarantees
3162 * that for len characters following the point, text has the correct
3163 * property. does not move point. adjusts text_properties_point and
3164 * text_properties_point_offset relative to the current value of
3167 insert_text_property (GtkText* text, GdkFont* font,
3168 GdkColor *fore, GdkColor* back, guint len)
3170 GtkPropertyMark *mark = &text->point;
3171 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3172 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3174 if (MARK_OFFSET(mark) == 0)
3176 /* Point is on the boundary of two properties.
3177 * If it is the same as either, grow, else insert
3180 if (text_properties_equal(forward_prop, font, fore, back))
3182 /* Grow the property in front of us. */
3184 MARK_PROPERTY_LENGTH(mark) += len;
3186 else if (backward_prop &&
3187 text_properties_equal(backward_prop, font, fore, back))
3189 /* Grow property behind us, point property and offset
3192 SET_PROPERTY_MARK (&text->point,
3193 MARK_PREV_LIST_PTR (mark),
3194 backward_prop->length);
3196 backward_prop->length += len;
3198 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3199 (forward_prop->length == 1))
3201 /* Next property just has last position, take it over */
3203 if (GTK_WIDGET_REALIZED (text))
3204 unrealize_property (text, forward_prop);
3206 forward_prop->flags = 0;
3209 forward_prop->flags |= PROPERTY_FONT;
3210 forward_prop->font = get_text_font (font);
3213 forward_prop->font = NULL;
3217 forward_prop->flags |= PROPERTY_FOREGROUND;
3218 forward_prop->fore_color = *fore;
3222 forward_prop->flags |= PROPERTY_BACKGROUND;
3223 forward_prop->back_color = *back;
3225 forward_prop->length += len;
3227 if (GTK_WIDGET_REALIZED (text))
3228 realize_property (text, forward_prop);
3232 /* Splice a new property into the list. */
3234 GList* new_prop = g_list_alloc();
3236 new_prop->next = MARK_LIST_PTR(mark);
3237 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3238 new_prop->next->prev = new_prop;
3241 new_prop->prev->next = new_prop;
3243 new_prop->data = new_text_property (text, font, fore, back, len);
3245 SET_PROPERTY_MARK (mark, new_prop, 0);
3250 /* The following will screw up the line_start cache,
3251 * we'll fix it up in correct_cache_insert
3254 /* In the middle of forward_prop, if properties are equal,
3255 * just add to its length, else split it into two and splice
3257 if (text_properties_equal (forward_prop, font, fore, back))
3259 forward_prop->length += len;
3261 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3262 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3264 /* Inserting before only the last position in the text */
3267 forward_prop->length -= 1;
3269 new_prop = g_list_alloc();
3270 new_prop->data = new_text_property (text, font, fore, back, len+1);
3271 new_prop->prev = MARK_LIST_PTR(mark);
3272 new_prop->next = NULL;
3273 MARK_NEXT_LIST_PTR(mark) = new_prop;
3275 SET_PROPERTY_MARK (mark, new_prop, 0);
3279 GList* new_prop = g_list_alloc();
3280 GList* new_prop_forward = g_list_alloc();
3281 gint old_length = forward_prop->length;
3282 GList* next = MARK_NEXT_LIST_PTR(mark);
3284 /* Set the new lengths according to where they are split. Construct
3285 * two new properties. */
3286 forward_prop->length = MARK_OFFSET(mark);
3288 new_prop_forward->data =
3289 new_text_property(text,
3290 forward_prop->flags & PROPERTY_FONT ?
3291 forward_prop->font->gdk_font : NULL,
3292 forward_prop->flags & PROPERTY_FOREGROUND ?
3293 &forward_prop->fore_color : NULL,
3294 forward_prop->flags & PROPERTY_BACKGROUND ?
3295 &forward_prop->back_color : NULL,
3296 old_length - forward_prop->length);
3298 new_prop->data = new_text_property(text, font, fore, back, len);
3300 /* Now splice things in. */
3301 MARK_NEXT_LIST_PTR(mark) = new_prop;
3302 new_prop->prev = MARK_LIST_PTR(mark);
3304 new_prop->next = new_prop_forward;
3305 new_prop_forward->prev = new_prop;
3307 new_prop_forward->next = next;
3310 next->prev = new_prop_forward;
3312 SET_PROPERTY_MARK (mark, new_prop, 0);
3316 while (text->text_properties_end->next)
3317 text->text_properties_end = text->text_properties_end->next;
3319 while (text->text_properties->prev)
3320 text->text_properties = text->text_properties->prev;
3324 delete_text_property (GtkText* text, guint nchars)
3326 /* Delete nchars forward from point. */
3328 /* Deleting text properties is problematical, because we
3329 * might be storing around marks pointing to a property.
3331 * The marks in question and how we handle them are:
3333 * point: We know the new value, since it will be at the
3334 * end of the deleted text, and we move it there
3336 * cursor: We just remove the mark and set it equal to the
3337 * point after the operation.
3338 * line-start cache: We replace most affected lines.
3339 * The current line gets used to fetch the new
3340 * lines so, if necessary, (delete at the beginning
3341 * of a line) we fix it up by setting it equal to the
3349 for(; nchars; nchars -= 1)
3351 prop = MARK_CURRENT_PROPERTY(&text->point);
3355 if (prop->length == 0)
3357 tmp = MARK_LIST_PTR (&text->point);
3359 is_first = tmp == text->text_properties;
3361 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3362 text->point.offset = 0;
3364 if (GTK_WIDGET_REALIZED (text))
3365 unrealize_property (text, prop);
3367 destroy_text_property (prop);
3368 g_list_free_1 (tmp);
3370 prop = MARK_CURRENT_PROPERTY (&text->point);
3373 text->text_properties = MARK_LIST_PTR (&text->point);
3375 g_assert (prop->length != 0);
3377 else if (prop->length == text->point.offset)
3379 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3380 text->point.offset = 0;
3384 /* Check to see if we have just the single final position remaining
3385 * along in a property; if so, combine it with the previous property
3387 if (LAST_INDEX (text, text->point) &&
3388 (MARK_OFFSET (&text->point) == 0) &&
3389 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3391 tmp = MARK_LIST_PTR (&text->point);
3392 prop = MARK_CURRENT_PROPERTY(&text->point);
3394 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3395 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3396 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3398 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3400 if (GTK_WIDGET_REALIZED (text))
3401 unrealize_property (text, prop);
3403 destroy_text_property (prop);
3404 g_list_free_1 (tmp);
3409 init_properties (GtkText *text)
3411 if (!text->text_properties)
3413 text->text_properties = g_list_alloc();
3414 text->text_properties->next = NULL;
3415 text->text_properties->prev = NULL;
3416 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3417 text->text_properties_end = text->text_properties;
3419 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3421 text->point.index = 0;
3426 /**********************************************************************/
3427 /* Property Movement */
3428 /**********************************************************************/
3431 move_mark_n (GtkPropertyMark* mark, gint n)
3434 advance_mark_n(mark, n);
3436 decrement_mark_n(mark, -n);
3440 advance_mark (GtkPropertyMark* mark)
3442 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3446 if (prop->length > mark->offset + 1)
3450 mark->property = MARK_NEXT_LIST_PTR (mark);
3456 advance_mark_n (GtkPropertyMark* mark, gint n)
3463 i = 0; /* otherwise it migth not be init. */
3464 prop = MARK_CURRENT_PROPERTY(mark);
3466 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3467 /* to make it easier */
3468 n += (mark->offset);
3469 mark->index -= mark->offset;
3471 /* first we take seven-mile-leaps to get to the right text
3473 while ((n-i) > prop->length - 1) {
3475 mark->index += prop->length;
3476 mark->property = MARK_NEXT_LIST_PTR (mark);
3477 prop = MARK_CURRENT_PROPERTY (mark);
3481 /* and then the rest */
3482 mark->index += n - i;
3483 mark->offset += n - i;
3487 decrement_mark (GtkPropertyMark* mark)
3491 if (mark->offset > 0)
3495 mark->property = MARK_PREV_LIST_PTR (mark);
3496 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3501 decrement_mark_n (GtkPropertyMark* mark, gint n)
3505 while (mark->offset < n) {
3506 /* jump to end of prev */
3507 n -= mark->offset + 1;
3508 mark->index -= mark->offset + 1;
3509 mark->property = MARK_PREV_LIST_PTR (mark);
3510 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3518 static GtkPropertyMark
3519 find_mark (GtkText* text, guint mark_position)
3521 return find_mark_near (text, mark_position, &text->point);
3525 * You can also start from the end, what a drag.
3527 static GtkPropertyMark
3528 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3533 GtkPropertyMark mark;
3536 diffa = mark_position + 1;
3538 diffa = mark_position - near->index;
3540 diffb = mark_position;
3552 mark.property = text->text_properties;
3556 move_mark_n (&mark, mark_position - mark.index);
3561 /* This routine must be called with scroll == FALSE, only when
3562 * point is at least partially on screen
3566 find_line_containing_point (GtkText* text, guint point,
3572 text->current_line = NULL;
3576 /* Scroll backwards until the point is on screen
3578 while (CACHE_DATA(text->line_start_cache).start.index > point)
3579 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3581 /* Now additionally try to make sure that the point is fully on screen
3585 while (text->first_cut_pixels != 0 &&
3586 text->line_start_cache->next &&
3587 CACHE_DATA(text->line_start_cache->next).start.index > point)
3588 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3591 gdk_window_get_size (text->text_area, NULL, &height);
3593 for (cache = text->line_start_cache; cache; cache = cache->next)
3597 if (CACHE_DATA(cache).end.index >= point ||
3598 LAST_INDEX(text, CACHE_DATA(cache).end))
3600 text->current_line = cache; /* LOOK HERE, this proc has an
3601 * important side effect. */
3605 TEXT_SHOW_LINE (text, cache, "cache");
3607 if (cache->next == NULL)
3608 fetch_lines_forward (text, 1);
3612 lph = pixel_height_of (text, cache->next);
3614 /* Scroll the bottom of the line is on screen, or until
3615 * the line is the first onscreen line.
3617 while (cache->next != text->line_start_cache && lph > height)
3619 TEXT_SHOW_LINE (text, cache, "cache");
3620 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3621 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3622 lph = pixel_height_of (text, cache->next);
3627 g_assert_not_reached (); /* Must set text->current_line here */
3631 pixel_height_of (GtkText* text, GList* cache_line)
3633 gint pixels = - text->first_cut_pixels;
3634 GList *cache = text->line_start_cache;
3637 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3639 if (cache->data == cache_line->data)
3642 cache = cache->next;
3648 /**********************************************************************/
3649 /* Search and Placement */
3650 /**********************************************************************/
3653 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3656 gint16* char_widths;
3658 if (LAST_INDEX (text, *mark))
3661 ch = GTK_TEXT_INDEX (text, mark->index);
3662 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3666 return tab_mark->to_next_tab * char_widths[' '];
3670 return char_widths[ch];
3674 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3679 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, GdkWChar ch)
3681 if (tab_mark->to_next_tab == 1 || ch == '\t')
3683 if (tab_mark->tab_stops->next)
3685 tab_mark->tab_stops = tab_mark->tab_stops->next;
3686 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3690 tab_mark->to_next_tab = text->default_tab_width;
3695 tab_mark->to_next_tab -= 1;
3700 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3704 advance_tab_mark (text, tab_mark, 0);
3708 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3711 GtkEditable *editable = (GtkEditable *)text;
3713 GtkPropertyMark mark = start_line->start;
3714 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3715 gint pixel_width = LINE_START_PIXEL (*start_line);
3717 while (mark.index < text->cursor_mark.index)
3719 pixel_width += find_char_width (text, &mark, &tab_mark);
3721 advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3722 advance_mark (&mark);
3725 text->cursor_pos_x = pixel_width;
3726 text->cursor_pos_y = pixel_height;
3727 text->cursor_char_offset = start_line->font_descent;
3728 text->cursor_mark = mark;
3730 ch = LAST_INDEX (text, mark) ?
3731 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3733 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3734 text->cursor_char = 0;
3736 text->cursor_char = ch;
3739 if (GTK_WIDGET_HAS_FOCUS(text) && gdk_im_ready() && editable->ic &&
3740 (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
3742 GdkICAttributesType mask = GDK_IC_SPOT_LOCATION |
3743 GDK_IC_PREEDIT_FOREGROUND |
3744 GDK_IC_PREEDIT_BACKGROUND;
3746 editable->ic_attr->spot_location.x = text->cursor_pos_x;
3747 editable->ic_attr->spot_location.y
3748 = text->cursor_pos_y - text->cursor_char_offset;
3749 editable->ic_attr->preedit_foreground = *MARK_CURRENT_FORE (text, &mark);
3750 editable->ic_attr->preedit_background = *MARK_CURRENT_BACK (text, &mark);
3752 if (MARK_CURRENT_FONT (text, &mark)->type == GDK_FONT_FONTSET)
3754 mask |= GDK_IC_PREEDIT_FONTSET;
3755 editable->ic_attr->preedit_fontset = MARK_CURRENT_FONT (text, &mark);
3758 gdk_ic_set_attr (editable->ic, editable->ic_attr, mask);
3764 find_cursor (GtkText* text, gboolean scroll)
3766 if (GTK_WIDGET_REALIZED (text))
3768 find_line_containing_point (text, text->cursor_mark.index, scroll);
3770 if (text->current_line)
3771 find_cursor_at_line (text,
3772 &CACHE_DATA(text->current_line),
3773 pixel_height_of(text, text->current_line));
3776 GTK_EDITABLE (text)->current_pos = text->cursor_mark.index;
3780 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3781 guint line_pixel_height,
3784 GtkPropertyMark mark = lp->start;
3785 TabStopMark tab_mark = lp->tab_cont.tab_start;
3787 gint char_width = find_char_width(text, &mark, &tab_mark);
3788 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3790 text->cursor_pos_y = line_pixel_height;
3794 GdkWChar ch = LAST_INDEX (text, mark) ?
3795 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3797 if (button_x < pixel_width || mark.index == lp->end.index)
3799 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3800 text->cursor_mark = mark;
3801 text->cursor_char_offset = lp->font_descent;
3803 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3804 text->cursor_char = 0;
3806 text->cursor_char = ch;
3811 advance_tab_mark (text, &tab_mark, ch);
3812 advance_mark (&mark);
3814 pixel_width += char_width/2;
3816 char_width = find_char_width (text, &mark, &tab_mark);
3818 pixel_width += (char_width+1)/2;
3823 find_mouse_cursor (GtkText* text, gint x, gint y)
3826 GList* cache = text->line_start_cache;
3830 pixel_height = - text->first_cut_pixels;
3832 for (; cache; cache = cache->next)
3834 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3836 if (y < pixel_height || !cache->next)
3838 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3840 find_cursor (text, FALSE);
3847 /**********************************************************************/
3849 /**********************************************************************/
3852 free_cache (GtkText* text)
3854 GList* cache = text->line_start_cache;
3859 cache = cache->prev;
3861 text->line_start_cache = cache;
3864 for (; cache; cache = cache->next)
3865 g_mem_chunk_free (params_mem_chunk, cache->data);
3867 g_list_free (text->line_start_cache);
3869 text->line_start_cache = NULL;
3873 remove_cache_line (GtkText* text, GList* member)
3880 if (member == text->line_start_cache)
3881 text->line_start_cache = text->line_start_cache->next;
3884 member->prev->next = member->next;
3887 member->next->prev = member->prev;
3889 list = member->next;
3891 g_mem_chunk_free (params_mem_chunk, member->data);
3892 g_list_free_1 (member);
3897 /**********************************************************************/
3899 /**********************************************************************/
3902 move_cursor_buffer_ver (GtkText *text, int dir)
3904 undraw_cursor (text, FALSE);
3908 scroll_int (text, text->vadj->upper);
3909 text->cursor_mark = find_this_line_start_mark (text,
3911 &text->cursor_mark);
3915 scroll_int (text, - text->vadj->value);
3916 text->cursor_mark = find_this_line_start_mark (text,
3918 &text->cursor_mark);
3921 find_cursor (text, TRUE);
3922 draw_cursor (text, FALSE);
3926 move_cursor_page_ver (GtkText *text, int dir)
3928 scroll_int (text, dir * text->vadj->page_increment);
3932 move_cursor_ver (GtkText *text, int count)
3935 GtkPropertyMark mark;
3938 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3939 offset = text->cursor_mark.index - mark.index;
3941 if (offset > text->cursor_virtual_x)
3942 text->cursor_virtual_x = offset;
3946 if (mark.index == 0)
3949 decrement_mark (&mark);
3950 mark = find_this_line_start_mark (text, mark.index, &mark);
3954 mark = text->cursor_mark;
3956 while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3957 advance_mark (&mark);
3959 if (LAST_INDEX(text, mark))
3962 advance_mark (&mark);
3965 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3966 if (LAST_INDEX(text, mark) ||
3967 GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3970 undraw_cursor (text, FALSE);
3972 text->cursor_mark = mark;
3974 find_cursor (text, TRUE);
3976 draw_cursor (text, FALSE);
3980 move_cursor_hor (GtkText *text, int count)
3982 /* count should be +-1. */
3983 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3984 (count < 0 && text->cursor_mark.index < (- count)) ||
3988 text->cursor_virtual_x = 0;
3990 undraw_cursor (text, FALSE);
3992 move_mark_n (&text->cursor_mark, count);
3994 find_cursor (text, TRUE);
3996 draw_cursor (text, FALSE);
4000 gtk_text_move_cursor (GtkEditable *editable,
4007 move_cursor_hor (GTK_TEXT (editable), 1);
4012 move_cursor_hor (GTK_TEXT (editable), -1);
4018 move_cursor_ver (GTK_TEXT (editable), 1);
4023 move_cursor_ver (GTK_TEXT (editable), -1);
4028 gtk_text_move_forward_character (GtkText *text)
4030 move_cursor_hor (text, 1);
4034 gtk_text_move_backward_character (GtkText *text)
4036 move_cursor_hor (text, -1);
4040 gtk_text_move_next_line (GtkText *text)
4042 move_cursor_ver (text, 1);
4046 gtk_text_move_previous_line (GtkText *text)
4048 move_cursor_ver (text, -1);
4052 gtk_text_move_word (GtkEditable *editable,
4058 gtk_text_move_forward_word (GTK_TEXT (editable));
4063 gtk_text_move_backward_word (GTK_TEXT (editable));
4068 gtk_text_move_forward_word (GtkText *text)
4070 text->cursor_virtual_x = 0;
4072 undraw_cursor (text, FALSE);
4074 if (text->use_wchar)
4076 while (!LAST_INDEX (text, text->cursor_mark) &&
4077 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4078 advance_mark (&text->cursor_mark);
4080 while (!LAST_INDEX (text, text->cursor_mark) &&
4081 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4082 advance_mark (&text->cursor_mark);
4086 while (!LAST_INDEX (text, text->cursor_mark) &&
4087 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4088 advance_mark (&text->cursor_mark);
4090 while (!LAST_INDEX (text, text->cursor_mark) &&
4091 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
4092 advance_mark (&text->cursor_mark);
4095 find_cursor (text, TRUE);
4096 draw_cursor (text, FALSE);
4100 gtk_text_move_backward_word (GtkText *text)
4102 text->cursor_virtual_x = 0;
4104 undraw_cursor (text, FALSE);
4106 if (text->use_wchar)
4108 while ((text->cursor_mark.index > 0) &&
4109 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4110 decrement_mark (&text->cursor_mark);
4112 while ((text->cursor_mark.index > 0) &&
4113 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4114 decrement_mark (&text->cursor_mark);
4118 while ((text->cursor_mark.index > 0) &&
4119 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4120 decrement_mark (&text->cursor_mark);
4122 while ((text->cursor_mark.index > 0) &&
4123 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4124 decrement_mark (&text->cursor_mark);
4127 find_cursor (text, TRUE);
4128 draw_cursor (text, FALSE);
4132 gtk_text_move_page (GtkEditable *editable,
4137 scroll_int (GTK_TEXT (editable),
4138 y * GTK_TEXT(editable)->vadj->page_increment);
4142 gtk_text_move_to_row (GtkEditable *editable,
4148 gtk_text_move_to_column (GtkEditable *editable,
4153 text = GTK_TEXT (editable);
4155 text->cursor_virtual_x = 0; /* FIXME */
4157 undraw_cursor (text, FALSE);
4159 /* Move to the beginning of the line */
4160 while ((text->cursor_mark.index > 0) &&
4161 (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4162 decrement_mark (&text->cursor_mark);
4164 while (!LAST_INDEX (text, text->cursor_mark) &&
4165 (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4169 else if (column == 0)
4172 advance_mark (&text->cursor_mark);
4175 find_cursor (text, TRUE);
4176 draw_cursor (text, FALSE);
4180 gtk_text_move_beginning_of_line (GtkText *text)
4182 gtk_text_move_to_column (GTK_EDITABLE (text), 0);
4187 gtk_text_move_end_of_line (GtkText *text)
4189 gtk_text_move_to_column (GTK_EDITABLE (text), -1);
4193 gtk_text_kill_char (GtkEditable *editable,
4198 text = GTK_TEXT (editable);
4200 if (editable->selection_start_pos != editable->selection_end_pos)
4201 gtk_editable_delete_selection (editable);
4206 if (text->point.index + 1 <= TEXT_LENGTH (text))
4207 gtk_editable_delete_text (editable, text->point.index, text->point.index + 1);
4211 if (text->point.index > 0)
4212 gtk_editable_delete_text (editable, text->point.index - 1, text->point.index);
4218 gtk_text_delete_forward_character (GtkText *text)
4220 gtk_text_kill_char (GTK_EDITABLE (text), 1);
4224 gtk_text_delete_backward_character (GtkText *text)
4226 gtk_text_kill_char (GTK_EDITABLE (text), -1);
4230 gtk_text_kill_word (GtkEditable *editable,
4233 if (editable->selection_start_pos != editable->selection_end_pos)
4234 gtk_editable_delete_selection (editable);
4237 gint old_pos = editable->current_pos;
4240 gtk_text_move_word (editable, 1);
4241 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
4245 gtk_text_move_word (editable, -1);
4246 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
4252 gtk_text_delete_forward_word (GtkText *text)
4254 gtk_text_kill_word (GTK_EDITABLE (text), 1);
4258 gtk_text_delete_backward_word (GtkText *text)
4260 gtk_text_kill_word (GTK_EDITABLE (text), -1);
4264 gtk_text_kill_line (GtkEditable *editable,
4267 gint old_pos = editable->current_pos;
4270 gtk_text_move_to_column (editable, -1);
4271 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
4275 gtk_text_move_to_column (editable, 0);
4276 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
4281 gtk_text_delete_line (GtkText *text)
4283 gtk_text_move_to_column (GTK_EDITABLE (text), 0);
4284 gtk_text_kill_line (GTK_EDITABLE (text), 1);
4288 gtk_text_delete_to_line_end (GtkText *text)
4290 gtk_text_kill_line (GTK_EDITABLE (text), 1);
4294 gtk_text_select_word (GtkText *text, guint32 time)
4299 GtkEditable *editable;
4300 editable = GTK_EDITABLE (text);
4302 gtk_text_move_backward_word (text);
4303 start_pos = text->cursor_mark.index;
4305 gtk_text_move_forward_word (text);
4306 end_pos = text->cursor_mark.index;
4308 editable->has_selection = TRUE;
4309 gtk_text_set_selection (editable, start_pos, end_pos);
4310 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
4314 gtk_text_select_line (GtkText *text, guint32 time)
4319 GtkEditable *editable;
4320 editable = GTK_EDITABLE (text);
4322 gtk_text_move_beginning_of_line (text);
4323 start_pos = text->cursor_mark.index;
4325 gtk_text_move_end_of_line (text);
4326 gtk_text_move_forward_character (text);
4327 end_pos = text->cursor_mark.index;
4329 editable->has_selection = TRUE;
4330 gtk_text_set_selection (editable, start_pos, end_pos);
4331 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
4334 /**********************************************************************/
4336 /**********************************************************************/
4339 adjust_adj (GtkText* text, GtkAdjustment* adj)
4343 gdk_window_get_size (text->text_area, NULL, &height);
4345 adj->step_increment = MIN (adj->upper, (float) SCROLL_PIXELS);
4346 adj->page_increment = MIN (adj->upper, height - (float) KEY_SCROLL_PIXELS);
4347 adj->page_size = MIN (adj->upper, height);
4348 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4349 adj->value = MAX (adj->value, 0.0);
4351 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4355 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
4357 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4359 if ((text->first_line_start_index >= lp->start.index) &&
4360 (text->first_line_start_index <= lp->end.index))
4362 svdata->mark = lp->start;
4364 if (text->first_line_start_index == lp->start.index)
4366 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4370 text->first_onscreen_ver_pixel = svdata->pixel_height;
4371 text->first_cut_pixels = 0;
4374 text->vadj->value = (float) text->first_onscreen_ver_pixel;
4377 svdata->pixel_height += LINE_HEIGHT (*lp);
4383 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
4385 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4388 if (svdata->pixel_height <= (gint) text->vadj->value &&
4389 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4391 svdata->mark = lp->start;
4393 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4394 text->first_onscreen_ver_pixel = svdata->pixel_height;
4395 text->first_line_start_index = lp->start.index;
4401 svdata->pixel_height += LINE_HEIGHT (*lp);
4409 static GtkPropertyMark
4410 set_vertical_scroll (GtkText* text)
4412 GtkPropertyMark mark = find_mark (text, 0);
4413 SetVerticalScrollData data;
4417 data.pixel_height = 0;
4418 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4420 text->vadj->upper = (float) data.pixel_height;
4421 orig_value = (gint) text->vadj->value;
4423 gdk_window_get_size (text->text_area, NULL, &height);
4425 text->vadj->step_increment = MIN (text->vadj->upper, (float) SCROLL_PIXELS);
4426 text->vadj->page_increment = MIN (text->vadj->upper, height - (float) KEY_SCROLL_PIXELS);
4427 text->vadj->page_size = MIN (text->vadj->upper, height);
4428 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4429 text->vadj->value = MAX (text->vadj->value, 0.0);
4431 text->last_ver_value = (gint)text->vadj->value;
4433 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4435 if (text->vadj->value != orig_value)
4437 /* We got clipped, and don't really know which line to put first. */
4438 data.pixel_height = 0;
4439 data.last_didnt_wrap = TRUE;
4441 line_params_iterate (text, &mark, NULL,
4443 set_vertical_scroll_find_iterator);
4450 scroll_int (GtkText* text, gint diff)
4454 text->vadj->value += diff;
4456 upper = text->vadj->upper - text->vadj->page_size;
4457 text->vadj->value = MIN (text->vadj->value, upper);
4458 text->vadj->value = MAX (text->vadj->value, 0.0);
4460 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4464 process_exposes (GtkText *text)
4468 /* Make sure graphics expose events are processed before scrolling
4471 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4473 gtk_widget_event (GTK_WIDGET (text), event);
4474 if (event->expose.count == 0)
4476 gdk_event_free (event);
4479 gdk_event_free (event);
4483 static gint last_visible_line_height (GtkText* text)
4485 GList *cache = text->line_start_cache;
4488 gdk_window_get_size (text->text_area, NULL, &height);
4490 for (; cache->next; cache = cache->next)
4491 if (pixel_height_of(text, cache->next) > height)
4495 return pixel_height_of(text, cache) - 1;
4500 static gint first_visible_line_height (GtkText* text)
4502 if (text->first_cut_pixels)
4503 return pixel_height_of(text, text->line_start_cache) + 1;
4509 scroll_down (GtkText* text, gint diff0)
4515 text->first_onscreen_ver_pixel += diff0;
4519 g_assert (text->line_start_cache);
4521 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4523 text->first_cut_pixels += 1;
4527 text->first_cut_pixels = 0;
4529 text->line_start_cache = text->line_start_cache->next;
4531 text->first_line_start_index =
4532 CACHE_DATA(text->line_start_cache).start.index;
4534 if (!text->line_start_cache->next)
4535 fetch_lines_forward (text, 1);
4541 gdk_window_get_size (text->text_area, &width, &height);
4542 if (height > real_diff)
4543 gdk_draw_pixmap (text->text_area,
4551 height - real_diff);
4554 rect.y = MAX (0, height - real_diff);
4556 rect.height = MIN (height, real_diff);
4558 expose_text (text, &rect, FALSE);
4559 gtk_text_draw_focus ( (GtkWidget *) text);
4561 if (text->current_line)
4565 text->cursor_pos_y -= real_diff;
4566 cursor_min = drawn_cursor_min(text);
4569 find_mouse_cursor (text, text->cursor_pos_x,
4570 first_visible_line_height (text));
4573 if (height > real_diff)
4574 process_exposes (text);
4578 scroll_up (GtkText* text, gint diff0)
4584 text->first_onscreen_ver_pixel += diff0;
4588 g_assert (text->line_start_cache);
4590 if (text->first_cut_pixels > 0)
4592 text->first_cut_pixels -= 1;
4596 if (!text->line_start_cache->prev)
4597 fetch_lines_backward (text);
4599 text->line_start_cache = text->line_start_cache->prev;
4601 text->first_line_start_index =
4602 CACHE_DATA(text->line_start_cache).start.index;
4604 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4610 gdk_window_get_size (text->text_area, &width, &height);
4611 if (height > real_diff)
4612 gdk_draw_pixmap (text->text_area,
4620 height - real_diff);
4625 rect.height = MIN (height, real_diff);
4627 expose_text (text, &rect, FALSE);
4628 gtk_text_draw_focus ( (GtkWidget *) text);
4630 if (text->current_line)
4635 text->cursor_pos_y += real_diff;
4636 cursor_max = drawn_cursor_max(text);
4637 gdk_window_get_size (text->text_area, NULL, &height);
4639 if (cursor_max >= height)
4640 find_mouse_cursor (text, text->cursor_pos_x,
4641 last_visible_line_height (text));
4644 if (height > real_diff)
4645 process_exposes (text);
4648 /**********************************************************************/
4650 /**********************************************************************/
4652 /* Assumes mark starts a line. Calculates the height, width, and
4653 * displayable character count of a single DISPLAYABLE line. That
4654 * means that in line-wrap mode, this does may not compute the
4655 * properties of an entire line. */
4657 find_line_params (GtkText* text,
4658 const GtkPropertyMark* mark,
4659 const PrevTabCont *tab_cont,
4660 PrevTabCont *next_cont)
4663 TabStopMark tab_mark = tab_cont->tab_start;
4664 guint max_display_pixels;
4669 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4670 max_display_pixels -= LINE_WRAP_ROOM;
4673 lp.tab_cont = *tab_cont;
4676 lp.pixel_width = tab_cont->pixel_offset;
4677 lp.displayable_chars = 0;
4679 lp.font_descent = 0;
4681 init_tab_cont (text, next_cont);
4683 while (!LAST_INDEX(text, lp.end))
4685 g_assert (lp.end.property);
4687 ch = GTK_TEXT_INDEX (text, lp.end.index);
4688 font = MARK_CURRENT_FONT (text, &lp.end);
4690 if (ch == LINE_DELIM)
4692 /* Newline doesn't count in computation of line height, even
4693 * if its in a bigger font than the rest of the line. Unless,
4694 * of course, there are no other characters. */
4696 if (!lp.font_ascent && !lp.font_descent)
4698 lp.font_ascent = font->ascent;
4699 lp.font_descent = font->descent;
4702 lp.tab_cont_next = *next_cont;
4707 ch_width = find_char_width (text, &lp.end, &tab_mark);
4709 if ((ch_width + lp.pixel_width > max_display_pixels) &&
4710 (lp.end.index > lp.start.index))
4714 if (text->line_wrap)
4716 next_cont->tab_start = tab_mark;
4717 next_cont->pixel_offset = 0;
4721 /* Here's the tough case, a tab is wrapping. */
4722 gint pixels_avail = max_display_pixels - lp.pixel_width;
4723 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
4724 gint spaces_avail = pixels_avail / space_width;
4726 if (spaces_avail == 0)
4728 decrement_mark (&lp.end);
4732 advance_tab_mark (text, &next_cont->tab_start, '\t');
4733 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4735 lp.displayable_chars += 1;
4740 if (text->word_wrap)
4742 GtkPropertyMark saved_mark = lp.end;
4743 guint saved_characters = lp.displayable_chars;
4745 lp.displayable_chars += 1;
4747 if (text->use_wchar)
4749 while (!gdk_iswspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4750 (lp.end.index > lp.start.index))
4752 decrement_mark (&lp.end);
4753 lp.displayable_chars -= 1;
4758 while (!isspace(GTK_TEXT_INDEX (text, lp.end.index)) &&
4759 (lp.end.index > lp.start.index))
4761 decrement_mark (&lp.end);
4762 lp.displayable_chars -= 1;
4766 /* If whole line is one word, revert to char wrapping */
4767 if (lp.end.index == lp.start.index)
4769 lp.end = saved_mark;
4770 lp.displayable_chars = saved_characters;
4771 decrement_mark (&lp.end);
4776 /* Don't include this character, it will wrap. */
4777 decrement_mark (&lp.end);
4781 lp.tab_cont_next = *next_cont;
4788 lp.displayable_chars += 1;
4791 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4792 lp.font_descent = MAX (font->descent, lp.font_descent);
4793 lp.pixel_width += ch_width;
4795 advance_mark(&lp.end);
4796 advance_tab_mark (text, &tab_mark, ch);
4799 if (LAST_INDEX(text, lp.start))
4801 /* Special case, empty last line. */
4802 font = MARK_CURRENT_FONT (text, &lp.end);
4804 lp.font_ascent = font->ascent;
4805 lp.font_descent = font->descent;
4808 lp.tab_cont_next = *next_cont;
4814 expand_scratch_buffer (GtkText* text, guint len)
4816 if (len >= text->scratch_buffer_len)
4820 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4822 if (text->use_wchar)
4824 if (text->scratch_buffer.wc)
4825 text->scratch_buffer.wc = g_new (GdkWChar, i);
4827 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
4828 i*sizeof (GdkWChar));
4832 if (text->scratch_buffer.ch)
4833 text->scratch_buffer.ch = g_new (guchar, i);
4835 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
4838 text->scratch_buffer_len = i;
4842 /* Side effect: modifies text->gc
4846 draw_bg_rect (GtkText* text, GtkPropertyMark *mark,
4847 gint x, gint y, gint width, gint height,
4848 gboolean already_cleared)
4850 GtkEditable *editable = GTK_EDITABLE(text);
4852 if ((mark->index >= MIN(editable->selection_start_pos, editable->selection_end_pos) &&
4853 mark->index < MAX(editable->selection_start_pos, editable->selection_end_pos)))
4855 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
4856 editable->has_selection ?
4857 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
4859 NULL, GTK_WIDGET(text), "text",
4860 x, y, width, height);
4862 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
4863 >K_WIDGET(text)->style->base[GTK_STATE_NORMAL]))
4865 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
4867 gdk_draw_rectangle (text->text_area,
4869 TRUE, x, y, width, height);
4871 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4878 rect.height = height;
4880 clear_area (text, &rect);
4882 else if (!already_cleared)
4883 gdk_window_clear_area (text->text_area, x, y, width, height);
4887 draw_line (GtkText* text,
4888 gint pixel_start_height,
4891 GdkGCValues gc_values;
4894 guint running_offset = lp->tab_cont.pixel_offset;
4895 union { GdkWChar *wc; guchar *ch; } buffer;
4898 GtkEditable *editable = GTK_EDITABLE(text);
4900 guint selection_start_pos = MIN (editable->selection_start_pos, editable->selection_end_pos);
4901 guint selection_end_pos = MAX (editable->selection_start_pos, editable->selection_end_pos);
4903 GtkPropertyMark mark = lp->start;
4904 TabStopMark tab_mark = lp->tab_cont.tab_start;
4905 gint pixel_height = pixel_start_height + lp->font_ascent;
4906 guint chars = lp->displayable_chars;
4908 /* First provide a contiguous segment of memory. This makes reading
4909 * the code below *much* easier, and only incurs the cost of copying
4910 * when the line being displayed spans the gap. */
4911 if (mark.index <= text->gap_position &&
4912 mark.index + chars > text->gap_position)
4914 expand_scratch_buffer (text, chars);
4916 if (text->use_wchar)
4918 for (i = 0; i < chars; i += 1)
4919 text->scratch_buffer.wc[i] = GTK_TEXT_INDEX(text, mark.index + i);
4920 buffer.wc = text->scratch_buffer.wc;
4924 for (i = 0; i < chars; i += 1)
4925 text->scratch_buffer.ch[i] = GTK_TEXT_INDEX(text, mark.index + i);
4926 buffer.ch = text->scratch_buffer.ch;
4931 if (text->use_wchar)
4933 if (mark.index >= text->gap_position)
4934 buffer.wc = text->text.wc + mark.index + text->gap_size;
4936 buffer.wc = text->text.wc + mark.index;
4940 if (mark.index >= text->gap_position)
4941 buffer.ch = text->text.ch + mark.index + text->gap_size;
4943 buffer.ch = text->text.ch + mark.index;
4948 if (running_offset > 0)
4950 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
4951 LINE_HEIGHT (*lp), TRUE);
4957 if ((text->use_wchar && buffer.wc[0] != '\t') ||
4958 (!text->use_wchar && buffer.ch[0] != '\t'))
4960 union { GdkWChar *wc; guchar *ch; } next_tab;
4965 if (text->use_wchar)
4966 for (i=0; i<chars; i++)
4968 if (buffer.wc[i] == '\t')
4970 next_tab.wc = buffer.wc + i;
4975 next_tab.ch = memchr (buffer.ch, '\t', chars);
4977 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4979 if (text->use_wchar)
4982 len = MIN (len, next_tab.wc - buffer.wc);
4987 len = MIN (len, next_tab.ch - buffer.ch);
4990 if (mark.index < selection_start_pos)
4991 len = MIN (len, selection_start_pos - mark.index);
4992 else if (mark.index < selection_end_pos)
4993 len = MIN (len, selection_end_pos - mark.index);
4995 font = MARK_CURRENT_FONT (text, &mark);
4996 if (font->type == GDK_FONT_FONT)
4998 gdk_gc_set_font (text->gc, font);
4999 gdk_gc_get_values (text->gc, &gc_values);
5000 if (text->use_wchar)
5001 pixel_width = gdk_text_width_wc (gc_values.font,
5004 pixel_width = gdk_text_width (gc_values.font,
5009 if (text->use_wchar)
5010 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
5012 pixel_width = gdk_text_width (font, buffer.ch, len);
5015 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
5016 pixel_width, LINE_HEIGHT (*lp), TRUE);
5018 if ((mark.index >= selection_start_pos) &&
5019 (mark.index < selection_end_pos))
5021 if (editable->has_selection)
5022 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_SELECTED];
5024 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_ACTIVE];
5028 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
5032 if (text->use_wchar)
5033 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
5040 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
5047 running_offset += pixel_width;
5049 advance_tab_mark_n (text, &tab_mark, len);
5053 gint pixels_remaining;
5059 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
5060 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
5062 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
5064 spaces_avail = pixels_remaining / space_width;
5065 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
5067 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
5068 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
5070 running_offset += tab_mark.to_next_tab *
5071 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
5073 advance_tab_mark (text, &tab_mark, '\t');
5076 advance_mark_n (&mark, len);
5077 if (text->use_wchar)
5086 draw_line_wrap (GtkText* text, guint height /* baseline height */)
5093 if (text->line_wrap)
5095 bitmap = text->line_wrap_bitmap;
5096 bitmap_width = line_wrap_width;
5097 bitmap_height = line_wrap_height;
5101 bitmap = text->line_arrow_bitmap;
5102 bitmap_width = line_arrow_width;
5103 bitmap_height = line_arrow_height;
5106 gdk_window_get_size (text->text_area, &width, NULL);
5107 width -= LINE_WRAP_ROOM;
5109 gdk_gc_set_stipple (text->gc,
5112 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
5114 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5116 gdk_gc_set_ts_origin (text->gc,
5118 height - bitmap_height - 1);
5120 gdk_draw_rectangle (text->text_area,
5124 height - bitmap_height - 1 /* one pixel above the baseline. */,
5128 gdk_gc_set_ts_origin (text->gc, 0, 0);
5130 gdk_gc_set_fill (text->gc, GDK_SOLID);
5134 undraw_cursor (GtkText* text, gint absolute)
5136 GtkEditable *editable = (GtkEditable *)text;
5138 TDEBUG (("in undraw_cursor\n"));
5141 text->cursor_drawn_level = 0;
5143 if ((text->cursor_drawn_level ++ == 0) &&
5144 (editable->selection_start_pos == editable->selection_end_pos) &&
5145 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5149 g_assert(text->cursor_mark.property);
5151 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5153 draw_bg_rect (text, &text->cursor_mark,
5155 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5156 1, font->ascent + 1, FALSE);
5158 if (text->cursor_char)
5160 if (font->type == GDK_FONT_FONT)
5161 gdk_gc_set_font (text->gc, font);
5163 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5165 gdk_draw_text_wc (text->text_area, font,
5168 text->cursor_pos_y - text->cursor_char_offset,
5176 drawn_cursor_min (GtkText* text)
5180 g_assert(text->cursor_mark.property);
5182 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5184 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5188 drawn_cursor_max (GtkText* text)
5192 g_assert(text->cursor_mark.property);
5194 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5196 return text->cursor_pos_y - text->cursor_char_offset;
5200 draw_cursor (GtkText* text, gint absolute)
5202 GtkEditable *editable = (GtkEditable *)text;
5204 TDEBUG (("in draw_cursor\n"));
5207 text->cursor_drawn_level = 1;
5209 if ((--text->cursor_drawn_level == 0) &&
5210 editable->editable &&
5211 (editable->selection_start_pos == editable->selection_end_pos) &&
5212 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5216 g_assert (text->cursor_mark.property);
5218 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5220 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5222 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
5223 text->cursor_pos_y - text->cursor_char_offset,
5225 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5230 create_bg_gc (GtkText *text)
5234 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5235 values.fill = GDK_TILED;
5237 return gdk_gc_new_with_values (text->text_area, &values,
5238 GDK_GC_FILL | GDK_GC_TILE);
5242 clear_area (GtkText *text, GdkRectangle *area)
5244 GtkWidget *widget = GTK_WIDGET (text);
5250 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5252 gdk_gc_set_ts_origin (text->bg_gc,
5253 (- text->first_onscreen_hor_pixel) % width,
5254 (- text->first_onscreen_ver_pixel) % height);
5256 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5257 area->x, area->y, area->width, area->height);
5260 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5264 expose_text (GtkText* text, GdkRectangle *area, gboolean cursor)
5266 GList *cache = text->line_start_cache;
5267 gint pixels = - text->first_cut_pixels;
5268 gint min_y = MAX (0, area->y);
5269 gint max_y = MAX (0, area->y + area->height);
5272 gdk_window_get_size (text->text_area, NULL, &height);
5273 max_y = MIN (max_y, height);
5275 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5277 clear_area (text, area);
5279 for (; pixels < height; cache = cache->next)
5281 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5283 draw_line (text, pixels, &CACHE_DATA(cache));
5285 if (CACHE_DATA(cache).wraps)
5286 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5289 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5291 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5292 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5294 /* We undraw and draw the cursor here to get the drawn
5295 * level right ... FIXME - maybe the second parameter
5296 * of draw_cursor should work differently
5298 undraw_cursor (text, FALSE);
5299 draw_cursor (text, FALSE);
5303 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5307 fetch_lines_forward (text, 1);
5316 gtk_text_update_text (GtkEditable *editable,
5320 GtkText *text = GTK_TEXT (editable);
5322 GList *cache = text->line_start_cache;
5323 gint pixels = - text->first_cut_pixels;
5329 end_pos = TEXT_LENGTH (text);
5331 if (end_pos < start_pos)
5334 gdk_window_get_size (text->text_area, &width, &height);
5340 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5342 for (; pixels < height; cache = cache->next)
5344 if (CACHE_DATA(cache).start.index < end_pos)
5346 if (CACHE_DATA(cache).end.index >= start_pos)
5349 area.y = MAX(0,pixels);
5350 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5356 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5360 fetch_lines_forward (text, 1);
5368 expose_text (text, &area, TRUE);
5372 recompute_geometry (GtkText* text)
5374 GtkPropertyMark mark, start_mark;
5381 mark = start_mark = set_vertical_scroll (text);
5383 /* We need a real start of a line when calling fetch_lines().
5384 * not the start of a wrapped line.
5386 while (mark.index > 0 &&
5387 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5388 decrement_mark (&mark);
5390 gdk_window_get_size (text->text_area, &width, &height);
5392 /* Fetch an entire line, to make sure that we get all the text
5393 * we backed over above, in addition to enough text to fill up
5394 * the space vertically
5397 new_lines = fetch_lines (text,
5403 mark = CACHE_DATA (g_list_last (new_lines)).end;
5404 if (!LAST_INDEX (text, mark))
5406 advance_mark (&mark);
5408 new_lines = g_list_concat (new_lines,
5413 height + text->first_cut_pixels));
5416 /* Now work forward to the actual first onscreen line */
5418 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5419 new_lines = new_lines->next;
5421 text->line_start_cache = new_lines;
5423 find_cursor (text, TRUE);
5426 /**********************************************************************/
5428 /**********************************************************************/
5431 gtk_text_set_selection (GtkEditable *editable,
5435 GtkText *text = GTK_TEXT (editable);
5437 guint start1, end1, start2, end2;
5440 end = TEXT_LENGTH (text);
5442 start1 = MIN(start,end);
5443 end1 = MAX(start,end);
5444 start2 = MIN(editable->selection_start_pos, editable->selection_end_pos);
5445 end2 = MAX(editable->selection_start_pos, editable->selection_end_pos);
5447 if (start2 < start1)
5451 tmp = start1; start1 = start2; start2 = tmp;
5452 tmp = end1; end1 = end2; end2 = tmp;
5455 undraw_cursor (text, FALSE);
5456 editable->selection_start_pos = start;
5457 editable->selection_end_pos = end;
5458 draw_cursor (text, FALSE);
5460 /* Expose only what changed */
5462 if (start1 < start2)
5463 gtk_text_update_text (editable, start1, MIN(end1, start2));
5466 gtk_text_update_text (editable, MAX(end1, start2), end2);
5467 else if (end2 < end1)
5468 gtk_text_update_text (editable, end2, end1);
5472 /**********************************************************************/
5474 /**********************************************************************/
5476 #ifdef DEBUG_GTK_TEXT
5478 gtk_text_show_cache_line (GtkText *text, GList *cache,
5479 const char* what, const char* func, gint line)
5481 LineParams *lp = &CACHE_DATA(cache);
5484 if (cache == text->line_start_cache)
5485 g_message ("Line Start Cache: ");
5487 if (cache == text->current_line)
5488 g_message("Current Line: ");
5490 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
5498 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
5499 g_message ("%c", GTK_TEXT_INDEX (text, i));
5505 gtk_text_show_cache (GtkText *text, const char* func, gint line)
5507 GList *l = text->line_start_cache;
5513 /* back up to the absolute beginning of the line cache */
5517 g_message ("*** line cache ***\n");
5518 for (; l; l = l->next)
5519 gtk_text_show_cache_line (text, l, "all", func, line);
5523 gtk_text_assert_mark (GtkText *text,
5524 GtkPropertyMark *mark,
5525 GtkPropertyMark *before,
5526 GtkPropertyMark *after,
5531 GtkPropertyMark correct_mark = find_mark (text, mark->index);
5533 if (mark->offset != correct_mark.offset ||
5534 mark->property != correct_mark.property)
5535 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
5539 gtk_text_assert (GtkText *text,
5543 GList* cache = text->line_start_cache;
5544 GtkPropertyMark* before_mark = NULL;
5545 GtkPropertyMark* after_mark = NULL;
5547 gtk_text_show_props (text, msg, line);
5549 for (; cache->prev; cache = cache->prev)
5552 g_message ("*** line markers ***\n");
5554 for (; cache; cache = cache->next)
5556 after_mark = &CACHE_DATA(cache).end;
5557 gtk_text_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
5558 before_mark = &CACHE_DATA(cache).start;
5561 after_mark = &CACHE_DATA(cache->next).start;
5565 gtk_text_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
5566 before_mark = &CACHE_DATA(cache).end;
5571 gtk_text_show_adj (GtkText *text,
5577 g_message ("*** adjustment ***\n");
5579 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
5586 adj->step_increment,
5587 adj->page_increment,
5592 gtk_text_show_props (GtkText *text,
5596 GList* props = text->text_properties;
5599 g_message ("%s:%d: ", msg, line);
5601 for (; props; props = props->next)
5603 TextProperty *p = (TextProperty*)props->data;
5605 proplen += p->length;
5607 g_message ("[%d,%p,", p->length, p);
5608 if (p->flags & PROPERTY_FONT)
5609 g_message ("%p,", p->font);
5612 if (p->flags & PROPERTY_FOREGROUND)
5613 g_message ("%ld, ", p->fore_color.pixel);
5616 if (p->flags & PROPERTY_BACKGROUND)
5617 g_message ("%ld] ", p->back_color.pixel);
5624 if (proplen - 1 != TEXT_LENGTH(text))
5625 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);