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