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