1 /* GAIL - The GNOME Accessibility Implementation Library
2 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
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.
22 #include <sys/types.h>
26 #include <glib-object.h>
27 #include <glib/gstdio.h>
29 #include "gailtextview.h"
30 #include <libgail-util/gailmisc.h>
32 static void gail_text_view_class_init (GailTextViewClass *klass);
33 static void gail_text_view_init (GailTextView *text_view);
35 static void gail_text_view_real_initialize (AtkObject *obj,
37 static void gail_text_view_real_notify_gtk (GObject *obj,
40 static void gail_text_view_finalize (GObject *object);
42 static void atk_text_interface_init (AtkTextIface *iface);
46 static AtkStateSet* gail_text_view_ref_state_set (AtkObject *accessible);
50 static gchar* gail_text_view_get_text_after_offset (AtkText *text,
52 AtkTextBoundary boundary_type,
55 static gchar* gail_text_view_get_text_at_offset (AtkText *text,
57 AtkTextBoundary boundary_type,
60 static gchar* gail_text_view_get_text_before_offset (AtkText *text,
62 AtkTextBoundary boundary_type,
65 static gchar* gail_text_view_get_text (AtkText*text,
68 static gunichar gail_text_view_get_character_at_offset (AtkText *text,
70 static gint gail_text_view_get_character_count (AtkText *text);
71 static gint gail_text_view_get_caret_offset (AtkText *text);
72 static gboolean gail_text_view_set_caret_offset (AtkText *text,
74 static gint gail_text_view_get_offset_at_point (AtkText *text,
78 static gint gail_text_view_get_n_selections (AtkText *text);
79 static gchar* gail_text_view_get_selection (AtkText *text,
83 static gboolean gail_text_view_add_selection (AtkText *text,
86 static gboolean gail_text_view_remove_selection (AtkText *text,
88 static gboolean gail_text_view_set_selection (AtkText *text,
92 static void gail_text_view_get_character_extents (AtkText *text,
99 static AtkAttributeSet * gail_text_view_get_run_attributes
104 static AtkAttributeSet * gail_text_view_get_default_attributes
106 /* atkeditabletext.h */
108 static void atk_editable_text_interface_init (AtkEditableTextIface *iface);
109 static gboolean gail_text_view_set_run_attributes (AtkEditableText *text,
110 AtkAttributeSet *attrib_set,
113 static void gail_text_view_set_text_contents (AtkEditableText *text,
114 const gchar *string);
115 static void gail_text_view_insert_text (AtkEditableText *text,
119 static void gail_text_view_copy_text (AtkEditableText *text,
122 static void gail_text_view_cut_text (AtkEditableText *text,
125 static void gail_text_view_delete_text (AtkEditableText *text,
128 static void gail_text_view_paste_text (AtkEditableText *text,
130 static void gail_text_view_paste_received (GtkClipboard *clipboard,
133 /* AtkStreamableContent */
134 static void atk_streamable_content_interface_init (AtkStreamableContentIface *iface);
135 static gint gail_streamable_content_get_n_mime_types (AtkStreamableContent *streamable);
136 static G_CONST_RETURN gchar* gail_streamable_content_get_mime_type (AtkStreamableContent *streamable,
138 static GIOChannel* gail_streamable_content_get_stream (AtkStreamableContent *streamable,
139 const gchar *mime_type);
140 /* getURI requires atk-1.12.0 or later
141 static void gail_streamable_content_get_uri (AtkStreamableContent *streamable);
146 static void _gail_text_view_insert_text_cb (GtkTextBuffer *buffer,
151 static void _gail_text_view_delete_range_cb (GtkTextBuffer *buffer,
155 static void _gail_text_view_changed_cb (GtkTextBuffer *buffer,
157 static void _gail_text_view_mark_set_cb (GtkTextBuffer *buffer,
161 static gchar* get_text_near_offset (AtkText *text,
162 GailOffsetType function,
163 AtkTextBoundary boundary_type,
167 static gint get_insert_offset (GtkTextBuffer *buffer);
168 static gint get_selection_bound (GtkTextBuffer *buffer);
169 static void emit_text_caret_moved (GailTextView *gail_text_view,
171 static gint insert_idle_handler (gpointer data);
173 typedef struct _GailTextViewPaste GailTextViewPaste;
175 struct _GailTextViewPaste
177 GtkTextBuffer* buffer;
181 G_DEFINE_TYPE_WITH_CODE (GailTextView, gail_text_view, GAIL_TYPE_CONTAINER,
182 G_IMPLEMENT_INTERFACE (ATK_TYPE_EDITABLE_TEXT, atk_editable_text_interface_init)
183 G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, atk_text_interface_init)
184 G_IMPLEMENT_INTERFACE (ATK_TYPE_STREAMABLE_CONTENT, atk_streamable_content_interface_init))
187 gail_text_view_class_init (GailTextViewClass *klass)
189 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
190 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
191 GailWidgetClass *widget_class;
193 widget_class = (GailWidgetClass*)klass;
195 gobject_class->finalize = gail_text_view_finalize;
197 class->ref_state_set = gail_text_view_ref_state_set;
198 class->initialize = gail_text_view_real_initialize;
200 widget_class->notify_gtk = gail_text_view_real_notify_gtk;
204 gail_text_view_init (GailTextView *text_view)
206 text_view->textutil = NULL;
207 text_view->signal_name = NULL;
208 text_view->previous_insert_offset = -1;
209 text_view->previous_selection_bound = -1;
210 text_view->insert_notify_handler = 0;
214 setup_buffer (GtkTextView *view,
215 GailTextView *gail_view)
217 GtkTextBuffer *buffer;
219 buffer = gtk_text_view_get_buffer (view);
221 if (gail_view->textutil)
222 g_object_unref (gail_view->textutil);
224 gail_view->textutil = gail_text_util_new ();
225 gail_text_util_buffer_setup (gail_view->textutil, buffer);
227 /* Set up signal callbacks */
228 g_signal_connect_data (buffer, "insert-text",
229 (GCallback) _gail_text_view_insert_text_cb, view, NULL, 0);
230 g_signal_connect_data (buffer, "delete-range",
231 (GCallback) _gail_text_view_delete_range_cb, view, NULL, 0);
232 g_signal_connect_data (buffer, "mark-set",
233 (GCallback) _gail_text_view_mark_set_cb, view, NULL, 0);
234 g_signal_connect_data (buffer, "changed",
235 (GCallback) _gail_text_view_changed_cb, view, NULL, 0);
240 gail_text_view_real_initialize (AtkObject *obj,
244 GailTextView *gail_view;
246 ATK_OBJECT_CLASS (gail_text_view_parent_class)->initialize (obj, data);
248 view = GTK_TEXT_VIEW (data);
250 gail_view = GAIL_TEXT_VIEW (obj);
251 setup_buffer (view, gail_view);
253 obj->role = ATK_ROLE_TEXT;
258 gail_text_view_finalize (GObject *object)
260 GailTextView *text_view = GAIL_TEXT_VIEW (object);
262 g_object_unref (text_view->textutil);
263 if (text_view->insert_notify_handler)
264 g_source_remove (text_view->insert_notify_handler);
266 G_OBJECT_CLASS (gail_text_view_parent_class)->finalize (object);
270 gail_text_view_real_notify_gtk (GObject *obj,
273 if (!strcmp (pspec->name, "editable"))
278 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (obj));
279 editable = gtk_text_view_get_editable (GTK_TEXT_VIEW (obj));
280 atk_object_notify_state_change (atk_obj, ATK_STATE_EDITABLE,
283 else if (!strcmp (pspec->name, "buffer"))
287 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (obj));
288 setup_buffer (GTK_TEXT_VIEW (obj), GAIL_TEXT_VIEW (atk_obj));
291 GAIL_WIDGET_CLASS (gail_text_view_parent_class)->notify_gtk (obj, pspec);
297 gail_text_view_ref_state_set (AtkObject *accessible)
299 AtkStateSet *state_set;
300 GtkTextView *text_view;
303 state_set = ATK_OBJECT_CLASS (gail_text_view_parent_class)->ref_state_set (accessible);
304 widget = GTK_ACCESSIBLE (accessible)->widget;
309 text_view = GTK_TEXT_VIEW (widget);
311 if (gtk_text_view_get_editable (text_view))
312 atk_state_set_add_state (state_set, ATK_STATE_EDITABLE);
313 atk_state_set_add_state (state_set, ATK_STATE_MULTI_LINE);
321 atk_text_interface_init (AtkTextIface *iface)
323 iface->get_text = gail_text_view_get_text;
324 iface->get_text_after_offset = gail_text_view_get_text_after_offset;
325 iface->get_text_at_offset = gail_text_view_get_text_at_offset;
326 iface->get_text_before_offset = gail_text_view_get_text_before_offset;
327 iface->get_character_at_offset = gail_text_view_get_character_at_offset;
328 iface->get_character_count = gail_text_view_get_character_count;
329 iface->get_caret_offset = gail_text_view_get_caret_offset;
330 iface->set_caret_offset = gail_text_view_set_caret_offset;
331 iface->get_offset_at_point = gail_text_view_get_offset_at_point;
332 iface->get_character_extents = gail_text_view_get_character_extents;
333 iface->get_n_selections = gail_text_view_get_n_selections;
334 iface->get_selection = gail_text_view_get_selection;
335 iface->add_selection = gail_text_view_add_selection;
336 iface->remove_selection = gail_text_view_remove_selection;
337 iface->set_selection = gail_text_view_set_selection;
338 iface->get_run_attributes = gail_text_view_get_run_attributes;
339 iface->get_default_attributes = gail_text_view_get_default_attributes;
343 gail_text_view_get_text (AtkText *text,
348 GtkTextBuffer *buffer;
349 GtkTextIter start, end;
352 widget = GTK_ACCESSIBLE (text)->widget;
354 /* State is defunct */
357 view = GTK_TEXT_VIEW (widget);
358 buffer = gtk_text_view_get_buffer (view);
359 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
360 gtk_text_buffer_get_iter_at_offset (buffer, &end, end_offset);
362 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
366 gail_text_view_get_text_after_offset (AtkText *text,
368 AtkTextBoundary boundary_type,
374 widget = GTK_ACCESSIBLE (text)->widget;
376 /* State is defunct */
379 return get_text_near_offset (text, GAIL_AFTER_OFFSET,
380 boundary_type, offset,
381 start_offset, end_offset);
385 gail_text_view_get_text_at_offset (AtkText *text,
387 AtkTextBoundary boundary_type,
393 widget = GTK_ACCESSIBLE (text)->widget;
395 /* State is defunct */
398 return get_text_near_offset (text, GAIL_AT_OFFSET,
399 boundary_type, offset,
400 start_offset, end_offset);
404 gail_text_view_get_text_before_offset (AtkText *text,
406 AtkTextBoundary boundary_type,
412 widget = GTK_ACCESSIBLE (text)->widget;
414 /* State is defunct */
417 return get_text_near_offset (text, GAIL_BEFORE_OFFSET,
418 boundary_type, offset,
419 start_offset, end_offset);
423 gail_text_view_get_character_at_offset (AtkText *text,
427 GtkTextIter start, end;
428 GtkTextBuffer *buffer;
432 widget = GTK_ACCESSIBLE (text)->widget;
436 buffer = GAIL_TEXT_VIEW (text)->textutil->buffer;
437 if (offset >= gtk_text_buffer_get_char_count (buffer))
440 gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
442 gtk_text_iter_forward_char (&end);
443 string = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
444 unichar = g_utf8_get_char (string);
450 gail_text_view_get_character_count (AtkText *text)
453 GtkTextBuffer *buffer;
456 widget = GTK_ACCESSIBLE (text)->widget;
458 /* State is defunct */
461 view = GTK_TEXT_VIEW (widget);
462 buffer = gtk_text_view_get_buffer (view);
463 return gtk_text_buffer_get_char_count (buffer);
467 gail_text_view_get_caret_offset (AtkText *text)
472 widget = GTK_ACCESSIBLE (text)->widget;
474 /* State is defunct */
477 view = GTK_TEXT_VIEW (widget);
478 return get_insert_offset (gtk_text_view_get_buffer (view));
482 gail_text_view_set_caret_offset (AtkText *text,
487 GtkTextBuffer *buffer;
490 widget = GTK_ACCESSIBLE (text)->widget;
492 /* State is defunct */
495 view = GTK_TEXT_VIEW (widget);
496 buffer = gtk_text_view_get_buffer (view);
498 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, offset);
499 gtk_text_buffer_place_cursor (buffer, &pos_itr);
500 gtk_text_view_scroll_to_iter (view, &pos_itr, 0, FALSE, 0, 0);
505 gail_text_view_get_offset_at_point (AtkText *text,
511 GtkTextBuffer *buffer;
513 gint x_widget, y_widget, x_window, y_window, buff_x, buff_y;
518 widget = GTK_ACCESSIBLE (text)->widget;
520 /* State is defunct */
523 view = GTK_TEXT_VIEW (widget);
524 buffer = gtk_text_view_get_buffer (view);
526 window = gtk_text_view_get_window (view, GTK_TEXT_WINDOW_WIDGET);
527 gdk_window_get_origin (window, &x_widget, &y_widget);
529 if (coords == ATK_XY_SCREEN)
534 else if (coords == ATK_XY_WINDOW)
536 window = gdk_window_get_toplevel (window);
537 gdk_window_get_origin (window, &x_window, &y_window);
539 x = x - x_widget + x_window;
540 y = y - y_widget + y_window;
545 gtk_text_view_window_to_buffer_coords (view, GTK_TEXT_WINDOW_WIDGET,
546 x, y, &buff_x, &buff_y);
547 gtk_text_view_get_visible_rect (view, &rect);
549 * Clamp point to visible rectangle
551 buff_x = CLAMP (buff_x, rect.x, rect.x + rect.width - 1);
552 buff_y = CLAMP (buff_y, rect.y, rect.y + rect.height - 1);
554 gtk_text_view_get_iter_at_location (view, &loc_itr, buff_x, buff_y);
556 * The iter at a location sometimes points to the next character.
557 * See bug 111031. We work around that
559 gtk_text_view_get_iter_location (view, &loc_itr, &rect);
561 gtk_text_iter_backward_char (&loc_itr);
562 return gtk_text_iter_get_offset (&loc_itr);
566 gail_text_view_get_character_extents (AtkText *text,
575 GtkTextBuffer *buffer;
578 GdkRectangle rectangle;
580 gint x_widget, y_widget, x_window, y_window;
582 widget = GTK_ACCESSIBLE (text)->widget;
584 /* State is defunct */
587 view = GTK_TEXT_VIEW (widget);
588 buffer = gtk_text_view_get_buffer (view);
589 gtk_text_buffer_get_iter_at_offset (buffer, &iter, offset);
590 gtk_text_view_get_iter_location (view, &iter, &rectangle);
592 window = gtk_text_view_get_window (view, GTK_TEXT_WINDOW_WIDGET);
593 gdk_window_get_origin (window, &x_widget, &y_widget);
595 *height = rectangle.height;
596 *width = rectangle.width;
598 gtk_text_view_buffer_to_window_coords (view, GTK_TEXT_WINDOW_WIDGET,
599 rectangle.x, rectangle.y, x, y);
600 if (coords == ATK_XY_WINDOW)
602 window = gdk_window_get_toplevel (window);
603 gdk_window_get_origin (window, &x_window, &y_window);
604 *x += x_widget - x_window;
605 *y += y_widget - y_window;
607 else if (coords == ATK_XY_SCREEN)
621 static AtkAttributeSet*
622 gail_text_view_get_run_attributes (AtkText *text,
628 GtkTextBuffer *buffer;
631 widget = GTK_ACCESSIBLE (text)->widget;
633 /* State is defunct */
636 view = GTK_TEXT_VIEW (widget);
637 buffer = gtk_text_view_get_buffer (view);
639 return gail_misc_buffer_get_run_attributes (buffer, offset,
640 start_offset, end_offset);
643 static AtkAttributeSet*
644 gail_text_view_get_default_attributes (AtkText *text)
648 GtkTextAttributes *text_attrs;
649 AtkAttributeSet *attrib_set = NULL;
650 PangoFontDescription *font;
652 widget = GTK_ACCESSIBLE (text)->widget;
654 /* State is defunct */
657 view = GTK_TEXT_VIEW (widget);
658 text_attrs = gtk_text_view_get_default_attributes (view);
660 font = text_attrs->font;
664 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
665 ATK_TEXT_ATTR_STYLE);
667 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
668 ATK_TEXT_ATTR_VARIANT);
670 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
671 ATK_TEXT_ATTR_STRETCH);
674 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
675 ATK_TEXT_ATTR_JUSTIFICATION);
677 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
678 ATK_TEXT_ATTR_DIRECTION);
680 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
681 ATK_TEXT_ATTR_WRAP_MODE);
683 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
684 ATK_TEXT_ATTR_FG_STIPPLE);
686 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
687 ATK_TEXT_ATTR_BG_STIPPLE);
689 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
690 ATK_TEXT_ATTR_FG_COLOR);
692 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
693 ATK_TEXT_ATTR_BG_COLOR);
697 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
698 ATK_TEXT_ATTR_FAMILY_NAME);
701 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
702 ATK_TEXT_ATTR_LANGUAGE);
706 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
707 ATK_TEXT_ATTR_WEIGHT);
710 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
711 ATK_TEXT_ATTR_SCALE);
715 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
719 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
720 ATK_TEXT_ATTR_STRIKETHROUGH);
722 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
723 ATK_TEXT_ATTR_UNDERLINE);
725 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
728 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
729 ATK_TEXT_ATTR_BG_FULL_HEIGHT);
731 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
732 ATK_TEXT_ATTR_PIXELS_INSIDE_WRAP);
734 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
735 ATK_TEXT_ATTR_PIXELS_BELOW_LINES);
737 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
738 ATK_TEXT_ATTR_PIXELS_ABOVE_LINES);
740 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
741 ATK_TEXT_ATTR_EDITABLE);
743 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
744 ATK_TEXT_ATTR_INVISIBLE);
746 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
747 ATK_TEXT_ATTR_INDENT);
749 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
750 ATK_TEXT_ATTR_RIGHT_MARGIN);
752 attrib_set = gail_misc_add_to_attr_set (attrib_set, text_attrs,
753 ATK_TEXT_ATTR_LEFT_MARGIN);
755 gtk_text_attributes_unref (text_attrs);
760 gail_text_view_get_n_selections (AtkText *text)
764 GtkTextBuffer *buffer;
765 GtkTextIter start, end;
766 gint select_start, select_end;
768 widget = GTK_ACCESSIBLE (text)->widget;
770 /* State is defunct */
773 view = GTK_TEXT_VIEW (widget);
774 buffer = gtk_text_view_get_buffer (view);
776 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
777 select_start = gtk_text_iter_get_offset (&start);
778 select_end = gtk_text_iter_get_offset (&end);
780 if (select_start != select_end)
787 gail_text_view_get_selection (AtkText *text,
794 GtkTextBuffer *buffer;
795 GtkTextIter start, end;
797 widget = GTK_ACCESSIBLE (text)->widget;
799 /* State is defunct */
802 /* Only let the user get the selection if one is set, and if the
803 * selection_num is 0.
805 if (selection_num != 0)
808 view = GTK_TEXT_VIEW (widget);
809 buffer = gtk_text_view_get_buffer (view);
811 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
812 *start_pos = gtk_text_iter_get_offset (&start);
813 *end_pos = gtk_text_iter_get_offset (&end);
815 if (*start_pos != *end_pos)
816 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
822 gail_text_view_add_selection (AtkText *text,
828 GtkTextBuffer *buffer;
830 GtkTextIter start, end;
831 gint select_start, select_end;
833 widget = GTK_ACCESSIBLE (text)->widget;
835 /* State is defunct */
838 view = GTK_TEXT_VIEW (widget);
839 buffer = gtk_text_view_get_buffer (view);
841 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
842 select_start = gtk_text_iter_get_offset (&start);
843 select_end = gtk_text_iter_get_offset (&end);
845 /* If there is already a selection, then don't allow another to be added,
846 * since GtkTextView only supports one selected region.
848 if (select_start == select_end)
850 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, start_pos);
851 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &pos_itr);
852 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, end_pos);
853 gtk_text_buffer_move_mark_by_name (buffer, "insert", &pos_itr);
861 gail_text_view_remove_selection (AtkText *text,
866 GtkTextBuffer *buffer;
867 GtkTextMark *cursor_mark;
868 GtkTextIter cursor_itr;
869 GtkTextIter start, end;
870 gint select_start, select_end;
872 widget = GTK_ACCESSIBLE (text)->widget;
874 /* State is defunct */
877 if (selection_num != 0)
880 view = GTK_TEXT_VIEW (widget);
881 buffer = gtk_text_view_get_buffer (view);
883 gtk_text_buffer_get_selection_bounds(buffer, &start, &end);
884 select_start = gtk_text_iter_get_offset(&start);
885 select_end = gtk_text_iter_get_offset(&end);
887 if (select_start != select_end)
889 /* Setting the start & end of the selected region to the caret position
890 * turns off the selection.
892 cursor_mark = gtk_text_buffer_get_insert (buffer);
893 gtk_text_buffer_get_iter_at_mark (buffer, &cursor_itr, cursor_mark);
894 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &cursor_itr);
902 gail_text_view_set_selection (AtkText *text,
909 GtkTextBuffer *buffer;
911 GtkTextIter start, end;
912 gint select_start, select_end;
914 widget = GTK_ACCESSIBLE (text)->widget;
917 /* State is defunct */
921 /* Only let the user move the selection if one is set, and if the
924 if (selection_num != 0)
927 view = GTK_TEXT_VIEW (widget);
928 buffer = gtk_text_view_get_buffer (view);
930 gtk_text_buffer_get_selection_bounds(buffer, &start, &end);
931 select_start = gtk_text_iter_get_offset(&start);
932 select_end = gtk_text_iter_get_offset(&end);
934 if (select_start != select_end)
936 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, start_pos);
937 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &pos_itr);
938 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, end_pos);
939 gtk_text_buffer_move_mark_by_name (buffer, "insert", &pos_itr);
946 /* atkeditabletext.h */
949 atk_editable_text_interface_init (AtkEditableTextIface *iface)
951 iface->set_text_contents = gail_text_view_set_text_contents;
952 iface->insert_text = gail_text_view_insert_text;
953 iface->copy_text = gail_text_view_copy_text;
954 iface->cut_text = gail_text_view_cut_text;
955 iface->delete_text = gail_text_view_delete_text;
956 iface->paste_text = gail_text_view_paste_text;
957 iface->set_run_attributes = gail_text_view_set_run_attributes;
961 gail_text_view_set_run_attributes (AtkEditableText *text,
962 AtkAttributeSet *attrib_set,
967 GtkTextBuffer *buffer;
977 widget = GTK_ACCESSIBLE (text)->widget;
979 /* State is defunct */
982 view = GTK_TEXT_VIEW (widget);
983 if (!gtk_text_view_get_editable (view))
986 buffer = gtk_text_view_get_buffer (view);
988 if (attrib_set == NULL)
991 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
992 gtk_text_buffer_get_iter_at_offset (buffer, &end, end_offset);
994 tag = gtk_text_buffer_create_tag (buffer, NULL, NULL);
996 for (l = attrib_set; l; l = l->next)
1007 if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_LEFT_MARGIN)))
1008 g_object_set (G_OBJECT (tag), "left_margin", atoi (value), NULL);
1010 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_RIGHT_MARGIN)))
1011 g_object_set (G_OBJECT (tag), "right_margin", atoi (value), NULL);
1013 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_INDENT)))
1014 g_object_set (G_OBJECT (tag), "indent", atoi (value), NULL);
1016 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_PIXELS_ABOVE_LINES)))
1017 g_object_set (G_OBJECT (tag), "pixels_above_lines", atoi (value), NULL);
1019 else if (!strcmp(name, atk_text_attribute_get_name (ATK_TEXT_ATTR_PIXELS_BELOW_LINES)))
1020 g_object_set (G_OBJECT (tag), "pixels_below_lines", atoi (value), NULL);
1022 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_PIXELS_INSIDE_WRAP)))
1023 g_object_set (G_OBJECT (tag), "pixels_inside_wrap", atoi (value), NULL);
1025 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_SIZE)))
1026 g_object_set (G_OBJECT (tag), "size", atoi (value), NULL);
1028 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_RISE)))
1029 g_object_set (G_OBJECT (tag), "rise", atoi (value), NULL);
1031 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_WEIGHT)))
1032 g_object_set (G_OBJECT (tag), "weight", atoi (value), NULL);
1034 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_BG_FULL_HEIGHT)))
1036 g_object_set (G_OBJECT (tag), "bg_full_height",
1037 (strcmp (value, atk_text_attribute_get_value (ATK_TEXT_ATTR_BG_FULL_HEIGHT, 0))),
1041 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_LANGUAGE)))
1042 g_object_set (G_OBJECT (tag), "language", value, NULL);
1044 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_FAMILY_NAME)))
1045 g_object_set (G_OBJECT (tag), "family", value, NULL);
1047 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_EDITABLE)))
1049 g_object_set (G_OBJECT (tag), "editable",
1050 (strcmp (value, atk_text_attribute_get_value (ATK_TEXT_ATTR_EDITABLE, 0))),
1054 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_INVISIBLE)))
1056 g_object_set (G_OBJECT (tag), "invisible",
1057 (strcmp (value, atk_text_attribute_get_value (ATK_TEXT_ATTR_EDITABLE, 0))),
1061 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_UNDERLINE)))
1063 for (j = 0; j < 3; j++)
1065 if (!strcmp (value, atk_text_attribute_get_value (ATK_TEXT_ATTR_UNDERLINE, j)))
1067 g_object_set (G_OBJECT (tag), "underline", j, NULL);
1073 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_STRIKETHROUGH)))
1075 g_object_set (G_OBJECT (tag), "strikethrough",
1076 (strcmp (value, atk_text_attribute_get_value (ATK_TEXT_ATTR_STRIKETHROUGH, 0))),
1080 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_BG_COLOR)))
1082 RGB_vals = g_strsplit (value, ",", 3);
1083 color = g_malloc (sizeof (GdkColor));
1084 color->red = atoi (RGB_vals[0]);
1085 color->green = atoi (RGB_vals[1]);
1086 color->blue = atoi (RGB_vals[2]);
1087 g_object_set (G_OBJECT (tag), "background_gdk", color, NULL);
1090 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_FG_COLOR)))
1092 RGB_vals = g_strsplit (value, ",", 3);
1093 color = g_malloc (sizeof (GdkColor));
1094 color->red = atoi (RGB_vals[0]);
1095 color->green = atoi (RGB_vals[1]);
1096 color->blue = atoi (RGB_vals[2]);
1097 g_object_set (G_OBJECT (tag), "foreground_gdk", color, NULL);
1100 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_STRETCH)))
1102 for (j = 0; j < 9; j++)
1104 if (!strcmp (value, atk_text_attribute_get_value (ATK_TEXT_ATTR_STRETCH, j)))
1106 g_object_set (G_OBJECT (tag), "stretch", j, NULL);
1112 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_JUSTIFICATION)))
1114 for (j = 0; j < 4; j++)
1116 if (!strcmp (value, atk_text_attribute_get_value (ATK_TEXT_ATTR_JUSTIFICATION, j)))
1118 g_object_set (G_OBJECT (tag), "justification", j, NULL);
1124 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_DIRECTION)))
1126 for (j = 0; j < 3; j++)
1128 if (!strcmp (value, atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION, j)))
1130 g_object_set (G_OBJECT (tag), "direction", j, NULL);
1136 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_VARIANT)))
1138 for (j = 0; j < 2; j++)
1140 if (!strcmp (value, atk_text_attribute_get_value (ATK_TEXT_ATTR_VARIANT, j)))
1142 g_object_set (G_OBJECT (tag), "variant", j, NULL);
1148 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_WRAP_MODE)))
1150 for (j = 0; j < 3; j++)
1152 if (!strcmp (value, atk_text_attribute_get_value (ATK_TEXT_ATTR_WRAP_MODE, j)))
1154 g_object_set (G_OBJECT (tag), "wrap_mode", j, NULL);
1160 else if (!strcmp (name, atk_text_attribute_get_name (ATK_TEXT_ATTR_STYLE)))
1162 for (j = 0; j < 3; j++)
1164 if (!strcmp (value, atk_text_attribute_get_value (ATK_TEXT_ATTR_STYLE, j)))
1166 g_object_set (G_OBJECT (tag), "style", j, NULL);
1176 gtk_text_buffer_apply_tag (buffer, tag, &start, &end);
1182 gail_text_view_set_text_contents (AtkEditableText *text,
1183 const gchar *string)
1187 GtkTextBuffer *buffer;
1189 widget = GTK_ACCESSIBLE (text)->widget;
1191 /* State is defunct */
1194 view = GTK_TEXT_VIEW (widget);
1195 if (!gtk_text_view_get_editable (view))
1197 buffer = gtk_text_view_get_buffer (view);
1199 /* The -1 indicates that the input string must be null-terminated */
1200 gtk_text_buffer_set_text (buffer, string, -1);
1204 gail_text_view_insert_text (AtkEditableText *text,
1205 const gchar *string,
1211 GtkTextBuffer *buffer;
1212 GtkTextIter pos_itr;
1214 widget = GTK_ACCESSIBLE (text)->widget;
1216 /* State is defunct */
1219 view = GTK_TEXT_VIEW (widget);
1220 if (!gtk_text_view_get_editable (view))
1222 buffer = gtk_text_view_get_buffer (view);
1224 gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, *position);
1225 gtk_text_buffer_insert (buffer, &pos_itr, string, length);
1229 gail_text_view_copy_text (AtkEditableText *text,
1235 GtkTextBuffer *buffer;
1236 GtkTextIter start, end;
1238 GtkClipboard *clipboard;
1240 widget = GTK_ACCESSIBLE (text)->widget;
1242 /* State is defunct */
1245 view = GTK_TEXT_VIEW (widget);
1246 buffer = gtk_text_view_get_buffer (view);
1248 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_pos);
1249 gtk_text_buffer_get_iter_at_offset (buffer, &end, end_pos);
1250 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1251 clipboard = gtk_clipboard_get_for_display (gtk_widget_get_display (widget),
1252 GDK_SELECTION_CLIPBOARD);
1253 gtk_clipboard_set_text (clipboard, str, -1);
1257 gail_text_view_cut_text (AtkEditableText *text,
1263 GtkTextBuffer *buffer;
1264 GtkTextIter start, end;
1266 GtkClipboard *clipboard;
1268 widget = GTK_ACCESSIBLE (text)->widget;
1270 /* State is defunct */
1273 view = GTK_TEXT_VIEW (widget);
1274 if (!gtk_text_view_get_editable (view))
1276 buffer = gtk_text_view_get_buffer (view);
1278 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_pos);
1279 gtk_text_buffer_get_iter_at_offset (buffer, &end, end_pos);
1280 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1281 clipboard = gtk_clipboard_get_for_display (gtk_widget_get_display (widget),
1282 GDK_SELECTION_CLIPBOARD);
1283 gtk_clipboard_set_text (clipboard, str, -1);
1284 gtk_text_buffer_delete (buffer, &start, &end);
1288 gail_text_view_delete_text (AtkEditableText *text,
1294 GtkTextBuffer *buffer;
1295 GtkTextIter start_itr;
1296 GtkTextIter end_itr;
1298 widget = GTK_ACCESSIBLE (text)->widget;
1300 /* State is defunct */
1303 view = GTK_TEXT_VIEW (widget);
1304 if (!gtk_text_view_get_editable (view))
1306 buffer = gtk_text_view_get_buffer (view);
1308 gtk_text_buffer_get_iter_at_offset (buffer, &start_itr, start_pos);
1309 gtk_text_buffer_get_iter_at_offset (buffer, &end_itr, end_pos);
1310 gtk_text_buffer_delete (buffer, &start_itr, &end_itr);
1314 gail_text_view_paste_text (AtkEditableText *text,
1319 GtkTextBuffer *buffer;
1320 GailTextViewPaste paste_struct;
1321 GtkClipboard *clipboard;
1323 widget = GTK_ACCESSIBLE (text)->widget;
1325 /* State is defunct */
1328 view = GTK_TEXT_VIEW (widget);
1329 if (!gtk_text_view_get_editable (view))
1331 buffer = gtk_text_view_get_buffer (view);
1333 paste_struct.buffer = buffer;
1334 paste_struct.position = position;
1336 g_object_ref (paste_struct.buffer);
1337 clipboard = gtk_clipboard_get_for_display (gtk_widget_get_display (widget),
1338 GDK_SELECTION_CLIPBOARD);
1339 gtk_clipboard_request_text (clipboard,
1340 gail_text_view_paste_received, &paste_struct);
1344 gail_text_view_paste_received (GtkClipboard *clipboard,
1348 GailTextViewPaste* paste_struct = (GailTextViewPaste *)data;
1349 GtkTextIter pos_itr;
1353 gtk_text_buffer_get_iter_at_offset (paste_struct->buffer, &pos_itr,
1354 paste_struct->position);
1355 gtk_text_buffer_insert (paste_struct->buffer, &pos_itr, text, -1);
1358 g_object_unref (paste_struct->buffer);
1363 /* Note arg1 returns the start of the insert range, arg3 returns the
1364 * end of the insert range if multiple characters are inserted. If one
1365 * character is inserted they have the same value, which is the caret
1366 * location. arg2 returns the begin location of the insert.
1369 _gail_text_view_insert_text_cb (GtkTextBuffer *buffer,
1375 GtkTextView *text = (GtkTextView *) user_data;
1376 AtkObject *accessible;
1377 GailTextView *gail_text_view;
1381 g_return_if_fail (arg3 > 0);
1383 accessible = gtk_widget_get_accessible(GTK_WIDGET(text));
1384 gail_text_view = GAIL_TEXT_VIEW (accessible);
1386 gail_text_view->signal_name = "text_changed::insert";
1387 position = gtk_text_iter_get_offset (arg1);
1388 length = g_utf8_strlen(arg2, arg3);
1390 if (gail_text_view->length == 0)
1392 gail_text_view->position = position;
1393 gail_text_view->length = length;
1395 else if (gail_text_view->position + gail_text_view->length == position)
1397 gail_text_view->length += length;
1402 * We have a non-contiguous insert so report what we have
1404 if (gail_text_view->insert_notify_handler)
1406 g_source_remove (gail_text_view->insert_notify_handler);
1408 gail_text_view->insert_notify_handler = 0;
1409 insert_idle_handler (gail_text_view);
1410 gail_text_view->position = position;
1411 gail_text_view->length = length;
1415 * The signal will be emitted when the changed signal is received
1419 /* Note arg1 returns the start of the delete range, arg2 returns the
1420 * end of the delete range if multiple characters are deleted. If one
1421 * character is deleted they have the same value, which is the caret
1425 _gail_text_view_delete_range_cb (GtkTextBuffer *buffer,
1430 GtkTextView *text = (GtkTextView *) user_data;
1431 AtkObject *accessible;
1432 GailTextView *gail_text_view;
1433 gint offset = gtk_text_iter_get_offset (arg1);
1434 gint length = gtk_text_iter_get_offset (arg2) - offset;
1436 accessible = gtk_widget_get_accessible(GTK_WIDGET(text));
1437 gail_text_view = GAIL_TEXT_VIEW (accessible);
1438 if (gail_text_view->insert_notify_handler)
1440 g_source_remove (gail_text_view->insert_notify_handler);
1441 gail_text_view->insert_notify_handler = 0;
1442 if (gail_text_view->position == offset &&
1443 gail_text_view->length == length)
1446 * Do not bother with insert and delete notifications
1448 gail_text_view->signal_name = NULL;
1449 gail_text_view->position = 0;
1450 gail_text_view->length = 0;
1454 insert_idle_handler (gail_text_view);
1456 g_signal_emit_by_name (accessible, "text_changed::delete",
1460 /* Note arg1 and arg2 point to the same offset, which is the caret
1461 * position after the move
1464 _gail_text_view_mark_set_cb (GtkTextBuffer *buffer,
1469 GtkTextView *text = (GtkTextView *) user_data;
1470 AtkObject *accessible;
1471 GailTextView *gail_text_view;
1472 const char *mark_name = gtk_text_mark_get_name(arg2);
1474 accessible = gtk_widget_get_accessible(GTK_WIDGET(text));
1475 gail_text_view = GAIL_TEXT_VIEW (accessible);
1478 * Only generate the signal for the "insert" mark, which
1479 * represents the cursor.
1481 if (mark_name && !strcmp(mark_name, "insert"))
1483 int insert_offset, selection_bound;
1484 gboolean selection_changed;
1486 insert_offset = gtk_text_iter_get_offset (arg1);
1488 selection_bound = get_selection_bound (buffer);
1489 if (selection_bound != insert_offset)
1491 if (selection_bound != gail_text_view->previous_selection_bound ||
1492 insert_offset != gail_text_view->previous_insert_offset)
1494 selection_changed = TRUE;
1498 selection_changed = FALSE;
1501 else if (gail_text_view->previous_selection_bound != gail_text_view->previous_insert_offset)
1503 selection_changed = TRUE;
1507 selection_changed = FALSE;
1510 emit_text_caret_moved (gail_text_view, insert_offset);
1512 * insert and selection_bound marks are different to a selection
1515 if (selection_changed)
1516 g_signal_emit_by_name (accessible, "text_selection_changed");
1517 gail_text_view->previous_selection_bound = selection_bound;
1522 _gail_text_view_changed_cb (GtkTextBuffer *buffer,
1525 GtkTextView *text = (GtkTextView *) user_data;
1526 AtkObject *accessible;
1527 GailTextView *gail_text_view;
1529 accessible = gtk_widget_get_accessible (GTK_WIDGET (text));
1530 gail_text_view = GAIL_TEXT_VIEW (accessible);
1531 if (gail_text_view->signal_name)
1533 if (!gail_text_view->insert_notify_handler)
1535 gail_text_view->insert_notify_handler = gdk_threads_add_idle (insert_idle_handler, accessible);
1539 emit_text_caret_moved (gail_text_view, get_insert_offset (buffer));
1540 gail_text_view->previous_selection_bound = get_selection_bound (buffer);
1544 get_text_near_offset (AtkText *text,
1545 GailOffsetType function,
1546 AtkTextBoundary boundary_type,
1552 gpointer layout = NULL;
1554 view = GTK_TEXT_VIEW (GTK_ACCESSIBLE (text)->widget);
1557 * Pass the GtkTextView to the function gail_text_util_get_text()
1558 * so it can find the start and end of the current line on the display.
1560 if (boundary_type == ATK_TEXT_BOUNDARY_LINE_START ||
1561 boundary_type == ATK_TEXT_BOUNDARY_LINE_END)
1564 return gail_text_util_get_text (GAIL_TEXT_VIEW (text)->textutil, layout,
1565 function, boundary_type, offset,
1566 start_offset, end_offset);
1570 get_insert_offset (GtkTextBuffer *buffer)
1572 GtkTextMark *cursor_mark;
1573 GtkTextIter cursor_itr;
1575 cursor_mark = gtk_text_buffer_get_insert (buffer);
1576 gtk_text_buffer_get_iter_at_mark (buffer, &cursor_itr, cursor_mark);
1577 return gtk_text_iter_get_offset (&cursor_itr);
1581 get_selection_bound (GtkTextBuffer *buffer)
1583 GtkTextMark *selection_mark;
1584 GtkTextIter selection_itr;
1586 selection_mark = gtk_text_buffer_get_selection_bound (buffer);
1587 gtk_text_buffer_get_iter_at_mark (buffer, &selection_itr, selection_mark);
1588 return gtk_text_iter_get_offset (&selection_itr);
1592 emit_text_caret_moved (GailTextView *gail_text_view,
1596 * If we have text which has been inserted notify the user
1598 if (gail_text_view->insert_notify_handler)
1600 g_source_remove (gail_text_view->insert_notify_handler);
1601 gail_text_view->insert_notify_handler = 0;
1602 insert_idle_handler (gail_text_view);
1605 if (insert_offset != gail_text_view->previous_insert_offset)
1608 * If the caret position has not changed then don't bother notifying
1610 * When mouse click is used to change caret position, notification
1611 * is received on button down and button up.
1613 g_signal_emit_by_name (gail_text_view, "text_caret_moved", insert_offset);
1614 gail_text_view->previous_insert_offset = insert_offset;
1619 insert_idle_handler (gpointer data)
1621 GailTextView *gail_text_view;
1622 GtkTextBuffer *buffer;
1624 gail_text_view = GAIL_TEXT_VIEW (data);
1626 g_signal_emit_by_name (data,
1627 gail_text_view->signal_name,
1628 gail_text_view->position,
1629 gail_text_view->length);
1630 gail_text_view->signal_name = NULL;
1631 gail_text_view->position = 0;
1632 gail_text_view->length = 0;
1634 buffer = gail_text_view->textutil->buffer;
1635 if (gail_text_view->insert_notify_handler)
1638 * If called from idle handler notify caret moved
1640 gail_text_view->insert_notify_handler = 0;
1641 emit_text_caret_moved (gail_text_view, get_insert_offset (buffer));
1642 gail_text_view->previous_selection_bound = get_selection_bound (buffer);
1649 atk_streamable_content_interface_init (AtkStreamableContentIface *iface)
1651 iface->get_n_mime_types = gail_streamable_content_get_n_mime_types;
1652 iface->get_mime_type = gail_streamable_content_get_mime_type;
1653 iface->get_stream = gail_streamable_content_get_stream;
1656 static gint gail_streamable_content_get_n_mime_types (AtkStreamableContent *streamable)
1658 gint n_mime_types = 0;
1660 if (GAIL_IS_TEXT_VIEW (streamable) && GAIL_TEXT_VIEW (streamable)->textutil)
1663 gboolean advertises_plaintext = FALSE;
1665 gtk_text_buffer_get_serialize_formats (
1666 GAIL_TEXT_VIEW (streamable)->textutil->buffer,
1668 for (i = 0; i < n_mime_types-1; ++i)
1669 if (!strcmp ("text/plain", gdk_atom_name (atoms[i])))
1670 advertises_plaintext = TRUE;
1671 if (!advertises_plaintext) ++n_mime_types;
1672 /* we support text/plain even if the GtkTextBuffer doesn't */
1674 return n_mime_types;
1677 static G_CONST_RETURN gchar*
1678 gail_streamable_content_get_mime_type (AtkStreamableContent *streamable, gint i)
1680 if (GAIL_IS_TEXT_VIEW (streamable) && GAIL_TEXT_VIEW (streamable)->textutil)
1682 gint n_mime_types = 0;
1684 atoms = gtk_text_buffer_get_serialize_formats (
1685 GAIL_TEXT_VIEW (streamable)->textutil->buffer,
1687 if (i < n_mime_types)
1689 return gdk_atom_name (atoms [i]);
1691 else if (i == n_mime_types)
1692 return "text/plain";
1697 static GIOChannel* gail_streamable_content_get_stream (AtkStreamableContent *streamable,
1698 const gchar *mime_type)
1700 gint i, n_mime_types = 0;
1702 if (!GAIL_IS_TEXT_VIEW (streamable) || !GAIL_TEXT_VIEW (streamable)->textutil)
1704 atoms = gtk_text_buffer_get_serialize_formats (
1705 GAIL_TEXT_VIEW (streamable)->textutil->buffer,
1707 for (i = 0; i < n_mime_types; ++i)
1709 if (!strcmp ("text/plain", mime_type) ||
1710 !strcmp (gdk_atom_name (atoms[i]), mime_type)) {
1711 GtkTextBuffer *buffer;
1716 GtkTextIter start, end;
1717 GIOChannel *gio = NULL;
1719 buffer = GAIL_TEXT_VIEW (streamable)->textutil->buffer;
1720 gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
1721 gtk_text_buffer_get_iter_at_offset (buffer, &end, -1);
1722 if (!strcmp ("text/plain", mime_type))
1724 cbuf = (guint8*) gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1725 len = strlen ((const char *) cbuf);
1729 cbuf = gtk_text_buffer_serialize (buffer, buffer, atoms[i], &start, &end, &len);
1731 g_snprintf (tname, 20, "streamXXXXXX");
1732 fd = g_mkstemp (tname);
1733 gio = g_io_channel_unix_new (fd);
1734 g_io_channel_set_encoding (gio, NULL, &err);
1735 if (!err) g_io_channel_write_chars (gio, (const char *) cbuf, (gssize) len, &written, &err);
1736 else g_message ("%s", err->message);
1737 if (!err) g_io_channel_seek_position (gio, 0, G_SEEK_SET, &err);
1738 else g_message ("%s", err->message);
1739 if (!err) g_io_channel_flush (gio, &err);
1740 else g_message ("%s", err->message);
1742 g_message ("<error writing to stream [%s]>", tname);
1745 /* make sure the file is removed on unref of the giochannel */