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