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.LevelBar;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gobject.Signals;
31 private import gtk.OrientableIF;
32 private import gtk.OrientableT;
33 private import gtk.Widget;
34 private import gtk.c.functions;
35 public  import gtk.c.types;
36 public  import gtkc.gtktypes;
37 private import std.algorithm;
38 
39 
40 /**
41  * The #GtkLevelBar is a bar widget that can be used
42  * as a level indicator. Typical use cases are displaying the strength
43  * of a password, or showing the charge level of a battery.
44  * 
45  * Use gtk_level_bar_set_value() to set the current value, and
46  * gtk_level_bar_add_offset_value() to set the value offsets at which
47  * the bar will be considered in a different state. GTK will add a few
48  * offsets by default on the level bar: #GTK_LEVEL_BAR_OFFSET_LOW,
49  * #GTK_LEVEL_BAR_OFFSET_HIGH and #GTK_LEVEL_BAR_OFFSET_FULL, with
50  * values 0.25, 0.75 and 1.0 respectively.
51  * 
52  * Note that it is your responsibility to update preexisting offsets
53  * when changing the minimum or maximum value. GTK+ will simply clamp
54  * them to the new range.
55  * 
56  * ## Adding a custom offset on the bar
57  * 
58  * |[<!-- language="C" -->
59  * 
60  * static GtkWidget *
61  * create_level_bar (void)
62  * {
63  * GtkWidget *widget;
64  * GtkLevelBar *bar;
65  * 
66  * widget = gtk_level_bar_new ();
67  * bar = GTK_LEVEL_BAR (widget);
68  * 
69  * // This changes the value of the default low offset
70  * 
71  * gtk_level_bar_add_offset_value (bar,
72  * GTK_LEVEL_BAR_OFFSET_LOW,
73  * 0.10);
74  * 
75  * // This adds a new offset to the bar; the application will
76  * // be able to change its color CSS like this:
77  * //
78  * // levelbar block.my-offset {
79  * //   background-color: magenta;
80  * //   border-style: solid;
81  * //   border-color: black;
82  * //   border-style: 1px;
83  * // }
84  * 
85  * gtk_level_bar_add_offset_value (bar, "my-offset", 0.60);
86  * 
87  * return widget;
88  * }
89  * ]|
90  * 
91  * The default interval of values is between zero and one, but it’s possible to
92  * modify the interval using gtk_level_bar_set_min_value() and
93  * gtk_level_bar_set_max_value(). The value will be always drawn in proportion to
94  * the admissible interval, i.e. a value of 15 with a specified interval between
95  * 10 and 20 is equivalent to a value of 0.5 with an interval between 0 and 1.
96  * When #GTK_LEVEL_BAR_MODE_DISCRETE is used, the bar level is rendered
97  * as a finite number of separated blocks instead of a single one. The number
98  * of blocks that will be rendered is equal to the number of units specified by
99  * the admissible interval.
100  * 
101  * For instance, to build a bar rendered with five blocks, it’s sufficient to
102  * set the minimum value to 0 and the maximum value to 5 after changing the indicator
103  * mode to discrete.
104  * 
105  * GtkLevelBar was introduced in GTK+ 3.6.
106  * 
107  * # GtkLevelBar as GtkBuildable
108  * 
109  * The GtkLevelBar implementation of the GtkBuildable interface supports a
110  * custom <offsets> element, which can contain any number of <offset> elements,
111  * each of which must have name and value attributes.
112  * 
113  * # CSS nodes
114  * 
115  * |[<!-- language="plain" -->
116  * levelbar[.discrete]
117  * ╰── trough
118  * ├── block.filled.level-name
119  * ┊
120  * ├── block.empty
121  * ┊
122  * ]|
123  * 
124  * GtkLevelBar has a main CSS node with name levelbar and one of the style
125  * classes .discrete or .continuous and a subnode with name trough. Below the
126  * trough node are a number of nodes with name block and style class .filled
127  * or .empty. In continuous mode, there is exactly one node of each, in discrete
128  * mode, the number of filled and unfilled nodes corresponds to blocks that are
129  * drawn. The block.filled nodes also get a style class .level-name corresponding
130  * to the level for the current value.
131  * 
132  * In horizontal orientation, the nodes are always arranged from left to right,
133  * regardless of text direction.
134  */
135 public class LevelBar : Widget, OrientableIF
136 {
137 	/** the main Gtk struct */
138 	protected GtkLevelBar* gtkLevelBar;
139 
140 	/** Get the main Gtk struct */
141 	public GtkLevelBar* getLevelBarStruct(bool transferOwnership = false)
142 	{
143 		if (transferOwnership)
144 			ownedRef = false;
145 		return gtkLevelBar;
146 	}
147 
148 	/** the main Gtk struct as a void* */
149 	protected override void* getStruct()
150 	{
151 		return cast(void*)gtkLevelBar;
152 	}
153 
154 	protected override void setStruct(GObject* obj)
155 	{
156 		gtkLevelBar = cast(GtkLevelBar*)obj;
157 		super.setStruct(obj);
158 	}
159 
160 	/**
161 	 * Sets our main struct and passes it to the parent class.
162 	 */
163 	public this (GtkLevelBar* gtkLevelBar, bool ownedRef = false)
164 	{
165 		this.gtkLevelBar = gtkLevelBar;
166 		super(cast(GtkWidget*)gtkLevelBar, ownedRef);
167 	}
168 
169 	// add the Orientable capabilities
170 	mixin OrientableT!(GtkLevelBar);
171 
172 
173 	/** */
174 	public static GType getType()
175 	{
176 		return gtk_level_bar_get_type();
177 	}
178 
179 	/**
180 	 * Creates a new #GtkLevelBar.
181 	 *
182 	 * Returns: a #GtkLevelBar.
183 	 *
184 	 * Since: 3.6
185 	 *
186 	 * Throws: ConstructionException GTK+ fails to create the object.
187 	 */
188 	public this()
189 	{
190 		auto p = gtk_level_bar_new();
191 
192 		if(p is null)
193 		{
194 			throw new ConstructionException("null returned by new");
195 		}
196 
197 		this(cast(GtkLevelBar*) p);
198 	}
199 
200 	/**
201 	 * Utility constructor that creates a new #GtkLevelBar for the specified
202 	 * interval.
203 	 *
204 	 * Params:
205 	 *     minValue = a positive value
206 	 *     maxValue = a positive value
207 	 *
208 	 * Returns: a #GtkLevelBar
209 	 *
210 	 * Since: 3.6
211 	 *
212 	 * Throws: ConstructionException GTK+ fails to create the object.
213 	 */
214 	public this(double minValue, double maxValue)
215 	{
216 		auto p = gtk_level_bar_new_for_interval(minValue, maxValue);
217 
218 		if(p is null)
219 		{
220 			throw new ConstructionException("null returned by new_for_interval");
221 		}
222 
223 		this(cast(GtkLevelBar*) p);
224 	}
225 
226 	/**
227 	 * Adds a new offset marker on @self at the position specified by @value.
228 	 * When the bar value is in the interval topped by @value (or between @value
229 	 * and #GtkLevelBar:max-value in case the offset is the last one on the bar)
230 	 * a style class named `level-`@name will be applied
231 	 * when rendering the level bar fill.
232 	 * If another offset marker named @name exists, its value will be
233 	 * replaced by @value.
234 	 *
235 	 * Params:
236 	 *     name = the name of the new offset
237 	 *     value = the value for the new offset
238 	 *
239 	 * Since: 3.6
240 	 */
241 	public void addOffsetValue(string name, double value)
242 	{
243 		gtk_level_bar_add_offset_value(gtkLevelBar, Str.toStringz(name), value);
244 	}
245 
246 	/**
247 	 * Return the value of the #GtkLevelBar:inverted property.
248 	 *
249 	 * Returns: %TRUE if the level bar is inverted
250 	 *
251 	 * Since: 3.8
252 	 */
253 	public bool getInverted()
254 	{
255 		return gtk_level_bar_get_inverted(gtkLevelBar) != 0;
256 	}
257 
258 	/**
259 	 * Returns the value of the #GtkLevelBar:max-value property.
260 	 *
261 	 * Returns: a positive value
262 	 *
263 	 * Since: 3.6
264 	 */
265 	public double getMaxValue()
266 	{
267 		return gtk_level_bar_get_max_value(gtkLevelBar);
268 	}
269 
270 	/**
271 	 * Returns the value of the #GtkLevelBar:min-value property.
272 	 *
273 	 * Returns: a positive value
274 	 *
275 	 * Since: 3.6
276 	 */
277 	public double getMinValue()
278 	{
279 		return gtk_level_bar_get_min_value(gtkLevelBar);
280 	}
281 
282 	/**
283 	 * Returns the value of the #GtkLevelBar:mode property.
284 	 *
285 	 * Returns: a #GtkLevelBarMode
286 	 *
287 	 * Since: 3.6
288 	 */
289 	public GtkLevelBarMode getMode()
290 	{
291 		return gtk_level_bar_get_mode(gtkLevelBar);
292 	}
293 
294 	/**
295 	 * Fetches the value specified for the offset marker @name in @self,
296 	 * returning %TRUE in case an offset named @name was found.
297 	 *
298 	 * Params:
299 	 *     name = the name of an offset in the bar
300 	 *     value = location where to store the value
301 	 *
302 	 * Returns: %TRUE if the specified offset is found
303 	 *
304 	 * Since: 3.6
305 	 */
306 	public bool getOffsetValue(string name, out double value)
307 	{
308 		return gtk_level_bar_get_offset_value(gtkLevelBar, Str.toStringz(name), &value) != 0;
309 	}
310 
311 	/**
312 	 * Returns the value of the #GtkLevelBar:value property.
313 	 *
314 	 * Returns: a value in the interval between
315 	 *     #GtkLevelBar:min-value and #GtkLevelBar:max-value
316 	 *
317 	 * Since: 3.6
318 	 */
319 	public double getValue()
320 	{
321 		return gtk_level_bar_get_value(gtkLevelBar);
322 	}
323 
324 	/**
325 	 * Removes an offset marker previously added with
326 	 * gtk_level_bar_add_offset_value().
327 	 *
328 	 * Params:
329 	 *     name = the name of an offset in the bar
330 	 *
331 	 * Since: 3.6
332 	 */
333 	public void removeOffsetValue(string name)
334 	{
335 		gtk_level_bar_remove_offset_value(gtkLevelBar, Str.toStringz(name));
336 	}
337 
338 	/**
339 	 * Sets the value of the #GtkLevelBar:inverted property.
340 	 *
341 	 * Params:
342 	 *     inverted = %TRUE to invert the level bar
343 	 *
344 	 * Since: 3.8
345 	 */
346 	public void setInverted(bool inverted)
347 	{
348 		gtk_level_bar_set_inverted(gtkLevelBar, inverted);
349 	}
350 
351 	/**
352 	 * Sets the value of the #GtkLevelBar:max-value property.
353 	 *
354 	 * You probably want to update preexisting level offsets after calling
355 	 * this function.
356 	 *
357 	 * Params:
358 	 *     value = a positive value
359 	 *
360 	 * Since: 3.6
361 	 */
362 	public void setMaxValue(double value)
363 	{
364 		gtk_level_bar_set_max_value(gtkLevelBar, value);
365 	}
366 
367 	/**
368 	 * Sets the value of the #GtkLevelBar:min-value property.
369 	 *
370 	 * You probably want to update preexisting level offsets after calling
371 	 * this function.
372 	 *
373 	 * Params:
374 	 *     value = a positive value
375 	 *
376 	 * Since: 3.6
377 	 */
378 	public void setMinValue(double value)
379 	{
380 		gtk_level_bar_set_min_value(gtkLevelBar, value);
381 	}
382 
383 	/**
384 	 * Sets the value of the #GtkLevelBar:mode property.
385 	 *
386 	 * Params:
387 	 *     mode = a #GtkLevelBarMode
388 	 *
389 	 * Since: 3.6
390 	 */
391 	public void setMode(GtkLevelBarMode mode)
392 	{
393 		gtk_level_bar_set_mode(gtkLevelBar, mode);
394 	}
395 
396 	/**
397 	 * Sets the value of the #GtkLevelBar:value property.
398 	 *
399 	 * Params:
400 	 *     value = a value in the interval between
401 	 *         #GtkLevelBar:min-value and #GtkLevelBar:max-value
402 	 *
403 	 * Since: 3.6
404 	 */
405 	public void setValue(double value)
406 	{
407 		gtk_level_bar_set_value(gtkLevelBar, value);
408 	}
409 
410 	protected class OnOffsetChangedDelegateWrapper
411 	{
412 		void delegate(string, LevelBar) dlg;
413 		gulong handlerId;
414 
415 		this(void delegate(string, LevelBar) dlg)
416 		{
417 			this.dlg = dlg;
418 			onOffsetChangedListeners ~= this;
419 		}
420 
421 		void remove(OnOffsetChangedDelegateWrapper source)
422 		{
423 			foreach(index, wrapper; onOffsetChangedListeners)
424 			{
425 				if (wrapper.handlerId == source.handlerId)
426 				{
427 					onOffsetChangedListeners[index] = null;
428 					onOffsetChangedListeners = std.algorithm.remove(onOffsetChangedListeners, index);
429 					break;
430 				}
431 			}
432 		}
433 	}
434 	OnOffsetChangedDelegateWrapper[] onOffsetChangedListeners;
435 
436 	/**
437 	 * Emitted when an offset specified on the bar changes value as an
438 	 * effect to gtk_level_bar_add_offset_value() being called.
439 	 *
440 	 * The signal supports detailed connections; you can connect to the
441 	 * detailed signal "changed::x" in order to only receive callbacks when
442 	 * the value of offset "x" changes.
443 	 *
444 	 * Params:
445 	 *     name = the name of the offset that changed value
446 	 *
447 	 * Since: 3.6
448 	 */
449 	gulong addOnOffsetChanged(void delegate(string, LevelBar) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
450 	{
451 		auto wrapper = new OnOffsetChangedDelegateWrapper(dlg);
452 		wrapper.handlerId = Signals.connectData(
453 			this,
454 			"offset-changed",
455 			cast(GCallback)&callBackOffsetChanged,
456 			cast(void*)wrapper,
457 			cast(GClosureNotify)&callBackOffsetChangedDestroy,
458 			connectFlags);
459 		return wrapper.handlerId;
460 	}
461 
462 	extern(C) static void callBackOffsetChanged(GtkLevelBar* levelbarStruct, char* name, OnOffsetChangedDelegateWrapper wrapper)
463 	{
464 		wrapper.dlg(Str.toString(name), wrapper.outer);
465 	}
466 
467 	extern(C) static void callBackOffsetChangedDestroy(OnOffsetChangedDelegateWrapper wrapper, GClosure* closure)
468 	{
469 		wrapper.remove(wrapper);
470 	}
471 }