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 gstreamer.ChildProxyT;
26 
27 public  import glib.Str;
28 public  import gobject.ObjectG;
29 public  import gobject.ParamSpec;
30 public  import gobject.Signals;
31 public  import gobject.Value;
32 public  import gstreamerc.gstreamer;
33 public  import gstreamerc.gstreamertypes;
34 public  import gtkc.gdktypes;
35 public  import std.algorithm;
36 
37 
38 /**
39  * This interface abstracts handling of property sets for elements with
40  * children. Imagine elements such as mixers or polyphonic generators. They all
41  * have multiple #GstPad or some kind of voice objects. Another use case are
42  * container elements like #GstBin.
43  * The element implementing the interface acts as a parent for those child
44  * objects.
45  * 
46  * By implementing this interface the child properties can be accessed from the
47  * parent element by using gst_child_proxy_get() and gst_child_proxy_set().
48  * 
49  * Property names are written as "child-name::property-name". The whole naming
50  * scheme is recursive. Thus "child1::child2::property" is valid too, if
51  * "child1" and "child2" implement the #GstChildProxy interface.
52  */
53 public template ChildProxyT(TStruct)
54 {
55 	/** Get the main Gtk struct */
56 	public GstChildProxy* getChildProxyStruct()
57 	{
58 		return cast(GstChildProxy*)getStruct();
59 	}
60 
61 
62 	/**
63 	 * Emits the "child-added" signal.
64 	 *
65 	 * Params:
66 	 *     child = the newly added child
67 	 *     name = the name of the new child
68 	 */
69 	public void childAdded(ObjectG child, string name)
70 	{
71 		gst_child_proxy_child_added(getChildProxyStruct(), (child is null) ? null : child.getObjectGStruct(), Str.toStringz(name));
72 	}
73 
74 	/**
75 	 * Emits the "child-removed" signal.
76 	 *
77 	 * Params:
78 	 *     child = the removed child
79 	 *     name = the name of the old child
80 	 */
81 	public void childRemoved(ObjectG child, string name)
82 	{
83 		gst_child_proxy_child_removed(getChildProxyStruct(), (child is null) ? null : child.getObjectGStruct(), Str.toStringz(name));
84 	}
85 
86 	/**
87 	 * Fetches a child by its number.
88 	 *
89 	 * Params:
90 	 *     index = the child's position in the child list
91 	 *
92 	 * Return: the child object or %NULL if
93 	 *     not found (index too high). Unref after usage.
94 	 *
95 	 *     MT safe.
96 	 */
97 	public ObjectG getChildByIndex(uint index)
98 	{
99 		auto p = gst_child_proxy_get_child_by_index(getChildProxyStruct(), index);
100 		
101 		if(p is null)
102 		{
103 			return null;
104 		}
105 		
106 		return ObjectG.getDObject!(ObjectG)(cast(GObject*) p, true);
107 	}
108 
109 	/**
110 	 * Looks up a child element by the given name.
111 	 *
112 	 * This virtual method has a default implementation that uses #GstObject
113 	 * together with gst_object_get_name(). If the interface is to be used with
114 	 * #GObjects, this methods needs to be overridden.
115 	 *
116 	 * Params:
117 	 *     name = the child's name
118 	 *
119 	 * Return: the child object or %NULL if
120 	 *     not found. Unref after usage.
121 	 *
122 	 *     MT safe.
123 	 */
124 	public ObjectG getChildByName(string name)
125 	{
126 		auto p = gst_child_proxy_get_child_by_name(getChildProxyStruct(), Str.toStringz(name));
127 		
128 		if(p is null)
129 		{
130 			return null;
131 		}
132 		
133 		return ObjectG.getDObject!(ObjectG)(cast(GObject*) p, true);
134 	}
135 
136 	/**
137 	 * Gets the number of child objects this parent contains.
138 	 *
139 	 * Return: the number of child objects
140 	 *
141 	 *     MT safe.
142 	 */
143 	public uint getChildrenCount()
144 	{
145 		return gst_child_proxy_get_children_count(getChildProxyStruct());
146 	}
147 
148 	/**
149 	 * Gets a single property using the GstChildProxy mechanism.
150 	 * You are responsible for freeing it by calling g_value_unset()
151 	 *
152 	 * Params:
153 	 *     name = name of the property
154 	 *     value = a #GValue that should take the result.
155 	 */
156 	public void childGetProperty(string name, out Value value)
157 	{
158 		GValue* outvalue = gMalloc!GValue();
159 		
160 		gst_child_proxy_get_property(getChildProxyStruct(), Str.toStringz(name), outvalue);
161 		
162 		value = ObjectG.getDObject!(Value)(outvalue, true);
163 	}
164 
165 	/**
166 	 * Gets properties of the parent object and its children.
167 	 *
168 	 * Params:
169 	 *     firstPropertyName = name of the first property to get
170 	 *     varArgs = return location for the first property, followed optionally by more name/return location pairs, followed by %NULL
171 	 */
172 	public void childGetValist(string firstPropertyName, void* varArgs)
173 	{
174 		gst_child_proxy_get_valist(getChildProxyStruct(), Str.toStringz(firstPropertyName), varArgs);
175 	}
176 
177 	/**
178 	 * Looks up which object and #GParamSpec would be effected by the given @name.
179 	 *
180 	 * MT safe.
181 	 *
182 	 * Params:
183 	 *     name = name of the property to look up
184 	 *     target = pointer to a #GObject that
185 	 *         takes the real object to set property on
186 	 *     pspec = pointer to take the #GParamSpec
187 	 *         describing the property
188 	 *
189 	 * Return: %TRUE if @target and @pspec could be found. %FALSE otherwise. In that
190 	 *     case the values for @pspec and @target are not modified. Unref @target after
191 	 *     usage. For plain GObjects @target is the same as @object.
192 	 */
193 	public bool lookup(string name, out ObjectG target, out ParamSpec pspec)
194 	{
195 		GObject* outtarget = null;
196 		GParamSpec* outpspec = null;
197 		
198 		auto p = gst_child_proxy_lookup(getChildProxyStruct(), Str.toStringz(name), &outtarget, &outpspec) != 0;
199 		
200 		target = ObjectG.getDObject!(ObjectG)(outtarget);
201 		pspec = ObjectG.getDObject!(ParamSpec)(outpspec);
202 		
203 		return p;
204 	}
205 
206 	/**
207 	 * Sets a single property using the GstChildProxy mechanism.
208 	 *
209 	 * Params:
210 	 *     name = name of the property to set
211 	 *     value = new #GValue for the property
212 	 */
213 	public void childSetProperty(string name, Value value)
214 	{
215 		gst_child_proxy_set_property(getChildProxyStruct(), Str.toStringz(name), (value is null) ? null : value.getValueStruct());
216 	}
217 
218 	/**
219 	 * Sets properties of the parent object and its children.
220 	 *
221 	 * Params:
222 	 *     firstPropertyName = name of the first property to set
223 	 *     varArgs = value for the first property, followed optionally by more name/value pairs, followed by %NULL
224 	 */
225 	public void childSetValist(string firstPropertyName, void* varArgs)
226 	{
227 		gst_child_proxy_set_valist(getChildProxyStruct(), Str.toStringz(firstPropertyName), varArgs);
228 	}
229 
230 	protected class OnChildAddedDelegateWrapper
231 	{
232 		void delegate(ObjectG, string, ChildProxyIF) dlg;
233 		gulong handlerId;
234 		ConnectFlags flags;
235 		this(void delegate(ObjectG, string, ChildProxyIF) dlg, gulong handlerId, ConnectFlags flags)
236 		{
237 			this.dlg = dlg;
238 			this.handlerId = handlerId;
239 			this.flags = flags;
240 		}
241 	}
242 	protected OnChildAddedDelegateWrapper[] onChildAddedListeners;
243 
244 	/**
245 	 * Will be emitted after the @object was added to the @child_proxy.
246 	 *
247 	 * Params:
248 	 *     object = the #GObject that was added
249 	 *     name = the name of the new child
250 	 */
251 	gulong addOnChildAdded(void delegate(ObjectG, string, ChildProxyIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
252 	{
253 		onChildAddedListeners ~= new OnChildAddedDelegateWrapper(dlg, 0, connectFlags);
254 		onChildAddedListeners[onChildAddedListeners.length - 1].handlerId = Signals.connectData(
255 			this,
256 			"child-added",
257 			cast(GCallback)&callBackChildAdded,
258 			cast(void*)onChildAddedListeners[onChildAddedListeners.length - 1],
259 			cast(GClosureNotify)&callBackChildAddedDestroy,
260 			connectFlags);
261 		return onChildAddedListeners[onChildAddedListeners.length - 1].handlerId;
262 	}
263 	
264 	extern(C) static void callBackChildAdded(GstChildProxy* childproxyStruct, GObject* object, char* name,OnChildAddedDelegateWrapper wrapper)
265 	{
266 		wrapper.dlg(ObjectG.getDObject!(ObjectG)(object), Str.toString(name), wrapper.outer);
267 	}
268 	
269 	extern(C) static void callBackChildAddedDestroy(OnChildAddedDelegateWrapper wrapper, GClosure* closure)
270 	{
271 		wrapper.outer.internalRemoveOnChildAdded(wrapper);
272 	}
273 
274 	protected void internalRemoveOnChildAdded(OnChildAddedDelegateWrapper source)
275 	{
276 		foreach(index, wrapper; onChildAddedListeners)
277 		{
278 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
279 			{
280 				onChildAddedListeners[index] = null;
281 				onChildAddedListeners = std.algorithm.remove(onChildAddedListeners, index);
282 				break;
283 			}
284 		}
285 	}
286 	
287 
288 	protected class OnChildRemovedDelegateWrapper
289 	{
290 		void delegate(ObjectG, string, ChildProxyIF) dlg;
291 		gulong handlerId;
292 		ConnectFlags flags;
293 		this(void delegate(ObjectG, string, ChildProxyIF) dlg, gulong handlerId, ConnectFlags flags)
294 		{
295 			this.dlg = dlg;
296 			this.handlerId = handlerId;
297 			this.flags = flags;
298 		}
299 	}
300 	protected OnChildRemovedDelegateWrapper[] onChildRemovedListeners;
301 
302 	/**
303 	 * Will be emitted after the @object was removed from the @child_proxy.
304 	 *
305 	 * Params:
306 	 *     object = the #GObject that was removed
307 	 *     name = the name of the old child
308 	 */
309 	gulong addOnChildRemoved(void delegate(ObjectG, string, ChildProxyIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
310 	{
311 		onChildRemovedListeners ~= new OnChildRemovedDelegateWrapper(dlg, 0, connectFlags);
312 		onChildRemovedListeners[onChildRemovedListeners.length - 1].handlerId = Signals.connectData(
313 			this,
314 			"child-removed",
315 			cast(GCallback)&callBackChildRemoved,
316 			cast(void*)onChildRemovedListeners[onChildRemovedListeners.length - 1],
317 			cast(GClosureNotify)&callBackChildRemovedDestroy,
318 			connectFlags);
319 		return onChildRemovedListeners[onChildRemovedListeners.length - 1].handlerId;
320 	}
321 	
322 	extern(C) static void callBackChildRemoved(GstChildProxy* childproxyStruct, GObject* object, char* name,OnChildRemovedDelegateWrapper wrapper)
323 	{
324 		wrapper.dlg(ObjectG.getDObject!(ObjectG)(object), Str.toString(name), wrapper.outer);
325 	}
326 	
327 	extern(C) static void callBackChildRemovedDestroy(OnChildRemovedDelegateWrapper wrapper, GClosure* closure)
328 	{
329 		wrapper.outer.internalRemoveOnChildRemoved(wrapper);
330 	}
331 
332 	protected void internalRemoveOnChildRemoved(OnChildRemovedDelegateWrapper source)
333 	{
334 		foreach(index, wrapper; onChildRemovedListeners)
335 		{
336 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
337 			{
338 				onChildRemovedListeners[index] = null;
339 				onChildRemovedListeners = std.algorithm.remove(onChildRemovedListeners, index);
340 				break;
341 			}
342 		}
343 	}
344 	
345 }