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.Expression;
26 
27 private import glib.Str;
28 private import gobject.ObjectG;
29 private import gobject.Value;
30 private import gtk.ExpressionWatch;
31 private import gtk.c.functions;
32 public  import gtk.c.types;
33 private import gtkd.Loader;
34 
35 
36 /**
37  * `GtkExpression` provides a way to describe references to values.
38  * 
39  * An important aspect of expressions is that the value can be obtained
40  * from a source that is several steps away. For example, an expression
41  * may describe ‘the value of property A of `object1`, which is itself the
42  * value of a property of `object2`’. And `object1` may not even exist yet
43  * at the time that the expression is created. This is contrast to `GObject`
44  * property bindings, which can only create direct connections between
45  * the properties of two objects that must both exist for the duration
46  * of the binding.
47  * 
48  * An expression needs to be "evaluated" to obtain the value that it currently
49  * refers to. An evaluation always happens in the context of a current object
50  * called `this` (it mirrors the behavior of object-oriented languages),
51  * which may or may not influence the result of the evaluation. Use
52  * [method@Gtk.Expression.evaluate] for evaluating an expression.
53  * 
54  * Various methods for defining expressions exist, from simple constants via
55  * [ctor@Gtk.ConstantExpression.new] to looking up properties in a `GObject`
56  * (even recursively) via [ctor@Gtk.PropertyExpression.new] or providing
57  * custom functions to transform and combine expressions via
58  * [ctor@Gtk.ClosureExpression.new].
59  * 
60  * Here is an example of a complex expression:
61  * 
62  * ```c
63  * color_expr = gtk_property_expression_new (GTK_TYPE_LIST_ITEM,
64  * NULL, "item");
65  * expression = gtk_property_expression_new (GTK_TYPE_COLOR,
66  * color_expr, "name");
67  * ```
68  * 
69  * when evaluated with `this` being a `GtkListItem`, it will obtain the
70  * "item" property from the `GtkListItem`, and then obtain the "name" property
71  * from the resulting object (which is assumed to be of type `GTK_TYPE_COLOR`).
72  * 
73  * A more concise way to describe this would be
74  * 
75  * ```
76  * this->item->name
77  * ```
78  * 
79  * The most likely place where you will encounter expressions is in the context
80  * of list models and list widgets using them. For example, `GtkDropDown` is
81  * evaluating a `GtkExpression` to obtain strings from the items in its model
82  * that it can then use to match against the contents of its search entry.
83  * `GtkStringFilter` is using a `GtkExpression` for similar reasons.
84  * 
85  * By default, expressions are not paying attention to changes and evaluation is
86  * just a snapshot of the current state at a given time. To get informed about
87  * changes, an expression needs to be "watched" via a [struct@Gtk.ExpressionWatch],
88  * which will cause a callback to be called whenever the value of the expression may
89  * have changed; [method@Gtk.Expression.watch] starts watching an expression, and
90  * [method@Gtk.ExpressionWatch.unwatch] stops.
91  * 
92  * Watches can be created for automatically updating the property of an object,
93  * similar to GObject's `GBinding` mechanism, by using [method@Gtk.Expression.bind].
94  * 
95  * ## GtkExpression in GObject properties
96  * 
97  * In order to use a `GtkExpression` as a `GObject` property, you must use the
98  * [id@gtk_param_spec_expression] when creating a `GParamSpec` to install in the
99  * `GObject` class being defined; for instance:
100  * 
101  * ```c
102  * obj_props[PROP_EXPRESSION] =
103  * gtk_param_spec_expression ("expression",
104  * "Expression",
105  * "The expression used by the widget",
106  * G_PARAM_READWRITE |
107  * G_PARAM_STATIC_STRINGS |
108  * G_PARAM_EXPLICIT_NOTIFY);
109  * ```
110  * 
111  * When implementing the `GObjectClass.set_property` and `GObjectClass.get_property`
112  * virtual functions, you must use [id@gtk_value_get_expression], to retrieve the
113  * stored `GtkExpression` from the `GValue` container, and [id@gtk_value_set_expression],
114  * to store the `GtkExpression` into the `GValue`; for instance:
115  * 
116  * ```c
117  * // in set_property()...
118  * case PROP_EXPRESSION:
119  * foo_widget_set_expression (foo, gtk_value_get_expression (value));
120  * break;
121  * 
122  * // in get_property()...
123  * case PROP_EXPRESSION:
124  * gtk_value_set_expression (value, foo->expression);
125  * break;
126  * ```
127  * 
128  * ## GtkExpression in .ui files
129  * 
130  * `GtkBuilder` has support for creating expressions. The syntax here can be used where
131  * a `GtkExpression` object is needed like in a `<property>` tag for an expression
132  * property, or in a `<binding>` tag to bind a property to an expression.
133  * 
134  * To create an property expression, use the `<lookup>` element. It can have a `type`
135  * attribute to specify the object type, and a `name` attribute to specify the property
136  * to look up. The content of `<lookup>` can either be an element specfiying the expression
137  * to use the object, or a string that specifies the name of the object to use.
138  * 
139  * Example:
140  * 
141  * ```xml
142  * <lookup name='search'>string_filter</lookup>
143  * ```
144  * 
145  * To create a constant expression, use the `<constant>` element. If the type attribute
146  * is specified, the element content is interpreted as a value of that type. Otherwise,
147  * it is assumed to be an object. For instance:
148  * 
149  * ```xml
150  * <constant>string_filter</constant>
151  * <constant type='gchararray'>Hello, world</constant>
152  * ```
153  * 
154  * To create a closure expression, use the `<closure>` element. The `type` and `function`
155  * attributes specify what function to use for the closure, the content of the element
156  * contains the expressions for the parameters. For instance:
157  * 
158  * ```xml
159  * <closure type='gchararray' function='combine_args_somehow'>
160  * <constant type='gchararray'>File size:</constant>
161  * <lookup type='GFile' name='size'>myfile</lookup>
162  * </closure>
163  * ```
164  */
165 public class Expression
166 {
167 	/** the main Gtk struct */
168 	protected GtkExpression* gtkExpression;
169 	protected bool ownedRef;
170 
171 	/** Get the main Gtk struct */
172 	public GtkExpression* getExpressionStruct(bool transferOwnership = false)
173 	{
174 		if (transferOwnership)
175 			ownedRef = false;
176 		return gtkExpression;
177 	}
178 
179 	/** the main Gtk struct as a void* */
180 	protected void* getStruct()
181 	{
182 		return cast(void*)gtkExpression;
183 	}
184 
185 	/**
186 	 * Sets our main struct and passes it to the parent class.
187 	 */
188 	public this (GtkExpression* gtkExpression, bool ownedRef = false)
189 	{
190 		this.gtkExpression = gtkExpression;
191 		this.ownedRef = ownedRef;
192 	}
193 
194 	~this ()
195 	{
196 		if ( Linker.isLoaded(LIBRARY_GTK) && ownedRef )
197 			gtk_expression_unref(gtkExpression);
198 	}
199 
200 
201 	/** */
202 	public static GType getType()
203 	{
204 		return gtk_expression_get_type();
205 	}
206 
207 	/**
208 	 * Bind `target`'s property named `property` to `self`.
209 	 *
210 	 * The value that `self` evaluates to is set via `g_object_set()` on
211 	 * `target`. This is repeated whenever `self` changes to ensure that
212 	 * the object's property stays synchronized with `self`.
213 	 *
214 	 * If `self`'s evaluation fails, `target`'s `property` is not updated.
215 	 * You can ensure that this doesn't happen by using a fallback
216 	 * expression.
217 	 *
218 	 * Note that this function takes ownership of `self`. If you want
219 	 * to keep it around, you should [method@Gtk.Expression.ref] it beforehand.
220 	 *
221 	 * Params:
222 	 *     target = the target object to bind to
223 	 *     property = name of the property on `target` to bind to
224 	 *     this_ = the this argument for
225 	 *         the evaluation of `self`
226 	 *
227 	 * Returns: a `GtkExpressionWatch`
228 	 */
229 	public ExpressionWatch bind(ObjectG target, string property, ObjectG this_)
230 	{
231 		auto __p = gtk_expression_bind(gtkExpression, (target is null) ? null : target.getObjectGStruct(), Str.toStringz(property), (this_ is null) ? null : this_.getObjectGStruct());
232 
233 		if(__p is null)
234 		{
235 			return null;
236 		}
237 
238 		return ObjectG.getDObject!(ExpressionWatch)(cast(GtkExpressionWatch*) __p);
239 	}
240 
241 	/**
242 	 * Evaluates the given expression and on success stores the result
243 	 * in @value.
244 	 *
245 	 * The `GType` of `value` will be the type given by
246 	 * [method@Gtk.Expression.get_value_type].
247 	 *
248 	 * It is possible that expressions cannot be evaluated - for example
249 	 * when the expression references objects that have been destroyed or
250 	 * set to `NULL`. In that case `value` will remain empty and `FALSE`
251 	 * will be returned.
252 	 *
253 	 * Params:
254 	 *     this_ = the this argument for the evaluation
255 	 *     value = an empty `GValue`
256 	 *
257 	 * Returns: `TRUE` if the expression could be evaluated
258 	 */
259 	public bool evaluate(ObjectG this_, Value value)
260 	{
261 		return gtk_expression_evaluate(gtkExpression, (this_ is null) ? null : this_.getObjectGStruct(), (value is null) ? null : value.getValueStruct()) != 0;
262 	}
263 
264 	/**
265 	 * Gets the `GType` that this expression evaluates to.
266 	 *
267 	 * This type is constant and will not change over the lifetime
268 	 * of this expression.
269 	 *
270 	 * Returns: The type returned from [method@Gtk.Expression.evaluate]
271 	 */
272 	public GType getValueType()
273 	{
274 		return gtk_expression_get_value_type(gtkExpression);
275 	}
276 
277 	/**
278 	 * Checks if the expression is static.
279 	 *
280 	 * A static expression will never change its result when
281 	 * [method@Gtk.Expression.evaluate] is called on it with the same arguments.
282 	 *
283 	 * That means a call to [method@Gtk.Expression.watch] is not necessary because
284 	 * it will never trigger a notify.
285 	 *
286 	 * Returns: `TRUE` if the expression is static
287 	 */
288 	public bool isStatic()
289 	{
290 		return gtk_expression_is_static(gtkExpression) != 0;
291 	}
292 
293 	alias doref = ref_;
294 	/**
295 	 * Acquires a reference on the given `GtkExpression`.
296 	 *
297 	 * Returns: the `GtkExpression` with an additional reference
298 	 */
299 	public Expression ref_()
300 	{
301 		auto __p = gtk_expression_ref(gtkExpression);
302 
303 		if(__p is null)
304 		{
305 			return null;
306 		}
307 
308 		return ObjectG.getDObject!(Expression)(cast(GtkExpression*) __p, true);
309 	}
310 
311 	/**
312 	 * Releases a reference on the given `GtkExpression`.
313 	 *
314 	 * If the reference was the last, the resources associated to the `self` are
315 	 * freed.
316 	 */
317 	public void unref()
318 	{
319 		gtk_expression_unref(gtkExpression);
320 	}
321 
322 	/**
323 	 * Installs a watch for the given `expression` that calls the `notify` function
324 	 * whenever the evaluation of `self` may have changed.
325 	 *
326 	 * GTK cannot guarantee that the evaluation did indeed change when the @notify
327 	 * gets invoked, but it guarantees the opposite: When it did in fact change,
328 	 * the `notify` will be invoked.
329 	 *
330 	 * Params:
331 	 *     this_ = the `this` argument to
332 	 *         watch
333 	 *     notify = callback to invoke when the
334 	 *         expression changes
335 	 *     userData = user data to pass to the `notify` callback
336 	 *     userDestroy = destroy notify for `user_data`
337 	 *
338 	 * Returns: The newly installed watch. Note that the only
339 	 *     reference held to the watch will be released when the watch is unwatched
340 	 *     which can happen automatically, and not just via
341 	 *     [method@Gtk.ExpressionWatch.unwatch]. You should call [method@Gtk.ExpressionWatch.ref]
342 	 *     if you want to keep the watch around.
343 	 */
344 	public ExpressionWatch watch(ObjectG this_, GtkExpressionNotify notify, void* userData, GDestroyNotify userDestroy)
345 	{
346 		auto __p = gtk_expression_watch(gtkExpression, (this_ is null) ? null : this_.getObjectGStruct(), notify, userData, userDestroy);
347 
348 		if(__p is null)
349 		{
350 			return null;
351 		}
352 
353 		return ObjectG.getDObject!(ExpressionWatch)(cast(GtkExpressionWatch*) __p);
354 	}
355 }