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