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.TreeSelection;
26 
27 private import glib.ListG;
28 private import gobject.ObjectG;
29 private import gobject.Signals;
30 private import gtk.TreeIter;
31 private import gtk.TreeModel;
32 private import gtk.TreeModelIF;
33 private import gtk.TreePath;
34 private import gtk.TreeView;
35 public  import gtkc.gdktypes;
36 private import gtkc.gtk;
37 public  import gtkc.gtktypes;
38 
39 
40 /**
41  * The #GtkTreeSelection object is a helper object to manage the selection
42  * for a #GtkTreeView widget.  The #GtkTreeSelection object is
43  * automatically created when a new #GtkTreeView widget is created, and
44  * cannot exist independently of this widget.  The primary reason the
45  * #GtkTreeSelection objects exists is for cleanliness of code and API.
46  * That is, there is no conceptual reason all these functions could not be
47  * methods on the #GtkTreeView widget instead of a separate function.
48  * 
49  * The #GtkTreeSelection object is gotten from a #GtkTreeView by calling
50  * gtk_tree_view_get_selection().  It can be manipulated to check the
51  * selection status of the tree, as well as select and deselect individual
52  * rows.  Selection is done completely view side.  As a result, multiple
53  * views of the same model can have completely different selections.
54  * Additionally, you cannot change the selection of a row on the model that
55  * is not currently displayed by the view without expanding its parents
56  * first.
57  * 
58  * One of the important things to remember when monitoring the selection of
59  * a view is that the #GtkTreeSelection::changed signal is mostly a hint.
60  * That is, it may only emit one signal when a range of rows is selected.
61  * Additionally, it may on occasion emit a #GtkTreeSelection::changed signal
62  * when nothing has happened (mostly as a result of programmers calling
63  * select_row on an already selected row).
64  */
65 public class TreeSelection : ObjectG
66 {
67 	/** the main Gtk struct */
68 	protected GtkTreeSelection* gtkTreeSelection;
69 
70 	/** Get the main Gtk struct */
71 	public GtkTreeSelection* getTreeSelectionStruct()
72 	{
73 		return gtkTreeSelection;
74 	}
75 
76 	/** the main Gtk struct as a void* */
77 	protected override void* getStruct()
78 	{
79 		return cast(void*)gtkTreeSelection;
80 	}
81 
82 	protected override void setStruct(GObject* obj)
83 	{
84 		gtkTreeSelection = cast(GtkTreeSelection*)obj;
85 		super.setStruct(obj);
86 	}
87 
88 	/**
89 	 * Sets our main struct and passes it to the parent class.
90 	 */
91 	public this (GtkTreeSelection* gtkTreeSelection, bool ownedRef = false)
92 	{
93 		this.gtkTreeSelection = gtkTreeSelection;
94 		super(cast(GObject*)gtkTreeSelection, ownedRef);
95 	}
96 
97 	/**
98 	 * Returns an TreeIter set to the currently selected node if selection
99 	 * is set to Selection.SINGLE or Selection.BROWSE.
100 	 * This function will not work if you use selection is Selection.MULTIPLE.
101 	 * Returns: A TreeIter for the selected node.
102 	 */
103 	public TreeIter getSelected()
104 	{
105 		TreeModelIF model;
106 		TreeIter iter = new TreeIter();
107 		
108 		if ( getSelected(model, iter) )
109 		{
110 			iter.setModel(model);
111 			return iter;
112 		}
113 		else
114 		{
115 			return null;
116 		}
117 	}
118 	
119 	/**
120 	 * Creates a list of path of all selected rows. Additionally, if you are
121 	 * planning on modifying the model after calling this function, you may
122 	 * want to convert the returned list into a list of TreeRowReferences.
123 	 * Since: 2.2
124 	 * Params:
125 	 *  model = A pointer to set to the GtkTreeModel, or NULL.
126 	 * Returns:
127 	 *  A GList containing a GtkTreePath for each selected row.
128 	 */
129 	TreePath[] getSelectedRows(out TreeModelIF model)
130 	{
131 		TreePath[] paths;
132 		GtkTreeModel* outmodel = null;
133 		GList* gList = gtk_tree_selection_get_selected_rows(gtkTreeSelection, &outmodel);
134 		if ( gList !is null )
135 		{
136 			ListG list = new ListG(gList);
137 			for ( int i=0 ; i<list.length() ; i++ )
138 			{
139 				paths ~= new TreePath(cast(GtkTreePath*)list.nthData(i));
140 			}
141 		}
142 		model = ObjectG.getDObject!(TreeModel, TreeModelIF)(outmodel);
143 		
144 		return paths;
145 	}
146 
147 	/**
148 	 */
149 
150 	/** */
151 	public static GType getType()
152 	{
153 		return gtk_tree_selection_get_type();
154 	}
155 
156 	/**
157 	 * Returns the number of rows that have been selected in @tree.
158 	 *
159 	 * Return: The number of rows selected.
160 	 *
161 	 * Since: 2.2
162 	 */
163 	public int countSelectedRows()
164 	{
165 		return gtk_tree_selection_count_selected_rows(gtkTreeSelection);
166 	}
167 
168 	/**
169 	 * Gets the selection mode for @selection. See
170 	 * gtk_tree_selection_set_mode().
171 	 *
172 	 * Return: the current selection mode
173 	 */
174 	public GtkSelectionMode getMode()
175 	{
176 		return gtk_tree_selection_get_mode(gtkTreeSelection);
177 	}
178 
179 	/**
180 	 * Returns the current selection function.
181 	 *
182 	 * Return: The function.
183 	 *
184 	 * Since: 2.14
185 	 */
186 	public GtkTreeSelectionFunc getSelectFunction()
187 	{
188 		return gtk_tree_selection_get_select_function(gtkTreeSelection);
189 	}
190 
191 	/**
192 	 * Sets @iter to the currently selected node if @selection is set to
193 	 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE.  @iter may be NULL if you
194 	 * just want to test if @selection has any selected nodes.  @model is filled
195 	 * with the current model as a convenience.  This function will not work if you
196 	 * use @selection is #GTK_SELECTION_MULTIPLE.
197 	 *
198 	 * Params:
199 	 *     model = A pointer to set to the #GtkTreeModel, or NULL.
200 	 *     iter = The #GtkTreeIter, or NULL.
201 	 *
202 	 * Return: TRUE, if there is a selected node.
203 	 */
204 	public bool getSelected(out TreeModelIF model, out TreeIter iter)
205 	{
206 		GtkTreeModel* outmodel = null;
207 		GtkTreeIter* outiter = gMalloc!GtkTreeIter();
208 		
209 		auto p = gtk_tree_selection_get_selected(gtkTreeSelection, &outmodel, outiter) != 0;
210 		
211 		model = ObjectG.getDObject!(TreeModel, TreeModelIF)(outmodel);
212 		iter = ObjectG.getDObject!(TreeIter)(outiter, true);
213 		
214 		return p;
215 	}
216 
217 	/**
218 	 * Returns the tree view associated with @selection.
219 	 *
220 	 * Return: A #GtkTreeView
221 	 */
222 	public TreeView getTreeView()
223 	{
224 		auto p = gtk_tree_selection_get_tree_view(gtkTreeSelection);
225 		
226 		if(p is null)
227 		{
228 			return null;
229 		}
230 		
231 		return ObjectG.getDObject!(TreeView)(cast(GtkTreeView*) p);
232 	}
233 
234 	/**
235 	 * Returns the user data for the selection function.
236 	 *
237 	 * Return: The user data.
238 	 */
239 	public void* getUserData()
240 	{
241 		return gtk_tree_selection_get_user_data(gtkTreeSelection);
242 	}
243 
244 	/**
245 	 * Returns %TRUE if the row at @iter is currently selected.
246 	 *
247 	 * Params:
248 	 *     iter = A valid #GtkTreeIter
249 	 *
250 	 * Return: %TRUE, if @iter is selected
251 	 */
252 	public bool iterIsSelected(TreeIter iter)
253 	{
254 		return gtk_tree_selection_iter_is_selected(gtkTreeSelection, (iter is null) ? null : iter.getTreeIterStruct()) != 0;
255 	}
256 
257 	/**
258 	 * Returns %TRUE if the row pointed to by @path is currently selected.  If @path
259 	 * does not point to a valid location, %FALSE is returned
260 	 *
261 	 * Params:
262 	 *     path = A #GtkTreePath to check selection on.
263 	 *
264 	 * Return: %TRUE if @path is selected.
265 	 */
266 	public bool pathIsSelected(TreePath path)
267 	{
268 		return gtk_tree_selection_path_is_selected(gtkTreeSelection, (path is null) ? null : path.getTreePathStruct()) != 0;
269 	}
270 
271 	/**
272 	 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
273 	 * mode.
274 	 */
275 	public void selectAll()
276 	{
277 		gtk_tree_selection_select_all(gtkTreeSelection);
278 	}
279 
280 	/**
281 	 * Selects the specified iterator.
282 	 *
283 	 * Params:
284 	 *     iter = The #GtkTreeIter to be selected.
285 	 */
286 	public void selectIter(TreeIter iter)
287 	{
288 		gtk_tree_selection_select_iter(gtkTreeSelection, (iter is null) ? null : iter.getTreeIterStruct());
289 	}
290 
291 	/**
292 	 * Select the row at @path.
293 	 *
294 	 * Params:
295 	 *     path = The #GtkTreePath to be selected.
296 	 */
297 	public void selectPath(TreePath path)
298 	{
299 		gtk_tree_selection_select_path(gtkTreeSelection, (path is null) ? null : path.getTreePathStruct());
300 	}
301 
302 	/**
303 	 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
304 	 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
305 	 *
306 	 * Params:
307 	 *     startPath = The initial node of the range.
308 	 *     endPath = The final node of the range.
309 	 */
310 	public void selectRange(TreePath startPath, TreePath endPath)
311 	{
312 		gtk_tree_selection_select_range(gtkTreeSelection, (startPath is null) ? null : startPath.getTreePathStruct(), (endPath is null) ? null : endPath.getTreePathStruct());
313 	}
314 
315 	/**
316 	 * Calls a function for each selected node. Note that you cannot modify
317 	 * the tree or selection from within this function. As a result,
318 	 * gtk_tree_selection_get_selected_rows() might be more useful.
319 	 *
320 	 * Params:
321 	 *     func = The function to call for each selected node.
322 	 *     data = user data to pass to the function.
323 	 */
324 	public void selectedForeach(GtkTreeSelectionForeachFunc func, void* data)
325 	{
326 		gtk_tree_selection_selected_foreach(gtkTreeSelection, func, data);
327 	}
328 
329 	/**
330 	 * Sets the selection mode of the @selection.  If the previous type was
331 	 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
332 	 * previously selected.
333 	 *
334 	 * Params:
335 	 *     type = The selection mode
336 	 */
337 	public void setMode(GtkSelectionMode type)
338 	{
339 		gtk_tree_selection_set_mode(gtkTreeSelection, type);
340 	}
341 
342 	/**
343 	 * Sets the selection function.
344 	 *
345 	 * If set, this function is called before any node is selected or unselected,
346 	 * giving some control over which nodes are selected. The select function
347 	 * should return %TRUE if the state of the node may be toggled, and %FALSE
348 	 * if the state of the node should be left unchanged.
349 	 *
350 	 * Params:
351 	 *     func = The selection function. May be %NULL
352 	 *     data = The selection function’s data. May be %NULL
353 	 *     destroy = The destroy function for user data.  May be %NULL
354 	 */
355 	public void setSelectFunction(GtkTreeSelectionFunc func, void* data, GDestroyNotify destroy)
356 	{
357 		gtk_tree_selection_set_select_function(gtkTreeSelection, func, data, destroy);
358 	}
359 
360 	/**
361 	 * Unselects all the nodes.
362 	 */
363 	public void unselectAll()
364 	{
365 		gtk_tree_selection_unselect_all(gtkTreeSelection);
366 	}
367 
368 	/**
369 	 * Unselects the specified iterator.
370 	 *
371 	 * Params:
372 	 *     iter = The #GtkTreeIter to be unselected.
373 	 */
374 	public void unselectIter(TreeIter iter)
375 	{
376 		gtk_tree_selection_unselect_iter(gtkTreeSelection, (iter is null) ? null : iter.getTreeIterStruct());
377 	}
378 
379 	/**
380 	 * Unselects the row at @path.
381 	 *
382 	 * Params:
383 	 *     path = The #GtkTreePath to be unselected.
384 	 */
385 	public void unselectPath(TreePath path)
386 	{
387 		gtk_tree_selection_unselect_path(gtkTreeSelection, (path is null) ? null : path.getTreePathStruct());
388 	}
389 
390 	/**
391 	 * Unselects a range of nodes, determined by @start_path and @end_path
392 	 * inclusive.
393 	 *
394 	 * Params:
395 	 *     startPath = The initial node of the range.
396 	 *     endPath = The initial node of the range.
397 	 *
398 	 * Since: 2.2
399 	 */
400 	public void unselectRange(TreePath startPath, TreePath endPath)
401 	{
402 		gtk_tree_selection_unselect_range(gtkTreeSelection, (startPath is null) ? null : startPath.getTreePathStruct(), (endPath is null) ? null : endPath.getTreePathStruct());
403 	}
404 
405 	int[string] connectedSignals;
406 
407 	void delegate(TreeSelection)[] onChangedListeners;
408 	/**
409 	 * Emitted whenever the selection has (possibly) changed. Please note that
410 	 * this signal is mostly a hint.  It may only be emitted once when a range
411 	 * of rows are selected, and it may occasionally be emitted when nothing
412 	 * has happened.
413 	 */
414 	void addOnChanged(void delegate(TreeSelection) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
415 	{
416 		if ( "changed" !in connectedSignals )
417 		{
418 			Signals.connectData(
419 				this,
420 				"changed",
421 				cast(GCallback)&callBackChanged,
422 				cast(void*)this,
423 				null,
424 				connectFlags);
425 			connectedSignals["changed"] = 1;
426 		}
427 		onChangedListeners ~= dlg;
428 	}
429 	extern(C) static void callBackChanged(GtkTreeSelection* treeselectionStruct, TreeSelection _treeselection)
430 	{
431 		foreach ( void delegate(TreeSelection) dlg; _treeselection.onChangedListeners )
432 		{
433 			dlg(_treeselection);
434 		}
435 	}
436 }