1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #undef GDK_DISABLE_DEPRECATED
34 #include "gdk/gdkkeysyms.h"
35 #include "gdk/gdki18n.h"
37 #undef GTK_DISABLE_DEPRECATED
40 #include "gtkmarshalers.h"
41 #include "gtkselection.h"
42 #include "gtksignal.h"
44 #define GTK_ENABLE_BROKEN
46 #include "line-wrap.xbm"
47 #include "line-arrow.xbm"
48 #include "gtkprivate.h"
51 #define GTK_DISABLE_DEPRECATED
55 #define INITIAL_BUFFER_SIZE 1024
56 #define INITIAL_LINE_CACHE_SIZE 256
57 #define MIN_GAP_SIZE 256
58 #define LINE_DELIM '\n'
59 #define MIN_TEXT_WIDTH_LINES 20
60 #define MIN_TEXT_HEIGHT_LINES 10
61 #define TEXT_BORDER_ROOM 1
62 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
63 #define DEFAULT_TAB_STOP_WIDTH 4
64 #define SCROLL_PIXELS 5
65 #define KEY_SCROLL_PIXELS 10
66 #define SCROLL_TIME 100
67 #define FREEZE_LENGTH 1024
68 /* Freeze text when inserting or deleting more than this many characters */
70 #define SET_PROPERTY_MARK(m, p, o) do { \
71 (m)->property = (p); \
74 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
75 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
76 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
77 (mark)->property->prev->data \
79 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
80 #define MARK_LIST_PTR(mark) ((mark)->property)
81 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
82 #define MARK_OFFSET(mark) ((mark)->offset)
83 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
86 #define MARK_CURRENT_FONT(text, mark) \
87 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
88 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
89 gtk_style_get_font (GTK_WIDGET (text)->style))
90 #define MARK_CURRENT_FORE(text, mark) \
91 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
92 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
93 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
94 #define MARK_CURRENT_BACK(text, mark) \
95 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
96 &MARK_CURRENT_PROPERTY(mark)->back_color : \
97 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
98 #define MARK_CURRENT_TEXT_FONT(text, mark) \
99 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
100 MARK_CURRENT_PROPERTY(mark)->font : \
103 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
104 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
105 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
106 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
107 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
108 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
109 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
110 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
120 typedef struct _TextProperty TextProperty;
121 typedef struct _TabStopMark TabStopMark;
122 typedef struct _PrevTabCont PrevTabCont;
123 typedef struct _FetchLinesData FetchLinesData;
124 typedef struct _LineParams LineParams;
125 typedef struct _SetVerticalScrollData SetVerticalScrollData;
127 typedef gint (*LineIteratorFunction) (GtkText* text, LineParams* lp, void* data);
135 struct _SetVerticalScrollData {
137 gint last_didnt_wrap;
138 gint last_line_start;
139 GtkPropertyMark mark;
144 /* The actual font. */
148 gint16 char_widths[256];
152 PROPERTY_FONT = 1 << 0,
153 PROPERTY_FOREGROUND = 1 << 1,
154 PROPERTY_BACKGROUND = 1 << 2
162 /* Background Color. */
165 /* Foreground Color. */
168 /* Show which properties are set */
169 TextPropertyFlags flags;
171 /* Length of this property. */
177 GList* tab_stops; /* Index into list containing the next tab position. If
178 * NULL, using default widths. */
185 TabStopMark tab_start;
188 struct _FetchLinesData
201 guint displayable_chars;
204 PrevTabCont tab_cont;
205 PrevTabCont tab_cont_next;
207 GtkPropertyMark start;
212 static void gtk_text_class_init (GtkTextClass *klass);
213 static void gtk_text_set_property (GObject *object,
217 static void gtk_text_get_property (GObject *object,
221 static void gtk_text_editable_init (GtkEditableClass *iface);
222 static void gtk_text_init (GtkText *text);
223 static void gtk_text_destroy (GtkObject *object);
224 static void gtk_text_finalize (GObject *object);
225 static void gtk_text_realize (GtkWidget *widget);
226 static void gtk_text_unrealize (GtkWidget *widget);
227 static void gtk_text_style_set (GtkWidget *widget,
228 GtkStyle *previous_style);
229 static void gtk_text_state_changed (GtkWidget *widget,
230 GtkStateType previous_state);
231 static void gtk_text_draw_focus (GtkWidget *widget);
232 static void gtk_text_size_request (GtkWidget *widget,
233 GtkRequisition *requisition);
234 static void gtk_text_size_allocate (GtkWidget *widget,
235 GtkAllocation *allocation);
236 static void gtk_text_adjustment (GtkAdjustment *adjustment,
238 static void gtk_text_insert_text (GtkEditable *editable,
239 const gchar *new_text,
240 gint new_text_length,
242 static void gtk_text_delete_text (GtkEditable *editable,
245 static void gtk_text_update_text (GtkOldEditable *old_editable,
248 static gchar *gtk_text_get_chars (GtkOldEditable *old_editable,
251 static void gtk_text_set_selection (GtkOldEditable *old_editable,
254 static void gtk_text_real_set_editable (GtkOldEditable *old_editable,
255 gboolean is_editable);
257 static void gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
261 static gint gtk_text_expose (GtkWidget *widget,
262 GdkEventExpose *event);
263 static gint gtk_text_button_press (GtkWidget *widget,
264 GdkEventButton *event);
265 static gint gtk_text_button_release (GtkWidget *widget,
266 GdkEventButton *event);
267 static gint gtk_text_motion_notify (GtkWidget *widget,
268 GdkEventMotion *event);
269 static gint gtk_text_key_press (GtkWidget *widget,
272 static void move_gap (GtkText* text, guint index);
273 static void make_forward_space (GtkText* text, guint len);
275 /* Property management */
276 static GtkTextFont* get_text_font (GdkFont* gfont);
277 static void text_font_unref (GtkTextFont *text_font);
279 static void insert_text_property (GtkText* text, GdkFont* font,
280 const GdkColor *fore, const GdkColor* back, guint len);
281 static TextProperty* new_text_property (GtkText *text, GdkFont* font,
282 const GdkColor* fore, const GdkColor* back, guint length);
283 static void destroy_text_property (TextProperty *prop);
284 static void init_properties (GtkText *text);
285 static void realize_property (GtkText *text, TextProperty *prop);
286 static void realize_properties (GtkText *text);
287 static void unrealize_property (GtkText *text, TextProperty *prop);
288 static void unrealize_properties (GtkText *text);
290 static void delete_text_property (GtkText* text, guint len);
292 static guint pixel_height_of (GtkText* text, GList* cache_line);
294 /* Property Movement and Size Computations */
295 static void advance_mark (GtkPropertyMark* mark);
296 static void decrement_mark (GtkPropertyMark* mark);
297 static void advance_mark_n (GtkPropertyMark* mark, gint n);
298 static void decrement_mark_n (GtkPropertyMark* mark, gint n);
299 static void move_mark_n (GtkPropertyMark* mark, gint n);
300 static GtkPropertyMark find_mark (GtkText* text, guint mark_position);
301 static GtkPropertyMark find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near);
302 static void find_line_containing_point (GtkText* text, guint point,
306 static void compute_lines_pixels (GtkText* text, guint char_count,
307 guint *lines, guint *pixels);
309 static gint total_line_height (GtkText* text,
312 static LineParams find_line_params (GtkText* text,
313 const GtkPropertyMark *mark,
314 const PrevTabCont *tab_cont,
315 PrevTabCont *next_cont);
316 static void recompute_geometry (GtkText* text);
317 static void insert_expose (GtkText* text, guint old_pixels, gint nchars, guint new_line_count);
318 static void delete_expose (GtkText* text,
322 static GdkGC *create_bg_gc (GtkText *text);
323 static void clear_area (GtkText *text, GdkRectangle *area);
324 static void draw_line (GtkText* text,
327 static void draw_line_wrap (GtkText* text,
329 static void draw_cursor (GtkText* text, gint absolute);
330 static void undraw_cursor (GtkText* text, gint absolute);
331 static gint drawn_cursor_min (GtkText* text);
332 static gint drawn_cursor_max (GtkText* text);
333 static void expose_text (GtkText* text, GdkRectangle *area, gboolean cursor);
335 /* Search and Placement. */
336 static void find_cursor (GtkText* text,
338 static void find_cursor_at_line (GtkText* text,
339 const LineParams* start_line,
341 static void find_mouse_cursor (GtkText* text, gint x, gint y);
344 static void adjust_adj (GtkText* text, GtkAdjustment* adj);
345 static void scroll_up (GtkText* text, gint diff);
346 static void scroll_down (GtkText* text, gint diff);
347 static void scroll_int (GtkText* text, gint diff);
349 static void process_exposes (GtkText *text);
351 /* Cache Management. */
352 static void free_cache (GtkText* text);
353 static GList* remove_cache_line (GtkText* text, GList* list);
356 static void move_cursor_buffer_ver (GtkText *text, int dir);
357 static void move_cursor_page_ver (GtkText *text, int dir);
358 static void move_cursor_ver (GtkText *text, int count);
359 static void move_cursor_hor (GtkText *text, int count);
361 /* Binding actions */
362 static void gtk_text_move_cursor (GtkOldEditable *old_editable,
365 static void gtk_text_move_word (GtkOldEditable *old_editable,
367 static void gtk_text_move_page (GtkOldEditable *old_editable,
370 static void gtk_text_move_to_row (GtkOldEditable *old_editable,
372 static void gtk_text_move_to_column (GtkOldEditable *old_editable,
374 static void gtk_text_kill_char (GtkOldEditable *old_editable,
376 static void gtk_text_kill_word (GtkOldEditable *old_editable,
378 static void gtk_text_kill_line (GtkOldEditable *old_editable,
382 static void gtk_text_move_forward_character (GtkText *text);
383 static void gtk_text_move_backward_character (GtkText *text);
384 static void gtk_text_move_forward_word (GtkText *text);
385 static void gtk_text_move_backward_word (GtkText *text);
386 static void gtk_text_move_beginning_of_line (GtkText *text);
387 static void gtk_text_move_end_of_line (GtkText *text);
388 static void gtk_text_move_next_line (GtkText *text);
389 static void gtk_text_move_previous_line (GtkText *text);
391 static void gtk_text_delete_forward_character (GtkText *text);
392 static void gtk_text_delete_backward_character (GtkText *text);
393 static void gtk_text_delete_forward_word (GtkText *text);
394 static void gtk_text_delete_backward_word (GtkText *text);
395 static void gtk_text_delete_line (GtkText *text);
396 static void gtk_text_delete_to_line_end (GtkText *text);
397 static void gtk_text_select_word (GtkText *text,
399 static void gtk_text_select_line (GtkText *text,
402 static void gtk_text_set_position (GtkOldEditable *old_editable,
405 /* #define DEBUG_GTK_TEXT */
407 #if defined(DEBUG_GTK_TEXT) && defined(__GNUC__)
408 /* Debugging utilities. */
409 static void gtk_text_assert_mark (GtkText *text,
410 GtkPropertyMark *mark,
411 GtkPropertyMark *before,
412 GtkPropertyMark *after,
417 static void gtk_text_assert (GtkText *text,
420 static void gtk_text_show_cache_line (GtkText *text, GList *cache,
421 const char* what, const char* func, gint line);
422 static void gtk_text_show_cache (GtkText *text, const char* func, gint line);
423 static void gtk_text_show_adj (GtkText *text,
428 static void gtk_text_show_props (GtkText* test,
432 #define TDEBUG(args) g_message args
433 #define TEXT_ASSERT(text) gtk_text_assert (text,__PRETTY_FUNCTION__,__LINE__)
434 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_text_assert_mark (text,mark, \
435 __PRETTY_FUNCTION__,msg,__LINE__)
436 #define TEXT_SHOW(text) gtk_text_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
437 #define TEXT_SHOW_LINE(text,line,msg) gtk_text_show_cache_line (text,line,msg,\
438 __PRETTY_FUNCTION__,__LINE__)
439 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_text_show_adj (text,adj,msg, \
440 __PRETTY_FUNCTION__,__LINE__)
443 #define TEXT_ASSERT(text)
444 #define TEXT_ASSERT_MARK(text,mark,msg)
445 #define TEXT_SHOW(text)
446 #define TEXT_SHOW_LINE(text,line,msg)
447 #define TEXT_SHOW_ADJ(text,adj,msg)
450 static GtkWidgetClass *parent_class = NULL;
452 /**********************************************************************/
454 /**********************************************************************/
457 gtk_text_get_type (void)
459 static GtkType text_type = 0;
463 static const GtkTypeInfo text_info =
467 sizeof (GtkTextClass),
468 (GtkClassInitFunc) gtk_text_class_init,
469 (GtkObjectInitFunc) gtk_text_init,
470 /* reserved_1 */ NULL,
471 /* reserved_2 */ NULL,
472 (GtkClassInitFunc) NULL,
475 static const GInterfaceInfo editable_info =
477 (GInterfaceInitFunc) gtk_text_editable_init, /* interface_init */
478 NULL, /* interface_finalize */
479 NULL /* interface_data */
483 text_type = gtk_type_unique (GTK_TYPE_OLD_EDITABLE, &text_info);
484 g_type_add_interface_static (text_type,
493 gtk_text_class_init (GtkTextClass *class)
495 GObjectClass *gobject_class;
496 GtkObjectClass *object_class;
497 GtkWidgetClass *widget_class;
498 GtkOldEditableClass *old_editable_class;
500 gobject_class = G_OBJECT_CLASS (class);
501 object_class = (GtkObjectClass*) class;
502 widget_class = (GtkWidgetClass*) class;
503 old_editable_class = (GtkOldEditableClass*) class;
504 parent_class = gtk_type_class (GTK_TYPE_OLD_EDITABLE);
506 gobject_class->finalize = gtk_text_finalize;
507 gobject_class->set_property = gtk_text_set_property;
508 gobject_class->get_property = gtk_text_get_property;
510 object_class->destroy = gtk_text_destroy;
512 widget_class->realize = gtk_text_realize;
513 widget_class->unrealize = gtk_text_unrealize;
514 widget_class->style_set = gtk_text_style_set;
515 widget_class->state_changed = gtk_text_state_changed;
516 widget_class->size_request = gtk_text_size_request;
517 widget_class->size_allocate = gtk_text_size_allocate;
518 widget_class->expose_event = gtk_text_expose;
519 widget_class->button_press_event = gtk_text_button_press;
520 widget_class->button_release_event = gtk_text_button_release;
521 widget_class->motion_notify_event = gtk_text_motion_notify;
522 widget_class->key_press_event = gtk_text_key_press;
524 old_editable_class->set_editable = gtk_text_real_set_editable;
526 old_editable_class->move_cursor = gtk_text_move_cursor;
527 old_editable_class->move_word = gtk_text_move_word;
528 old_editable_class->move_page = gtk_text_move_page;
529 old_editable_class->move_to_row = gtk_text_move_to_row;
530 old_editable_class->move_to_column = gtk_text_move_to_column;
532 old_editable_class->kill_char = gtk_text_kill_char;
533 old_editable_class->kill_word = gtk_text_kill_word;
534 old_editable_class->kill_line = gtk_text_kill_line;
536 old_editable_class->update_text = gtk_text_update_text;
537 old_editable_class->get_chars = gtk_text_get_chars;
538 old_editable_class->set_selection = gtk_text_set_selection;
539 old_editable_class->set_position = gtk_text_set_position;
541 class->set_scroll_adjustments = gtk_text_set_adjustments;
543 g_object_class_install_property (gobject_class,
545 g_param_spec_object ("hadjustment",
546 P_("Horizontal Adjustment"),
547 P_("Horizontal adjustment for the text widget"),
549 GTK_PARAM_READWRITE));
551 g_object_class_install_property (gobject_class,
553 g_param_spec_object ("vadjustment",
554 P_("Vertical Adjustment"),
555 P_("Vertical adjustment for the text widget"),
557 GTK_PARAM_READWRITE));
559 g_object_class_install_property (gobject_class,
561 g_param_spec_boolean ("line-wrap",
563 P_("Whether lines are wrapped at widget edges"),
565 GTK_PARAM_READWRITE));
567 g_object_class_install_property (gobject_class,
569 g_param_spec_boolean ("word-wrap",
571 P_("Whether words are wrapped at widget edges"),
573 GTK_PARAM_READWRITE));
575 widget_class->set_scroll_adjustments_signal =
576 gtk_signal_new (I_("set_scroll_adjustments"),
578 GTK_CLASS_TYPE (object_class),
579 GTK_SIGNAL_OFFSET (GtkTextClass, set_scroll_adjustments),
580 _gtk_marshal_VOID__OBJECT_OBJECT,
581 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
585 gtk_text_set_property (GObject *object,
592 text = GTK_TEXT (object);
596 case PROP_HADJUSTMENT:
597 gtk_text_set_adjustments (text,
598 g_value_get_object (value),
601 case PROP_VADJUSTMENT:
602 gtk_text_set_adjustments (text,
604 g_value_get_object (value));
607 gtk_text_set_line_wrap (text, g_value_get_boolean (value));
610 gtk_text_set_word_wrap (text, g_value_get_boolean (value));
613 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
619 gtk_text_get_property (GObject *object,
626 text = GTK_TEXT (object);
630 case PROP_HADJUSTMENT:
631 g_value_set_object (value, text->hadj);
633 case PROP_VADJUSTMENT:
634 g_value_set_object (value, text->vadj);
637 g_value_set_boolean (value, text->line_wrap);
640 g_value_set_boolean (value, text->word_wrap);
643 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
649 gtk_text_editable_init (GtkEditableClass *iface)
651 iface->insert_text = gtk_text_insert_text;
652 iface->delete_text = gtk_text_delete_text;
656 gtk_text_init (GtkText *text)
658 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
660 text->text_area = NULL;
665 text->line_wrap_bitmap = NULL;
666 text->line_arrow_bitmap = NULL;
668 text->use_wchar = FALSE;
669 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
670 text->text_len = INITIAL_BUFFER_SIZE;
672 text->scratch_buffer.ch = NULL;
673 text->scratch_buffer_len = 0;
675 text->freeze_count = 0;
677 text->default_tab_width = 4;
678 text->tab_stops = NULL;
680 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
681 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
683 text->line_start_cache = NULL;
684 text->first_cut_pixels = 0;
686 text->line_wrap = TRUE;
687 text->word_wrap = FALSE;
692 text->current_font = NULL;
694 init_properties (text);
696 GTK_OLD_EDITABLE (text)->editable = FALSE;
698 gtk_text_set_adjustments (text, NULL, NULL);
699 gtk_editable_set_position (GTK_EDITABLE (text), 0);
703 gtk_text_new (GtkAdjustment *hadj,
709 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
711 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
713 text = g_object_new (GTK_TYPE_TEXT,
722 gtk_text_set_word_wrap (GtkText *text,
725 g_return_if_fail (GTK_IS_TEXT (text));
727 text->word_wrap = (word_wrap != FALSE);
729 if (GTK_WIDGET_REALIZED (text))
731 recompute_geometry (text);
732 gtk_widget_queue_draw (GTK_WIDGET (text));
735 g_object_notify (G_OBJECT (text), "word-wrap");
739 gtk_text_set_line_wrap (GtkText *text,
742 g_return_if_fail (GTK_IS_TEXT (text));
744 text->line_wrap = (line_wrap != FALSE);
746 if (GTK_WIDGET_REALIZED (text))
748 recompute_geometry (text);
749 gtk_widget_queue_draw (GTK_WIDGET (text));
752 g_object_notify (G_OBJECT (text), "line-wrap");
756 gtk_text_set_editable (GtkText *text,
757 gboolean is_editable)
759 g_return_if_fail (GTK_IS_TEXT (text));
761 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
765 gtk_text_real_set_editable (GtkOldEditable *old_editable,
766 gboolean is_editable)
770 g_return_if_fail (GTK_IS_TEXT (old_editable));
772 text = GTK_TEXT (old_editable);
774 old_editable->editable = (is_editable != FALSE);
777 draw_cursor (text, TRUE);
779 undraw_cursor (text, TRUE);
783 gtk_text_set_adjustments (GtkText *text,
787 g_return_if_fail (GTK_IS_TEXT (text));
789 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
791 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
793 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
795 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
797 if (text->hadj && (text->hadj != hadj))
799 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
800 gtk_object_unref (GTK_OBJECT (text->hadj));
803 if (text->vadj && (text->vadj != vadj))
805 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
806 gtk_object_unref (GTK_OBJECT (text->vadj));
809 g_object_freeze_notify (G_OBJECT (text));
810 if (text->hadj != hadj)
813 g_object_ref_sink (text->hadj);
815 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
816 (GtkSignalFunc) gtk_text_adjustment,
818 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
819 (GtkSignalFunc) gtk_text_adjustment,
821 gtk_signal_connect (GTK_OBJECT (text->hadj), "destroy",
822 (GtkSignalFunc) gtk_text_adjustment_destroyed,
824 gtk_text_adjustment (hadj, text);
826 g_object_notify (G_OBJECT (text), "hadjustment");
829 if (text->vadj != vadj)
832 g_object_ref_sink (text->vadj);
834 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
835 (GtkSignalFunc) gtk_text_adjustment,
837 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
838 (GtkSignalFunc) gtk_text_adjustment,
840 gtk_signal_connect (GTK_OBJECT (text->vadj), "destroy",
841 (GtkSignalFunc) gtk_text_adjustment_destroyed,
843 gtk_text_adjustment (vadj, text);
845 g_object_notify (G_OBJECT (text), "vadjustment");
847 g_object_thaw_notify (G_OBJECT (text));
851 gtk_text_set_point (GtkText *text,
854 g_return_if_fail (GTK_IS_TEXT (text));
855 g_return_if_fail (index <= TEXT_LENGTH (text));
857 text->point = find_mark (text, index);
861 gtk_text_get_point (GtkText *text)
863 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
865 return text->point.index;
869 gtk_text_get_length (GtkText *text)
871 g_return_val_if_fail (GTK_IS_TEXT (text), 0);
873 return TEXT_LENGTH (text);
877 gtk_text_freeze (GtkText *text)
879 g_return_if_fail (GTK_IS_TEXT (text));
881 text->freeze_count++;
885 gtk_text_thaw (GtkText *text)
887 g_return_if_fail (GTK_IS_TEXT (text));
889 if (text->freeze_count)
890 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
892 recompute_geometry (text);
893 gtk_widget_queue_draw (GTK_WIDGET (text));
898 gtk_text_insert (GtkText *text,
900 const GdkColor *fore,
901 const GdkColor *back,
905 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
906 gboolean frozen = FALSE;
908 gint new_line_count = 1;
909 guint old_height = 0;
914 g_return_if_fail (GTK_IS_TEXT (text));
917 length = strlen (chars);
924 if (!text->freeze_count && (length > FREEZE_LENGTH))
926 gtk_text_freeze (text);
930 if (!text->freeze_count && (text->line_start_cache != NULL))
932 find_line_containing_point (text, text->point.index, TRUE);
933 old_height = total_line_height (text, text->current_line, 1);
936 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
938 GtkWidget *widget = GTK_WIDGET (text);
940 gtk_widget_ensure_style (widget);
941 if ((widget->style) &&
942 (gtk_style_get_font (widget->style)->type == GDK_FONT_FONTSET))
944 text->use_wchar = TRUE;
945 g_free (text->text.ch);
946 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
947 text->text_len = INITIAL_BUFFER_SIZE;
948 g_free (text->scratch_buffer.ch);
949 text->scratch_buffer.wc = NULL;
950 text->scratch_buffer_len = 0;
954 move_gap (text, text->point.index);
955 make_forward_space (text, length);
959 char *chars_nt = (char *)chars;
962 chars_nt = g_new (char, length+1);
963 memcpy (chars_nt, chars, length);
964 chars_nt[length] = 0;
966 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
968 if (chars_nt != chars)
976 memcpy(text->text.ch + text->gap_position, chars, length);
979 if (!text->freeze_count && (text->line_start_cache != NULL))
983 for (i=0; i<numwcs; i++)
984 if (text->text.wc[text->gap_position + i] == '\n')
989 for (i=0; i<numwcs; i++)
990 if (text->text.ch[text->gap_position + i] == '\n')
997 insert_text_property (text, font, fore, back, numwcs);
999 text->gap_size -= numwcs;
1000 text->gap_position += numwcs;
1002 if (text->point.index < text->first_line_start_index)
1003 text->first_line_start_index += numwcs;
1004 if (text->point.index < old_editable->selection_start_pos)
1005 old_editable->selection_start_pos += numwcs;
1006 if (text->point.index < old_editable->selection_end_pos)
1007 old_editable->selection_end_pos += numwcs;
1008 /* We'll reset the cursor later anyways if we aren't frozen */
1009 if (text->point.index < text->cursor_mark.index)
1010 text->cursor_mark.index += numwcs;
1012 advance_mark_n (&text->point, numwcs);
1014 if (!text->freeze_count && (text->line_start_cache != NULL))
1015 insert_expose (text, old_height, numwcs, new_line_count);
1019 gtk_text_thaw (text);
1023 gtk_text_backward_delete (GtkText *text,
1026 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1028 if (nchars > text->point.index || nchars <= 0)
1031 gtk_text_set_point (text, text->point.index - nchars);
1033 return gtk_text_forward_delete (text, nchars);
1037 gtk_text_forward_delete (GtkText *text,
1040 guint old_lines = 0, old_height = 0;
1041 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
1042 gboolean frozen = FALSE;
1044 g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1046 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1049 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1051 gtk_text_freeze (text);
1055 if (!text->freeze_count && text->line_start_cache != NULL)
1057 /* We need to undraw the cursor here, since we may later
1058 * delete the cursor's property
1060 undraw_cursor (text, FALSE);
1061 find_line_containing_point (text, text->point.index, TRUE);
1062 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1065 /* FIXME, or resizing after deleting will be odd */
1066 if (text->point.index < text->first_line_start_index)
1068 if (text->point.index + nchars >= text->first_line_start_index)
1070 text->first_line_start_index = text->point.index;
1071 while ((text->first_line_start_index > 0) &&
1072 (GTK_TEXT_INDEX (text, text->first_line_start_index - 1)
1074 text->first_line_start_index -= 1;
1078 text->first_line_start_index -= nchars;
1081 if (text->point.index < old_editable->selection_start_pos)
1082 old_editable->selection_start_pos -=
1083 MIN(nchars, old_editable->selection_start_pos - text->point.index);
1084 if (text->point.index < old_editable->selection_end_pos)
1085 old_editable->selection_end_pos -=
1086 MIN(nchars, old_editable->selection_end_pos - text->point.index);
1087 /* We'll reset the cursor later anyways if we aren't frozen */
1088 if (text->point.index < text->cursor_mark.index)
1089 move_mark_n (&text->cursor_mark,
1090 -MIN(nchars, text->cursor_mark.index - text->point.index));
1092 move_gap (text, text->point.index);
1094 text->gap_size += nchars;
1096 delete_text_property (text, nchars);
1098 if (!text->freeze_count && (text->line_start_cache != NULL))
1100 delete_expose (text, nchars, old_lines, old_height);
1101 draw_cursor (text, FALSE);
1105 gtk_text_thaw (text);
1111 gtk_text_set_position (GtkOldEditable *old_editable,
1114 GtkText *text = (GtkText *) old_editable;
1117 position = gtk_text_get_length (text);
1119 undraw_cursor (text, FALSE);
1120 text->cursor_mark = find_mark (text, position);
1121 find_cursor (text, TRUE);
1122 draw_cursor (text, FALSE);
1123 gtk_editable_select_region (GTK_EDITABLE (old_editable), 0, 0);
1127 gtk_text_get_chars (GtkOldEditable *old_editable,
1135 g_return_val_if_fail (GTK_IS_TEXT (old_editable), NULL);
1136 text = GTK_TEXT (old_editable);
1139 end_pos = TEXT_LENGTH (text);
1141 if ((start_pos < 0) ||
1142 (end_pos > TEXT_LENGTH (text)) ||
1143 (end_pos < start_pos))
1146 move_gap (text, TEXT_LENGTH (text));
1147 make_forward_space (text, 1);
1149 if (text->use_wchar)
1152 ch = text->text.wc[end_pos];
1153 text->text.wc[end_pos] = 0;
1154 retval = gdk_wcstombs (text->text.wc + start_pos);
1155 text->text.wc[end_pos] = ch;
1160 ch = text->text.ch[end_pos];
1161 text->text.ch[end_pos] = 0;
1162 retval = g_strdup (text->text.ch + start_pos);
1163 text->text.ch[end_pos] = ch;
1171 gtk_text_destroy (GtkObject *object)
1173 GtkText *text = GTK_TEXT (object);
1177 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1178 gtk_object_unref (GTK_OBJECT (text->hadj));
1183 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1184 gtk_object_unref (GTK_OBJECT (text->vadj));
1190 g_source_remove (text->timer);
1194 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1198 gtk_text_finalize (GObject *object)
1200 GtkText *text = GTK_TEXT (object);
1203 /* Clean up the internal structures */
1204 if (text->use_wchar)
1205 g_free (text->text.wc);
1207 g_free (text->text.ch);
1209 tmp_list = text->text_properties;
1212 destroy_text_property (tmp_list->data);
1213 tmp_list = tmp_list->next;
1216 if (text->current_font)
1217 text_font_unref (text->current_font);
1219 g_list_free (text->text_properties);
1221 if (text->use_wchar)
1223 g_free (text->scratch_buffer.wc);
1227 g_free (text->scratch_buffer.ch);
1230 g_list_free (text->tab_stops);
1232 G_OBJECT_CLASS (parent_class)->finalize (object);
1236 gtk_text_realize (GtkWidget *widget)
1238 GtkText *text = GTK_TEXT (widget);
1239 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (widget);
1240 GdkWindowAttr attributes;
1241 gint attributes_mask;
1243 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1245 attributes.window_type = GDK_WINDOW_CHILD;
1246 attributes.x = widget->allocation.x;
1247 attributes.y = widget->allocation.y;
1248 attributes.width = widget->allocation.width;
1249 attributes.height = widget->allocation.height;
1250 attributes.wclass = GDK_INPUT_OUTPUT;
1251 attributes.visual = gtk_widget_get_visual (widget);
1252 attributes.colormap = gtk_widget_get_colormap (widget);
1253 attributes.event_mask = gtk_widget_get_events (widget);
1254 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1255 GDK_BUTTON_PRESS_MASK |
1256 GDK_BUTTON_RELEASE_MASK |
1257 GDK_BUTTON_MOTION_MASK |
1258 GDK_ENTER_NOTIFY_MASK |
1259 GDK_LEAVE_NOTIFY_MASK |
1260 GDK_KEY_PRESS_MASK);
1261 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1263 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1264 gdk_window_set_user_data (widget->window, text);
1266 attributes.x = (widget->style->xthickness + TEXT_BORDER_ROOM);
1267 attributes.y = (widget->style->ythickness + TEXT_BORDER_ROOM);
1268 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1269 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1271 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1272 attributes_mask |= GDK_WA_CURSOR;
1274 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1275 gdk_window_set_user_data (text->text_area, text);
1277 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1279 widget->style = gtk_style_attach (widget->style, widget->window);
1281 /* Can't call gtk_style_set_background here because it's handled specially */
1282 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1283 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1285 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1286 text->bg_gc = create_bg_gc (text);
1288 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1289 (gchar*) line_wrap_bits,
1293 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1294 (gchar*) line_arrow_bits,
1298 text->gc = gdk_gc_new (text->text_area);
1299 gdk_gc_set_exposures (text->gc, TRUE);
1300 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1302 realize_properties (text);
1303 gdk_window_show (text->text_area);
1304 init_properties (text);
1306 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1307 gtk_old_editable_claim_selection (old_editable, TRUE, GDK_CURRENT_TIME);
1309 recompute_geometry (text);
1313 gtk_text_style_set (GtkWidget *widget,
1314 GtkStyle *previous_style)
1316 GtkText *text = GTK_TEXT (widget);
1318 if (GTK_WIDGET_REALIZED (widget))
1320 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1321 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1325 gdk_gc_destroy (text->bg_gc);
1329 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1330 text->bg_gc = create_bg_gc (text);
1332 recompute_geometry (text);
1335 if (text->current_font)
1336 text_font_unref (text->current_font);
1337 text->current_font = get_text_font (gtk_style_get_font (widget->style));
1341 gtk_text_state_changed (GtkWidget *widget,
1342 GtkStateType previous_state)
1344 GtkText *text = GTK_TEXT (widget);
1346 if (GTK_WIDGET_REALIZED (widget))
1348 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1349 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1354 gtk_text_unrealize (GtkWidget *widget)
1356 GtkText *text = GTK_TEXT (widget);
1358 gdk_window_set_user_data (text->text_area, NULL);
1359 gdk_window_destroy (text->text_area);
1360 text->text_area = NULL;
1362 gdk_gc_destroy (text->gc);
1367 gdk_gc_destroy (text->bg_gc);
1371 gdk_pixmap_unref (text->line_wrap_bitmap);
1372 gdk_pixmap_unref (text->line_arrow_bitmap);
1374 unrealize_properties (text);
1378 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1379 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1383 clear_focus_area (GtkText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1385 GtkWidget *widget = GTK_WIDGET (text);
1388 gint ythick = TEXT_BORDER_ROOM + widget->style->ythickness;
1389 gint xthick = TEXT_BORDER_ROOM + widget->style->xthickness;
1393 if (area_width == 0 || area_height == 0)
1396 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1398 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1400 gdk_gc_set_ts_origin (text->bg_gc,
1401 (- text->first_onscreen_hor_pixel + xthick) % width,
1402 (- text->first_onscreen_ver_pixel + ythick) % height);
1407 gc = widget->style->base_gc[widget->state];
1409 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1410 area_x, area_y, area_width, area_height);
1414 gtk_text_draw_focus (GtkWidget *widget)
1420 g_return_if_fail (GTK_IS_TEXT (widget));
1422 text = GTK_TEXT (widget);
1424 if (GTK_WIDGET_DRAWABLE (widget))
1426 gint ythick = widget->style->ythickness;
1427 gint xthick = widget->style->xthickness;
1428 gint xextra = TEXT_BORDER_ROOM;
1429 gint yextra = TEXT_BORDER_ROOM;
1431 TDEBUG (("in gtk_text_draw_focus\n"));
1435 width = widget->allocation.width;
1436 height = widget->allocation.height;
1438 if (GTK_WIDGET_HAS_FOCUS (widget))
1447 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1448 NULL, widget, "text",
1450 widget->allocation.width,
1451 widget->allocation.height);
1454 gtk_paint_shadow (widget->style, widget->window,
1455 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1456 NULL, widget, "text",
1457 x, y, width, height);
1461 width -= 2 * xthick;
1462 height -= 2 * ythick;
1465 clear_focus_area (text, x, y, width, yextra);
1467 clear_focus_area (text, x, y + yextra,
1468 xextra, y + height - 2 * yextra);
1470 clear_focus_area (text, x + width - xextra, y + yextra,
1471 xextra, height - 2 * ythick);
1473 clear_focus_area (text, x, x + height - yextra, width, yextra);
1477 TDEBUG (("in gtk_text_draw_focus (undrawable !!!)\n"));
1482 gtk_text_size_request (GtkWidget *widget,
1483 GtkRequisition *requisition)
1491 xthickness = widget->style->xthickness + TEXT_BORDER_ROOM;
1492 ythickness = widget->style->ythickness + TEXT_BORDER_ROOM;
1494 font = gtk_style_get_font (widget->style);
1496 char_height = MIN_TEXT_HEIGHT_LINES * (font->ascent +
1499 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (font,
1500 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1504 requisition->width = char_width + xthickness * 2;
1505 requisition->height = char_height + ythickness * 2;
1509 gtk_text_size_allocate (GtkWidget *widget,
1510 GtkAllocation *allocation)
1512 GtkText *text = GTK_TEXT (widget);
1514 widget->allocation = *allocation;
1515 if (GTK_WIDGET_REALIZED (widget))
1517 gdk_window_move_resize (widget->window,
1518 allocation->x, allocation->y,
1519 allocation->width, allocation->height);
1521 gdk_window_move_resize (text->text_area,
1522 widget->style->xthickness + TEXT_BORDER_ROOM,
1523 widget->style->ythickness + TEXT_BORDER_ROOM,
1524 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->xthickness +
1525 (gint)TEXT_BORDER_ROOM) * 2),
1526 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->ythickness +
1527 (gint)TEXT_BORDER_ROOM) * 2));
1529 recompute_geometry (text);
1534 gtk_text_expose (GtkWidget *widget,
1535 GdkEventExpose *event)
1537 if (event->window == GTK_TEXT (widget)->text_area)
1539 TDEBUG (("in gtk_text_expose (expose)\n"));
1540 expose_text (GTK_TEXT (widget), &event->area, TRUE);
1542 else if (event->count == 0)
1544 TDEBUG (("in gtk_text_expose (focus)\n"));
1545 gtk_text_draw_focus (widget);
1552 gtk_text_scroll_timeout (gpointer data)
1556 GdkModifierType mask;
1558 text = GTK_TEXT (data);
1561 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1563 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1565 GdkEvent *event = gdk_event_new (GDK_MOTION_NOTIFY);
1567 event->motion.is_hint = 0;
1568 event->motion.x = x;
1569 event->motion.y = y;
1570 event->motion.state = mask;
1572 gtk_text_motion_notify (GTK_WIDGET (text), (GdkEventMotion *)event);
1574 gdk_event_free (event);
1581 gtk_text_button_press (GtkWidget *widget,
1582 GdkEventButton *event)
1584 GtkText *text = GTK_TEXT (widget);
1585 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (widget);
1587 if (text->button && (event->button != text->button))
1590 text->button = event->button;
1592 if (!GTK_WIDGET_HAS_FOCUS (widget))
1593 gtk_widget_grab_focus (widget);
1595 if (event->button == 1)
1597 switch (event->type)
1599 case GDK_BUTTON_PRESS:
1600 gtk_grab_add (widget);
1602 undraw_cursor (text, FALSE);
1603 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1604 draw_cursor (text, FALSE);
1606 /* Set it now, so we display things right. We'll unset it
1607 * later if things don't work out */
1608 old_editable->has_selection = TRUE;
1609 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1610 text->cursor_mark.index,
1611 text->cursor_mark.index);
1615 case GDK_2BUTTON_PRESS:
1616 gtk_text_select_word (text, event->time);
1619 case GDK_3BUTTON_PRESS:
1620 gtk_text_select_line (text, event->time);
1627 else if (event->type == GDK_BUTTON_PRESS)
1629 if ((event->button == 2) && old_editable->editable)
1631 if (old_editable->selection_start_pos == old_editable->selection_end_pos ||
1632 old_editable->has_selection)
1634 undraw_cursor (text, FALSE);
1635 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1636 draw_cursor (text, FALSE);
1640 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1641 gdk_atom_intern_static_string ("UTF8_STRING"),
1646 GdkDisplay *display = gtk_widget_get_display (widget);
1648 gtk_grab_add (widget);
1650 undraw_cursor (text, FALSE);
1651 find_mouse_cursor (text, event->x, event->y);
1652 draw_cursor (text, FALSE);
1654 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1655 text->cursor_mark.index,
1656 text->cursor_mark.index);
1658 old_editable->has_selection = FALSE;
1659 if (gdk_selection_owner_get_for_display (display,
1660 GDK_SELECTION_PRIMARY) == widget->window)
1661 gtk_selection_owner_set_for_display (display,
1663 GDK_SELECTION_PRIMARY,
1672 gtk_text_button_release (GtkWidget *widget,
1673 GdkEventButton *event)
1675 GtkText *text = GTK_TEXT (widget);
1676 GtkOldEditable *old_editable;
1677 GdkDisplay *display;
1679 gtk_grab_remove (widget);
1681 if (text->button != event->button)
1688 g_source_remove (text->timer);
1692 if (event->button == 1)
1694 text = GTK_TEXT (widget);
1695 old_editable = GTK_OLD_EDITABLE (widget);
1696 display = gtk_widget_get_display (widget);
1698 gtk_grab_remove (widget);
1700 old_editable->has_selection = FALSE;
1701 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1703 if (gtk_selection_owner_set_for_display (display,
1705 GDK_SELECTION_PRIMARY,
1707 old_editable->has_selection = TRUE;
1709 gtk_text_update_text (old_editable, old_editable->selection_start_pos,
1710 old_editable->selection_end_pos);
1714 if (gdk_selection_owner_get_for_display (display,
1715 GDK_SELECTION_PRIMARY) == widget->window)
1716 gtk_selection_owner_set_for_display (display,
1718 GDK_SELECTION_PRIMARY,
1722 else if (event->button == 3)
1724 gtk_grab_remove (widget);
1727 undraw_cursor (text, FALSE);
1728 find_cursor (text, TRUE);
1729 draw_cursor (text, FALSE);
1735 gtk_text_motion_notify (GtkWidget *widget,
1736 GdkEventMotion *event)
1738 GtkText *text = GTK_TEXT (widget);
1741 GdkModifierType mask;
1745 mask = event->state;
1746 if (event->is_hint || (text->text_area != event->window))
1748 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1751 if ((text->button == 0) ||
1752 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1755 gdk_window_get_size (text->text_area, NULL, &height);
1757 if ((y < 0) || (y > height))
1759 if (text->timer == 0)
1761 text->timer = gdk_threads_add_timeout (SCROLL_TIME,
1762 gtk_text_scroll_timeout,
1766 scroll_int (text, y/2);
1768 scroll_int (text, (y - height)/2);
1774 undraw_cursor (GTK_TEXT (widget), FALSE);
1775 find_mouse_cursor (GTK_TEXT (widget), x, y);
1776 draw_cursor (GTK_TEXT (widget), FALSE);
1778 gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1779 GTK_OLD_EDITABLE (text)->selection_start_pos,
1780 text->cursor_mark.index);
1786 gtk_text_insert_text (GtkEditable *editable,
1787 const gchar *new_text,
1788 gint new_text_length,
1791 GtkText *text = GTK_TEXT (editable);
1793 GdkColor *fore, *back;
1795 TextProperty *property;
1797 gtk_text_set_point (text, *position);
1799 property = MARK_CURRENT_PROPERTY (&text->point);
1800 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
1801 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
1802 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
1804 gtk_text_insert (text, font, fore, back, new_text, new_text_length);
1806 *position = text->point.index;
1810 gtk_text_delete_text (GtkEditable *editable,
1814 GtkText *text = GTK_TEXT (editable);
1816 g_return_if_fail (start_pos >= 0);
1818 gtk_text_set_point (text, start_pos);
1820 end_pos = TEXT_LENGTH (text);
1822 if (end_pos > start_pos)
1823 gtk_text_forward_delete (text, end_pos - start_pos);
1827 gtk_text_key_press (GtkWidget *widget,
1830 GtkText *text = GTK_TEXT (widget);
1831 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (widget);
1836 key = event->keyval;
1839 if ((GTK_OLD_EDITABLE(text)->editable == FALSE))
1841 switch (event->keyval)
1845 if (event->state & GDK_CONTROL_MASK)
1846 scroll_int (text, -text->vadj->value);
1852 if (event->state & GDK_CONTROL_MASK)
1853 scroll_int (text, +text->vadj->upper);
1857 case GDK_KP_Page_Up:
1858 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
1859 case GDK_KP_Page_Down:
1860 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
1862 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
1864 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
1868 if (event->state & GDK_CONTROL_MASK)
1869 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
1880 gint extend_selection;
1882 guint initial_pos = old_editable->current_pos;
1884 text->point = find_mark (text, text->cursor_mark.index);
1886 extend_selection = event->state & GDK_SHIFT_MASK;
1887 extend_start = FALSE;
1889 if (extend_selection)
1891 old_editable->has_selection = TRUE;
1893 if (old_editable->selection_start_pos == old_editable->selection_end_pos)
1895 old_editable->selection_start_pos = text->point.index;
1896 old_editable->selection_end_pos = text->point.index;
1899 extend_start = (text->point.index == old_editable->selection_start_pos);
1902 switch (event->keyval)
1906 if (event->state & GDK_CONTROL_MASK)
1907 move_cursor_buffer_ver (text, -1);
1909 gtk_text_move_beginning_of_line (text);
1913 if (event->state & GDK_CONTROL_MASK)
1914 move_cursor_buffer_ver (text, +1);
1916 gtk_text_move_end_of_line (text);
1918 case GDK_KP_Page_Up:
1919 case GDK_Page_Up: move_cursor_page_ver (text, -1); break;
1920 case GDK_KP_Page_Down:
1921 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
1922 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
1924 case GDK_Up: move_cursor_ver (text, -1); break;
1926 case GDK_Down: move_cursor_ver (text, +1); break;
1929 if (event->state & GDK_CONTROL_MASK)
1930 gtk_text_move_backward_word (text);
1932 move_cursor_hor (text, -1);
1936 if (event->state & GDK_CONTROL_MASK)
1937 gtk_text_move_forward_word (text);
1939 move_cursor_hor (text, +1);
1943 if (event->state & GDK_CONTROL_MASK)
1944 gtk_text_delete_backward_word (text);
1946 gtk_text_delete_backward_character (text);
1949 gtk_text_delete_line (text);
1953 if (event->state & GDK_SHIFT_MASK)
1955 extend_selection = FALSE;
1956 gtk_editable_paste_clipboard (GTK_EDITABLE (old_editable));
1958 else if (event->state & GDK_CONTROL_MASK)
1960 gtk_editable_copy_clipboard (GTK_EDITABLE (old_editable));
1964 /* gtk_toggle_insert(text) -- IMPLEMENT */
1969 if (event->state & GDK_CONTROL_MASK)
1970 gtk_text_delete_forward_word (text);
1971 else if (event->state & GDK_SHIFT_MASK)
1973 extend_selection = FALSE;
1974 gtk_editable_cut_clipboard (GTK_EDITABLE (old_editable));
1977 gtk_text_delete_forward_character (text);
1980 case GDK_ISO_Left_Tab:
1982 position = text->point.index;
1983 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\t", 1, &position);
1988 if (event->state & GDK_CONTROL_MASK)
1989 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
1992 position = text->point.index;
1993 gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\n", 1, &position);
1997 /* Don't insert literally */
2004 if (event->state & GDK_CONTROL_MASK)
2007 if ((key >= 'A') && (key <= 'Z'))
2013 gtk_text_move_beginning_of_line (text);
2016 gtk_text_move_backward_character (text);
2019 gtk_editable_copy_clipboard (GTK_EDITABLE (text));
2022 gtk_text_delete_forward_character (text);
2025 gtk_text_move_end_of_line (text);
2028 gtk_text_move_forward_character (text);
2031 gtk_text_delete_backward_character (text);
2034 gtk_text_delete_to_line_end (text);
2037 gtk_text_move_next_line (text);
2040 gtk_text_move_previous_line (text);
2043 gtk_text_delete_line (text);
2046 gtk_editable_paste_clipboard (GTK_EDITABLE (text));
2049 gtk_text_delete_backward_word (text);
2052 gtk_editable_cut_clipboard (GTK_EDITABLE (text));
2060 else if (event->state & GDK_MOD1_MASK)
2063 if ((key >= 'A') && (key <= 'Z'))
2069 gtk_text_move_backward_word (text);
2072 gtk_text_delete_forward_word (text);
2075 gtk_text_move_forward_word (text);
2083 else if (event->length > 0)
2085 extend_selection = FALSE;
2087 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
2088 position = text->point.index;
2089 gtk_editable_insert_text (GTK_EDITABLE (old_editable), event->string, event->length, &position);
2095 if (return_val && (old_editable->current_pos != initial_pos))
2097 if (extend_selection)
2099 if (old_editable->current_pos < old_editable->selection_start_pos)
2100 gtk_text_set_selection (old_editable, old_editable->current_pos,
2101 old_editable->selection_end_pos);
2102 else if (old_editable->current_pos > old_editable->selection_end_pos)
2103 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2104 old_editable->current_pos);
2108 gtk_text_set_selection (old_editable, old_editable->current_pos,
2109 old_editable->selection_end_pos);
2111 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2112 old_editable->current_pos);
2116 gtk_text_set_selection (old_editable, 0, 0);
2118 gtk_old_editable_claim_selection (old_editable,
2119 old_editable->selection_start_pos != old_editable->selection_end_pos,
2128 gtk_text_adjustment (GtkAdjustment *adjustment,
2131 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2132 g_return_if_fail (GTK_IS_TEXT (text));
2134 /* Just ignore it if we haven't been size-allocated and realized yet */
2135 if (text->line_start_cache == NULL)
2138 if (adjustment == text->hadj)
2140 g_warning ("horizontal scrolling not implemented");
2144 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2148 undraw_cursor (text, FALSE);
2151 scroll_down (text, diff);
2152 else /* if (diff < 0) */
2153 scroll_up (text, diff);
2155 draw_cursor (text, FALSE);
2157 text->last_ver_value = adjustment->value;
2163 gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
2166 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2167 g_return_if_fail (GTK_IS_TEXT (text));
2169 if (adjustment == text->hadj)
2170 gtk_text_set_adjustments (text, NULL, text->vadj);
2171 if (adjustment == text->vadj)
2172 gtk_text_set_adjustments (text, text->hadj, NULL);
2176 static GtkPropertyMark
2177 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2179 GtkPropertyMark mark;
2181 mark = find_mark_near (text, point_position, near);
2183 while (mark.index > 0 &&
2184 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2185 decrement_mark (&mark);
2191 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2193 tab_cont->pixel_offset = 0;
2194 tab_cont->tab_start.tab_stops = text->tab_stops;
2195 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2197 if (!tab_cont->tab_start.to_next_tab)
2198 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2202 line_params_iterate (GtkText* text,
2203 const GtkPropertyMark* mark0,
2204 const PrevTabCont* tab_mark0,
2207 LineIteratorFunction iter)
2208 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2209 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2210 * for each line following MARK, iteration continues unless ITER_CALL
2213 GtkPropertyMark mark = *mark0;
2214 PrevTabCont tab_conts[2];
2215 LineParams *lp, lpbuf;
2216 gint tab_cont_index = 0;
2219 tab_conts[0] = *tab_mark0;
2221 init_tab_cont (text, tab_conts);
2226 lp = g_slice_new (LineParams);
2230 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2231 tab_conts + (tab_cont_index + 1) % 2);
2233 if ((*iter) (text, lp, data))
2236 if (LAST_INDEX (text, lp->end))
2240 advance_mark (&mark);
2241 tab_cont_index = (tab_cont_index + 1) % 2;
2246 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2248 FetchLinesData *fldata = (FetchLinesData*) data;
2250 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2252 switch (fldata->fl_type)
2254 case FetchLinesCount:
2255 if (!text->line_wrap || !lp->wraps)
2258 if (fldata->data >= fldata->data_max)
2262 case FetchLinesPixels:
2264 fldata->data += LINE_HEIGHT(*lp);
2266 if (fldata->data >= fldata->data_max)
2276 fetch_lines (GtkText* text,
2277 const GtkPropertyMark* mark0,
2278 const PrevTabCont* tab_cont0,
2282 FetchLinesData fl_data;
2284 fl_data.new_lines = NULL;
2286 fl_data.data_max = data;
2287 fl_data.fl_type = fl_type;
2289 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2291 return g_list_reverse (fl_data.new_lines);
2295 fetch_lines_backward (GtkText* text)
2297 GList *new_line_start;
2298 GtkPropertyMark mark;
2300 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2303 mark = find_this_line_start_mark (text,
2304 CACHE_DATA(text->line_start_cache).start.index - 1,
2305 &CACHE_DATA(text->line_start_cache).start);
2307 new_line_start = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2309 while (new_line_start->next)
2310 new_line_start = new_line_start->next;
2312 new_line_start->next = text->line_start_cache;
2313 text->line_start_cache->prev = new_line_start;
2317 fetch_lines_forward (GtkText* text, gint line_count)
2319 GtkPropertyMark mark;
2320 GList* line = text->line_start_cache;
2325 mark = CACHE_DATA(line).end;
2327 if (LAST_INDEX (text, mark))
2330 advance_mark(&mark);
2332 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2335 line->next->prev = line;
2338 /* Compute the number of lines, and vertical pixels for n characters
2339 * starting from the point
2342 compute_lines_pixels (GtkText* text, guint char_count,
2343 guint *lines, guint *pixels)
2345 GList *line = text->current_line;
2346 gint chars_left = char_count;
2351 /* If chars_left == 0, that means we're joining two lines in a
2352 * deletion, so add in the values for the next line as well
2354 for (; line && chars_left >= 0; line = line->next)
2356 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2358 if (line == text->current_line)
2359 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2361 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2363 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2367 chars_left = 0; /* force another loop */
2370 fetch_lines_forward (text, 1);
2375 total_line_height (GtkText* text, GList* line, gint line_count)
2379 for (; line && line_count > 0; line = line->next)
2381 height += LINE_HEIGHT(CACHE_DATA(line));
2383 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2387 fetch_lines_forward (text, line_count);
2394 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2396 if (old == text->line_start_cache)
2400 for (; old_line_count > 0; old_line_count -= 1)
2402 while (text->line_start_cache &&
2404 CACHE_DATA(text->line_start_cache).wraps)
2405 remove_cache_line(text, text->line_start_cache);
2407 remove_cache_line(text, text->line_start_cache);
2410 last = g_list_last (new);
2412 last->next = text->line_start_cache;
2414 if (text->line_start_cache)
2415 text->line_start_cache->prev = last;
2417 text->line_start_cache = new;
2423 g_assert (old->prev);
2427 for (; old_line_count > 0; old_line_count -= 1)
2429 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2430 old = remove_cache_line (text, old);
2432 old = remove_cache_line (text, old);
2438 last = g_list_last (new);
2448 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2450 GList* cache = text->current_line;
2453 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2456 for (; cache; cache = cache->next)
2458 GtkPropertyMark *start = &CACHE_DATA(cache).start;
2459 GtkPropertyMark *end = &CACHE_DATA(cache).end;
2461 start->index -= nchars;
2462 end->index -= nchars;
2464 if (LAST_INDEX (text, text->point) &&
2465 start->index == text->point.index)
2466 *start = text->point;
2467 else if (start->property == text->point.property)
2468 start->offset = start->index - (text->point.index - text->point.offset);
2470 if (LAST_INDEX (text, text->point) &&
2471 end->index == text->point.index)
2473 if (end->property == text->point.property)
2474 end->offset = end->index - (text->point.index - text->point.offset);
2476 /*TEXT_ASSERT_MARK(text, start, "start");*/
2477 /*TEXT_ASSERT_MARK(text, end, "end");*/
2482 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2484 GtkWidget *widget = GTK_WIDGET (text);
2487 guint new_pixels = 0;
2489 GList* new_line = NULL;
2492 text->cursor_virtual_x = 0;
2494 correct_cache_delete (text, nchars, old_lines);
2496 pixel_height = pixel_height_of(text, text->current_line) -
2497 LINE_HEIGHT(CACHE_DATA(text->current_line));
2499 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2500 CACHE_DATA(text->current_line).start = text->point;
2502 new_line = fetch_lines (text,
2503 &CACHE_DATA(text->current_line).start,
2504 &CACHE_DATA(text->current_line).tab_cont,
2508 swap_lines (text, text->current_line, new_line, old_lines);
2510 text->current_line = new_line;
2512 new_pixels = total_line_height (text, new_line, 1);
2514 gdk_window_get_size (text->text_area, &width, &height);
2516 if (old_pixels != new_pixels)
2518 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2520 gdk_draw_pixmap (text->text_area,
2524 pixel_height + old_pixels,
2526 pixel_height + new_pixels,
2530 text->vadj->upper += new_pixels;
2531 text->vadj->upper -= old_pixels;
2532 adjust_adj (text, text->vadj);
2536 rect.y = pixel_height;
2538 rect.height = new_pixels;
2540 expose_text (text, &rect, FALSE);
2541 gtk_text_draw_focus ( (GtkWidget *) text);
2543 text->cursor_mark = text->point;
2545 find_cursor (text, TRUE);
2547 if (old_pixels != new_pixels)
2549 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2552 rect.y = pixel_height + new_pixels;
2554 rect.height = height - rect.y;
2556 expose_text (text, &rect, FALSE);
2559 process_exposes (text);
2566 /* note, the point has already been moved forward */
2568 correct_cache_insert (GtkText* text, gint nchars)
2571 GtkPropertyMark *start;
2572 GtkPropertyMark *end;
2573 gboolean was_split = FALSE;
2575 /* We need to distinguish whether the property was split in the
2576 * insert or not, so we check if the point (which points after
2577 * the insertion here), points to the same character as the
2578 * point before. Ugh.
2582 GtkPropertyMark tmp_mark = text->point;
2583 move_mark_n (&tmp_mark, -1);
2585 if (tmp_mark.property != text->point.property)
2589 /* If we inserted a property exactly at the beginning of the
2590 * line, we have to correct here, or fetch_lines will
2593 start = &CACHE_DATA(text->current_line).start;
2595 /* Check if if we split exactly at the beginning of the line:
2596 * (was_split won't be set if we are inserting at the end of the text,
2597 * so we don't check)
2599 if (start->offset == MARK_CURRENT_PROPERTY (start)->length)
2600 SET_PROPERTY_MARK (start, start->property->next, 0);
2601 /* Check if we inserted a property at the beginning of the text: */
2602 else if (was_split &&
2603 (start->property == text->point.property) &&
2604 (start->index == text->point.index - nchars))
2605 SET_PROPERTY_MARK (start, start->property->prev, 0);
2607 /* Now correct the offsets, and check for start or end marks that
2608 * are after the point, yet point to a property before the point's
2609 * property. This indicates that they are meant to point to the
2610 * second half of a property we split in insert_text_property(), so
2611 * we fix them up that way.
2613 cache = text->current_line->next;
2615 for (; cache; cache = cache->next)
2617 start = &CACHE_DATA(cache).start;
2618 end = &CACHE_DATA(cache).end;
2620 if (LAST_INDEX (text, text->point) &&
2621 start->index == text->point.index)
2622 *start = text->point;
2623 else if (start->index >= text->point.index - nchars)
2625 if (!was_split && start->property == text->point.property)
2626 move_mark_n(start, nchars);
2629 if (start->property->next &&
2630 (start->property->next->next == text->point.property))
2632 g_assert (start->offset >= MARK_CURRENT_PROPERTY (start)->length);
2633 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2634 start->property = text->point.property;
2636 start->index += nchars;
2640 if (LAST_INDEX (text, text->point) &&
2641 end->index == text->point.index)
2643 if (end->index >= text->point.index - nchars)
2645 if (!was_split && end->property == text->point.property)
2646 move_mark_n(end, nchars);
2649 if (end->property->next &&
2650 (end->property->next->next == text->point.property))
2652 g_assert (end->offset >= MARK_CURRENT_PROPERTY (end)->length);
2653 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2654 end->property = text->point.property;
2656 end->index += nchars;
2660 /*TEXT_ASSERT_MARK(text, start, "start");*/
2661 /*TEXT_ASSERT_MARK(text, end, "end");*/
2667 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2668 guint new_line_count)
2670 GtkWidget *widget = GTK_WIDGET (text);
2673 guint new_pixels = 0;
2675 GList* new_lines = NULL;
2678 text->cursor_virtual_x = 0;
2680 undraw_cursor (text, FALSE);
2682 correct_cache_insert (text, nchars);
2684 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2686 pixel_height = pixel_height_of(text, text->current_line) -
2687 LINE_HEIGHT(CACHE_DATA(text->current_line));
2689 new_lines = fetch_lines (text,
2690 &CACHE_DATA(text->current_line).start,
2691 &CACHE_DATA(text->current_line).tab_cont,
2695 swap_lines (text, text->current_line, new_lines, 1);
2697 text->current_line = new_lines;
2699 new_pixels = total_line_height (text, new_lines, new_line_count);
2701 gdk_window_get_size (text->text_area, &width, &height);
2703 if (old_pixels != new_pixels)
2705 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2707 gdk_draw_pixmap (text->text_area,
2711 pixel_height + old_pixels,
2713 pixel_height + new_pixels,
2715 height + (old_pixels - new_pixels) - pixel_height);
2718 text->vadj->upper += new_pixels;
2719 text->vadj->upper -= old_pixels;
2720 adjust_adj (text, text->vadj);
2724 rect.y = pixel_height;
2726 rect.height = new_pixels;
2728 expose_text (text, &rect, FALSE);
2729 gtk_text_draw_focus ( (GtkWidget *) text);
2731 text->cursor_mark = text->point;
2733 find_cursor (text, TRUE);
2735 draw_cursor (text, FALSE);
2737 if (old_pixels != new_pixels)
2739 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2742 rect.y = pixel_height + new_pixels;
2744 rect.height = height - rect.y;
2746 expose_text (text, &rect, FALSE);
2749 process_exposes (text);
2752 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2757 /* Text property functions */
2760 font_hash (gconstpointer font)
2762 return gdk_font_id ((const GdkFont*) font);
2765 static GHashTable *font_cache_table = NULL;
2768 get_text_font (GdkFont* gfont)
2773 if (!font_cache_table)
2774 font_cache_table = g_hash_table_new (font_hash, (GEqualFunc) gdk_font_equal);
2776 tf = g_hash_table_lookup (font_cache_table, gfont);
2784 tf = g_new (GtkTextFont, 1);
2787 tf->gdk_font = gfont;
2788 gdk_font_ref (gfont);
2790 for(i = 0; i < 256; i += 1)
2791 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2793 g_hash_table_insert (font_cache_table, gfont, tf);
2799 text_font_unref (GtkTextFont *text_font)
2801 text_font->ref_count--;
2802 if (text_font->ref_count == 0)
2804 g_hash_table_remove (font_cache_table, text_font->gdk_font);
2805 gdk_font_unref (text_font->gdk_font);
2811 text_properties_equal (TextProperty* prop, GdkFont* font, const GdkColor *fore, const GdkColor *back)
2813 if (prop->flags & PROPERTY_FONT)
2816 GtkTextFont *text_font;
2821 text_font = get_text_font (font);
2823 retval = (prop->font == text_font);
2824 text_font_unref (text_font);
2833 if (prop->flags & PROPERTY_FOREGROUND)
2835 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
2842 if (prop->flags & PROPERTY_BACKGROUND)
2844 if (!back || !gdk_color_equal (&prop->back_color, back))
2855 realize_property (GtkText *text, TextProperty *prop)
2857 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2859 if (prop->flags & PROPERTY_FOREGROUND)
2860 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
2862 if (prop->flags & PROPERTY_BACKGROUND)
2863 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
2867 realize_properties (GtkText *text)
2869 GList *tmp_list = text->text_properties;
2873 realize_property (text, tmp_list->data);
2875 tmp_list = tmp_list->next;
2880 unrealize_property (GtkText *text, TextProperty *prop)
2882 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2884 if (prop->flags & PROPERTY_FOREGROUND)
2885 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
2887 if (prop->flags & PROPERTY_BACKGROUND)
2888 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
2892 unrealize_properties (GtkText *text)
2894 GList *tmp_list = text->text_properties;
2898 unrealize_property (text, tmp_list->data);
2900 tmp_list = tmp_list->next;
2904 static TextProperty*
2905 new_text_property (GtkText *text, GdkFont *font, const GdkColor* fore,
2906 const GdkColor* back, guint length)
2910 prop = g_slice_new (TextProperty);
2915 prop->flags |= PROPERTY_FONT;
2916 prop->font = get_text_font (font);
2923 prop->flags |= PROPERTY_FOREGROUND;
2924 prop->fore_color = *fore;
2929 prop->flags |= PROPERTY_BACKGROUND;
2930 prop->back_color = *back;
2933 prop->length = length;
2935 if (GTK_WIDGET_REALIZED (text))
2936 realize_property (text, prop);
2942 destroy_text_property (TextProperty *prop)
2945 text_font_unref (prop->font);
2947 g_slice_free (TextProperty, prop);
2950 /* Flop the memory between the point and the gap around like a
2953 move_gap (GtkText* text, guint index)
2955 if (text->gap_position < index)
2957 gint diff = index - text->gap_position;
2959 if (text->use_wchar)
2960 g_memmove (text->text.wc + text->gap_position,
2961 text->text.wc + text->gap_position + text->gap_size,
2962 diff*sizeof (GdkWChar));
2964 g_memmove (text->text.ch + text->gap_position,
2965 text->text.ch + text->gap_position + text->gap_size,
2968 text->gap_position = index;
2970 else if (text->gap_position > index)
2972 gint diff = text->gap_position - index;
2974 if (text->use_wchar)
2975 g_memmove (text->text.wc + index + text->gap_size,
2976 text->text.wc + index,
2977 diff*sizeof (GdkWChar));
2979 g_memmove (text->text.ch + index + text->gap_size,
2980 text->text.ch + index,
2983 text->gap_position = index;
2987 /* Increase the gap size. */
2989 make_forward_space (GtkText* text, guint len)
2991 if (text->gap_size < len)
2993 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
2995 if (sum >= text->text_len)
2999 while (i <= sum) i <<= 1;
3001 if (text->use_wchar)
3002 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3003 i*sizeof(GdkWChar));
3005 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3009 if (text->use_wchar)
3010 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3011 text->text.wc + text->gap_position + text->gap_size,
3012 (text->text_end - (text->gap_position + text->gap_size))
3015 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3016 text->text.ch + text->gap_position + text->gap_size,
3017 text->text_end - (text->gap_position + text->gap_size));
3019 text->text_end += len*2;
3020 text->gap_size += len*2;
3024 /* Inserts into the text property list a list element that guarantees
3025 * that for len characters following the point, text has the correct
3026 * property. does not move point. adjusts text_properties_point and
3027 * text_properties_point_offset relative to the current value of
3030 insert_text_property (GtkText* text, GdkFont* font,
3031 const GdkColor *fore, const GdkColor* back, guint len)
3033 GtkPropertyMark *mark = &text->point;
3034 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3035 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3037 if (MARK_OFFSET(mark) == 0)
3039 /* Point is on the boundary of two properties.
3040 * If it is the same as either, grow, else insert
3043 if (text_properties_equal(forward_prop, font, fore, back))
3045 /* Grow the property in front of us. */
3047 MARK_PROPERTY_LENGTH(mark) += len;
3049 else if (backward_prop &&
3050 text_properties_equal(backward_prop, font, fore, back))
3052 /* Grow property behind us, point property and offset
3055 SET_PROPERTY_MARK (&text->point,
3056 MARK_PREV_LIST_PTR (mark),
3057 backward_prop->length);
3059 backward_prop->length += len;
3061 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3062 (forward_prop->length == 1))
3064 /* Next property just has last position, take it over */
3066 if (GTK_WIDGET_REALIZED (text))
3067 unrealize_property (text, forward_prop);
3069 forward_prop->flags = 0;
3072 forward_prop->flags |= PROPERTY_FONT;
3073 forward_prop->font = get_text_font (font);
3076 forward_prop->font = NULL;
3080 forward_prop->flags |= PROPERTY_FOREGROUND;
3081 forward_prop->fore_color = *fore;
3085 forward_prop->flags |= PROPERTY_BACKGROUND;
3086 forward_prop->back_color = *back;
3088 forward_prop->length += len;
3090 if (GTK_WIDGET_REALIZED (text))
3091 realize_property (text, forward_prop);
3095 /* Splice a new property into the list. */
3097 GList* new_prop = g_list_alloc();
3099 new_prop->next = MARK_LIST_PTR(mark);
3100 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3101 new_prop->next->prev = new_prop;
3104 new_prop->prev->next = new_prop;
3106 new_prop->data = new_text_property (text, font, fore, back, len);
3108 SET_PROPERTY_MARK (mark, new_prop, 0);
3113 /* The following will screw up the line_start cache,
3114 * we'll fix it up in correct_cache_insert
3117 /* In the middle of forward_prop, if properties are equal,
3118 * just add to its length, else split it into two and splice
3120 if (text_properties_equal (forward_prop, font, fore, back))
3122 forward_prop->length += len;
3124 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3125 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3127 /* Inserting before only the last position in the text */
3130 forward_prop->length -= 1;
3132 new_prop = g_list_alloc();
3133 new_prop->data = new_text_property (text, font, fore, back, len+1);
3134 new_prop->prev = MARK_LIST_PTR(mark);
3135 new_prop->next = NULL;
3136 MARK_NEXT_LIST_PTR(mark) = new_prop;
3138 SET_PROPERTY_MARK (mark, new_prop, 0);
3142 GList* new_prop = g_list_alloc();
3143 GList* new_prop_forward = g_list_alloc();
3144 gint old_length = forward_prop->length;
3145 GList* next = MARK_NEXT_LIST_PTR(mark);
3147 /* Set the new lengths according to where they are split. Construct
3148 * two new properties. */
3149 forward_prop->length = MARK_OFFSET(mark);
3151 new_prop_forward->data =
3152 new_text_property(text,
3153 forward_prop->flags & PROPERTY_FONT ?
3154 forward_prop->font->gdk_font : NULL,
3155 forward_prop->flags & PROPERTY_FOREGROUND ?
3156 &forward_prop->fore_color : NULL,
3157 forward_prop->flags & PROPERTY_BACKGROUND ?
3158 &forward_prop->back_color : NULL,
3159 old_length - forward_prop->length);
3161 new_prop->data = new_text_property(text, font, fore, back, len);
3163 /* Now splice things in. */
3164 MARK_NEXT_LIST_PTR(mark) = new_prop;
3165 new_prop->prev = MARK_LIST_PTR(mark);
3167 new_prop->next = new_prop_forward;
3168 new_prop_forward->prev = new_prop;
3170 new_prop_forward->next = next;
3173 next->prev = new_prop_forward;
3175 SET_PROPERTY_MARK (mark, new_prop, 0);
3179 while (text->text_properties_end->next)
3180 text->text_properties_end = text->text_properties_end->next;
3182 while (text->text_properties->prev)
3183 text->text_properties = text->text_properties->prev;
3187 delete_text_property (GtkText* text, guint nchars)
3189 /* Delete nchars forward from point. */
3191 /* Deleting text properties is problematical, because we
3192 * might be storing around marks pointing to a property.
3194 * The marks in question and how we handle them are:
3196 * point: We know the new value, since it will be at the
3197 * end of the deleted text, and we move it there
3199 * cursor: We just remove the mark and set it equal to the
3200 * point after the operation.
3201 * line-start cache: We replace most affected lines.
3202 * The current line gets used to fetch the new
3203 * lines so, if necessary, (delete at the beginning
3204 * of a line) we fix it up by setting it equal to the
3212 for(; nchars; nchars -= 1)
3214 prop = MARK_CURRENT_PROPERTY(&text->point);
3218 if (prop->length == 0)
3220 tmp = MARK_LIST_PTR (&text->point);
3222 is_first = tmp == text->text_properties;
3224 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3225 text->point.offset = 0;
3227 if (GTK_WIDGET_REALIZED (text))
3228 unrealize_property (text, prop);
3230 destroy_text_property (prop);
3231 g_list_free_1 (tmp);
3233 prop = MARK_CURRENT_PROPERTY (&text->point);
3236 text->text_properties = MARK_LIST_PTR (&text->point);
3238 g_assert (prop->length != 0);
3240 else if (prop->length == text->point.offset)
3242 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3243 text->point.offset = 0;
3247 /* Check to see if we have just the single final position remaining
3248 * along in a property; if so, combine it with the previous property
3250 if (LAST_INDEX (text, text->point) &&
3251 (MARK_OFFSET (&text->point) == 0) &&
3252 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3254 tmp = MARK_LIST_PTR (&text->point);
3255 prop = MARK_CURRENT_PROPERTY(&text->point);
3257 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3258 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3259 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3261 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3263 if (GTK_WIDGET_REALIZED (text))
3264 unrealize_property (text, prop);
3266 destroy_text_property (prop);
3267 g_list_free_1 (tmp);
3272 init_properties (GtkText *text)
3274 if (!text->text_properties)
3276 text->text_properties = g_list_alloc();
3277 text->text_properties->next = NULL;
3278 text->text_properties->prev = NULL;
3279 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3280 text->text_properties_end = text->text_properties;
3282 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3284 text->point.index = 0;
3289 /**********************************************************************/
3290 /* Property Movement */
3291 /**********************************************************************/
3294 move_mark_n (GtkPropertyMark* mark, gint n)
3297 advance_mark_n(mark, n);
3299 decrement_mark_n(mark, -n);
3303 advance_mark (GtkPropertyMark* mark)
3305 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3309 if (prop->length > mark->offset + 1)
3313 mark->property = MARK_NEXT_LIST_PTR (mark);
3319 advance_mark_n (GtkPropertyMark* mark, gint n)
3326 i = 0; /* otherwise it migth not be init. */
3327 prop = MARK_CURRENT_PROPERTY(mark);
3329 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3330 /* to make it easier */
3331 n += (mark->offset);
3332 mark->index -= mark->offset;
3334 /* first we take seven-mile-leaps to get to the right text
3336 while ((n-i) > prop->length - 1) {
3338 mark->index += prop->length;
3339 mark->property = MARK_NEXT_LIST_PTR (mark);
3340 prop = MARK_CURRENT_PROPERTY (mark);
3344 /* and then the rest */
3345 mark->index += n - i;
3346 mark->offset += n - i;
3350 decrement_mark (GtkPropertyMark* mark)
3354 if (mark->offset > 0)
3358 mark->property = MARK_PREV_LIST_PTR (mark);
3359 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3364 decrement_mark_n (GtkPropertyMark* mark, gint n)
3368 while (mark->offset < n) {
3369 /* jump to end of prev */
3370 n -= mark->offset + 1;
3371 mark->index -= mark->offset + 1;
3372 mark->property = MARK_PREV_LIST_PTR (mark);
3373 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3381 static GtkPropertyMark
3382 find_mark (GtkText* text, guint mark_position)
3384 return find_mark_near (text, mark_position, &text->point);
3388 * You can also start from the end, what a drag.
3390 static GtkPropertyMark
3391 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3396 GtkPropertyMark mark;
3399 diffa = mark_position + 1;
3401 diffa = mark_position - near->index;
3403 diffb = mark_position;
3415 mark.property = text->text_properties;
3419 move_mark_n (&mark, mark_position - mark.index);
3424 /* This routine must be called with scroll == FALSE, only when
3425 * point is at least partially on screen
3429 find_line_containing_point (GtkText* text, guint point,
3435 text->current_line = NULL;
3439 /* Scroll backwards until the point is on screen
3441 while (CACHE_DATA(text->line_start_cache).start.index > point)
3442 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3444 /* Now additionally try to make sure that the point is fully on screen
3448 while (text->first_cut_pixels != 0 &&
3449 text->line_start_cache->next &&
3450 CACHE_DATA(text->line_start_cache->next).start.index > point)
3451 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3454 gdk_window_get_size (text->text_area, NULL, &height);
3456 for (cache = text->line_start_cache; cache; cache = cache->next)
3460 if (CACHE_DATA(cache).end.index >= point ||
3461 LAST_INDEX(text, CACHE_DATA(cache).end))
3463 text->current_line = cache; /* LOOK HERE, this proc has an
3464 * important side effect. */
3468 TEXT_SHOW_LINE (text, cache, "cache");
3470 if (cache->next == NULL)
3471 fetch_lines_forward (text, 1);
3475 lph = pixel_height_of (text, cache->next);
3477 /* Scroll the bottom of the line is on screen, or until
3478 * the line is the first onscreen line.
3480 while (cache->next != text->line_start_cache && lph > height)
3482 TEXT_SHOW_LINE (text, cache, "cache");
3483 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3484 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3485 lph = pixel_height_of (text, cache->next);
3490 g_assert_not_reached (); /* Must set text->current_line here */
3494 pixel_height_of (GtkText* text, GList* cache_line)
3496 gint pixels = - text->first_cut_pixels;
3497 GList *cache = text->line_start_cache;
3500 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3502 if (cache->data == cache_line->data)
3505 cache = cache->next;
3511 /**********************************************************************/
3512 /* Search and Placement */
3513 /**********************************************************************/
3516 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3519 gint16* char_widths;
3521 if (LAST_INDEX (text, *mark))
3524 ch = GTK_TEXT_INDEX (text, mark->index);
3525 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3529 return tab_mark->to_next_tab * char_widths[' '];
3533 return char_widths[ch];
3537 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3542 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, GdkWChar ch)
3544 if (tab_mark->to_next_tab == 1 || ch == '\t')
3546 if (tab_mark->tab_stops->next)
3548 tab_mark->tab_stops = tab_mark->tab_stops->next;
3549 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3553 tab_mark->to_next_tab = text->default_tab_width;
3558 tab_mark->to_next_tab -= 1;
3563 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3567 advance_tab_mark (text, tab_mark, 0);
3571 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3575 GtkPropertyMark mark = start_line->start;
3576 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3577 gint pixel_width = LINE_START_PIXEL (*start_line);
3579 while (mark.index < text->cursor_mark.index)
3581 pixel_width += find_char_width (text, &mark, &tab_mark);
3583 advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3584 advance_mark (&mark);
3587 text->cursor_pos_x = pixel_width;
3588 text->cursor_pos_y = pixel_height;
3589 text->cursor_char_offset = start_line->font_descent;
3590 text->cursor_mark = mark;
3592 ch = LAST_INDEX (text, mark) ?
3593 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3595 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3596 text->cursor_char = 0;
3598 text->cursor_char = ch;
3602 find_cursor (GtkText* text, gboolean scroll)
3604 if (GTK_WIDGET_REALIZED (text))
3606 find_line_containing_point (text, text->cursor_mark.index, scroll);
3608 if (text->current_line)
3609 find_cursor_at_line (text,
3610 &CACHE_DATA(text->current_line),
3611 pixel_height_of(text, text->current_line));
3614 GTK_OLD_EDITABLE (text)->current_pos = text->cursor_mark.index;
3618 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3619 guint line_pixel_height,
3622 GtkPropertyMark mark = lp->start;
3623 TabStopMark tab_mark = lp->tab_cont.tab_start;
3625 gint char_width = find_char_width(text, &mark, &tab_mark);
3626 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3628 text->cursor_pos_y = line_pixel_height;
3632 GdkWChar ch = LAST_INDEX (text, mark) ?
3633 LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3635 if (button_x < pixel_width || mark.index == lp->end.index)
3637 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3638 text->cursor_mark = mark;
3639 text->cursor_char_offset = lp->font_descent;
3641 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3642 text->cursor_char = 0;
3644 text->cursor_char = ch;
3649 advance_tab_mark (text, &tab_mark, ch);
3650 advance_mark (&mark);
3652 pixel_width += char_width/2;
3654 char_width = find_char_width (text, &mark, &tab_mark);
3656 pixel_width += (char_width+1)/2;
3661 find_mouse_cursor (GtkText* text, gint x, gint y)
3664 GList* cache = text->line_start_cache;
3668 pixel_height = - text->first_cut_pixels;
3670 for (; cache; cache = cache->next)
3672 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3674 if (y < pixel_height || !cache->next)
3676 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3678 find_cursor (text, FALSE);
3685 /**********************************************************************/
3687 /**********************************************************************/
3690 free_cache (GtkText* text)
3692 GList* cache = text->line_start_cache;
3697 cache = cache->prev;
3699 text->line_start_cache = cache;
3702 for (; cache; cache = cache->next)
3703 g_slice_free (LineParams, cache->data);
3705 g_list_free (text->line_start_cache);
3707 text->line_start_cache = NULL;
3711 remove_cache_line (GtkText* text, GList* member)
3718 if (member == text->line_start_cache)
3719 text->line_start_cache = text->line_start_cache->next;
3722 member->prev->next = member->next;
3725 member->next->prev = member->prev;
3727 list = member->next;
3729 g_slice_free (LineParams, member->data);
3730 g_list_free_1 (member);
3735 /**********************************************************************/
3737 /**********************************************************************/
3740 move_cursor_buffer_ver (GtkText *text, int dir)
3742 undraw_cursor (text, FALSE);
3746 scroll_int (text, text->vadj->upper);
3747 text->cursor_mark = find_this_line_start_mark (text,
3749 &text->cursor_mark);
3753 scroll_int (text, - text->vadj->value);
3754 text->cursor_mark = find_this_line_start_mark (text,
3756 &text->cursor_mark);
3759 find_cursor (text, TRUE);
3760 draw_cursor (text, FALSE);
3764 move_cursor_page_ver (GtkText *text, int dir)
3766 scroll_int (text, dir * text->vadj->page_increment);
3770 move_cursor_ver (GtkText *text, int count)
3773 GtkPropertyMark mark;
3776 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3777 offset = text->cursor_mark.index - mark.index;
3779 if (offset > text->cursor_virtual_x)
3780 text->cursor_virtual_x = offset;
3784 if (mark.index == 0)
3787 decrement_mark (&mark);
3788 mark = find_this_line_start_mark (text, mark.index, &mark);
3792 mark = text->cursor_mark;
3794 while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3795 advance_mark (&mark);
3797 if (LAST_INDEX(text, mark))
3800 advance_mark (&mark);
3803 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3804 if (LAST_INDEX(text, mark) ||
3805 GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3808 undraw_cursor (text, FALSE);
3810 text->cursor_mark = mark;
3812 find_cursor (text, TRUE);
3814 draw_cursor (text, FALSE);
3818 move_cursor_hor (GtkText *text, int count)
3820 /* count should be +-1. */
3821 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3822 (count < 0 && text->cursor_mark.index < (- count)) ||
3826 text->cursor_virtual_x = 0;
3828 undraw_cursor (text, FALSE);
3830 move_mark_n (&text->cursor_mark, count);
3832 find_cursor (text, TRUE);
3834 draw_cursor (text, FALSE);
3838 gtk_text_move_cursor (GtkOldEditable *old_editable,
3845 move_cursor_hor (GTK_TEXT (old_editable), 1);
3850 move_cursor_hor (GTK_TEXT (old_editable), -1);
3856 move_cursor_ver (GTK_TEXT (old_editable), 1);
3861 move_cursor_ver (GTK_TEXT (old_editable), -1);
3866 gtk_text_move_forward_character (GtkText *text)
3868 move_cursor_hor (text, 1);
3872 gtk_text_move_backward_character (GtkText *text)
3874 move_cursor_hor (text, -1);
3878 gtk_text_move_next_line (GtkText *text)
3880 move_cursor_ver (text, 1);
3884 gtk_text_move_previous_line (GtkText *text)
3886 move_cursor_ver (text, -1);
3890 gtk_text_move_word (GtkOldEditable *old_editable,
3896 gtk_text_move_forward_word (GTK_TEXT (old_editable));
3901 gtk_text_move_backward_word (GTK_TEXT (old_editable));
3906 gtk_text_move_forward_word (GtkText *text)
3908 text->cursor_virtual_x = 0;
3910 undraw_cursor (text, FALSE);
3912 if (text->use_wchar)
3914 while (!LAST_INDEX (text, text->cursor_mark) &&
3915 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3916 advance_mark (&text->cursor_mark);
3918 while (!LAST_INDEX (text, text->cursor_mark) &&
3919 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3920 advance_mark (&text->cursor_mark);
3924 while (!LAST_INDEX (text, text->cursor_mark) &&
3925 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3926 advance_mark (&text->cursor_mark);
3928 while (!LAST_INDEX (text, text->cursor_mark) &&
3929 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3930 advance_mark (&text->cursor_mark);
3933 find_cursor (text, TRUE);
3934 draw_cursor (text, FALSE);
3938 gtk_text_move_backward_word (GtkText *text)
3940 text->cursor_virtual_x = 0;
3942 undraw_cursor (text, FALSE);
3944 if (text->use_wchar)
3946 while ((text->cursor_mark.index > 0) &&
3947 !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3948 decrement_mark (&text->cursor_mark);
3950 while ((text->cursor_mark.index > 0) &&
3951 gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3952 decrement_mark (&text->cursor_mark);
3956 while ((text->cursor_mark.index > 0) &&
3957 !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3958 decrement_mark (&text->cursor_mark);
3960 while ((text->cursor_mark.index > 0) &&
3961 isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3962 decrement_mark (&text->cursor_mark);
3965 find_cursor (text, TRUE);
3966 draw_cursor (text, FALSE);
3970 gtk_text_move_page (GtkOldEditable *old_editable,
3975 scroll_int (GTK_TEXT (old_editable),
3976 y * GTK_TEXT(old_editable)->vadj->page_increment);
3980 gtk_text_move_to_row (GtkOldEditable *old_editable,
3986 gtk_text_move_to_column (GtkOldEditable *old_editable,
3991 text = GTK_TEXT (old_editable);
3993 text->cursor_virtual_x = 0; /* FIXME */
3995 undraw_cursor (text, FALSE);
3997 /* Move to the beginning of the line */
3998 while ((text->cursor_mark.index > 0) &&
3999 (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4000 decrement_mark (&text->cursor_mark);
4002 while (!LAST_INDEX (text, text->cursor_mark) &&
4003 (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4007 else if (column == 0)
4010 advance_mark (&text->cursor_mark);
4013 find_cursor (text, TRUE);
4014 draw_cursor (text, FALSE);
4018 gtk_text_move_beginning_of_line (GtkText *text)
4020 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4025 gtk_text_move_end_of_line (GtkText *text)
4027 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), -1);
4031 gtk_text_kill_char (GtkOldEditable *old_editable,
4036 text = GTK_TEXT (old_editable);
4038 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4039 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4044 if (text->point.index + 1 <= TEXT_LENGTH (text))
4045 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index, text->point.index + 1);
4049 if (text->point.index > 0)
4050 gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index - 1, text->point.index);
4056 gtk_text_delete_forward_character (GtkText *text)
4058 gtk_text_kill_char (GTK_OLD_EDITABLE (text), 1);
4062 gtk_text_delete_backward_character (GtkText *text)
4064 gtk_text_kill_char (GTK_OLD_EDITABLE (text), -1);
4068 gtk_text_kill_word (GtkOldEditable *old_editable,
4071 if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4072 gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4075 gint old_pos = old_editable->current_pos;
4078 gtk_text_move_word (old_editable, 1);
4079 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4083 gtk_text_move_word (old_editable, -1);
4084 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4090 gtk_text_delete_forward_word (GtkText *text)
4092 gtk_text_kill_word (GTK_OLD_EDITABLE (text), 1);
4096 gtk_text_delete_backward_word (GtkText *text)
4098 gtk_text_kill_word (GTK_OLD_EDITABLE (text), -1);
4102 gtk_text_kill_line (GtkOldEditable *old_editable,
4105 gint old_pos = old_editable->current_pos;
4108 gtk_text_move_to_column (old_editable, -1);
4109 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4113 gtk_text_move_to_column (old_editable, 0);
4114 gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4119 gtk_text_delete_line (GtkText *text)
4121 gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4122 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4126 gtk_text_delete_to_line_end (GtkText *text)
4128 gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4132 gtk_text_select_word (GtkText *text, guint32 time)
4137 GtkOldEditable *old_editable;
4138 old_editable = GTK_OLD_EDITABLE (text);
4140 gtk_text_move_backward_word (text);
4141 start_pos = text->cursor_mark.index;
4143 gtk_text_move_forward_word (text);
4144 end_pos = text->cursor_mark.index;
4146 old_editable->has_selection = TRUE;
4147 gtk_text_set_selection (old_editable, start_pos, end_pos);
4148 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4152 gtk_text_select_line (GtkText *text, guint32 time)
4157 GtkOldEditable *old_editable;
4158 old_editable = GTK_OLD_EDITABLE (text);
4160 gtk_text_move_beginning_of_line (text);
4161 start_pos = text->cursor_mark.index;
4163 gtk_text_move_end_of_line (text);
4164 gtk_text_move_forward_character (text);
4165 end_pos = text->cursor_mark.index;
4167 old_editable->has_selection = TRUE;
4168 gtk_text_set_selection (old_editable, start_pos, end_pos);
4169 gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4172 /**********************************************************************/
4174 /**********************************************************************/
4177 adjust_adj (GtkText* text, GtkAdjustment* adj)
4181 gdk_window_get_size (text->text_area, NULL, &height);
4183 adj->step_increment = MIN (adj->upper, SCROLL_PIXELS);
4184 adj->page_increment = MIN (adj->upper, height - KEY_SCROLL_PIXELS);
4185 adj->page_size = MIN (adj->upper, height);
4186 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4187 adj->value = MAX (adj->value, 0.0);
4189 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4193 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
4195 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4197 if ((text->first_line_start_index >= lp->start.index) &&
4198 (text->first_line_start_index <= lp->end.index))
4200 svdata->mark = lp->start;
4202 if (text->first_line_start_index == lp->start.index)
4204 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4208 text->first_onscreen_ver_pixel = svdata->pixel_height;
4209 text->first_cut_pixels = 0;
4212 text->vadj->value = text->first_onscreen_ver_pixel;
4215 svdata->pixel_height += LINE_HEIGHT (*lp);
4221 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
4223 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4226 if (svdata->pixel_height <= (gint) text->vadj->value &&
4227 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4229 svdata->mark = lp->start;
4231 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4232 text->first_onscreen_ver_pixel = svdata->pixel_height;
4233 text->first_line_start_index = lp->start.index;
4239 svdata->pixel_height += LINE_HEIGHT (*lp);
4247 static GtkPropertyMark
4248 set_vertical_scroll (GtkText* text)
4250 GtkPropertyMark mark = find_mark (text, 0);
4251 SetVerticalScrollData data;
4255 data.pixel_height = 0;
4256 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4258 text->vadj->upper = data.pixel_height;
4259 orig_value = (gint) text->vadj->value;
4261 gdk_window_get_size (text->text_area, NULL, &height);
4263 text->vadj->step_increment = MIN (text->vadj->upper, SCROLL_PIXELS);
4264 text->vadj->page_increment = MIN (text->vadj->upper, height - KEY_SCROLL_PIXELS);
4265 text->vadj->page_size = MIN (text->vadj->upper, height);
4266 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4267 text->vadj->value = MAX (text->vadj->value, 0.0);
4269 text->last_ver_value = (gint)text->vadj->value;
4271 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4273 if (text->vadj->value != orig_value)
4275 /* We got clipped, and don't really know which line to put first. */
4276 data.pixel_height = 0;
4277 data.last_didnt_wrap = TRUE;
4279 line_params_iterate (text, &mark, NULL,
4281 set_vertical_scroll_find_iterator);
4288 scroll_int (GtkText* text, gint diff)
4292 text->vadj->value += diff;
4294 upper = text->vadj->upper - text->vadj->page_size;
4295 text->vadj->value = MIN (text->vadj->value, upper);
4296 text->vadj->value = MAX (text->vadj->value, 0.0);
4298 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4302 process_exposes (GtkText *text)
4306 /* Make sure graphics expose events are processed before scrolling
4309 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4311 gtk_widget_send_expose (GTK_WIDGET (text), event);
4312 if (event->expose.count == 0)
4314 gdk_event_free (event);
4317 gdk_event_free (event);
4321 static gint last_visible_line_height (GtkText* text)
4323 GList *cache = text->line_start_cache;
4326 gdk_window_get_size (text->text_area, NULL, &height);
4328 for (; cache->next; cache = cache->next)
4329 if (pixel_height_of(text, cache->next) > height)
4333 return pixel_height_of(text, cache) - 1;
4338 static gint first_visible_line_height (GtkText* text)
4340 if (text->first_cut_pixels)
4341 return pixel_height_of(text, text->line_start_cache) + 1;
4347 scroll_down (GtkText* text, gint diff0)
4353 text->first_onscreen_ver_pixel += diff0;
4357 g_assert (text->line_start_cache);
4359 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4361 text->first_cut_pixels += 1;
4365 text->first_cut_pixels = 0;
4367 text->line_start_cache = text->line_start_cache->next;
4369 text->first_line_start_index =
4370 CACHE_DATA(text->line_start_cache).start.index;
4372 if (!text->line_start_cache->next)
4373 fetch_lines_forward (text, 1);
4379 gdk_window_get_size (text->text_area, &width, &height);
4380 if (height > real_diff)
4381 gdk_draw_pixmap (text->text_area,
4389 height - real_diff);
4392 rect.y = MAX (0, height - real_diff);
4394 rect.height = MIN (height, real_diff);
4396 expose_text (text, &rect, FALSE);
4397 gtk_text_draw_focus ( (GtkWidget *) text);
4399 if (text->current_line)
4403 text->cursor_pos_y -= real_diff;
4404 cursor_min = drawn_cursor_min(text);
4407 find_mouse_cursor (text, text->cursor_pos_x,
4408 first_visible_line_height (text));
4411 if (height > real_diff)
4412 process_exposes (text);
4416 scroll_up (GtkText* text, gint diff0)
4422 text->first_onscreen_ver_pixel += diff0;
4426 g_assert (text->line_start_cache);
4428 if (text->first_cut_pixels > 0)
4430 text->first_cut_pixels -= 1;
4434 if (!text->line_start_cache->prev)
4435 fetch_lines_backward (text);
4437 text->line_start_cache = text->line_start_cache->prev;
4439 text->first_line_start_index =
4440 CACHE_DATA(text->line_start_cache).start.index;
4442 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4448 gdk_window_get_size (text->text_area, &width, &height);
4449 if (height > real_diff)
4450 gdk_draw_pixmap (text->text_area,
4458 height - real_diff);
4463 rect.height = MIN (height, real_diff);
4465 expose_text (text, &rect, FALSE);
4466 gtk_text_draw_focus ( (GtkWidget *) text);
4468 if (text->current_line)
4473 text->cursor_pos_y += real_diff;
4474 cursor_max = drawn_cursor_max(text);
4475 gdk_window_get_size (text->text_area, NULL, &height);
4477 if (cursor_max >= height)
4478 find_mouse_cursor (text, text->cursor_pos_x,
4479 last_visible_line_height (text));
4482 if (height > real_diff)
4483 process_exposes (text);
4486 /**********************************************************************/
4488 /**********************************************************************/
4490 /* Assumes mark starts a line. Calculates the height, width, and
4491 * displayable character count of a single DISPLAYABLE line. That
4492 * means that in line-wrap mode, this does may not compute the
4493 * properties of an entire line. */
4495 find_line_params (GtkText* text,
4496 const GtkPropertyMark* mark,
4497 const PrevTabCont *tab_cont,
4498 PrevTabCont *next_cont)
4501 TabStopMark tab_mark = tab_cont->tab_start;
4502 guint max_display_pixels;
4507 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4508 max_display_pixels -= LINE_WRAP_ROOM;
4511 lp.tab_cont = *tab_cont;
4514 lp.pixel_width = tab_cont->pixel_offset;
4515 lp.displayable_chars = 0;
4517 lp.font_descent = 0;
4519 init_tab_cont (text, next_cont);
4521 while (!LAST_INDEX(text, lp.end))
4523 g_assert (lp.end.property);
4525 ch = GTK_TEXT_INDEX (text, lp.end.index);
4526 font = MARK_CURRENT_FONT (text, &lp.end);
4528 if (ch == LINE_DELIM)
4530 /* Newline doesn't count in computation of line height, even
4531 * if its in a bigger font than the rest of the line. Unless,
4532 * of course, there are no other characters. */
4534 if (!lp.font_ascent && !lp.font_descent)
4536 lp.font_ascent = font->ascent;
4537 lp.font_descent = font->descent;
4540 lp.tab_cont_next = *next_cont;
4545 ch_width = find_char_width (text, &lp.end, &tab_mark);
4547 if ((ch_width + lp.pixel_width > max_display_pixels) &&
4548 (lp.end.index > lp.start.index))
4552 if (text->line_wrap)
4554 next_cont->tab_start = tab_mark;
4555 next_cont->pixel_offset = 0;
4559 /* Here's the tough case, a tab is wrapping. */
4560 gint pixels_avail = max_display_pixels - lp.pixel_width;
4561 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
4562 gint spaces_avail = pixels_avail / space_width;
4564 if (spaces_avail == 0)
4566 decrement_mark (&lp.end);
4570 advance_tab_mark (text, &next_cont->tab_start, '\t');
4571 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4573 lp.displayable_chars += 1;
4578 if (text->word_wrap)
4580 GtkPropertyMark saved_mark = lp.end;
4581 guint saved_characters = lp.displayable_chars;
4583 lp.displayable_chars += 1;
4585 if (text->use_wchar)
4587 while (!gdk_iswspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4588 (lp.end.index > lp.start.index))
4590 decrement_mark (&lp.end);
4591 lp.displayable_chars -= 1;
4596 while (!isspace(GTK_TEXT_INDEX (text, lp.end.index)) &&
4597 (lp.end.index > lp.start.index))
4599 decrement_mark (&lp.end);
4600 lp.displayable_chars -= 1;
4604 /* If whole line is one word, revert to char wrapping */
4605 if (lp.end.index == lp.start.index)
4607 lp.end = saved_mark;
4608 lp.displayable_chars = saved_characters;
4609 decrement_mark (&lp.end);
4614 /* Don't include this character, it will wrap. */
4615 decrement_mark (&lp.end);
4619 lp.tab_cont_next = *next_cont;
4626 lp.displayable_chars += 1;
4629 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4630 lp.font_descent = MAX (font->descent, lp.font_descent);
4631 lp.pixel_width += ch_width;
4633 advance_mark(&lp.end);
4634 advance_tab_mark (text, &tab_mark, ch);
4637 if (LAST_INDEX(text, lp.start))
4639 /* Special case, empty last line. */
4640 font = MARK_CURRENT_FONT (text, &lp.end);
4642 lp.font_ascent = font->ascent;
4643 lp.font_descent = font->descent;
4646 lp.tab_cont_next = *next_cont;
4652 expand_scratch_buffer (GtkText* text, guint len)
4654 if (len >= text->scratch_buffer_len)
4658 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4660 if (text->use_wchar)
4662 if (text->scratch_buffer.wc)
4663 text->scratch_buffer.wc = g_new (GdkWChar, i);
4665 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
4666 i*sizeof (GdkWChar));
4670 if (text->scratch_buffer.ch)
4671 text->scratch_buffer.ch = g_new (guchar, i);
4673 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
4676 text->scratch_buffer_len = i;
4680 /* Side effect: modifies text->gc
4683 draw_bg_rect (GtkText* text, GtkPropertyMark *mark,
4684 gint x, gint y, gint width, gint height,
4685 gboolean already_cleared)
4687 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4689 if ((mark->index >= MIN(old_editable->selection_start_pos, old_editable->selection_end_pos) &&
4690 mark->index < MAX(old_editable->selection_start_pos, old_editable->selection_end_pos)))
4692 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
4693 old_editable->has_selection ?
4694 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
4696 NULL, GTK_WIDGET(text), "text",
4697 x, y, width, height);
4699 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
4700 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
4702 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
4704 gdk_draw_rectangle (text->text_area,
4706 TRUE, x, y, width, height);
4708 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4715 rect.height = height;
4717 clear_area (text, &rect);
4719 else if (!already_cleared)
4720 gdk_window_clear_area (text->text_area, x, y, width, height);
4724 draw_line (GtkText* text,
4725 gint pixel_start_height,
4728 GdkGCValues gc_values;
4731 guint running_offset = lp->tab_cont.pixel_offset;
4732 union { GdkWChar *wc; guchar *ch; } buffer;
4735 GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4737 guint selection_start_pos = MIN (old_editable->selection_start_pos, old_editable->selection_end_pos);
4738 guint selection_end_pos = MAX (old_editable->selection_start_pos, old_editable->selection_end_pos);
4740 GtkPropertyMark mark = lp->start;
4741 TabStopMark tab_mark = lp->tab_cont.tab_start;
4742 gint pixel_height = pixel_start_height + lp->font_ascent;
4743 guint chars = lp->displayable_chars;
4745 /* First provide a contiguous segment of memory. This makes reading
4746 * the code below *much* easier, and only incurs the cost of copying
4747 * when the line being displayed spans the gap. */
4748 if (mark.index <= text->gap_position &&
4749 mark.index + chars > text->gap_position)
4751 expand_scratch_buffer (text, chars);
4753 if (text->use_wchar)
4755 for (i = 0; i < chars; i += 1)
4756 text->scratch_buffer.wc[i] = GTK_TEXT_INDEX(text, mark.index + i);
4757 buffer.wc = text->scratch_buffer.wc;
4761 for (i = 0; i < chars; i += 1)
4762 text->scratch_buffer.ch[i] = GTK_TEXT_INDEX(text, mark.index + i);
4763 buffer.ch = text->scratch_buffer.ch;
4768 if (text->use_wchar)
4770 if (mark.index >= text->gap_position)
4771 buffer.wc = text->text.wc + mark.index + text->gap_size;
4773 buffer.wc = text->text.wc + mark.index;
4777 if (mark.index >= text->gap_position)
4778 buffer.ch = text->text.ch + mark.index + text->gap_size;
4780 buffer.ch = text->text.ch + mark.index;
4785 if (running_offset > 0)
4787 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
4788 LINE_HEIGHT (*lp), TRUE);
4794 if ((text->use_wchar && buffer.wc[0] != '\t') ||
4795 (!text->use_wchar && buffer.ch[0] != '\t'))
4797 union { GdkWChar *wc; guchar *ch; } next_tab;
4802 if (text->use_wchar)
4803 for (i=0; i<chars; i++)
4805 if (buffer.wc[i] == '\t')
4807 next_tab.wc = buffer.wc + i;
4812 next_tab.ch = memchr (buffer.ch, '\t', chars);
4814 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4816 if (text->use_wchar)
4819 len = MIN (len, next_tab.wc - buffer.wc);
4824 len = MIN (len, next_tab.ch - buffer.ch);
4827 if (mark.index < selection_start_pos)
4828 len = MIN (len, selection_start_pos - mark.index);
4829 else if (mark.index < selection_end_pos)
4830 len = MIN (len, selection_end_pos - mark.index);
4832 font = MARK_CURRENT_FONT (text, &mark);
4833 if (font->type == GDK_FONT_FONT)
4835 gdk_gc_set_font (text->gc, font);
4836 gdk_gc_get_values (text->gc, &gc_values);
4837 if (text->use_wchar)
4838 pixel_width = gdk_text_width_wc (gc_values.font,
4841 pixel_width = gdk_text_width (gc_values.font,
4846 if (text->use_wchar)
4847 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
4849 pixel_width = gdk_text_width (font, buffer.ch, len);
4852 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4853 pixel_width, LINE_HEIGHT (*lp), TRUE);
4855 if ((mark.index >= selection_start_pos) &&
4856 (mark.index < selection_end_pos))
4858 if (old_editable->has_selection)
4859 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_SELECTED];
4861 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_ACTIVE];
4865 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
4869 if (text->use_wchar)
4870 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
4877 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
4884 running_offset += pixel_width;
4886 advance_tab_mark_n (text, &tab_mark, len);
4890 gint pixels_remaining;
4896 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
4897 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
4899 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4901 spaces_avail = pixels_remaining / space_width;
4902 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
4904 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4905 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
4907 running_offset += tab_mark.to_next_tab *
4908 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4910 advance_tab_mark (text, &tab_mark, '\t');
4913 advance_mark_n (&mark, len);
4914 if (text->use_wchar)
4923 draw_line_wrap (GtkText* text, guint height /* baseline height */)
4930 if (text->line_wrap)
4932 bitmap = text->line_wrap_bitmap;
4933 bitmap_width = line_wrap_width;
4934 bitmap_height = line_wrap_height;
4938 bitmap = text->line_arrow_bitmap;
4939 bitmap_width = line_arrow_width;
4940 bitmap_height = line_arrow_height;
4943 gdk_window_get_size (text->text_area, &width, NULL);
4944 width -= LINE_WRAP_ROOM;
4946 gdk_gc_set_stipple (text->gc,
4949 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
4951 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
4953 gdk_gc_set_ts_origin (text->gc,
4955 height - bitmap_height - 1);
4957 gdk_draw_rectangle (text->text_area,
4961 height - bitmap_height - 1 /* one pixel above the baseline. */,
4965 gdk_gc_set_ts_origin (text->gc, 0, 0);
4967 gdk_gc_set_fill (text->gc, GDK_SOLID);
4971 undraw_cursor (GtkText* text, gint absolute)
4973 GtkOldEditable *old_editable = (GtkOldEditable *) text;
4975 TDEBUG (("in undraw_cursor\n"));
4978 text->cursor_drawn_level = 0;
4980 if ((text->cursor_drawn_level ++ == 0) &&
4981 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
4982 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
4986 g_assert(text->cursor_mark.property);
4988 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
4990 draw_bg_rect (text, &text->cursor_mark,
4992 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
4993 1, font->ascent + 1, FALSE);
4995 if (text->cursor_char)
4997 if (font->type == GDK_FONT_FONT)
4998 gdk_gc_set_font (text->gc, font);
5000 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5002 gdk_draw_text_wc (text->text_area, font,
5005 text->cursor_pos_y - text->cursor_char_offset,
5013 drawn_cursor_min (GtkText* text)
5017 g_assert(text->cursor_mark.property);
5019 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5021 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5025 drawn_cursor_max (GtkText* text)
5027 g_assert(text->cursor_mark.property);
5029 return text->cursor_pos_y - text->cursor_char_offset;
5033 draw_cursor (GtkText* text, gint absolute)
5035 GtkOldEditable *old_editable = (GtkOldEditable *)text;
5037 TDEBUG (("in draw_cursor\n"));
5040 text->cursor_drawn_level = 1;
5042 if ((--text->cursor_drawn_level == 0) &&
5043 old_editable->editable &&
5044 (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5045 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5049 g_assert (text->cursor_mark.property);
5051 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5053 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5055 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
5056 text->cursor_pos_y - text->cursor_char_offset,
5058 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5063 create_bg_gc (GtkText *text)
5067 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5068 values.fill = GDK_TILED;
5070 return gdk_gc_new_with_values (text->text_area, &values,
5071 GDK_GC_FILL | GDK_GC_TILE);
5075 clear_area (GtkText *text, GdkRectangle *area)
5077 GtkWidget *widget = GTK_WIDGET (text);
5083 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5085 gdk_gc_set_ts_origin (text->bg_gc,
5086 (- text->first_onscreen_hor_pixel) % width,
5087 (- text->first_onscreen_ver_pixel) % height);
5089 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5090 area->x, area->y, area->width, area->height);
5093 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5097 expose_text (GtkText* text, GdkRectangle *area, gboolean cursor)
5099 GList *cache = text->line_start_cache;
5100 gint pixels = - text->first_cut_pixels;
5101 gint min_y = MAX (0, area->y);
5102 gint max_y = MAX (0, area->y + area->height);
5105 gdk_window_get_size (text->text_area, NULL, &height);
5106 max_y = MIN (max_y, height);
5108 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5110 clear_area (text, area);
5112 for (; pixels < height; cache = cache->next)
5114 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5116 draw_line (text, pixels, &CACHE_DATA(cache));
5118 if (CACHE_DATA(cache).wraps)
5119 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5122 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5124 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5125 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5127 /* We undraw and draw the cursor here to get the drawn
5128 * level right ... FIXME - maybe the second parameter
5129 * of draw_cursor should work differently
5131 undraw_cursor (text, FALSE);
5132 draw_cursor (text, FALSE);
5136 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5140 fetch_lines_forward (text, 1);
5149 gtk_text_update_text (GtkOldEditable *old_editable,
5153 GtkText *text = GTK_TEXT (old_editable);
5155 GList *cache = text->line_start_cache;
5156 gint pixels = - text->first_cut_pixels;
5162 end_pos = TEXT_LENGTH (text);
5164 if (end_pos < start_pos)
5167 gdk_window_get_size (text->text_area, &width, &height);
5173 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5175 for (; pixels < height; cache = cache->next)
5177 if (CACHE_DATA(cache).start.index < end_pos)
5179 if (CACHE_DATA(cache).end.index >= start_pos)
5182 area.y = MAX(0,pixels);
5183 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5189 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5193 fetch_lines_forward (text, 1);
5201 expose_text (text, &area, TRUE);
5205 recompute_geometry (GtkText* text)
5207 GtkPropertyMark mark, start_mark;
5214 mark = start_mark = set_vertical_scroll (text);
5216 /* We need a real start of a line when calling fetch_lines().
5217 * not the start of a wrapped line.
5219 while (mark.index > 0 &&
5220 GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5221 decrement_mark (&mark);
5223 gdk_window_get_size (text->text_area, &width, &height);
5225 /* Fetch an entire line, to make sure that we get all the text
5226 * we backed over above, in addition to enough text to fill up
5227 * the space vertically
5230 new_lines = fetch_lines (text,
5236 mark = CACHE_DATA (g_list_last (new_lines)).end;
5237 if (!LAST_INDEX (text, mark))
5239 advance_mark (&mark);
5241 new_lines = g_list_concat (new_lines,
5246 height + text->first_cut_pixels));
5249 /* Now work forward to the actual first onscreen line */
5251 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5252 new_lines = new_lines->next;
5254 text->line_start_cache = new_lines;
5256 find_cursor (text, TRUE);
5259 /**********************************************************************/
5261 /**********************************************************************/
5264 gtk_text_set_selection (GtkOldEditable *old_editable,
5268 GtkText *text = GTK_TEXT (old_editable);
5270 guint start1, end1, start2, end2;
5273 end = TEXT_LENGTH (text);
5275 start1 = MIN(start,end);
5276 end1 = MAX(start,end);
5277 start2 = MIN(old_editable->selection_start_pos, old_editable->selection_end_pos);
5278 end2 = MAX(old_editable->selection_start_pos, old_editable->selection_end_pos);
5280 if (start2 < start1)
5284 tmp = start1; start1 = start2; start2 = tmp;
5285 tmp = end1; end1 = end2; end2 = tmp;
5288 undraw_cursor (text, FALSE);
5289 old_editable->selection_start_pos = start;
5290 old_editable->selection_end_pos = end;
5291 draw_cursor (text, FALSE);
5293 /* Expose only what changed */
5295 if (start1 < start2)
5296 gtk_text_update_text (old_editable, start1, MIN(end1, start2));
5299 gtk_text_update_text (old_editable, MAX(end1, start2), end2);
5300 else if (end2 < end1)
5301 gtk_text_update_text (old_editable, end2, end1);
5305 /**********************************************************************/
5307 /**********************************************************************/
5309 #ifdef DEBUG_GTK_TEXT
5311 gtk_text_show_cache_line (GtkText *text, GList *cache,
5312 const char* what, const char* func, gint line)
5314 LineParams *lp = &CACHE_DATA(cache);
5317 if (cache == text->line_start_cache)
5318 g_message ("Line Start Cache: ");
5320 if (cache == text->current_line)
5321 g_message("Current Line: ");
5323 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
5331 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
5332 g_message ("%c", GTK_TEXT_INDEX (text, i));
5338 gtk_text_show_cache (GtkText *text, const char* func, gint line)
5340 GList *l = text->line_start_cache;
5346 /* back up to the absolute beginning of the line cache */
5350 g_message ("*** line cache ***\n");
5351 for (; l; l = l->next)
5352 gtk_text_show_cache_line (text, l, "all", func, line);
5356 gtk_text_assert_mark (GtkText *text,
5357 GtkPropertyMark *mark,
5358 GtkPropertyMark *before,
5359 GtkPropertyMark *after,
5364 GtkPropertyMark correct_mark = find_mark (text, mark->index);
5366 if (mark->offset != correct_mark.offset ||
5367 mark->property != correct_mark.property)
5368 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
5372 gtk_text_assert (GtkText *text,
5376 GList* cache = text->line_start_cache;
5377 GtkPropertyMark* before_mark = NULL;
5378 GtkPropertyMark* after_mark = NULL;
5380 gtk_text_show_props (text, msg, line);
5382 for (; cache->prev; cache = cache->prev)
5385 g_message ("*** line markers ***\n");
5387 for (; cache; cache = cache->next)
5389 after_mark = &CACHE_DATA(cache).end;
5390 gtk_text_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
5391 before_mark = &CACHE_DATA(cache).start;
5394 after_mark = &CACHE_DATA(cache->next).start;
5398 gtk_text_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
5399 before_mark = &CACHE_DATA(cache).end;
5404 gtk_text_show_adj (GtkText *text,
5410 g_message ("*** adjustment ***\n");
5412 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
5419 adj->step_increment,
5420 adj->page_increment,
5425 gtk_text_show_props (GtkText *text,
5429 GList* props = text->text_properties;
5432 g_message ("%s:%d: ", msg, line);
5434 for (; props; props = props->next)
5436 TextProperty *p = (TextProperty*)props->data;
5438 proplen += p->length;
5440 g_message ("[%d,%p,", p->length, p);
5441 if (p->flags & PROPERTY_FONT)
5442 g_message ("%p,", p->font);
5445 if (p->flags & PROPERTY_FOREGROUND)
5446 g_message ("%ld, ", p->fore_color.pixel);
5449 if (p->flags & PROPERTY_BACKGROUND)
5450 g_message ("%ld] ", p->back_color.pixel);
5457 if (proplen - 1 != TEXT_LENGTH(text))
5458 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);
5462 #define __GTK_TEXT_C__
5463 #include "gtkaliasdef.c"