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.EditableT;
26 
27 public  import glib.Str;
28 public  import glib.c.functions;
29 public  import gobject.ObjectClass;
30 public  import gobject.ObjectG;
31 public  import gobject.ParamSpec;
32 public  import gobject.Signals;
33 public  import gobject.Value;
34 public  import gtk.EditableIF;
35 public  import gtk.c.functions;
36 public  import gtk.c.types;
37 public  import std.algorithm;
38 
39 
40 /**
41  * `GtkEditable` is an interface for text editing widgets.
42  * 
43  * Typical examples of editable widgets are [class@Gtk.Entry] and
44  * [class@Gtk.SpinButton]. It contains functions for generically manipulating
45  * an editable widget, a large number of action signals used for key bindings,
46  * and several signals that an application can connect to modify the behavior
47  * of a widget.
48  * 
49  * As an example of the latter usage, by connecting the following handler to
50  * [signal@Gtk.Editable::insert-text], an application can convert all entry
51  * into a widget into uppercase.
52  * 
53  * ## Forcing entry to uppercase.
54  * 
55  * ```c
56  * #include <ctype.h>
57  * 
58  * void
59  * insert_text_handler (GtkEditable *editable,
60  * const char  *text,
61  * int          length,
62  * int         *position,
63  * gpointer     data)
64  * {
65  * char *result = g_utf8_strup (text, length);
66  * 
67  * g_signal_handlers_block_by_func (editable,
68  * (gpointer) insert_text_handler, data);
69  * gtk_editable_insert_text (editable, result, length, position);
70  * g_signal_handlers_unblock_by_func (editable,
71  * (gpointer) insert_text_handler, data);
72  * 
73  * g_signal_stop_emission_by_name (editable, "insert_text");
74  * 
75  * g_free (result);
76  * }
77  * ```
78  * 
79  * ## Implementing GtkEditable
80  * 
81  * The most likely scenario for implementing `GtkEditable` on your own widget
82  * is that you will embed a #GtkText inside a complex widget, and want to
83  * delegate the editable functionality to that text widget. `GtkEditable`
84  * provides some utility functions to make this easy.
85  * 
86  * In your class_init function, call [func@Gtk.Editable.install_properties],
87  * passing the first available property ID:
88  * 
89  * ```c
90  * static void
91  * my_class_init (MyClass *class)
92  * {
93  * ...
94  * g_object_class_install_properties (object_class, NUM_PROPERTIES, props);
95  * gtk_editable_install_properties (object_clas, NUM_PROPERTIES);
96  * ...
97  * }
98  * ```
99  * 
100  * In your interface_init function for the `GtkEditable` interface, provide
101  * an implementation for the get_delegate vfunc that returns your text widget:
102  * 
103  * ```c
104  * GtkEditable *
105  * get_editable_delegate (GtkEditable *editable)
106  * {
107  * return GTK_EDITABLE (MY_WIDGET (editable)->text_widget);
108  * }
109  * 
110  * static void
111  * my_editable_init (GtkEditableInterface *iface)
112  * {
113  * iface->get_delegate = get_editable_delegate;
114  * }
115  * ```
116  * 
117  * You don't need to provide any other vfuncs. The default implementations
118  * work by forwarding to the delegate that the GtkEditableInterface.get_delegate()
119  * vfunc returns.
120  * 
121  * In your instance_init function, create your text widget, and then call
122  * [method@Gtk.Editable.init_delegate]:
123  * 
124  * ```c
125  * static void
126  * my_widget_init (MyWidget *self)
127  * {
128  * ...
129  * self->text_widget = gtk_text_new ();
130  * gtk_editable_init_delegate (GTK_EDITABLE (self));
131  * ...
132  * }
133  * ```
134  * 
135  * In your dispose function, call [method@Gtk.Editable.finish_delegate] before
136  * destroying your text widget:
137  * 
138  * ```c
139  * static void
140  * my_widget_dispose (GObject *object)
141  * {
142  * ...
143  * gtk_editable_finish_delegate (GTK_EDITABLE (self));
144  * g_clear_pointer (&self->text_widget, gtk_widget_unparent);
145  * ...
146  * }
147  * ```
148  * 
149  * Finally, use [func@Gtk.Editable.delegate_set_property] in your `set_property`
150  * function (and similar for `get_property`), to set the editable properties:
151  * 
152  * ```c
153  * ...
154  * if (gtk_editable_delegate_set_property (object, prop_id, value, pspec))
155  * return;
156  * 
157  * switch (prop_id)
158  * ...
159  * ```
160  * 
161  * It is important to note that if you create a `GtkEditable` that uses
162  * a delegate, the low level [signal@Gtk.Editable::insert-text] and
163  * [signal@Gtk.Editable::delete-text] signals will be propagated from the
164  * "wrapper" editable to the delegate, but they will not be propagated from
165  * the delegate to the "wrapper" editable, as they would cause an infinite
166  * recursion. If you wish to connect to the [signal@Gtk.Editable::insert-text]
167  * and [signal@Gtk.Editable::delete-text] signals, you will need to connect
168  * to them on the delegate obtained via [method@Gtk.Editable.get_delegate].
169  */
170 public template EditableT(TStruct)
171 {
172 	/** Get the main Gtk struct */
173 	public GtkEditable* getEditableStruct(bool transferOwnership = false)
174 	{
175 		if (transferOwnership)
176 			ownedRef = false;
177 		return cast(GtkEditable*)getStruct();
178 	}
179 
180 
181 	/**
182 	 * Deletes the currently selected text of the editable.
183 	 *
184 	 * This call doesn’t do anything if there is no selected text.
185 	 */
186 	public void deleteSelection()
187 	{
188 		gtk_editable_delete_selection(getEditableStruct());
189 	}
190 
191 	/**
192 	 * Deletes a sequence of characters.
193 	 *
194 	 * The characters that are deleted are those characters at positions
195 	 * from @start_pos up to, but not including @end_pos. If @end_pos is
196 	 * negative, then the characters deleted are those from @start_pos to
197 	 * the end of the text.
198 	 *
199 	 * Note that the positions are specified in characters, not bytes.
200 	 *
201 	 * Params:
202 	 *     startPos = start position
203 	 *     endPos = end position
204 	 */
205 	public void deleteText(int startPos, int endPos)
206 	{
207 		gtk_editable_delete_text(getEditableStruct(), startPos, endPos);
208 	}
209 
210 	/**
211 	 * Undoes the setup done by [method@Gtk.Editable.init_delegate].
212 	 *
213 	 * This is a helper function that should be called from dispose,
214 	 * before removing the delegate object.
215 	 */
216 	public void finishDelegate()
217 	{
218 		gtk_editable_finish_delegate(getEditableStruct());
219 	}
220 
221 	/**
222 	 * Gets the alignment of the editable.
223 	 *
224 	 * Returns: the alignment
225 	 */
226 	public float getAlignment()
227 	{
228 		return gtk_editable_get_alignment(getEditableStruct());
229 	}
230 
231 	/**
232 	 * Retrieves a sequence of characters.
233 	 *
234 	 * The characters that are retrieved are those characters at positions
235 	 * from @start_pos up to, but not including @end_pos. If @end_pos is negative,
236 	 * then the characters retrieved are those characters from @start_pos to
237 	 * the end of the text.
238 	 *
239 	 * Note that positions are specified in characters, not bytes.
240 	 *
241 	 * Params:
242 	 *     startPos = start of text
243 	 *     endPos = end of text
244 	 *
245 	 * Returns: a pointer to the contents of the widget as a
246 	 *     string. This string is allocated by the `GtkEditable` implementation
247 	 *     and should be freed by the caller.
248 	 */
249 	public string getChars(int startPos, int endPos)
250 	{
251 		auto retStr = gtk_editable_get_chars(getEditableStruct(), startPos, endPos);
252 
253 		scope(exit) Str.freeString(retStr);
254 		return Str.toString(retStr);
255 	}
256 
257 	/**
258 	 * Gets the `GtkEditable` that @editable is delegating its
259 	 * implementation to.
260 	 *
261 	 * Typically, the delegate is a [class@Gtk.Text] widget.
262 	 *
263 	 * Returns: the delegate `GtkEditable`
264 	 */
265 	public EditableIF getDelegate()
266 	{
267 		auto __p = gtk_editable_get_delegate(getEditableStruct());
268 
269 		if(__p is null)
270 		{
271 			return null;
272 		}
273 
274 		return ObjectG.getDObject!(EditableIF)(cast(GtkEditable*) __p);
275 	}
276 
277 	/**
278 	 * Retrieves whether @editable is editable.
279 	 *
280 	 * Returns: %TRUE if @editable is editable.
281 	 */
282 	public bool getEditable()
283 	{
284 		return gtk_editable_get_editable(getEditableStruct()) != 0;
285 	}
286 
287 	/**
288 	 * Gets if undo/redo actions are enabled for @editable
289 	 *
290 	 * Returns: %TRUE if undo is enabled
291 	 */
292 	public bool getEnableUndo()
293 	{
294 		return gtk_editable_get_enable_undo(getEditableStruct()) != 0;
295 	}
296 
297 	/**
298 	 * Retrieves the desired maximum width of @editable, in characters.
299 	 *
300 	 * Returns: the maximum width of the entry, in characters
301 	 */
302 	public int getMaxWidthChars()
303 	{
304 		return gtk_editable_get_max_width_chars(getEditableStruct());
305 	}
306 
307 	/**
308 	 * Retrieves the current position of the cursor relative
309 	 * to the start of the content of the editable.
310 	 *
311 	 * Note that this position is in characters, not in bytes.
312 	 *
313 	 * Returns: the cursor position
314 	 */
315 	public int getPosition()
316 	{
317 		return gtk_editable_get_position(getEditableStruct());
318 	}
319 
320 	/**
321 	 * Retrieves the selection bound of the editable.
322 	 *
323 	 * @start_pos will be filled with the start of the selection and
324 	 * @end_pos with end. If no text was selected both will be identical
325 	 * and %FALSE will be returned.
326 	 *
327 	 * Note that positions are specified in characters, not bytes.
328 	 *
329 	 * Params:
330 	 *     startPos = location to store the starting position, or %NULL
331 	 *     endPos = location to store the end position, or %NULL
332 	 *
333 	 * Returns: %TRUE if there is a non-empty selection, %FALSE otherwise
334 	 */
335 	public bool getSelectionBounds(out int startPos, out int endPos)
336 	{
337 		return gtk_editable_get_selection_bounds(getEditableStruct(), &startPos, &endPos) != 0;
338 	}
339 
340 	/**
341 	 * Retrieves the contents of @editable.
342 	 *
343 	 * The returned string is owned by GTK and must not be modified or freed.
344 	 *
345 	 * Returns: a pointer to the contents of the editable
346 	 */
347 	public string getText()
348 	{
349 		return Str.toString(gtk_editable_get_text(getEditableStruct()));
350 	}
351 
352 	/**
353 	 * Gets the number of characters of space reserved
354 	 * for the contents of the editable.
355 	 *
356 	 * Returns: number of chars to request space for, or negative if unset
357 	 */
358 	public int getWidthChars()
359 	{
360 		return gtk_editable_get_width_chars(getEditableStruct());
361 	}
362 
363 	/**
364 	 * Sets up a delegate for `GtkEditable`.
365 	 *
366 	 * This is assuming that the get_delegate vfunc in the `GtkEditable`
367 	 * interface has been set up for the @editable's type.
368 	 *
369 	 * This is a helper function that should be called in instance init,
370 	 * after creating the delegate object.
371 	 */
372 	public void initDelegate()
373 	{
374 		gtk_editable_init_delegate(getEditableStruct());
375 	}
376 
377 	/**
378 	 * Inserts @length bytes of @text into the contents of the
379 	 * widget, at position @position.
380 	 *
381 	 * Note that the position is in characters, not in bytes.
382 	 * The function updates @position to point after the newly
383 	 * inserted text.
384 	 *
385 	 * Params:
386 	 *     text = the text to append
387 	 *     length = the length of the text in bytes, or -1
388 	 *     position = location of the position text will be inserted at
389 	 */
390 	public void insertText(string text, int length, ref int position)
391 	{
392 		gtk_editable_insert_text(getEditableStruct(), Str.toStringz(text), length, &position);
393 	}
394 
395 	/**
396 	 * Selects a region of text.
397 	 *
398 	 * The characters that are selected are those characters at positions
399 	 * from @start_pos up to, but not including @end_pos. If @end_pos is
400 	 * negative, then the characters selected are those characters from
401 	 * @start_pos to  the end of the text.
402 	 *
403 	 * Note that positions are specified in characters, not bytes.
404 	 *
405 	 * Params:
406 	 *     startPos = start of region
407 	 *     endPos = end of region
408 	 */
409 	public void selectRegion(int startPos, int endPos)
410 	{
411 		gtk_editable_select_region(getEditableStruct(), startPos, endPos);
412 	}
413 
414 	/**
415 	 * Sets the alignment for the contents of the editable.
416 	 *
417 	 * This controls the horizontal positioning of the contents when
418 	 * the displayed text is shorter than the width of the editable.
419 	 *
420 	 * Params:
421 	 *     xalign = The horizontal alignment, from 0 (left) to 1 (right).
422 	 *         Reversed for RTL layouts
423 	 */
424 	public void setAlignment(float xalign)
425 	{
426 		gtk_editable_set_alignment(getEditableStruct(), xalign);
427 	}
428 
429 	/**
430 	 * Determines if the user can edit the text in the editable widget.
431 	 *
432 	 * Params:
433 	 *     isEditable = %TRUE if the user is allowed to edit the text
434 	 *         in the widget
435 	 */
436 	public void setEditable(bool isEditable)
437 	{
438 		gtk_editable_set_editable(getEditableStruct(), isEditable);
439 	}
440 
441 	/**
442 	 * If enabled, changes to @editable will be saved for undo/redo
443 	 * actions.
444 	 *
445 	 * This results in an additional copy of text changes and are not
446 	 * stored in secure memory. As such, undo is forcefully disabled
447 	 * when [property@Gtk.Text:visibility] is set to %FALSE.
448 	 *
449 	 * Params:
450 	 *     enableUndo = if undo/redo should be enabled
451 	 */
452 	public void setEnableUndo(bool enableUndo)
453 	{
454 		gtk_editable_set_enable_undo(getEditableStruct(), enableUndo);
455 	}
456 
457 	/**
458 	 * Sets the desired maximum width in characters of @editable.
459 	 *
460 	 * Params:
461 	 *     nChars = the new desired maximum width, in characters
462 	 */
463 	public void setMaxWidthChars(int nChars)
464 	{
465 		gtk_editable_set_max_width_chars(getEditableStruct(), nChars);
466 	}
467 
468 	/**
469 	 * Sets the cursor position in the editable to the given value.
470 	 *
471 	 * The cursor is displayed before the character with the given (base 0)
472 	 * index in the contents of the editable. The value must be less than
473 	 * or equal to the number of characters in the editable. A value of -1
474 	 * indicates that the position should be set after the last character
475 	 * of the editable. Note that @position is in characters, not in bytes.
476 	 *
477 	 * Params:
478 	 *     position = the position of the cursor
479 	 */
480 	public void setPosition(int position)
481 	{
482 		gtk_editable_set_position(getEditableStruct(), position);
483 	}
484 
485 	/**
486 	 * Sets the text in the editable to the given value.
487 	 *
488 	 * This is replacing the current contents.
489 	 *
490 	 * Params:
491 	 *     text = the text to set
492 	 */
493 	public void setText(string text)
494 	{
495 		gtk_editable_set_text(getEditableStruct(), Str.toStringz(text));
496 	}
497 
498 	/**
499 	 * Changes the size request of the editable to be about the
500 	 * right size for @n_chars characters.
501 	 *
502 	 * Note that it changes the size request, the size can still
503 	 * be affected by how you pack the widget into containers.
504 	 * If @n_chars is -1, the size reverts to the default size.
505 	 *
506 	 * Params:
507 	 *     nChars = width in chars
508 	 */
509 	public void setWidthChars(int nChars)
510 	{
511 		gtk_editable_set_width_chars(getEditableStruct(), nChars);
512 	}
513 
514 	/**
515 	 * Emitted at the end of a single user-visible operation on the
516 	 * contents.
517 	 *
518 	 * E.g., a paste operation that replaces the contents of the
519 	 * selection will cause only one signal emission (even though it
520 	 * is implemented by first deleting the selection, then inserting
521 	 * the new content, and may cause multiple ::notify::text signals
522 	 * to be emitted).
523 	 */
524 	gulong addOnChanged(void delegate(EditableIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
525 	{
526 		return Signals.connect(this, "changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
527 	}
528 
529 	/**
530 	 * Emitted when text is deleted from the widget by the user.
531 	 *
532 	 * The default handler for this signal will normally be responsible for
533 	 * deleting the text, so by connecting to this signal and then stopping
534 	 * the signal with g_signal_stop_emission(), it is possible to modify the
535 	 * range of deleted text, or prevent it from being deleted entirely.
536 	 *
537 	 * The @start_pos and @end_pos parameters are interpreted as for
538 	 * [method@Gtk.Editable.delete_text].
539 	 *
540 	 * Params:
541 	 *     startPos = the starting position
542 	 *     endPos = the end position
543 	 */
544 	gulong addOnDeleteText(void delegate(int, int, EditableIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
545 	{
546 		return Signals.connect(this, "delete-text", dlg, connectFlags ^ ConnectFlags.SWAPPED);
547 	}
548 
549 	/**
550 	 * Emitted when text is inserted into the widget by the user.
551 	 *
552 	 * The default handler for this signal will normally be responsible
553 	 * for inserting the text, so by connecting to this signal and then
554 	 * stopping the signal with g_signal_stop_emission(), it is possible
555 	 * to modify the inserted text, or prevent it from being inserted entirely.
556 	 *
557 	 * Params:
558 	 *     text = the new text to insert
559 	 *     length = the length of the new text, in bytes,
560 	 *         or -1 if new_text is nul-terminated
561 	 *     position = the position, in characters,
562 	 *         at which to insert the new text. this is an in-out
563 	 *         parameter.  After the signal emission is finished, it
564 	 *         should point after the newly inserted text.
565 	 */
566 	gulong addOnInsertText(void delegate(string, int, void*, EditableIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
567 	{
568 		return Signals.connect(this, "insert-text", dlg, connectFlags ^ ConnectFlags.SWAPPED);
569 	}
570 }