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