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.EntryCompletion;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import glib.c.functions;
30 private import gobject.ObjectG;
31 private import gobject.Signals;
32 private import gtk.BuildableIF;
33 private import gtk.BuildableT;
34 private import gtk.CellArea;
35 private import gtk.CellLayoutIF;
36 private import gtk.CellLayoutT;
37 private import gtk.TreeIter;
38 private import gtk.TreeModelIF;
39 private import gtk.Widget;
40 private import gtk.c.functions;
41 public  import gtk.c.types;
42 private import std.algorithm;
43 
44 
45 /**
46  * `GtkEntryCompletion` is an auxiliary object to provide completion functionality
47  * for `GtkEntry`.
48  * 
49  * It implements the [iface@Gtk.CellLayout] interface, to allow the user
50  * to add extra cells to the `GtkTreeView` with completion matches.
51  * 
52  * “Completion functionality” means that when the user modifies the text
53  * in the entry, `GtkEntryCompletion` checks which rows in the model match
54  * the current content of the entry, and displays a list of matches.
55  * By default, the matching is done by comparing the entry text
56  * case-insensitively against the text column of the model (see
57  * [method@Gtk.EntryCompletion.set_text_column]), but this can be overridden
58  * with a custom match function (see [method@Gtk.EntryCompletion.set_match_func]).
59  * 
60  * When the user selects a completion, the content of the entry is
61  * updated. By default, the content of the entry is replaced by the
62  * text column of the model, but this can be overridden by connecting
63  * to the [signal@Gtk.EntryCompletion::match-selected] signal and updating the
64  * entry in the signal handler. Note that you should return %TRUE from
65  * the signal handler to suppress the default behaviour.
66  * 
67  * To add completion functionality to an entry, use
68  * [method@Gtk.Entry.set_completion].
69  * 
70  * `GtkEntryCompletion` uses a [class@Gtk.TreeModelFilter] model to
71  * represent the subset of the entire model that is currently matching.
72  * While the `GtkEntryCompletion` signals
73  * [signal@Gtk.EntryCompletion::match-selected] and
74  * [signal@Gtk.EntryCompletion::cursor-on-match] take the original model
75  * and an iter pointing to that model as arguments, other callbacks and
76  * signals (such as `GtkCellLayoutDataFunc` or
77  * [signal@Gtk.CellArea::apply-attributes)]
78  * will generally take the filter model as argument. As long as you are
79  * only calling [method@Gtk.TreeModel.get], this will make no difference to
80  * you. If for some reason, you need the original model, use
81  * [method@Gtk.TreeModelFilter.get_model]. Don’t forget to use
82  * [method@Gtk.TreeModelFilter.convert_iter_to_child_iter] to obtain a
83  * matching iter.
84  */
85 public class EntryCompletion : ObjectG, BuildableIF, CellLayoutIF
86 {
87 	/** the main Gtk struct */
88 	protected GtkEntryCompletion* gtkEntryCompletion;
89 
90 	/** Get the main Gtk struct */
91 	public GtkEntryCompletion* getEntryCompletionStruct(bool transferOwnership = false)
92 	{
93 		if (transferOwnership)
94 			ownedRef = false;
95 		return gtkEntryCompletion;
96 	}
97 
98 	/** the main Gtk struct as a void* */
99 	protected override void* getStruct()
100 	{
101 		return cast(void*)gtkEntryCompletion;
102 	}
103 
104 	/**
105 	 * Sets our main struct and passes it to the parent class.
106 	 */
107 	public this (GtkEntryCompletion* gtkEntryCompletion, bool ownedRef = false)
108 	{
109 		this.gtkEntryCompletion = gtkEntryCompletion;
110 		super(cast(GObject*)gtkEntryCompletion, ownedRef);
111 	}
112 
113 	// add the Buildable capabilities
114 	mixin BuildableT!(GtkEntryCompletion);
115 
116 	// add the CellLayout capabilities
117 	mixin CellLayoutT!(GtkEntryCompletion);
118 
119 
120 	/** */
121 	public static GType getType()
122 	{
123 		return gtk_entry_completion_get_type();
124 	}
125 
126 	/**
127 	 * Creates a new `GtkEntryCompletion` object.
128 	 *
129 	 * Returns: A newly created `GtkEntryCompletion` object
130 	 *
131 	 * Throws: ConstructionException GTK+ fails to create the object.
132 	 */
133 	public this()
134 	{
135 		auto __p = gtk_entry_completion_new();
136 
137 		if(__p is null)
138 		{
139 			throw new ConstructionException("null returned by new");
140 		}
141 
142 		this(cast(GtkEntryCompletion*) __p, true);
143 	}
144 
145 	/**
146 	 * Creates a new `GtkEntryCompletion` object using the
147 	 * specified @area.
148 	 *
149 	 * The `GtkCellArea` is used to layout cells in the underlying
150 	 * `GtkTreeViewColumn` for the drop-down menu.
151 	 *
152 	 * Params:
153 	 *     area = the #GtkCellArea used to layout cells
154 	 *
155 	 * Returns: A newly created `GtkEntryCompletion` object
156 	 *
157 	 * Throws: ConstructionException GTK+ fails to create the object.
158 	 */
159 	public this(CellArea area)
160 	{
161 		auto __p = gtk_entry_completion_new_with_area((area is null) ? null : area.getCellAreaStruct());
162 
163 		if(__p is null)
164 		{
165 			throw new ConstructionException("null returned by new_with_area");
166 		}
167 
168 		this(cast(GtkEntryCompletion*) __p, true);
169 	}
170 
171 	/**
172 	 * Requests a completion operation, or in other words a refiltering of the
173 	 * current list with completions, using the current key.
174 	 *
175 	 * The completion list view will be updated accordingly.
176 	 */
177 	public void complete()
178 	{
179 		gtk_entry_completion_complete(gtkEntryCompletion);
180 	}
181 
182 	/**
183 	 * Computes the common prefix that is shared by all rows in @completion
184 	 * that start with @key.
185 	 *
186 	 * If no row matches @key, %NULL will be returned.
187 	 * Note that a text column must have been set for this function to work,
188 	 * see [method@Gtk.EntryCompletion.set_text_column] for details.
189 	 *
190 	 * Params:
191 	 *     key = The text to complete for
192 	 *
193 	 * Returns: The common prefix all rows
194 	 *     starting with @key or %NULL if no row matches @key.
195 	 */
196 	public string computePrefix(string key)
197 	{
198 		auto retStr = gtk_entry_completion_compute_prefix(gtkEntryCompletion, Str.toStringz(key));
199 
200 		scope(exit) Str.freeString(retStr);
201 		return Str.toString(retStr);
202 	}
203 
204 	/**
205 	 * Get the original text entered by the user that triggered
206 	 * the completion or %NULL if there’s no completion ongoing.
207 	 *
208 	 * Returns: the prefix for the current completion
209 	 */
210 	public string getCompletionPrefix()
211 	{
212 		return Str.toString(gtk_entry_completion_get_completion_prefix(gtkEntryCompletion));
213 	}
214 
215 	/**
216 	 * Gets the entry @completion has been attached to.
217 	 *
218 	 * Returns: The entry @completion has been attached to
219 	 */
220 	public Widget getEntry()
221 	{
222 		auto __p = gtk_entry_completion_get_entry(gtkEntryCompletion);
223 
224 		if(__p is null)
225 		{
226 			return null;
227 		}
228 
229 		return ObjectG.getDObject!(Widget)(cast(GtkWidget*) __p);
230 	}
231 
232 	/**
233 	 * Returns whether the common prefix of the possible completions should
234 	 * be automatically inserted in the entry.
235 	 *
236 	 * Returns: %TRUE if inline completion is turned on
237 	 */
238 	public bool getInlineCompletion()
239 	{
240 		return gtk_entry_completion_get_inline_completion(gtkEntryCompletion) != 0;
241 	}
242 
243 	/**
244 	 * Returns %TRUE if inline-selection mode is turned on.
245 	 *
246 	 * Returns: %TRUE if inline-selection mode is on
247 	 */
248 	public bool getInlineSelection()
249 	{
250 		return gtk_entry_completion_get_inline_selection(gtkEntryCompletion) != 0;
251 	}
252 
253 	/**
254 	 * Returns the minimum key length as set for @completion.
255 	 *
256 	 * Returns: The currently used minimum key length
257 	 */
258 	public int getMinimumKeyLength()
259 	{
260 		return gtk_entry_completion_get_minimum_key_length(gtkEntryCompletion);
261 	}
262 
263 	/**
264 	 * Returns the model the `GtkEntryCompletion` is using as data source.
265 	 *
266 	 * Returns %NULL if the model is unset.
267 	 *
268 	 * Returns: A #GtkTreeModel, or %NULL if none
269 	 *     is currently being used
270 	 */
271 	public TreeModelIF getModel()
272 	{
273 		auto __p = gtk_entry_completion_get_model(gtkEntryCompletion);
274 
275 		if(__p is null)
276 		{
277 			return null;
278 		}
279 
280 		return ObjectG.getDObject!(TreeModelIF)(cast(GtkTreeModel*) __p);
281 	}
282 
283 	/**
284 	 * Returns whether the completions should be presented in a popup window.
285 	 *
286 	 * Returns: %TRUE if popup completion is turned on
287 	 */
288 	public bool getPopupCompletion()
289 	{
290 		return gtk_entry_completion_get_popup_completion(gtkEntryCompletion) != 0;
291 	}
292 
293 	/**
294 	 * Returns whether the completion popup window will be resized to the
295 	 * width of the entry.
296 	 *
297 	 * Returns: %TRUE if the popup window will be resized to the width of
298 	 *     the entry
299 	 */
300 	public bool getPopupSetWidth()
301 	{
302 		return gtk_entry_completion_get_popup_set_width(gtkEntryCompletion) != 0;
303 	}
304 
305 	/**
306 	 * Returns whether the completion popup window will appear even if there is
307 	 * only a single match.
308 	 *
309 	 * Returns: %TRUE if the popup window will appear regardless of the
310 	 *     number of matches
311 	 */
312 	public bool getPopupSingleMatch()
313 	{
314 		return gtk_entry_completion_get_popup_single_match(gtkEntryCompletion) != 0;
315 	}
316 
317 	/**
318 	 * Returns the column in the model of @completion to get strings from.
319 	 *
320 	 * Returns: the column containing the strings
321 	 */
322 	public int getTextColumn()
323 	{
324 		return gtk_entry_completion_get_text_column(gtkEntryCompletion);
325 	}
326 
327 	/**
328 	 * Requests a prefix insertion.
329 	 */
330 	public void insertPrefix()
331 	{
332 		gtk_entry_completion_insert_prefix(gtkEntryCompletion);
333 	}
334 
335 	/**
336 	 * Sets whether the common prefix of the possible completions should
337 	 * be automatically inserted in the entry.
338 	 *
339 	 * Params:
340 	 *     inlineCompletion = %TRUE to do inline completion
341 	 */
342 	public void setInlineCompletion(bool inlineCompletion)
343 	{
344 		gtk_entry_completion_set_inline_completion(gtkEntryCompletion, inlineCompletion);
345 	}
346 
347 	/**
348 	 * Sets whether it is possible to cycle through the possible completions
349 	 * inside the entry.
350 	 *
351 	 * Params:
352 	 *     inlineSelection = %TRUE to do inline selection
353 	 */
354 	public void setInlineSelection(bool inlineSelection)
355 	{
356 		gtk_entry_completion_set_inline_selection(gtkEntryCompletion, inlineSelection);
357 	}
358 
359 	/**
360 	 * Sets the match function for @completion to be @func.
361 	 *
362 	 * The match function is used to determine if a row should or
363 	 * should not be in the completion list.
364 	 *
365 	 * Params:
366 	 *     func = the `GtkEntryCompletion`MatchFunc to use
367 	 *     funcData = user data for @func
368 	 *     funcNotify = destroy notify for @func_data.
369 	 */
370 	public void setMatchFunc(GtkEntryCompletionMatchFunc func, void* funcData, GDestroyNotify funcNotify)
371 	{
372 		gtk_entry_completion_set_match_func(gtkEntryCompletion, func, funcData, funcNotify);
373 	}
374 
375 	/**
376 	 * Requires the length of the search key for @completion to be at least
377 	 * @length.
378 	 *
379 	 * This is useful for long lists, where completing using a small
380 	 * key takes a lot of time and will come up with meaningless results anyway
381 	 * (ie, a too large dataset).
382 	 *
383 	 * Params:
384 	 *     length = the minimum length of the key in order to start completing
385 	 */
386 	public void setMinimumKeyLength(int length)
387 	{
388 		gtk_entry_completion_set_minimum_key_length(gtkEntryCompletion, length);
389 	}
390 
391 	/**
392 	 * Sets the model for a `GtkEntryCompletion`.
393 	 *
394 	 * If @completion already has a model set, it will remove it
395 	 * before setting the new model. If model is %NULL, then it
396 	 * will unset the model.
397 	 *
398 	 * Params:
399 	 *     model = the `GtkTreeModel`
400 	 */
401 	public void setModel(TreeModelIF model)
402 	{
403 		gtk_entry_completion_set_model(gtkEntryCompletion, (model is null) ? null : model.getTreeModelStruct());
404 	}
405 
406 	/**
407 	 * Sets whether the completions should be presented in a popup window.
408 	 *
409 	 * Params:
410 	 *     popupCompletion = %TRUE to do popup completion
411 	 */
412 	public void setPopupCompletion(bool popupCompletion)
413 	{
414 		gtk_entry_completion_set_popup_completion(gtkEntryCompletion, popupCompletion);
415 	}
416 
417 	/**
418 	 * Sets whether the completion popup window will be resized to be the same
419 	 * width as the entry.
420 	 *
421 	 * Params:
422 	 *     popupSetWidth = %TRUE to make the width of the popup the same as the entry
423 	 */
424 	public void setPopupSetWidth(bool popupSetWidth)
425 	{
426 		gtk_entry_completion_set_popup_set_width(gtkEntryCompletion, popupSetWidth);
427 	}
428 
429 	/**
430 	 * Sets whether the completion popup window will appear even if there is
431 	 * only a single match.
432 	 *
433 	 * You may want to set this to %FALSE if you
434 	 * are using [property@Gtk.EntryCompletion:inline-completion].
435 	 *
436 	 * Params:
437 	 *     popupSingleMatch = %TRUE if the popup should appear even for a single
438 	 *         match
439 	 */
440 	public void setPopupSingleMatch(bool popupSingleMatch)
441 	{
442 		gtk_entry_completion_set_popup_single_match(gtkEntryCompletion, popupSingleMatch);
443 	}
444 
445 	/**
446 	 * Convenience function for setting up the most used case of this code: a
447 	 * completion list with just strings.
448 	 *
449 	 * This function will set up @completion
450 	 * to have a list displaying all (and just) strings in the completion list,
451 	 * and to get those strings from @column in the model of @completion.
452 	 *
453 	 * This functions creates and adds a `GtkCellRendererText` for the selected
454 	 * column. If you need to set the text column, but don't want the cell
455 	 * renderer, use g_object_set() to set the
456 	 * [property@Gtk.EntryCompletion:text-column] property directly.
457 	 *
458 	 * Params:
459 	 *     column = the column in the model of @completion to get strings from
460 	 */
461 	public void setTextColumn(int column)
462 	{
463 		gtk_entry_completion_set_text_column(gtkEntryCompletion, column);
464 	}
465 
466 	/**
467 	 * Emitted when a match from the cursor is on a match of the list.
468 	 *
469 	 * The default behaviour is to replace the contents
470 	 * of the entry with the contents of the text column in the row
471 	 * pointed to by @iter.
472 	 *
473 	 * Note that @model is the model that was passed to
474 	 * [method@Gtk.EntryCompletion.set_model].
475 	 *
476 	 * Params:
477 	 *     model = the `GtkTreeModel` containing the matches
478 	 *     iter = a `GtkTreeIter` positioned at the selected match
479 	 *
480 	 * Returns: %TRUE if the signal has been handled
481 	 */
482 	gulong addOnCursorOnMatch(bool delegate(TreeModelIF, TreeIter, EntryCompletion) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
483 	{
484 		return Signals.connect(this, "cursor-on-match", dlg, connectFlags ^ ConnectFlags.SWAPPED);
485 	}
486 
487 	/**
488 	 * Emitted when the inline autocompletion is triggered.
489 	 *
490 	 * The default behaviour is to make the entry display the
491 	 * whole prefix and select the newly inserted part.
492 	 *
493 	 * Applications may connect to this signal in order to insert only a
494 	 * smaller part of the @prefix into the entry - e.g. the entry used in
495 	 * the #GtkFileChooser inserts only the part of the prefix up to the
496 	 * next '/'.
497 	 *
498 	 * Params:
499 	 *     prefix = the common prefix of all possible completions
500 	 *
501 	 * Returns: %TRUE if the signal has been handled
502 	 */
503 	gulong addOnInsertPrefix(bool delegate(string, EntryCompletion) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
504 	{
505 		return Signals.connect(this, "insert-prefix", dlg, connectFlags ^ ConnectFlags.SWAPPED);
506 	}
507 
508 	/**
509 	 * Emitted when a match from the list is selected.
510 	 *
511 	 * The default behaviour is to replace the contents of the
512 	 * entry with the contents of the text column in the row
513 	 * pointed to by @iter.
514 	 *
515 	 * Note that @model is the model that was passed to
516 	 * [method@Gtk.EntryCompletion.set_model].
517 	 *
518 	 * Params:
519 	 *     model = the `GtkTreeModel` containing the matches
520 	 *     iter = a `GtkTreeIter` positioned at the selected match
521 	 *
522 	 * Returns: %TRUE if the signal has been handled
523 	 */
524 	gulong addOnMatchSelected(bool delegate(TreeModelIF, TreeIter, EntryCompletion) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
525 	{
526 		return Signals.connect(this, "match-selected", dlg, connectFlags ^ ConnectFlags.SWAPPED);
527 	}
528 
529 	/**
530 	 * Emitted when the filter model has zero
531 	 * number of rows in completion_complete method.
532 	 *
533 	 * In other words when `GtkEntryCompletion` is out of suggestions.
534 	 */
535 	gulong addOnNoMatches(void delegate(EntryCompletion) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
536 	{
537 		return Signals.connect(this, "no-matches", dlg, connectFlags ^ ConnectFlags.SWAPPED);
538 	}
539 }