1 /*
2  * This file is part of d-dazzle.
3  *
4  * d-dazzle 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  * d-dazzle 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 d-dazzle; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module dazzle.BindingGroup;
20 
21 private import dazzle.c.functions;
22 public  import dazzle.c.types;
23 private import glib.ConstructionException;
24 private import glib.Str;
25 private import gobject.Closure;
26 private import gobject.ObjectG;
27 
28 
29 /**
30  * #DzlBindingGroup manages to simplify the process of binding
31  * many properties from a #GObject as a group. As such there is no API
32  * to unbind a property from the group.
33  * 
34  * In particular, this allows you to change the source instance for the
35  * bindings. This automatically causes the unbinding of the properties
36  * from the old instance and binding to the new instance.
37  * 
38  * This should not be confused with #GtkBindingGroup.
39  */
40 public class BindingGroup : ObjectG
41 {
42 	/** the main Gtk struct */
43 	protected DzlBindingGroup* dzlBindingGroup;
44 
45 	/** Get the main Gtk struct */
46 	public DzlBindingGroup* getBindingGroupStruct(bool transferOwnership = false)
47 	{
48 		if (transferOwnership)
49 			ownedRef = false;
50 		return dzlBindingGroup;
51 	}
52 
53 	/** the main Gtk struct as a void* */
54 	protected override void* getStruct()
55 	{
56 		return cast(void*)dzlBindingGroup;
57 	}
58 
59 	/**
60 	 * Sets our main struct and passes it to the parent class.
61 	 */
62 	public this (DzlBindingGroup* dzlBindingGroup, bool ownedRef = false)
63 	{
64 		this.dzlBindingGroup = dzlBindingGroup;
65 		super(cast(GObject*)dzlBindingGroup, ownedRef);
66 	}
67 
68 
69 	/** */
70 	public static GType getType()
71 	{
72 		return dzl_binding_group_get_type();
73 	}
74 
75 	/**
76 	 * Creates a new #DzlBindingGroup.
77 	 *
78 	 * Returns: a new #DzlBindingGroup
79 	 *
80 	 * Throws: ConstructionException GTK+ fails to create the object.
81 	 */
82 	public this()
83 	{
84 		auto p = dzl_binding_group_new();
85 
86 		if(p is null)
87 		{
88 			throw new ConstructionException("null returned by new");
89 		}
90 
91 		this(cast(DzlBindingGroup*) p, true);
92 	}
93 
94 	/**
95 	 * Creates a binding between @source_property on the source object
96 	 * and @target_property on @target. Whenever the @source_property
97 	 * is changed the @target_property is updated using the same value.
98 	 * The binding flags #G_BINDING_SYNC_CREATE is automatically specified.
99 	 *
100 	 * See: g_object_bind_property().
101 	 *
102 	 * Params:
103 	 *     sourceProperty = the property on the source to bind
104 	 *     target = the target #GObject
105 	 *     targetProperty = the property on @target to bind
106 	 *     flags = the flags used to create the #GBinding
107 	 */
108 	public void bind(string sourceProperty, ObjectG target, string targetProperty, GBindingFlags flags)
109 	{
110 		dzl_binding_group_bind(dzlBindingGroup, Str.toStringz(sourceProperty), (target is null) ? null : target.getObjectGStruct(), Str.toStringz(targetProperty), flags);
111 	}
112 
113 	/**
114 	 * Creates a binding between @source_property on the source object and
115 	 * @target_property on @target, allowing you to set the transformation
116 	 * functions to be used by the binding. The binding flags
117 	 * #G_BINDING_SYNC_CREATE is automatically specified.
118 	 *
119 	 * See: g_object_bind_property_full().
120 	 *
121 	 * Params:
122 	 *     sourceProperty = the property on the source to bind
123 	 *     target = the target #GObject
124 	 *     targetProperty = the property on @target to bind
125 	 *     flags = the flags used to create the #GBinding
126 	 *     transformTo = the transformation function
127 	 *         from the source object to the @target, or %NULL to use the default
128 	 *     transformFrom = the transformation function
129 	 *         from the @target to the source object, or %NULL to use the default
130 	 *     userData = custom data to be passed to the transformation
131 	 *         functions, or %NULL
132 	 *     userDataDestroy = function to be called when disposing the binding,
133 	 *         to free the resources used by the transformation functions
134 	 */
135 	public void bindFull(string sourceProperty, ObjectG target, string targetProperty, GBindingFlags flags, GBindingTransformFunc transformTo, GBindingTransformFunc transformFrom, void* userData, GDestroyNotify userDataDestroy)
136 	{
137 		dzl_binding_group_bind_full(dzlBindingGroup, Str.toStringz(sourceProperty), (target is null) ? null : target.getObjectGStruct(), Str.toStringz(targetProperty), flags, transformTo, transformFrom, userData, userDataDestroy);
138 	}
139 
140 	/**
141 	 * Creates a binding between @source_property on the source object and
142 	 * @target_property on @target, allowing you to set the transformation
143 	 * functions to be used by the binding. The binding flags
144 	 * #G_BINDING_SYNC_CREATE is automatically specified.
145 	 *
146 	 * This function is the language bindings friendly version of
147 	 * dzl_binding_group_bind_property_full(), using #GClosures
148 	 * instead of function pointers.
149 	 *
150 	 * See: g_object_bind_property_with_closures().
151 	 *
152 	 * Params:
153 	 *     sourceProperty = the property on the source to bind
154 	 *     target = the target #GObject
155 	 *     targetProperty = the property on @target to bind
156 	 *     flags = the flags used to create the #GBinding
157 	 *     transformTo = a #GClosure wrapping the
158 	 *         transformation function from the source object to the @target,
159 	 *         or %NULL to use the default
160 	 *     transformFrom = a #GClosure wrapping the
161 	 *         transformation function from the @target to the source object,
162 	 *         or %NULL to use the default
163 	 */
164 	public void bindWithClosures(string sourceProperty, ObjectG target, string targetProperty, GBindingFlags flags, Closure transformTo, Closure transformFrom)
165 	{
166 		dzl_binding_group_bind_with_closures(dzlBindingGroup, Str.toStringz(sourceProperty), (target is null) ? null : target.getObjectGStruct(), Str.toStringz(targetProperty), flags, (transformTo is null) ? null : transformTo.getClosureStruct(), (transformFrom is null) ? null : transformFrom.getClosureStruct());
167 	}
168 
169 	/**
170 	 * Gets the source object used for binding properties.
171 	 *
172 	 * Returns: the source object.
173 	 */
174 	public ObjectG getSource()
175 	{
176 		auto p = dzl_binding_group_get_source(dzlBindingGroup);
177 
178 		if(p is null)
179 		{
180 			return null;
181 		}
182 
183 		return ObjectG.getDObject!(ObjectG)(cast(GObject*) p);
184 	}
185 
186 	/**
187 	 * Sets @source as the source object used for creating property
188 	 * bindings. If there is already a source object all bindings from it
189 	 * will be removed.
190 	 *
191 	 * Note: All properties that have been bound must exist on @source.
192 	 *
193 	 * Params:
194 	 *     source = the source #GObject
195 	 */
196 	public void setSource(ObjectG source)
197 	{
198 		dzl_binding_group_set_source(dzlBindingGroup, (source is null) ? null : source.getObjectGStruct());
199 	}
200 }