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