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