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.ComboBoxText;
26 
27 public  import gdk.c.types;
28 private import glib.ConstructionException;
29 private import glib.Str;
30 private import gobject.Signals;
31 private import gtk.ComboBox;
32 private import gtk.TreeIter;
33 private import gtk.TreeModelIF;
34 private import gtk.c.functions;
35 public  import gtk.c.types;
36 public  import gtkc.gtktypes;
37 private import std.algorithm;
38 
39 
40 /**
41  * A GtkComboBoxText is a simple variant of #GtkComboBox that hides
42  * the model-view complexity for simple text-only use cases.
43  * 
44  * To create a GtkComboBoxText, use gtk_combo_box_text_new() or
45  * gtk_combo_box_text_new_with_entry().
46  * 
47  * You can add items to a GtkComboBoxText with
48  * gtk_combo_box_text_append_text(), gtk_combo_box_text_insert_text()
49  * or gtk_combo_box_text_prepend_text() and remove options with
50  * gtk_combo_box_text_remove().
51  * 
52  * If the GtkComboBoxText contains an entry (via the “has-entry” property),
53  * its contents can be retrieved using gtk_combo_box_text_get_active_text().
54  * The entry itself can be accessed by calling gtk_bin_get_child() on the
55  * combo box.
56  * 
57  * You should not call gtk_combo_box_set_model() or attempt to pack more cells
58  * into this combo box via its GtkCellLayout interface.
59  * 
60  * # GtkComboBoxText as GtkBuildable
61  * 
62  * The GtkComboBoxText implementation of the GtkBuildable interface supports
63  * adding items directly using the <items> element and specifying <item>
64  * elements for each item. Each <item> element can specify the “id”
65  * corresponding to the appended text and also supports the regular
66  * translation attributes “translatable”, “context” and “comments”.
67  * 
68  * Here is a UI definition fragment specifying GtkComboBoxText items:
69  * |[
70  * <object class="GtkComboBoxText">
71  * <items>
72  * <item translatable="yes" id="factory">Factory</item>
73  * <item translatable="yes" id="home">Home</item>
74  * <item translatable="yes" id="subway">Subway</item>
75  * </items>
76  * </object>
77  * ]|
78  * 
79  * # CSS nodes
80  * 
81  * |[<!-- language="plain" -->
82  * combobox
83  * ╰── box.linked
84  * ├── entry.combo
85  * ├── button.combo
86  * ╰── window.popup
87  * ]|
88  * 
89  * GtkComboBoxText has a single CSS node with name combobox. It adds
90  * the style class .combo to the main CSS nodes of its entry and button
91  * children, and the .linked class to the node of its internal box.
92  */
93 public class ComboBoxText : ComboBox
94 {
95 	/** the main Gtk struct */
96 	protected GtkComboBoxText* gtkComboBoxText;
97 
98 	/** Get the main Gtk struct */
99 	public GtkComboBoxText* getComboBoxTextStruct(bool transferOwnership = false)
100 	{
101 		if (transferOwnership)
102 			ownedRef = false;
103 		return gtkComboBoxText;
104 	}
105 
106 	/** the main Gtk struct as a void* */
107 	protected override void* getStruct()
108 	{
109 		return cast(void*)gtkComboBoxText;
110 	}
111 
112 	/**
113 	 * Sets our main struct and passes it to the parent class.
114 	 */
115 	public this (GtkComboBoxText* gtkComboBoxText, bool ownedRef = false)
116 	{
117 		this.gtkComboBoxText = gtkComboBoxText;
118 		super(cast(GtkComboBox*)gtkComboBoxText, ownedRef);
119 	}
120 
121 	/**
122 	 * Creates a new ComboBoxText, which is a ComboBox just displaying strings.
123 	 * Params:
124 	 *   entry = If true, create an ComboBox with an entry.
125 	 * Throws: ConstructionException GTK+ fails to create the object.
126 	 */
127 	public this (bool entry=true)
128 	{
129 		GtkComboBoxText* p;
130 		if ( entry )
131 		{
132 			// GtkWidget* gtk_combo_box_text_new_with_entry (void);
133 			p = cast(GtkComboBoxText*)gtk_combo_box_text_new_with_entry();
134 		}
135 		else
136 		{
137 			// GtkWidget* gtk_combo_box_text_new (void);
138 			p = cast(GtkComboBoxText*)gtk_combo_box_text_new();
139 		}
140 
141 		if(p is null)
142 		{
143 			throw new ConstructionException("null returned by gtk_combo_box_new");
144 		}
145 
146 		this(p);
147 	}
148 
149 	/** */
150 	public void setActiveText(string text, bool insert=false)
151 	{
152 		int active = 0;
153 		setActive(0);
154 		while ( getActive() >= 0 ) // returns -1 if end of list if reached
155 		{
156 			if( text == getActiveText() ) return;
157 			++active;
158 			setActive(active);
159 		}
160 		// was not found, the combo has now nothing selected
161 		if ( insert )
162 		{
163 			append("", text);
164 			setActive(active);
165 		}
166 	}
167 
168 	/** */
169 	int getIndex(string text)
170 	{
171 		TreeIter iter;
172 		TreeModelIF model = getModel();
173 		int index = 0;
174 		bool found = false;
175 		bool end = false;
176 		if ( model.getIterFirst(iter) )
177 		{
178 			iter.setModel(model);
179 			while ( !end && iter !is  null && !found )
180 			{
181 				found = iter.getValueString(0) == text;
182 				if ( !found )
183 				{
184 					end = !model.iterNext(iter);
185 					++index;
186 				}
187 			}
188 		}
189 		else
190 		{
191 			end = true;
192 		}
193 		return end ? -1 : index;
194 	}
195 
196 	/** */
197 	void prependOrReplaceText(string text)
198 	{
199 		int index = getIndex(text);
200 		if ( index > 0 )
201 		{
202 			remove(index);
203 			prepend("", text);
204 		}
205 		else if ( index == -1 )
206 		{
207 			prepend("", text);
208 		}
209 	}
210 
211 	/**
212 	 * The changed signal is emitted when the active
213 	 * item is changed. The can be due to the user selecting
214 	 * a different item from the list, or due to a
215 	 * call to gtk_combo_box_set_active_iter().
216 	 * It will also be emitted while typing into the entry of a combo box
217 	 * with an entry.
218 	 *
219 	 * Since: 2.4
220 	 */
221 	gulong addOnChanged(void delegate(ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
222 	{
223 		return Signals.connect(this, "changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
224 	}
225 
226 	/**
227 	 * For combo boxes that are created with an entry (See GtkComboBox:has-entry).
228 	 *
229 	 * A signal which allows you to change how the text displayed in a combo box's
230 	 * entry is displayed.
231 	 *
232 	 * Connect a signal handler which returns an allocated string representing
233 	 * @path. That string will then be used to set the text in the combo box's entry.
234 	 * The default signal handler uses the text from the GtkComboBox::entry-text-column
235 	 * model column.
236 	 *
237 	 * Here's an example signal handler which fetches data from the model and
238 	 * displays it in the entry.
239 	 * |[<!-- language="C" -->
240 	 * static gchar*
241 	 * format_entry_text_callback (GtkComboBox *combo,
242 	 * const gchar *path,
243 	 * gpointer     user_data)
244 	 * {
245 	 * GtkTreeIter iter;
246 	 * GtkTreeModel model;
247 	 * gdouble      value;
248 	 *
249 	 * model = gtk_combo_box_get_model (combo);
250 	 *
251 	 * gtk_tree_model_get_iter_from_string (model, &iter, path);
252 	 * gtk_tree_model_get (model, &iter,
253 	 * THE_DOUBLE_VALUE_COLUMN, &value,
254 	 * -1);
255 	 *
256 	 * return g_strdup_printf ("%g", value);
257 	 * }
258 	 * ]|
259 	 *
260 	 * Params:
261 	 *     path = the GtkTreePath string from the combo box's current model to format text for
262 	 *
263 	 * Return: a newly allocated string representing @path
264 	 *     for the current GtkComboBox model.
265 	 *
266 	 * Since: 3.4
267 	 */
268 	gulong addOnFormatEntryText(string delegate(string, ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
269 	{
270 		return Signals.connect(this, "format-entry-text", dlg, connectFlags ^ ConnectFlags.SWAPPED);
271 	}
272 
273 	/**
274 	 * The ::move-active signal is a
275 	 * [keybinding signal][GtkBindingSignal]
276 	 * which gets emitted to move the active selection.
277 	 *
278 	 * Params:
279 	 *     scrollType = a #GtkScrollType
280 	 *
281 	 * Since: 2.12
282 	 */
283 	gulong addOnMoveActive(void delegate(GtkScrollType, ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
284 	{
285 		return Signals.connect(this, "move-active", dlg, connectFlags ^ ConnectFlags.SWAPPED);
286 	}
287 
288 	/**
289 	 * The ::popdown signal is a
290 	 * [keybinding signal][GtkBindingSignal]
291 	 * which gets emitted to popdown the combo box list.
292 	 *
293 	 * The default bindings for this signal are Alt+Up and Escape.
294 	 *
295 	 * Since: 2.12
296 	 */
297 	gulong addOnPopdown(bool delegate(ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
298 	{
299 		return Signals.connect(this, "popdown", dlg, connectFlags ^ ConnectFlags.SWAPPED);
300 	}
301 
302 	/**
303 	 * The ::popup signal is a
304 	 * [keybinding signal][GtkBindingSignal]
305 	 * which gets emitted to popup the combo box list.
306 	 *
307 	 * The default binding for this signal is Alt+Down.
308 	 *
309 	 * Since: 2.12
310 	 */
311 	gulong addOnPopup(void delegate(ComboBoxText) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
312 	{
313 		return Signals.connect(this, "popup", dlg, connectFlags ^ ConnectFlags.SWAPPED);
314 	}
315 
316 	/**
317 	 */
318 
319 	/** */
320 	public static GType getType()
321 	{
322 		return gtk_combo_box_text_get_type();
323 	}
324 
325 	/**
326 	 * Appends @text to the list of strings stored in @combo_box.
327 	 * If @id is non-%NULL then it is used as the ID of the row.
328 	 *
329 	 * This is the same as calling gtk_combo_box_text_insert() with a
330 	 * position of -1.
331 	 *
332 	 * Params:
333 	 *     id = a string ID for this value, or %NULL
334 	 *     text = A string
335 	 *
336 	 * Since: 2.24
337 	 */
338 	public void append(string id, string text)
339 	{
340 		gtk_combo_box_text_append(gtkComboBoxText, Str.toStringz(id), Str.toStringz(text));
341 	}
342 
343 	/**
344 	 * Appends @text to the list of strings stored in @combo_box.
345 	 *
346 	 * This is the same as calling gtk_combo_box_text_insert_text() with a
347 	 * position of -1.
348 	 *
349 	 * Params:
350 	 *     text = A string
351 	 *
352 	 * Since: 2.24
353 	 */
354 	public void appendText(string text)
355 	{
356 		gtk_combo_box_text_append_text(gtkComboBoxText, Str.toStringz(text));
357 	}
358 
359 	/**
360 	 * Returns the currently active string in @combo_box, or %NULL
361 	 * if none is selected. If @combo_box contains an entry, this
362 	 * function will return its contents (which will not necessarily
363 	 * be an item from the list).
364 	 *
365 	 * Returns: a newly allocated string containing the
366 	 *     currently active text. Must be freed with g_free().
367 	 *
368 	 * Since: 2.24
369 	 */
370 	public string getActiveText()
371 	{
372 		auto retStr = gtk_combo_box_text_get_active_text(gtkComboBoxText);
373 
374 		scope(exit) Str.freeString(retStr);
375 		return Str.toString(retStr);
376 	}
377 
378 	/**
379 	 * Inserts @text at @position in the list of strings stored in @combo_box.
380 	 * If @id is non-%NULL then it is used as the ID of the row.  See
381 	 * #GtkComboBox:id-column.
382 	 *
383 	 * If @position is negative then @text is appended.
384 	 *
385 	 * Params:
386 	 *     position = An index to insert @text
387 	 *     id = a string ID for this value, or %NULL
388 	 *     text = A string to display
389 	 *
390 	 * Since: 3.0
391 	 */
392 	public void insert(int position, string id, string text)
393 	{
394 		gtk_combo_box_text_insert(gtkComboBoxText, position, Str.toStringz(id), Str.toStringz(text));
395 	}
396 
397 	/**
398 	 * Inserts @text at @position in the list of strings stored in @combo_box.
399 	 *
400 	 * If @position is negative then @text is appended.
401 	 *
402 	 * This is the same as calling gtk_combo_box_text_insert() with a %NULL
403 	 * ID string.
404 	 *
405 	 * Params:
406 	 *     position = An index to insert @text
407 	 *     text = A string
408 	 *
409 	 * Since: 2.24
410 	 */
411 	public void insertText(int position, string text)
412 	{
413 		gtk_combo_box_text_insert_text(gtkComboBoxText, position, Str.toStringz(text));
414 	}
415 
416 	/**
417 	 * Prepends @text to the list of strings stored in @combo_box.
418 	 * If @id is non-%NULL then it is used as the ID of the row.
419 	 *
420 	 * This is the same as calling gtk_combo_box_text_insert() with a
421 	 * position of 0.
422 	 *
423 	 * Params:
424 	 *     id = a string ID for this value, or %NULL
425 	 *     text = a string
426 	 *
427 	 * Since: 2.24
428 	 */
429 	public void prepend(string id, string text)
430 	{
431 		gtk_combo_box_text_prepend(gtkComboBoxText, Str.toStringz(id), Str.toStringz(text));
432 	}
433 
434 	/**
435 	 * Prepends @text to the list of strings stored in @combo_box.
436 	 *
437 	 * This is the same as calling gtk_combo_box_text_insert_text() with a
438 	 * position of 0.
439 	 *
440 	 * Params:
441 	 *     text = A string
442 	 *
443 	 * Since: 2.24
444 	 */
445 	public void prependText(string text)
446 	{
447 		gtk_combo_box_text_prepend_text(gtkComboBoxText, Str.toStringz(text));
448 	}
449 
450 	/**
451 	 * Removes the string at @position from @combo_box.
452 	 *
453 	 * Params:
454 	 *     position = Index of the item to remove
455 	 *
456 	 * Since: 2.24
457 	 */
458 	public void remove(int position)
459 	{
460 		gtk_combo_box_text_remove(gtkComboBoxText, position);
461 	}
462 
463 	/**
464 	 * Removes all the text entries from the combo box.
465 	 *
466 	 * Since: 3.0
467 	 */
468 	public override void removeAll()
469 	{
470 		gtk_combo_box_text_remove_all(gtkComboBoxText);
471 	}
472 }