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