1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.EntryBuffer;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gobject.Signals;
31 private import gtkc.gtk;
32 public  import gtkc.gtktypes;
33 private import std.algorithm;
34 
35 
36 /**
37  * The #GtkEntryBuffer class contains the actual text displayed in a
38  * #GtkEntry widget.
39  * 
40  * A single #GtkEntryBuffer object can be shared by multiple #GtkEntry
41  * widgets which will then share the same text content, but not the cursor
42  * position, visibility attributes, icon etc.
43  * 
44  * #GtkEntryBuffer may be derived from. Such a derived class might allow
45  * text to be stored in an alternate location, such as non-pageable memory,
46  * useful in the case of important passwords. Or a derived class could
47  * integrate with an application’s concept of undo/redo.
48  */
49 public class EntryBuffer : ObjectG
50 {
51 	/** the main Gtk struct */
52 	protected GtkEntryBuffer* gtkEntryBuffer;
53 
54 	/** Get the main Gtk struct */
55 	public GtkEntryBuffer* getEntryBufferStruct()
56 	{
57 		return gtkEntryBuffer;
58 	}
59 
60 	/** the main Gtk struct as a void* */
61 	protected override void* getStruct()
62 	{
63 		return cast(void*)gtkEntryBuffer;
64 	}
65 
66 	protected override void setStruct(GObject* obj)
67 	{
68 		gtkEntryBuffer = cast(GtkEntryBuffer*)obj;
69 		super.setStruct(obj);
70 	}
71 
72 	/**
73 	 * Sets our main struct and passes it to the parent class.
74 	 */
75 	public this (GtkEntryBuffer* gtkEntryBuffer, bool ownedRef = false)
76 	{
77 		this.gtkEntryBuffer = gtkEntryBuffer;
78 		super(cast(GObject*)gtkEntryBuffer, ownedRef);
79 	}
80 
81 
82 	/** */
83 	public static GType getType()
84 	{
85 		return gtk_entry_buffer_get_type();
86 	}
87 
88 	/**
89 	 * Create a new GtkEntryBuffer object.
90 	 *
91 	 * Optionally, specify initial text to set in the buffer.
92 	 *
93 	 * Params:
94 	 *     initialChars = initial buffer text, or %NULL
95 	 *     nInitialChars = number of characters in @initial_chars, or -1
96 	 *
97 	 * Returns: A new GtkEntryBuffer object.
98 	 *
99 	 * Since: 2.18
100 	 *
101 	 * Throws: ConstructionException GTK+ fails to create the object.
102 	 */
103 	public this(string initialChars, int nInitialChars)
104 	{
105 		auto p = gtk_entry_buffer_new(Str.toStringz(initialChars), nInitialChars);
106 		
107 		if(p is null)
108 		{
109 			throw new ConstructionException("null returned by new");
110 		}
111 		
112 		this(cast(GtkEntryBuffer*) p, true);
113 	}
114 
115 	/**
116 	 * Deletes a sequence of characters from the buffer. @n_chars characters are
117 	 * deleted starting at @position. If @n_chars is negative, then all characters
118 	 * until the end of the text are deleted.
119 	 *
120 	 * If @position or @n_chars are out of bounds, then they are coerced to sane
121 	 * values.
122 	 *
123 	 * Note that the positions are specified in characters, not bytes.
124 	 *
125 	 * Params:
126 	 *     position = position at which to delete text
127 	 *     nChars = number of characters to delete
128 	 *
129 	 * Returns: The number of characters deleted.
130 	 *
131 	 * Since: 2.18
132 	 */
133 	public uint deleteText(uint position, int nChars)
134 	{
135 		return gtk_entry_buffer_delete_text(gtkEntryBuffer, position, nChars);
136 	}
137 
138 	/**
139 	 * Used when subclassing #GtkEntryBuffer
140 	 *
141 	 * Params:
142 	 *     position = position at which text was deleted
143 	 *     nChars = number of characters deleted
144 	 *
145 	 * Since: 2.18
146 	 */
147 	public void emitDeletedText(uint position, uint nChars)
148 	{
149 		gtk_entry_buffer_emit_deleted_text(gtkEntryBuffer, position, nChars);
150 	}
151 
152 	/**
153 	 * Used when subclassing #GtkEntryBuffer
154 	 *
155 	 * Params:
156 	 *     position = position at which text was inserted
157 	 *     chars = text that was inserted
158 	 *     nChars = number of characters inserted
159 	 *
160 	 * Since: 2.18
161 	 */
162 	public void emitInsertedText(uint position, string chars, uint nChars)
163 	{
164 		gtk_entry_buffer_emit_inserted_text(gtkEntryBuffer, position, Str.toStringz(chars), nChars);
165 	}
166 
167 	/**
168 	 * Retrieves the length in bytes of the buffer.
169 	 * See gtk_entry_buffer_get_length().
170 	 *
171 	 * Returns: The byte length of the buffer.
172 	 *
173 	 * Since: 2.18
174 	 */
175 	public size_t getBytes()
176 	{
177 		return gtk_entry_buffer_get_bytes(gtkEntryBuffer);
178 	}
179 
180 	/**
181 	 * Retrieves the length in characters of the buffer.
182 	 *
183 	 * Returns: The number of characters in the buffer.
184 	 *
185 	 * Since: 2.18
186 	 */
187 	public uint getLength()
188 	{
189 		return gtk_entry_buffer_get_length(gtkEntryBuffer);
190 	}
191 
192 	/**
193 	 * Retrieves the maximum allowed length of the text in
194 	 * @buffer. See gtk_entry_buffer_set_max_length().
195 	 *
196 	 * Returns: the maximum allowed number of characters
197 	 *     in #GtkEntryBuffer, or 0 if there is no maximum.
198 	 *
199 	 * Since: 2.18
200 	 */
201 	public int getMaxLength()
202 	{
203 		return gtk_entry_buffer_get_max_length(gtkEntryBuffer);
204 	}
205 
206 	/**
207 	 * Retrieves the contents of the buffer.
208 	 *
209 	 * The memory pointer returned by this call will not change
210 	 * unless this object emits a signal, or is finalized.
211 	 *
212 	 * Returns: a pointer to the contents of the widget as a
213 	 *     string. This string points to internally allocated
214 	 *     storage in the buffer and must not be freed, modified or
215 	 *     stored.
216 	 *
217 	 * Since: 2.18
218 	 */
219 	public string getText()
220 	{
221 		return Str.toString(gtk_entry_buffer_get_text(gtkEntryBuffer));
222 	}
223 
224 	/**
225 	 * Inserts @n_chars characters of @chars into the contents of the
226 	 * buffer, at position @position.
227 	 *
228 	 * If @n_chars is negative, then characters from chars will be inserted
229 	 * until a null-terminator is found. If @position or @n_chars are out of
230 	 * bounds, or the maximum buffer text length is exceeded, then they are
231 	 * coerced to sane values.
232 	 *
233 	 * Note that the position and length are in characters, not in bytes.
234 	 *
235 	 * Params:
236 	 *     position = the position at which to insert text.
237 	 *     chars = the text to insert into the buffer.
238 	 *     nChars = the length of the text in characters, or -1
239 	 *
240 	 * Returns: The number of characters actually inserted.
241 	 *
242 	 * Since: 2.18
243 	 */
244 	public uint insertText(uint position, string chars, int nChars)
245 	{
246 		return gtk_entry_buffer_insert_text(gtkEntryBuffer, position, Str.toStringz(chars), nChars);
247 	}
248 
249 	/**
250 	 * Sets the maximum allowed length of the contents of the buffer. If
251 	 * the current contents are longer than the given length, then they
252 	 * will be truncated to fit.
253 	 *
254 	 * Params:
255 	 *     maxLength = the maximum length of the entry buffer, or 0 for no maximum.
256 	 *         (other than the maximum length of entries.) The value passed in will
257 	 *         be clamped to the range 0-65536.
258 	 *
259 	 * Since: 2.18
260 	 */
261 	public void setMaxLength(int maxLength)
262 	{
263 		gtk_entry_buffer_set_max_length(gtkEntryBuffer, maxLength);
264 	}
265 
266 	/**
267 	 * Sets the text in the buffer.
268 	 *
269 	 * This is roughly equivalent to calling gtk_entry_buffer_delete_text()
270 	 * and gtk_entry_buffer_insert_text().
271 	 *
272 	 * Note that @n_chars is in characters, not in bytes.
273 	 *
274 	 * Params:
275 	 *     chars = the new text
276 	 *     nChars = the number of characters in @text, or -1
277 	 *
278 	 * Since: 2.18
279 	 */
280 	public void setText(string chars, int nChars)
281 	{
282 		gtk_entry_buffer_set_text(gtkEntryBuffer, Str.toStringz(chars), nChars);
283 	}
284 
285 	protected class OnDeletedTextDelegateWrapper
286 	{
287 		static OnDeletedTextDelegateWrapper[] listeners;
288 		void delegate(uint, uint, EntryBuffer) dlg;
289 		gulong handlerId;
290 		
291 		this(void delegate(uint, uint, EntryBuffer) dlg)
292 		{
293 			this.dlg = dlg;
294 			this.listeners ~= this;
295 		}
296 		
297 		void remove(OnDeletedTextDelegateWrapper source)
298 		{
299 			foreach(index, wrapper; listeners)
300 			{
301 				if (wrapper.handlerId == source.handlerId)
302 				{
303 					listeners[index] = null;
304 					listeners = std.algorithm.remove(listeners, index);
305 					break;
306 				}
307 			}
308 		}
309 	}
310 
311 	/**
312 	 * This signal is emitted after text is deleted from the buffer.
313 	 *
314 	 * Params:
315 	 *     position = the position the text was deleted at.
316 	 *     nChars = The number of characters that were deleted.
317 	 *
318 	 * Since: 2.18
319 	 */
320 	gulong addOnDeletedText(void delegate(uint, uint, EntryBuffer) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
321 	{
322 		auto wrapper = new OnDeletedTextDelegateWrapper(dlg);
323 		wrapper.handlerId = Signals.connectData(
324 			this,
325 			"deleted-text",
326 			cast(GCallback)&callBackDeletedText,
327 			cast(void*)wrapper,
328 			cast(GClosureNotify)&callBackDeletedTextDestroy,
329 			connectFlags);
330 		return wrapper.handlerId;
331 	}
332 	
333 	extern(C) static void callBackDeletedText(GtkEntryBuffer* entrybufferStruct, uint position, uint nChars, OnDeletedTextDelegateWrapper wrapper)
334 	{
335 		wrapper.dlg(position, nChars, wrapper.outer);
336 	}
337 	
338 	extern(C) static void callBackDeletedTextDestroy(OnDeletedTextDelegateWrapper wrapper, GClosure* closure)
339 	{
340 		wrapper.remove(wrapper);
341 	}
342 
343 	protected class OnInsertedTextDelegateWrapper
344 	{
345 		static OnInsertedTextDelegateWrapper[] listeners;
346 		void delegate(uint, string, uint, EntryBuffer) dlg;
347 		gulong handlerId;
348 		
349 		this(void delegate(uint, string, uint, EntryBuffer) dlg)
350 		{
351 			this.dlg = dlg;
352 			this.listeners ~= this;
353 		}
354 		
355 		void remove(OnInsertedTextDelegateWrapper source)
356 		{
357 			foreach(index, wrapper; listeners)
358 			{
359 				if (wrapper.handlerId == source.handlerId)
360 				{
361 					listeners[index] = null;
362 					listeners = std.algorithm.remove(listeners, index);
363 					break;
364 				}
365 			}
366 		}
367 	}
368 
369 	/**
370 	 * This signal is emitted after text is inserted into the buffer.
371 	 *
372 	 * Params:
373 	 *     position = the position the text was inserted at.
374 	 *     chars = The text that was inserted.
375 	 *     nChars = The number of characters that were inserted.
376 	 *
377 	 * Since: 2.18
378 	 */
379 	gulong addOnInsertedText(void delegate(uint, string, uint, EntryBuffer) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
380 	{
381 		auto wrapper = new OnInsertedTextDelegateWrapper(dlg);
382 		wrapper.handlerId = Signals.connectData(
383 			this,
384 			"inserted-text",
385 			cast(GCallback)&callBackInsertedText,
386 			cast(void*)wrapper,
387 			cast(GClosureNotify)&callBackInsertedTextDestroy,
388 			connectFlags);
389 		return wrapper.handlerId;
390 	}
391 	
392 	extern(C) static void callBackInsertedText(GtkEntryBuffer* entrybufferStruct, uint position, char* chars, uint nChars, OnInsertedTextDelegateWrapper wrapper)
393 	{
394 		wrapper.dlg(position, Str.toString(chars), nChars, wrapper.outer);
395 	}
396 	
397 	extern(C) static void callBackInsertedTextDestroy(OnInsertedTextDelegateWrapper wrapper, GClosure* closure)
398 	{
399 		wrapper.remove(wrapper);
400 	}
401 }