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 	public static GType getType()
151 	{
152 		return gtk_tree_selection_get_type();
153 	}
154 
155 	/**
156 	 * Returns the number of rows that have been selected in @tree.
157 	 *
158 	 * Return: The number of rows selected.
159 	 *
160 	 * Since: 2.2
161 	 */
162 	public int countSelectedRows()
163 	{
164 		return gtk_tree_selection_count_selected_rows(gtkTreeSelection);
165 	}
166 
167 	/**
168 	 * Gets the selection mode for @selection. See
169 	 * gtk_tree_selection_set_mode().
170 	 *
171 	 * Return: the current selection mode
172 	 */
173 	public GtkSelectionMode getMode()
174 	{
175 		return gtk_tree_selection_get_mode(gtkTreeSelection);
176 	}
177 
178 	/**
179 	 * Returns the current selection function.
180 	 *
181 	 * Return: The function.
182 	 *
183 	 * Since: 2.14
184 	 */
185 	public GtkTreeSelectionFunc getSelectFunction()
186 	{
187 		return gtk_tree_selection_get_select_function(gtkTreeSelection);
188 	}
189 
190 	/**
191 	 * Sets @iter to the currently selected node if @selection is set to
192 	 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE.  @iter may be NULL if you
193 	 * just want to test if @selection has any selected nodes.  @model is filled
194 	 * with the current model as a convenience.  This function will not work if you
195 	 * use @selection is #GTK_SELECTION_MULTIPLE.
196 	 *
197 	 * Params:
198 	 *     model = A pointer to set to the #GtkTreeModel, or NULL.
199 	 *     iter = The #GtkTreeIter, or NULL.
200 	 *
201 	 * Return: TRUE, if there is a selected node.
202 	 */
203 	public bool getSelected(out TreeModelIF model, out TreeIter iter)
204 	{
205 		GtkTreeModel* outmodel = null;
206 		GtkTreeIter* outiter = new GtkTreeIter;
207 		
208 		auto p = gtk_tree_selection_get_selected(gtkTreeSelection, &outmodel, outiter) != 0;
209 		
210 		model = ObjectG.getDObject!(TreeModel, TreeModelIF)(outmodel);
211 		iter = ObjectG.getDObject!(TreeIter)(outiter);
212 		
213 		return p;
214 	}
215 
216 	/**
217 	 * Returns the tree view associated with @selection.
218 	 *
219 	 * Return: A #GtkTreeView
220 	 */
221 	public TreeView getTreeView()
222 	{
223 		auto p = gtk_tree_selection_get_tree_view(gtkTreeSelection);
224 		
225 		if(p is null)
226 		{
227 			return null;
228 		}
229 		
230 		return ObjectG.getDObject!(TreeView)(cast(GtkTreeView*) p);
231 	}
232 
233 	/**
234 	 * Returns the user data for the selection function.
235 	 *
236 	 * Return: The user data.
237 	 */
238 	public void* getUserData()
239 	{
240 		return gtk_tree_selection_get_user_data(gtkTreeSelection);
241 	}
242 
243 	/**
244 	 * Returns %TRUE if the row at @iter is currently selected.
245 	 *
246 	 * Params:
247 	 *     iter = A valid #GtkTreeIter
248 	 *
249 	 * Return: %TRUE, if @iter is selected
250 	 */
251 	public bool iterIsSelected(TreeIter iter)
252 	{
253 		return gtk_tree_selection_iter_is_selected(gtkTreeSelection, (iter is null) ? null : iter.getTreeIterStruct()) != 0;
254 	}
255 
256 	/**
257 	 * Returns %TRUE if the row pointed to by @path is currently selected.  If @path
258 	 * does not point to a valid location, %FALSE is returned
259 	 *
260 	 * Params:
261 	 *     path = A #GtkTreePath to check selection on.
262 	 *
263 	 * Return: %TRUE if @path is selected.
264 	 */
265 	public bool pathIsSelected(TreePath path)
266 	{
267 		return gtk_tree_selection_path_is_selected(gtkTreeSelection, (path is null) ? null : path.getTreePathStruct()) != 0;
268 	}
269 
270 	/**
271 	 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
272 	 * mode.
273 	 */
274 	public void selectAll()
275 	{
276 		gtk_tree_selection_select_all(gtkTreeSelection);
277 	}
278 
279 	/**
280 	 * Selects the specified iterator.
281 	 *
282 	 * Params:
283 	 *     iter = The #GtkTreeIter to be selected.
284 	 */
285 	public void selectIter(TreeIter iter)
286 	{
287 		gtk_tree_selection_select_iter(gtkTreeSelection, (iter is null) ? null : iter.getTreeIterStruct());
288 	}
289 
290 	/**
291 	 * Select the row at @path.
292 	 *
293 	 * Params:
294 	 *     path = The #GtkTreePath to be selected.
295 	 */
296 	public void selectPath(TreePath path)
297 	{
298 		gtk_tree_selection_select_path(gtkTreeSelection, (path is null) ? null : path.getTreePathStruct());
299 	}
300 
301 	/**
302 	 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
303 	 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
304 	 *
305 	 * Params:
306 	 *     startPath = The initial node of the range.
307 	 *     endPath = The final node of the range.
308 	 */
309 	public void selectRange(TreePath startPath, TreePath endPath)
310 	{
311 		gtk_tree_selection_select_range(gtkTreeSelection, (startPath is null) ? null : startPath.getTreePathStruct(), (endPath is null) ? null : endPath.getTreePathStruct());
312 	}
313 
314 	/**
315 	 * Calls a function for each selected node. Note that you cannot modify
316 	 * the tree or selection from within this function. As a result,
317 	 * gtk_tree_selection_get_selected_rows() might be more useful.
318 	 *
319 	 * Params:
320 	 *     func = The function to call for each selected node.
321 	 *     data = user data to pass to the function.
322 	 */
323 	public void selectedForeach(GtkTreeSelectionForeachFunc func, void* data)
324 	{
325 		gtk_tree_selection_selected_foreach(gtkTreeSelection, func, data);
326 	}
327 
328 	/**
329 	 * Sets the selection mode of the @selection.  If the previous type was
330 	 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
331 	 * previously selected.
332 	 *
333 	 * Params:
334 	 *     type = The selection mode
335 	 */
336 	public void setMode(GtkSelectionMode type)
337 	{
338 		gtk_tree_selection_set_mode(gtkTreeSelection, type);
339 	}
340 
341 	/**
342 	 * Sets the selection function.
343 	 *
344 	 * If set, this function is called before any node is selected or unselected,
345 	 * giving some control over which nodes are selected. The select function
346 	 * should return %TRUE if the state of the node may be toggled, and %FALSE
347 	 * if the state of the node should be left unchanged.
348 	 *
349 	 * Params:
350 	 *     func = The selection function. May be %NULL
351 	 *     data = The selection function’s data. May be %NULL
352 	 *     destroy = The destroy function for user data.  May be %NULL
353 	 */
354 	public void setSelectFunction(GtkTreeSelectionFunc func, void* data, GDestroyNotify destroy)
355 	{
356 		gtk_tree_selection_set_select_function(gtkTreeSelection, func, data, destroy);
357 	}
358 
359 	/**
360 	 * Unselects all the nodes.
361 	 */
362 	public void unselectAll()
363 	{
364 		gtk_tree_selection_unselect_all(gtkTreeSelection);
365 	}
366 
367 	/**
368 	 * Unselects the specified iterator.
369 	 *
370 	 * Params:
371 	 *     iter = The #GtkTreeIter to be unselected.
372 	 */
373 	public void unselectIter(TreeIter iter)
374 	{
375 		gtk_tree_selection_unselect_iter(gtkTreeSelection, (iter is null) ? null : iter.getTreeIterStruct());
376 	}
377 
378 	/**
379 	 * Unselects the row at @path.
380 	 *
381 	 * Params:
382 	 *     path = The #GtkTreePath to be unselected.
383 	 */
384 	public void unselectPath(TreePath path)
385 	{
386 		gtk_tree_selection_unselect_path(gtkTreeSelection, (path is null) ? null : path.getTreePathStruct());
387 	}
388 
389 	/**
390 	 * Unselects a range of nodes, determined by @start_path and @end_path
391 	 * inclusive.
392 	 *
393 	 * Params:
394 	 *     startPath = The initial node of the range.
395 	 *     endPath = The initial node of the range.
396 	 *
397 	 * Since: 2.2
398 	 */
399 	public void unselectRange(TreePath startPath, TreePath endPath)
400 	{
401 		gtk_tree_selection_unselect_range(gtkTreeSelection, (startPath is null) ? null : startPath.getTreePathStruct(), (endPath is null) ? null : endPath.getTreePathStruct());
402 	}
403 
404 	int[string] connectedSignals;
405 
406 	void delegate(TreeSelection)[] onChangedListeners;
407 	/**
408 	 * Emitted whenever the selection has (possibly) changed. Please note that
409 	 * this signal is mostly a hint.  It may only be emitted once when a range
410 	 * of rows are selected, and it may occasionally be emitted when nothing
411 	 * has happened.
412 	 */
413 	void addOnChanged(void delegate(TreeSelection) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
414 	{
415 		if ( "changed" !in connectedSignals )
416 		{
417 			Signals.connectData(
418 				this,
419 				"changed",
420 				cast(GCallback)&callBackChanged,
421 				cast(void*)this,
422 				null,
423 				connectFlags);
424 			connectedSignals["changed"] = 1;
425 		}
426 		onChangedListeners ~= dlg;
427 	}
428 	extern(C) static void callBackChanged(GtkTreeSelection* treeselectionStruct, TreeSelection _treeselection)
429 	{
430 		foreach ( void delegate(TreeSelection) dlg; _treeselection.onChangedListeners )
431 		{
432 			dlg(_treeselection);
433 		}
434 	}
435 }