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 	protected override void setStruct(GObject* obj)
86 	{
87 		gtkTreeSelection = cast(GtkTreeSelection*)obj;
88 		super.setStruct(obj);
89 	}
90 
91 	/**
92 	 * Sets our main struct and passes it to the parent class.
93 	 */
94 	public this (GtkTreeSelection* gtkTreeSelection, bool ownedRef = false)
95 	{
96 		this.gtkTreeSelection = gtkTreeSelection;
97 		super(cast(GObject*)gtkTreeSelection, ownedRef);
98 	}
99 
100 	/**
101 	 * Returns an TreeIter set to the currently selected node if selection
102 	 * is set to Selection.SINGLE or Selection.BROWSE.
103 	 * This function will not work if you use selection is Selection.MULTIPLE.
104 	 * Returns: A TreeIter for the selected node.
105 	 */
106 	public TreeIter getSelected()
107 	{
108 		TreeModelIF model;
109 		TreeIter iter = new TreeIter();
110 
111 		if ( getSelected(model, iter) )
112 		{
113 			iter.setModel(model);
114 			return iter;
115 		}
116 		else
117 		{
118 			return null;
119 		}
120 	}
121 
122 	/**
123 	 * Creates a list of path of all selected rows. Additionally, if you are
124 	 * planning on modifying the model after calling this function, you may
125 	 * want to convert the returned list into a list of TreeRowReferences.
126 	 * Since: 2.2
127 	 * Params:
128 	 *  model = A pointer to set to the GtkTreeModel, or NULL.
129 	 * Returns:
130 	 *  A GList containing a GtkTreePath for each selected row.
131 	 */
132 	TreePath[] getSelectedRows(out TreeModelIF model)
133 	{
134 		TreePath[] paths;
135 		GtkTreeModel* outmodel = null;
136 		GList* gList = gtk_tree_selection_get_selected_rows(gtkTreeSelection, &outmodel);
137 		if ( gList !is null )
138 		{
139 			ListG list = new ListG(gList);
140 			for ( int i=0 ; i<list.length() ; i++ )
141 			{
142 				paths ~= new TreePath(cast(GtkTreePath*)list.nthData(i));
143 			}
144 		}
145 		model = ObjectG.getDObject!(TreeModelIF)(outmodel);
146 
147 		return paths;
148 	}
149 
150 	/**
151 	 */
152 
153 	/** */
154 	public static GType getType()
155 	{
156 		return gtk_tree_selection_get_type();
157 	}
158 
159 	/**
160 	 * Returns the number of rows that have been selected in @tree.
161 	 *
162 	 * Returns: The number of rows selected.
163 	 *
164 	 * Since: 2.2
165 	 */
166 	public int countSelectedRows()
167 	{
168 		return gtk_tree_selection_count_selected_rows(gtkTreeSelection);
169 	}
170 
171 	/**
172 	 * Gets the selection mode for @selection. See
173 	 * gtk_tree_selection_set_mode().
174 	 *
175 	 * Returns: the current selection mode
176 	 */
177 	public GtkSelectionMode getMode()
178 	{
179 		return gtk_tree_selection_get_mode(gtkTreeSelection);
180 	}
181 
182 	/**
183 	 * Returns the current selection function.
184 	 *
185 	 * Returns: The function.
186 	 *
187 	 * Since: 2.14
188 	 */
189 	public GtkTreeSelectionFunc getSelectFunction()
190 	{
191 		return gtk_tree_selection_get_select_function(gtkTreeSelection);
192 	}
193 
194 	/**
195 	 * Sets @iter to the currently selected node if @selection is set to
196 	 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE.  @iter may be NULL if you
197 	 * just want to test if @selection has any selected nodes.  @model is filled
198 	 * with the current model as a convenience.  This function will not work if you
199 	 * use @selection is #GTK_SELECTION_MULTIPLE.
200 	 *
201 	 * Params:
202 	 *     model = A pointer to set to the #GtkTreeModel, or NULL.
203 	 *     iter = The #GtkTreeIter, or NULL.
204 	 *
205 	 * Returns: TRUE, if there is a selected node.
206 	 */
207 	public bool getSelected(out TreeModelIF model, out TreeIter iter)
208 	{
209 		GtkTreeModel* outmodel = null;
210 		GtkTreeIter* outiter = sliceNew!GtkTreeIter();
211 
212 		auto p = gtk_tree_selection_get_selected(gtkTreeSelection, &outmodel, outiter) != 0;
213 
214 		model = ObjectG.getDObject!(TreeModelIF)(outmodel);
215 		iter = ObjectG.getDObject!(TreeIter)(outiter, true);
216 
217 		return p;
218 	}
219 
220 	/**
221 	 * Returns the tree view associated with @selection.
222 	 *
223 	 * Returns: A #GtkTreeView
224 	 */
225 	public TreeView getTreeView()
226 	{
227 		auto p = gtk_tree_selection_get_tree_view(gtkTreeSelection);
228 
229 		if(p is null)
230 		{
231 			return null;
232 		}
233 
234 		return ObjectG.getDObject!(TreeView)(cast(GtkTreeView*) p);
235 	}
236 
237 	/**
238 	 * Returns the user data for the selection function.
239 	 *
240 	 * Returns: The user data.
241 	 */
242 	public void* getUserData()
243 	{
244 		return gtk_tree_selection_get_user_data(gtkTreeSelection);
245 	}
246 
247 	/**
248 	 * Returns %TRUE if the row at @iter is currently selected.
249 	 *
250 	 * Params:
251 	 *     iter = A valid #GtkTreeIter
252 	 *
253 	 * Returns: %TRUE, if @iter is selected
254 	 */
255 	public bool iterIsSelected(TreeIter iter)
256 	{
257 		return gtk_tree_selection_iter_is_selected(gtkTreeSelection, (iter is null) ? null : iter.getTreeIterStruct()) != 0;
258 	}
259 
260 	/**
261 	 * Returns %TRUE if the row pointed to by @path is currently selected.  If @path
262 	 * does not point to a valid location, %FALSE is returned
263 	 *
264 	 * Params:
265 	 *     path = A #GtkTreePath to check selection on.
266 	 *
267 	 * Returns: %TRUE if @path is selected.
268 	 */
269 	public bool pathIsSelected(TreePath path)
270 	{
271 		return gtk_tree_selection_path_is_selected(gtkTreeSelection, (path is null) ? null : path.getTreePathStruct()) != 0;
272 	}
273 
274 	/**
275 	 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
276 	 * mode.
277 	 */
278 	public void selectAll()
279 	{
280 		gtk_tree_selection_select_all(gtkTreeSelection);
281 	}
282 
283 	/**
284 	 * Selects the specified iterator.
285 	 *
286 	 * Params:
287 	 *     iter = The #GtkTreeIter to be selected.
288 	 */
289 	public void selectIter(TreeIter iter)
290 	{
291 		gtk_tree_selection_select_iter(gtkTreeSelection, (iter is null) ? null : iter.getTreeIterStruct());
292 	}
293 
294 	/**
295 	 * Select the row at @path.
296 	 *
297 	 * Params:
298 	 *     path = The #GtkTreePath to be selected.
299 	 */
300 	public void selectPath(TreePath path)
301 	{
302 		gtk_tree_selection_select_path(gtkTreeSelection, (path is null) ? null : path.getTreePathStruct());
303 	}
304 
305 	/**
306 	 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
307 	 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
308 	 *
309 	 * Params:
310 	 *     startPath = The initial node of the range.
311 	 *     endPath = The final node of the range.
312 	 */
313 	public void selectRange(TreePath startPath, TreePath endPath)
314 	{
315 		gtk_tree_selection_select_range(gtkTreeSelection, (startPath is null) ? null : startPath.getTreePathStruct(), (endPath is null) ? null : endPath.getTreePathStruct());
316 	}
317 
318 	/**
319 	 * Calls a function for each selected node. Note that you cannot modify
320 	 * the tree or selection from within this function. As a result,
321 	 * gtk_tree_selection_get_selected_rows() might be more useful.
322 	 *
323 	 * Params:
324 	 *     func = The function to call for each selected node.
325 	 *     data = user data to pass to the function.
326 	 */
327 	public void selectedForeach(GtkTreeSelectionForeachFunc func, void* data)
328 	{
329 		gtk_tree_selection_selected_foreach(gtkTreeSelection, func, data);
330 	}
331 
332 	/**
333 	 * Sets the selection mode of the @selection.  If the previous type was
334 	 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
335 	 * previously selected.
336 	 *
337 	 * Params:
338 	 *     type = The selection mode
339 	 */
340 	public void setMode(GtkSelectionMode type)
341 	{
342 		gtk_tree_selection_set_mode(gtkTreeSelection, type);
343 	}
344 
345 	/**
346 	 * Sets the selection function.
347 	 *
348 	 * If set, this function is called before any node is selected or unselected,
349 	 * giving some control over which nodes are selected. The select function
350 	 * should return %TRUE if the state of the node may be toggled, and %FALSE
351 	 * if the state of the node should be left unchanged.
352 	 *
353 	 * Params:
354 	 *     func = The selection function. May be %NULL
355 	 *     data = The selection function’s data. May be %NULL
356 	 *     destroy = The destroy function for user data.  May be %NULL
357 	 */
358 	public void setSelectFunction(GtkTreeSelectionFunc func, void* data, GDestroyNotify destroy)
359 	{
360 		gtk_tree_selection_set_select_function(gtkTreeSelection, func, data, destroy);
361 	}
362 
363 	/**
364 	 * Unselects all the nodes.
365 	 */
366 	public void unselectAll()
367 	{
368 		gtk_tree_selection_unselect_all(gtkTreeSelection);
369 	}
370 
371 	/**
372 	 * Unselects the specified iterator.
373 	 *
374 	 * Params:
375 	 *     iter = The #GtkTreeIter to be unselected.
376 	 */
377 	public void unselectIter(TreeIter iter)
378 	{
379 		gtk_tree_selection_unselect_iter(gtkTreeSelection, (iter is null) ? null : iter.getTreeIterStruct());
380 	}
381 
382 	/**
383 	 * Unselects the row at @path.
384 	 *
385 	 * Params:
386 	 *     path = The #GtkTreePath to be unselected.
387 	 */
388 	public void unselectPath(TreePath path)
389 	{
390 		gtk_tree_selection_unselect_path(gtkTreeSelection, (path is null) ? null : path.getTreePathStruct());
391 	}
392 
393 	/**
394 	 * Unselects a range of nodes, determined by @start_path and @end_path
395 	 * inclusive.
396 	 *
397 	 * Params:
398 	 *     startPath = The initial node of the range.
399 	 *     endPath = The initial node of the range.
400 	 *
401 	 * Since: 2.2
402 	 */
403 	public void unselectRange(TreePath startPath, TreePath endPath)
404 	{
405 		gtk_tree_selection_unselect_range(gtkTreeSelection, (startPath is null) ? null : startPath.getTreePathStruct(), (endPath is null) ? null : endPath.getTreePathStruct());
406 	}
407 
408 	protected class OnChangedDelegateWrapper
409 	{
410 		void delegate(TreeSelection) dlg;
411 		gulong handlerId;
412 
413 		this(void delegate(TreeSelection) dlg)
414 		{
415 			this.dlg = dlg;
416 			onChangedListeners ~= this;
417 		}
418 
419 		void remove(OnChangedDelegateWrapper source)
420 		{
421 			foreach(index, wrapper; onChangedListeners)
422 			{
423 				if (wrapper.handlerId == source.handlerId)
424 				{
425 					onChangedListeners[index] = null;
426 					onChangedListeners = std.algorithm.remove(onChangedListeners, index);
427 					break;
428 				}
429 			}
430 		}
431 	}
432 	OnChangedDelegateWrapper[] onChangedListeners;
433 
434 	/**
435 	 * Emitted whenever the selection has (possibly) changed. Please note that
436 	 * this signal is mostly a hint.  It may only be emitted once when a range
437 	 * of rows are selected, and it may occasionally be emitted when nothing
438 	 * has happened.
439 	 */
440 	gulong addOnChanged(void delegate(TreeSelection) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
441 	{
442 		auto wrapper = new OnChangedDelegateWrapper(dlg);
443 		wrapper.handlerId = Signals.connectData(
444 			this,
445 			"changed",
446 			cast(GCallback)&callBackChanged,
447 			cast(void*)wrapper,
448 			cast(GClosureNotify)&callBackChangedDestroy,
449 			connectFlags);
450 		return wrapper.handlerId;
451 	}
452 
453 	extern(C) static void callBackChanged(GtkTreeSelection* treeselectionStruct, OnChangedDelegateWrapper wrapper)
454 	{
455 		wrapper.dlg(wrapper.outer);
456 	}
457 
458 	extern(C) static void callBackChangedDestroy(OnChangedDelegateWrapper wrapper, GClosure* closure)
459 	{
460 		wrapper.remove(wrapper);
461 	}
462 }