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