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