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.SelectionModelT;
26 
27 public  import gobject.ObjectG;
28 public  import gobject.Signals;
29 public  import gtk.Bitset;
30 public  import gtk.c.functions;
31 public  import gtk.c.types;
32 public  import std.algorithm;
33 
34 
35 /**
36  * `GtkSelectionModel` is an interface that add support for selection to list models.
37  * 
38  * This support is then used by widgets using list models to add the ability
39  * to select and unselect various items.
40  * 
41  * GTK provides default implementations of the most common selection modes such
42  * as [class@Gtk.SingleSelection], so you will only need to implement this
43  * interface if you want detailed control about how selections should be handled.
44  * 
45  * A `GtkSelectionModel` supports a single boolean per item indicating if an item is
46  * selected or not. This can be queried via [method@Gtk.SelectionModel.is_selected].
47  * When the selected state of one or more items changes, the model will emit the
48  * [signal@Gtk.SelectionModel::selection-changed] signal by calling the
49  * [method@Gtk.SelectionModel.selection_changed] function. The positions given
50  * in that signal may have their selection state changed, though that is not a
51  * requirement. If new items added to the model via the ::items-changed signal
52  * are selected or not is up to the implementation.
53  * 
54  * Note that items added via ::items-changed may already be selected and no
55  * [Gtk.SelectionModel::selection-changed] will be emitted for them. So to
56  * track which items are selected, it is necessary to listen to both signals.
57  * 
58  * Additionally, the interface can expose functionality to select and unselect
59  * items. If these functions are implemented, GTK's list widgets will allow users
60  * to select and unselect items. However, `GtkSelectionModel`s are free to only
61  * implement them partially or not at all. In that case the widgets will not
62  * support the unimplemented operations.
63  * 
64  * When selecting or unselecting is supported by a model, the return values of
65  * the selection functions do *not* indicate if selection or unselection happened.
66  * They are only meant to indicate complete failure, like when this mode of
67  * selecting is not supported by the model.
68  * 
69  * Selections may happen asynchronously, so the only reliable way to find out
70  * when an item was selected is to listen to the signals that indicate selection.
71  */
72 public template SelectionModelT(TStruct)
73 {
74 	/** Get the main Gtk struct */
75 	public GtkSelectionModel* getSelectionModelStruct(bool transferOwnership = false)
76 	{
77 		if (transferOwnership)
78 			ownedRef = false;
79 		return cast(GtkSelectionModel*)getStruct();
80 	}
81 
82 
83 	/**
84 	 * Gets the set containing all currently selected items in the model.
85 	 *
86 	 * This function may be slow, so if you are only interested in single item,
87 	 * consider using [method@Gtk.SelectionModel.is_selected] or if you are only
88 	 * interested in a few, consider [method@Gtk.SelectionModel.get_selection_in_range].
89 	 *
90 	 * Returns: a `GtkBitset` containing all the values currently
91 	 *     selected in @model. If no items are selected, the bitset is empty.
92 	 *     The bitset must not be modified.
93 	 */
94 	public Bitset getSelection()
95 	{
96 		auto __p = gtk_selection_model_get_selection(getSelectionModelStruct());
97 
98 		if(__p is null)
99 		{
100 			return null;
101 		}
102 
103 		return ObjectG.getDObject!(Bitset)(cast(GtkBitset*) __p, true);
104 	}
105 
106 	/**
107 	 * Gets the set of selected items in a range.
108 	 *
109 	 * This function is an optimization for
110 	 * [method@Gtk.SelectionModel.get_selection] when you are only
111 	 * interested in part of the model's selected state. A common use
112 	 * case is in response to the [signal@Gtk.SelectionModel::selection-changed]
113 	 * signal.
114 	 *
115 	 * Params:
116 	 *     position = start of the queired range
117 	 *     nItems = number of items in the queried range
118 	 *
119 	 * Returns: A `GtkBitset` that matches the selection state
120 	 *     for the given range with all other values being undefined.
121 	 *     The bitset must not be modified.
122 	 */
123 	public Bitset getSelectionInRange(uint position, uint nItems)
124 	{
125 		auto __p = gtk_selection_model_get_selection_in_range(getSelectionModelStruct(), position, nItems);
126 
127 		if(__p is null)
128 		{
129 			return null;
130 		}
131 
132 		return ObjectG.getDObject!(Bitset)(cast(GtkBitset*) __p, true);
133 	}
134 
135 	/**
136 	 * Checks if the given item is selected.
137 	 *
138 	 * Params:
139 	 *     position = the position of the item to query
140 	 *
141 	 * Returns: %TRUE if the item is selected
142 	 */
143 	public bool isSelected(uint position)
144 	{
145 		return gtk_selection_model_is_selected(getSelectionModelStruct(), position) != 0;
146 	}
147 
148 	/**
149 	 * Requests to select all items in the model.
150 	 *
151 	 * Returns: %TRUE if this action was supported and no fallback should be
152 	 *     tried. This does not mean that all items are now selected.
153 	 */
154 	public bool selectAll()
155 	{
156 		return gtk_selection_model_select_all(getSelectionModelStruct()) != 0;
157 	}
158 
159 	/**
160 	 * Requests to select an item in the model.
161 	 *
162 	 * Params:
163 	 *     position = the position of the item to select
164 	 *     unselectRest = whether previously selected items should be unselected
165 	 *
166 	 * Returns: %TRUE if this action was supported and no fallback should be
167 	 *     tried. This does not mean the item was selected.
168 	 */
169 	public bool selectItem(uint position, bool unselectRest)
170 	{
171 		return gtk_selection_model_select_item(getSelectionModelStruct(), position, unselectRest) != 0;
172 	}
173 
174 	/**
175 	 * Requests to select a range of items in the model.
176 	 *
177 	 * Params:
178 	 *     position = the first item to select
179 	 *     nItems = the number of items to select
180 	 *     unselectRest = whether previously selected items should be unselected
181 	 *
182 	 * Returns: %TRUE if this action was supported and no fallback should be
183 	 *     tried. This does not mean the range was selected.
184 	 */
185 	public bool selectRange(uint position, uint nItems, bool unselectRest)
186 	{
187 		return gtk_selection_model_select_range(getSelectionModelStruct(), position, nItems, unselectRest) != 0;
188 	}
189 
190 	/**
191 	 * Helper function for implementations of `GtkSelectionModel`.
192 	 *
193 	 * Call this when a the selection changes to emit the
194 	 * [signal@Gtk.SelectionModel::selection-changed] signal.
195 	 *
196 	 * Params:
197 	 *     position = the first changed item
198 	 *     nItems = the number of changed items
199 	 */
200 	public void selectionChanged(uint position, uint nItems)
201 	{
202 		gtk_selection_model_selection_changed(getSelectionModelStruct(), position, nItems);
203 	}
204 
205 	/**
206 	 * Make selection changes.
207 	 *
208 	 * This is the most advanced selection updating method that allows
209 	 * the most fine-grained control over selection changes. If you can,
210 	 * you should try the simpler versions, as implementations are more
211 	 * likely to implement support for those.
212 	 *
213 	 * Requests that the selection state of all positions set in @mask
214 	 * be updated to the respective value in the @selected bitmask.
215 	 *
216 	 * In pseudocode, it would look something like this:
217 	 *
218 	 * ```c
219 	 * for (i = 0; i < n_items; i++)
220 	 * {
221 	 * // don't change values not in the mask
222 	 * if (!gtk_bitset_contains (mask, i))
223 	 * continue;
224 	 *
225 	 * if (gtk_bitset_contains (selected, i))
226 	 * select_item (i);
227 	 * else
228 	 * unselect_item (i);
229 	 * }
230 	 *
231 	 * gtk_selection_model_selection_changed (model,
232 	 * first_changed_item,
233 	 * n_changed_items);
234 	 * ```
235 	 *
236 	 * @mask and @selected must not be modified. They may refer to the
237 	 * same bitset, which would mean that every item in the set should
238 	 * be selected.
239 	 *
240 	 * Params:
241 	 *     selected = bitmask specifying if items should be selected or
242 	 *         unselected
243 	 *     mask = bitmask specifying which items should be updated
244 	 *
245 	 * Returns: %TRUE if this action was supported and no fallback should be
246 	 *     tried. This does not mean that all items were updated according
247 	 *     to the inputs.
248 	 */
249 	public bool setSelection(Bitset selected, Bitset mask)
250 	{
251 		return gtk_selection_model_set_selection(getSelectionModelStruct(), (selected is null) ? null : selected.getBitsetStruct(), (mask is null) ? null : mask.getBitsetStruct()) != 0;
252 	}
253 
254 	/**
255 	 * Requests to unselect all items in the model.
256 	 *
257 	 * Returns: %TRUE if this action was supported and no fallback should be
258 	 *     tried. This does not mean that all items are now unselected.
259 	 */
260 	public bool unselectAll()
261 	{
262 		return gtk_selection_model_unselect_all(getSelectionModelStruct()) != 0;
263 	}
264 
265 	/**
266 	 * Requests to unselect an item in the model.
267 	 *
268 	 * Params:
269 	 *     position = the position of the item to unselect
270 	 *
271 	 * Returns: %TRUE if this action was supported and no fallback should be
272 	 *     tried. This does not mean the item was unselected.
273 	 */
274 	public bool unselectItem(uint position)
275 	{
276 		return gtk_selection_model_unselect_item(getSelectionModelStruct(), position) != 0;
277 	}
278 
279 	/**
280 	 * Requests to unselect a range of items in the model.
281 	 *
282 	 * Params:
283 	 *     position = the first item to unselect
284 	 *     nItems = the number of items to unselect
285 	 *
286 	 * Returns: %TRUE if this action was supported and no fallback should be
287 	 *     tried. This does not mean the range was unselected.
288 	 */
289 	public bool unselectRange(uint position, uint nItems)
290 	{
291 		return gtk_selection_model_unselect_range(getSelectionModelStruct(), position, nItems) != 0;
292 	}
293 
294 	/**
295 	 * Emitted when the selection state of some of the items in @model changes.
296 	 *
297 	 * Note that this signal does not specify the new selection state of the
298 	 * items, they need to be queried manually. It is also not necessary for
299 	 * a model to change the selection state of any of the items in the selection
300 	 * model, though it would be rather useless to emit such a signal.
301 	 *
302 	 * Params:
303 	 *     position = The first item that may have changed
304 	 *     nItems = number of items with changes
305 	 */
306 	gulong addOnSelectionChanged(void delegate(uint, uint, SelectionModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
307 	{
308 		return Signals.connect(this, "selection-changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
309 	}
310 }