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