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