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 gio.ListModelT;
26 
27 public  import gobject.ObjectG;
28 public  import gobject.Signals;
29 public  import gtkc.gdktypes;
30 public  import gtkc.gio;
31 public  import gtkc.giotypes;
32 public  import std.algorithm;
33 
34 
35 /**
36  * #GListModel is an interface that represents a mutable list of
37  * #GObjects. Its main intention is as a model for various widgets in
38  * user interfaces, such as list views, but it can also be used as a
39  * convenient method of returning lists of data, with support for
40  * updates.
41  * 
42  * Each object in the list may also report changes in itself via some
43  * mechanism (normally the #GObject::notify signal).  Taken together
44  * with the #GListModel::items-changed signal, this provides for a list
45  * that can change its membership, and in which the members can change
46  * their individual properties.
47  * 
48  * A good example would be the list of visible wireless network access
49  * points, where each access point can report dynamic properties such as
50  * signal strength.
51  * 
52  * It is important to note that the #GListModel itself does not report
53  * changes to the individual items.  It only reports changes to the list
54  * membership.  If you want to observe changes to the objects themselves
55  * then you need to connect signals to the objects that you are
56  * interested in.
57  * 
58  * All items in a #GListModel are of (or derived from) the same type.
59  * g_list_model_get_item_type() returns that type.  The type may be an
60  * interface, in which case all objects in the list must implement it.
61  * 
62  * The semantics are close to that of an array:
63  * g_list_model_get_n_items() returns the number of items in the list and
64  * g_list_model_get_item() returns an item at a (0-based) position. In
65  * order to allow implementations to calculate the list length lazily,
66  * you can also iterate over items: starting from 0, repeatedly call
67  * g_list_model_get_item() until it returns %NULL.
68  * 
69  * An implementation may create objects lazily, but must take care to
70  * return the same object for a given position until all references to
71  * it are gone.
72  * 
73  * On the other side, a consumer is expected only to hold references on
74  * objects that are currently "user visible", in order to faciliate the
75  * maximum level of laziness in the implementation of the list and to
76  * reduce the required number of signal connections at a given time.
77  * 
78  * This interface is intended only to be used from a single thread.  The
79  * thread in which it is appropriate to use it depends on the particular
80  * implementation, but typically it will be from the thread that owns
81  * the [thread-default main context][g-main-context-push-thread-default]
82  * in effect at the time that the model was created.
83  */
84 public template ListModelT(TStruct)
85 {
86 	/** Get the main Gtk struct */
87 	public GListModel* getListModelStruct()
88 	{
89 		return cast(GListModel*)getStruct();
90 	}
91 
92 
93 	/**
94 	 * Get the item at @position. If @position is greater than the number of
95 	 * items in @list, %NULL is returned.
96 	 *
97 	 * %NULL is never returned for an index that is smaller than the length
98 	 * of the list.  See g_list_model_get_n_items().
99 	 *
100 	 * Params:
101 	 *     position = the position of the item to fetch
102 	 *
103 	 * Return: the item at @position.
104 	 *
105 	 * Since: 2.44
106 	 */
107 	public ObjectG getItem(uint position)
108 	{
109 		auto p = g_list_model_get_item(getListModelStruct(), position);
110 		
111 		if(p is null)
112 		{
113 			return null;
114 		}
115 		
116 		return ObjectG.getDObject!(ObjectG)(cast(GObject*) p, true);
117 	}
118 
119 	/**
120 	 * Gets the type of the items in @list. All items returned from
121 	 * g_list_model_get_type() are of that type or a subtype, or are an
122 	 * implementation of that interface.
123 	 *
124 	 * The item type of a #GListModel can not change during the life of the
125 	 * model.
126 	 *
127 	 * Return: the #GType of the items contained in @list.
128 	 *
129 	 * Since: 2.44
130 	 */
131 	public GType getItemType()
132 	{
133 		return g_list_model_get_item_type(getListModelStruct());
134 	}
135 
136 	/**
137 	 * Gets the number of items in @list.
138 	 *
139 	 * Depending on the model implementation, calling this function may be
140 	 * less efficient than iterating the list with increasing values for
141 	 * @position until g_list_model_get_item() returns %NULL.
142 	 *
143 	 * Return: the number of items in @list.
144 	 *
145 	 * Since: 2.44
146 	 */
147 	public uint getNItems()
148 	{
149 		return g_list_model_get_n_items(getListModelStruct());
150 	}
151 
152 	/**
153 	 * Get the item at @position. If @position is greater than the number of
154 	 * items in @list, %NULL is returned.
155 	 *
156 	 * %NULL is never returned for an index that is smaller than the length
157 	 * of the list.  See g_list_model_get_n_items().
158 	 *
159 	 * Params:
160 	 *     position = the position of the item to fetch
161 	 *
162 	 * Return: the object at @position.
163 	 *
164 	 * Since: 2.44
165 	 */
166 	public ObjectG getObject(uint position)
167 	{
168 		auto p = g_list_model_get_object(getListModelStruct(), position);
169 		
170 		if(p is null)
171 		{
172 			return null;
173 		}
174 		
175 		return ObjectG.getDObject!(ObjectG)(cast(GObject*) p, true);
176 	}
177 
178 	/**
179 	 * Emits the #GListModel::items-changed signal on @list.
180 	 *
181 	 * This function should only be called by classes implementing
182 	 * #GListModel. It has to be called after the internal representation
183 	 * of @list has been updated, because handlers connected to this signal
184 	 * might query the new state of the list.
185 	 *
186 	 * Implementations must only make changes to the model (as visible to
187 	 * its consumer) in places that will not cause problems for that
188 	 * consumer.  For models that are driven directly by a write API (such
189 	 * as #GListStore), changes can be reported in response to uses of that
190 	 * API.  For models that represent remote data, changes should only be
191 	 * made from a fresh mainloop dispatch.  It is particularly not
192 	 * permitted to make changes in response to a call to the #GListModel
193 	 * consumer API.
194 	 *
195 	 * Stated another way: in general, it is assumed that code making a
196 	 * series of accesses to the model via the API, without returning to the
197 	 * mainloop, and without calling other code, will continue to view the
198 	 * same contents of the model.
199 	 *
200 	 * Params:
201 	 *     position = the position at which @list changed
202 	 *     removed = the number of items removed
203 	 *     added = the number of items added
204 	 *
205 	 * Since: 2.44
206 	 */
207 	public void itemsChanged(uint position, uint removed, uint added)
208 	{
209 		g_list_model_items_changed(getListModelStruct(), position, removed, added);
210 	}
211 
212 	protected class OnItemsChangedDelegateWrapper
213 	{
214 		void delegate(uint, uint, uint, ListModelIF) dlg;
215 		gulong handlerId;
216 		ConnectFlags flags;
217 		this(void delegate(uint, uint, uint, ListModelIF) dlg, gulong handlerId, ConnectFlags flags)
218 		{
219 			this.dlg = dlg;
220 			this.handlerId = handlerId;
221 			this.flags = flags;
222 		}
223 	}
224 	protected OnItemsChangedDelegateWrapper[] onItemsChangedListeners;
225 
226 	/**
227 	 * This signal is emitted whenever items were added or removed to
228 	 * @list. At @position, @removed items were removed and @added items
229 	 * were added in their place.
230 	 *
231 	 * Params:
232 	 *     position = the position at which @list changed
233 	 *     removed = the number of items removed
234 	 *     added = the number of items added
235 	 *
236 	 * Since: 2.44
237 	 */
238 	gulong addOnItemsChanged(void delegate(uint, uint, uint, ListModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
239 	{
240 		onItemsChangedListeners ~= new OnItemsChangedDelegateWrapper(dlg, 0, connectFlags);
241 		onItemsChangedListeners[onItemsChangedListeners.length - 1].handlerId = Signals.connectData(
242 			this,
243 			"items-changed",
244 			cast(GCallback)&callBackItemsChanged,
245 			cast(void*)onItemsChangedListeners[onItemsChangedListeners.length - 1],
246 			cast(GClosureNotify)&callBackItemsChangedDestroy,
247 			connectFlags);
248 		return onItemsChangedListeners[onItemsChangedListeners.length - 1].handlerId;
249 	}
250 	
251 	extern(C) static void callBackItemsChanged(GListModel* listmodelStruct, uint position, uint removed, uint added,OnItemsChangedDelegateWrapper wrapper)
252 	{
253 		wrapper.dlg(position, removed, added, wrapper.outer);
254 	}
255 	
256 	extern(C) static void callBackItemsChangedDestroy(OnItemsChangedDelegateWrapper wrapper, GClosure* closure)
257 	{
258 		wrapper.outer.internalRemoveOnItemsChanged(wrapper);
259 	}
260 
261 	protected void internalRemoveOnItemsChanged(OnItemsChangedDelegateWrapper source)
262 	{
263 		foreach(index, wrapper; onItemsChangedListeners)
264 		{
265 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
266 			{
267 				onItemsChangedListeners[index] = null;
268 				onItemsChangedListeners = std.algorithm.remove(onItemsChangedListeners, index);
269 				break;
270 			}
271 		}
272 	}
273 	
274 }