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;
160 		TreeModelIF model = getModel();
161 		int index = 0;
162 		bool found = false;
163 		bool end = false;
164 		if ( model.getIterFirst(iter) )
165 		{
166 			iter.setModel(model);
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 	/** */
407 	public static GType getType()
408 	{
409 		return gtk_combo_box_text_get_type();
410 	}
411 
412 	/**
413 	 * Appends @text to the list of strings stored in @combo_box.
414 	 * If @id is non-%NULL then it is used as the ID of the row.
415 	 *
416 	 * This is the same as calling gtk_combo_box_text_insert() with a
417 	 * position of -1.
418 	 *
419 	 * Params:
420 	 *     id = a string ID for this value, or %NULL
421 	 *     text = A string
422 	 *
423 	 * Since: 2.24
424 	 */
425 	public void append(string id, string text)
426 	{
427 		gtk_combo_box_text_append(gtkComboBoxText, Str.toStringz(id), Str.toStringz(text));
428 	}
429 
430 	/**
431 	 * Appends @text to the list of strings stored in @combo_box.
432 	 *
433 	 * This is the same as calling gtk_combo_box_text_insert_text() with a
434 	 * position of -1.
435 	 *
436 	 * Params:
437 	 *     text = A string
438 	 *
439 	 * Since: 2.24
440 	 */
441 	public void appendText(string text)
442 	{
443 		gtk_combo_box_text_append_text(gtkComboBoxText, Str.toStringz(text));
444 	}
445 
446 	/**
447 	 * Returns the currently active string in @combo_box, or %NULL
448 	 * if none is selected. If @combo_box contains an entry, this
449 	 * function will return its contents (which will not necessarily
450 	 * be an item from the list).
451 	 *
452 	 * Return: a newly allocated string containing the
453 	 *     currently active text. Must be freed with g_free().
454 	 *
455 	 * Since: 2.24
456 	 */
457 	public string getActiveText()
458 	{
459 		return Str.toString(gtk_combo_box_text_get_active_text(gtkComboBoxText));
460 	}
461 
462 	/**
463 	 * Inserts @text at @position in the list of strings stored in @combo_box.
464 	 * If @id is non-%NULL then it is used as the ID of the row.  See
465 	 * #GtkComboBox:id-column.
466 	 *
467 	 * If @position is negative then @text is appended.
468 	 *
469 	 * Params:
470 	 *     position = An index to insert @text
471 	 *     id = a string ID for this value, or %NULL
472 	 *     text = A string to display
473 	 *
474 	 * Since: 3.0
475 	 */
476 	public void insert(int position, string id, string text)
477 	{
478 		gtk_combo_box_text_insert(gtkComboBoxText, position, Str.toStringz(id), Str.toStringz(text));
479 	}
480 
481 	/**
482 	 * Inserts @text at @position in the list of strings stored in @combo_box.
483 	 *
484 	 * If @position is negative then @text is appended.
485 	 *
486 	 * This is the same as calling gtk_combo_box_text_insert() with a %NULL
487 	 * ID string.
488 	 *
489 	 * Params:
490 	 *     position = An index to insert @text
491 	 *     text = A string
492 	 *
493 	 * Since: 2.24
494 	 */
495 	public void insertText(int position, string text)
496 	{
497 		gtk_combo_box_text_insert_text(gtkComboBoxText, position, Str.toStringz(text));
498 	}
499 
500 	/**
501 	 * Prepends @text to the list of strings stored in @combo_box.
502 	 * If @id is non-%NULL then it is used as the ID of the row.
503 	 *
504 	 * This is the same as calling gtk_combo_box_text_insert() with a
505 	 * position of 0.
506 	 *
507 	 * Params:
508 	 *     id = a string ID for this value, or %NULL
509 	 *     text = a string
510 	 *
511 	 * Since: 2.24
512 	 */
513 	public void prepend(string id, string text)
514 	{
515 		gtk_combo_box_text_prepend(gtkComboBoxText, Str.toStringz(id), Str.toStringz(text));
516 	}
517 
518 	/**
519 	 * Prepends @text to the list of strings stored in @combo_box.
520 	 *
521 	 * This is the same as calling gtk_combo_box_text_insert_text() with a
522 	 * position of 0.
523 	 *
524 	 * Params:
525 	 *     text = A string
526 	 *
527 	 * Since: 2.24
528 	 */
529 	public void prependText(string text)
530 	{
531 		gtk_combo_box_text_prepend_text(gtkComboBoxText, Str.toStringz(text));
532 	}
533 
534 	/**
535 	 * Removes the string at @position from @combo_box.
536 	 *
537 	 * Params:
538 	 *     position = Index of the item to remove
539 	 *
540 	 * Since: 2.24
541 	 */
542 	public void remove(int position)
543 	{
544 		gtk_combo_box_text_remove(gtkComboBoxText, position);
545 	}
546 
547 	/**
548 	 * Removes all the text entries from the combo box.
549 	 *
550 	 * Since: 3.0
551 	 */
552 	public override void removeAll()
553 	{
554 		gtk_combo_box_text_remove_all(gtkComboBoxText);
555 	}
556 }