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