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 public  import gtkc.gdktypes;
35 private import gtkc.gtk;
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()
142 	{
143 		return gtkLevelBar;
144 	}
145 
146 	/** the main Gtk struct as a void* */
147 	protected override void* getStruct()
148 	{
149 		return cast(void*)gtkLevelBar;
150 	}
151 
152 	protected override void setStruct(GObject* obj)
153 	{
154 		gtkLevelBar = cast(GtkLevelBar*)obj;
155 		super.setStruct(obj);
156 	}
157 
158 	/**
159 	 * Sets our main struct and passes it to the parent class.
160 	 */
161 	public this (GtkLevelBar* gtkLevelBar, bool ownedRef = false)
162 	{
163 		this.gtkLevelBar = gtkLevelBar;
164 		super(cast(GtkWidget*)gtkLevelBar, ownedRef);
165 	}
166 
167 	// add the Orientable capabilities
168 	mixin OrientableT!(GtkLevelBar);
169 
170 
171 	/** */
172 	public static GType getType()
173 	{
174 		return gtk_level_bar_get_type();
175 	}
176 
177 	/**
178 	 * Creates a new #GtkLevelBar.
179 	 *
180 	 * Return: a #GtkLevelBar.
181 	 *
182 	 * Since: 3.6
183 	 *
184 	 * Throws: ConstructionException GTK+ fails to create the object.
185 	 */
186 	public this()
187 	{
188 		auto p = gtk_level_bar_new();
189 		
190 		if(p is null)
191 		{
192 			throw new ConstructionException("null returned by new");
193 		}
194 		
195 		this(cast(GtkLevelBar*) p);
196 	}
197 
198 	/**
199 	 * Utility constructor that creates a new #GtkLevelBar for the specified
200 	 * interval.
201 	 *
202 	 * Params:
203 	 *     minValue = a positive value
204 	 *     maxValue = a positive value
205 	 *
206 	 * Return: a #GtkLevelBar
207 	 *
208 	 * Since: 3.6
209 	 *
210 	 * Throws: ConstructionException GTK+ fails to create the object.
211 	 */
212 	public this(double minValue, double maxValue)
213 	{
214 		auto p = gtk_level_bar_new_for_interval(minValue, maxValue);
215 		
216 		if(p is null)
217 		{
218 			throw new ConstructionException("null returned by new_for_interval");
219 		}
220 		
221 		this(cast(GtkLevelBar*) p);
222 	}
223 
224 	/**
225 	 * Adds a new offset marker on @self at the position specified by @value.
226 	 * When the bar value is in the interval topped by @value (or between @value
227 	 * and #GtkLevelBar:max-value in case the offset is the last one on the bar)
228 	 * a style class named `level-`@name will be applied
229 	 * when rendering the level bar fill.
230 	 * If another offset marker named @name exists, its value will be
231 	 * replaced by @value.
232 	 *
233 	 * Params:
234 	 *     name = the name of the new offset
235 	 *     value = the value for the new offset
236 	 *
237 	 * Since: 3.6
238 	 */
239 	public void addOffsetValue(string name, double value)
240 	{
241 		gtk_level_bar_add_offset_value(gtkLevelBar, Str.toStringz(name), value);
242 	}
243 
244 	/**
245 	 * Return the value of the #GtkLevelBar:inverted property.
246 	 *
247 	 * Return: %TRUE if the level bar is inverted
248 	 *
249 	 * Since: 3.8
250 	 */
251 	public bool getInverted()
252 	{
253 		return gtk_level_bar_get_inverted(gtkLevelBar) != 0;
254 	}
255 
256 	/**
257 	 * Returns the value of the #GtkLevelBar:max-value property.
258 	 *
259 	 * Return: a positive value
260 	 *
261 	 * Since: 3.6
262 	 */
263 	public double getMaxValue()
264 	{
265 		return gtk_level_bar_get_max_value(gtkLevelBar);
266 	}
267 
268 	/**
269 	 * Returns the value of the #GtkLevelBar:min-value property.
270 	 *
271 	 * Return: a positive value
272 	 *
273 	 * Since: 3.6
274 	 */
275 	public double getMinValue()
276 	{
277 		return gtk_level_bar_get_min_value(gtkLevelBar);
278 	}
279 
280 	/**
281 	 * Returns the value of the #GtkLevelBar:mode property.
282 	 *
283 	 * Return: a #GtkLevelBarMode
284 	 *
285 	 * Since: 3.6
286 	 */
287 	public GtkLevelBarMode getMode()
288 	{
289 		return gtk_level_bar_get_mode(gtkLevelBar);
290 	}
291 
292 	/**
293 	 * Fetches the value specified for the offset marker @name in @self,
294 	 * returning %TRUE in case an offset named @name was found.
295 	 *
296 	 * Params:
297 	 *     name = the name of an offset in the bar
298 	 *     value = location where to store the value
299 	 *
300 	 * Return: %TRUE if the specified offset is found
301 	 *
302 	 * Since: 3.6
303 	 */
304 	public bool getOffsetValue(string name, out double value)
305 	{
306 		return gtk_level_bar_get_offset_value(gtkLevelBar, Str.toStringz(name), &value) != 0;
307 	}
308 
309 	/**
310 	 * Returns the value of the #GtkLevelBar:value property.
311 	 *
312 	 * Return: a value in the interval between
313 	 *     #GtkLevelBar:min-value and #GtkLevelBar:max-value
314 	 *
315 	 * Since: 3.6
316 	 */
317 	public double getValue()
318 	{
319 		return gtk_level_bar_get_value(gtkLevelBar);
320 	}
321 
322 	/**
323 	 * Removes an offset marker previously added with
324 	 * gtk_level_bar_add_offset_value().
325 	 *
326 	 * Params:
327 	 *     name = the name of an offset in the bar
328 	 *
329 	 * Since: 3.6
330 	 */
331 	public void removeOffsetValue(string name)
332 	{
333 		gtk_level_bar_remove_offset_value(gtkLevelBar, Str.toStringz(name));
334 	}
335 
336 	/**
337 	 * Sets the value of the #GtkLevelBar:inverted property.
338 	 *
339 	 * Params:
340 	 *     inverted = %TRUE to invert the level bar
341 	 *
342 	 * Since: 3.8
343 	 */
344 	public void setInverted(bool inverted)
345 	{
346 		gtk_level_bar_set_inverted(gtkLevelBar, inverted);
347 	}
348 
349 	/**
350 	 * Sets the value of the #GtkLevelBar:max-value property.
351 	 *
352 	 * You probably want to update preexisting level offsets after calling
353 	 * this function.
354 	 *
355 	 * Params:
356 	 *     value = a positive value
357 	 *
358 	 * Since: 3.6
359 	 */
360 	public void setMaxValue(double value)
361 	{
362 		gtk_level_bar_set_max_value(gtkLevelBar, value);
363 	}
364 
365 	/**
366 	 * Sets the value of the #GtkLevelBar:min-value property.
367 	 *
368 	 * You probably want to update preexisting level offsets after calling
369 	 * this function.
370 	 *
371 	 * Params:
372 	 *     value = a positive value
373 	 *
374 	 * Since: 3.6
375 	 */
376 	public void setMinValue(double value)
377 	{
378 		gtk_level_bar_set_min_value(gtkLevelBar, value);
379 	}
380 
381 	/**
382 	 * Sets the value of the #GtkLevelBar:mode property.
383 	 *
384 	 * Params:
385 	 *     mode = a #GtkLevelBarMode
386 	 *
387 	 * Since: 3.6
388 	 */
389 	public void setMode(GtkLevelBarMode mode)
390 	{
391 		gtk_level_bar_set_mode(gtkLevelBar, mode);
392 	}
393 
394 	/**
395 	 * Sets the value of the #GtkLevelBar:value property.
396 	 *
397 	 * Params:
398 	 *     value = a value in the interval between
399 	 *         #GtkLevelBar:min-value and #GtkLevelBar:max-value
400 	 *
401 	 * Since: 3.6
402 	 */
403 	public void setValue(double value)
404 	{
405 		gtk_level_bar_set_value(gtkLevelBar, value);
406 	}
407 
408 	protected class OnOffsetChangedDelegateWrapper
409 	{
410 		void delegate(string, LevelBar) dlg;
411 		gulong handlerId;
412 		ConnectFlags flags;
413 		this(void delegate(string, LevelBar) dlg, gulong handlerId, ConnectFlags flags)
414 		{
415 			this.dlg = dlg;
416 			this.handlerId = handlerId;
417 			this.flags = flags;
418 		}
419 	}
420 	protected OnOffsetChangedDelegateWrapper[] onOffsetChangedListeners;
421 
422 	/**
423 	 * Emitted when an offset specified on the bar changes value as an
424 	 * effect to gtk_level_bar_add_offset_value() being called.
425 	 *
426 	 * The signal supports detailed connections; you can connect to the
427 	 * detailed signal "changed::x" in order to only receive callbacks when
428 	 * the value of offset "x" changes.
429 	 *
430 	 * Params:
431 	 *     name = the name of the offset that changed value
432 	 *
433 	 * Since: 3.6
434 	 */
435 	gulong addOnOffsetChanged(void delegate(string, LevelBar) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
436 	{
437 		onOffsetChangedListeners ~= new OnOffsetChangedDelegateWrapper(dlg, 0, connectFlags);
438 		onOffsetChangedListeners[onOffsetChangedListeners.length - 1].handlerId = Signals.connectData(
439 			this,
440 			"offset-changed",
441 			cast(GCallback)&callBackOffsetChanged,
442 			cast(void*)onOffsetChangedListeners[onOffsetChangedListeners.length - 1],
443 			cast(GClosureNotify)&callBackOffsetChangedDestroy,
444 			connectFlags);
445 		return onOffsetChangedListeners[onOffsetChangedListeners.length - 1].handlerId;
446 	}
447 	
448 	extern(C) static void callBackOffsetChanged(GtkLevelBar* levelbarStruct, char* name,OnOffsetChangedDelegateWrapper wrapper)
449 	{
450 		wrapper.dlg(Str.toString(name), wrapper.outer);
451 	}
452 	
453 	extern(C) static void callBackOffsetChangedDestroy(OnOffsetChangedDelegateWrapper wrapper, GClosure* closure)
454 	{
455 		wrapper.outer.internalRemoveOnOffsetChanged(wrapper);
456 	}
457 
458 	protected void internalRemoveOnOffsetChanged(OnOffsetChangedDelegateWrapper source)
459 	{
460 		foreach(index, wrapper; onOffsetChangedListeners)
461 		{
462 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
463 			{
464 				onOffsetChangedListeners[index] = null;
465 				onOffsetChangedListeners = std.algorithm.remove(onOffsetChangedListeners, index);
466 				break;
467 			}
468 		}
469 	}
470 	
471 }