1 // TextBuffer.cs - customizations to Gtk.TextBuffer.
2 //
3 // Authors:  Mike Kestner  <mkestner@ximian.com>
4 //
5 // Copyright (c) 2004-2006  Novell, Inc.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of version 2 of the Lesser GNU General
9 // Public License as published by the Free Software Foundation.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this program; if not, write to the
18 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, USA.
20 
21 namespace Gtk {
22 
23 	using System;
24 	using System.Runtime.InteropServices;
25 
26 	public partial class TextBuffer {
27 
28 		[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
gtk_text_buffer_set_text(IntPtr raw, IntPtr text, int len)29 		static extern void gtk_text_buffer_set_text (IntPtr raw, IntPtr text, int len);
30 
Clear()31 		public void Clear ()
32 		{
33 			Gtk.TextIter start = StartIter, end = EndIter;
34 			Delete (ref start, ref end);
35 		}
36 
37 		[Obsolete ("Replaced by 'ref TextIter, ref TextIter' overload")]
Delete(TextIter start, TextIter end )38 		public void Delete (TextIter start, TextIter end )
39 		{
40 			Delete (ref start, ref end);
41 		}
42 
43 		// overload to paste clipboard contents at cursor editable by default.
PasteClipboard(Gtk.Clipboard clipboard)44 		public void PasteClipboard (Gtk.Clipboard clipboard)
45 		{
46 			gtk_text_buffer_paste_clipboard(Handle, clipboard.Handle, IntPtr.Zero, true);
47 		}
48 
49 		[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
gtk_text_buffer_insert(IntPtr raw, ref Gtk.TextIter iter, IntPtr text, int len)50 		static extern void gtk_text_buffer_insert (IntPtr raw, ref Gtk.TextIter iter, IntPtr text, int len);
51 
52 		[Obsolete ("Replaced by 'ref TextIter iter' overload")]
Insert(TextIter iter, string text)53 		public void Insert (TextIter iter, string text)
54 		{
55 			Insert (ref iter, text);
56 		}
57 
Insert(ref Gtk.TextIter iter, string text)58 		public void Insert (ref Gtk.TextIter iter, string text)
59 		{
60 			IntPtr native = GLib.Marshaller.StringToPtrGStrdup (text);
61 			gtk_text_buffer_insert (Handle, ref iter, native, -1);
62 			GLib.Marshaller.Free (native);
63 		}
64 
65 		[Obsolete ("Replaced by 'ref TextIter iter' overload")]
InsertRange(TextIter iter, TextIter start, TextIter end )66 		public void InsertRange (TextIter iter, TextIter start, TextIter end )
67 		{
68 			InsertRange (ref iter, start, end);
69 		}
70 
71 		[Obsolete ("Replaced by 'ref TextIter iter' overload")]
InsertWithTags(TextIter iter, string text, params TextTag[] tags)72 		public void InsertWithTags (TextIter iter, string text, params TextTag[] tags)
73 		{
74 			InsertWithTags (ref iter, text, tags);
75 		}
76 
InsertWithTags(ref TextIter iter, string text, params TextTag[] tags)77 		public void InsertWithTags (ref TextIter iter, string text, params TextTag[] tags)
78 		{
79 			TextIter start;
80 			int offset = iter.Offset;
81 			Insert (ref iter, text);
82 
83 			start = GetIterAtOffset (offset);
84 			iter = GetIterAtOffset (offset + text.Length);
85 
86 			foreach (TextTag t in tags)
87 				this.ApplyTag (t, start, iter);
88 		}
89 
InsertWithTagsByName(ref TextIter iter, string text, params string[] tagnames)90 		public void InsertWithTagsByName (ref TextIter iter, string text, params string[] tagnames)
91 		{
92 			TextIter start;
93 			int offset = iter.Offset;
94 			Insert (ref iter, text);
95 
96 			start = GetIterAtOffset (offset);
97 			iter = GetIterAtOffset (offset + text.Length);
98 
99 			foreach (string tagname in tagnames) {
100 				TextTag tag = TagTable.Lookup (tagname);
101 				if (tag != null)
102 					this.ApplyTag (tag, start, iter);
103 			}
104 		}
105 
106 		[Obsolete("Use the TextBuffer.Text property's setter")]
SetText(string text)107 		public void SetText (string text)
108 		{
109 			Text = text;
110 		}
111 
112 		[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
gtk_text_buffer_insert_interactive(IntPtr raw, ref Gtk.TextIter iter, IntPtr text, int len, bool default_editable)113 		static extern bool gtk_text_buffer_insert_interactive(IntPtr raw, ref Gtk.TextIter iter, IntPtr text, int len, bool default_editable);
114 
InsertInteractive(ref Gtk.TextIter iter, string text, bool default_editable)115 		public bool InsertInteractive(ref Gtk.TextIter iter, string text, bool default_editable)
116 		{
117 			IntPtr native = GLib.Marshaller.StringToPtrGStrdup (text);
118 			bool result = gtk_text_buffer_insert_interactive(Handle, ref iter, native, -1, default_editable);
119 			GLib.Marshaller.Free (native);
120 			return result;
121 		}
122 
123 		[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
gtk_text_buffer_insert_interactive_at_cursor(IntPtr raw, IntPtr text, int len, bool default_editable)124 		static extern bool gtk_text_buffer_insert_interactive_at_cursor(IntPtr raw, IntPtr text, int len, bool default_editable);
125 
InsertInteractiveAtCursor(string text, bool default_editable)126 		public bool InsertInteractiveAtCursor(string text, bool default_editable)
127 		{
128 			IntPtr native = GLib.Marshaller.StringToPtrGStrdup (text);
129 			bool result = gtk_text_buffer_insert_interactive_at_cursor(Handle, native, -1, default_editable);
130 			GLib.Marshaller.Free (native);
131 			return result;
132 		}
133 
134 		[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
gtk_text_buffer_insert_at_cursor(IntPtr raw, IntPtr text, int len)135 		static extern void gtk_text_buffer_insert_at_cursor(IntPtr raw, IntPtr text, int len);
136 
InsertAtCursor(string text)137 		public void InsertAtCursor(string text)
138 		{
139 			IntPtr native = GLib.Marshaller.StringToPtrGStrdup (text);
140 			gtk_text_buffer_insert_at_cursor(Handle, native, -1);
141 			GLib.Marshaller.Free (native);
142 		}
143 
144 		[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
gtk_text_buffer_serialize(IntPtr raw, IntPtr content_buffer, IntPtr format, ref Gtk.TextIter start, ref Gtk.TextIter end, out UIntPtr length)145 		static extern IntPtr gtk_text_buffer_serialize (IntPtr raw, IntPtr content_buffer, IntPtr format, ref Gtk.TextIter start, ref Gtk.TextIter end, out UIntPtr length);
146 
Serialize(Gtk.TextBuffer content_buffer, Gdk.Atom format, Gtk.TextIter start, Gtk.TextIter end)147 		public byte[] Serialize(Gtk.TextBuffer content_buffer, Gdk.Atom format, Gtk.TextIter start, Gtk.TextIter end)
148 		{
149 			UIntPtr length;
150 			IntPtr raw_ret = gtk_text_buffer_serialize (Handle, content_buffer == null ? IntPtr.Zero : content_buffer.Handle, format == null ? IntPtr.Zero : format.Handle, ref start, ref end, out length);
151 			if (raw_ret == IntPtr.Zero)
152 				return new byte [0];
153 			int sz = (int) (uint) length;
154 			byte[] ret = new byte [sz];
155 			Marshal.Copy (raw_ret, ret, 0, sz);
156 			return ret;
157 		}
158 
159 		[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
gtk_text_buffer_get_serialize_formats(IntPtr raw, out int n_formats)160 		static extern IntPtr gtk_text_buffer_get_serialize_formats(IntPtr raw, out int n_formats);
161 
162 		[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
gtk_text_buffer_get_deserialize_formats(IntPtr raw, out int n_formats)163 		static extern IntPtr gtk_text_buffer_get_deserialize_formats(IntPtr raw, out int n_formats);
164 
165 		public Gdk.Atom[] DeserializeFormats {
166 			get {
167 				int n_formats;
168 				IntPtr raw_ret = gtk_text_buffer_get_deserialize_formats(Handle, out n_formats);
169 				Gdk.Atom[] result = new Gdk.Atom [n_formats];
170 				for (int i = 0; i < n_formats; i++) {
171 					IntPtr format = Marshal.ReadIntPtr (raw_ret, i * IntPtr.Size);
172 					result [i] = format == IntPtr.Zero ? null : (Gdk.Atom) GLib.Opaque.GetOpaque (format, typeof (Gdk.Atom), false);
173 				}
174 				return result;
175 			}
176 		}
177 
178 		public Gdk.Atom[] SerializeFormats {
179 			get {
180 				int n_formats;
181 				IntPtr raw_ret = gtk_text_buffer_get_serialize_formats(Handle, out n_formats);
182 				Gdk.Atom[] result = new Gdk.Atom [n_formats];
183 				for (int i = 0; i < n_formats; i++) {
184 					IntPtr format = Marshal.ReadIntPtr (raw_ret, i * IntPtr.Size);
185 					result [i] = format == IntPtr.Zero ? null : (Gdk.Atom) GLib.Opaque.GetOpaque (format, typeof (Gdk.Atom), false);
186 				}
187 				return result;
188 			}
189 		}
190 	}
191 }
192