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.Scale;
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.Adjustment;
32 private import gtk.Range;
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 pango.PgLayout;
38 private import std.algorithm;
39 
40 
41 /**
42  * A GtkScale is a slider control used to select a numeric value.
43  * To use it, you’ll probably want to investigate the methods on
44  * its base class, #GtkRange, in addition to the methods for GtkScale itself.
45  * To set the value of a scale, you would normally use gtk_range_set_value().
46  * To detect changes to the value, you would normally use the
47  * #GtkRange::value-changed signal.
48  * 
49  * Note that using the same upper and lower bounds for the #GtkScale (through
50  * the #GtkRange methods) will hide the slider itself. This is useful for
51  * applications that want to show an undeterminate value on the scale, without
52  * changing the layout of the application (such as movie or music players).
53  * 
54  * # GtkScale as GtkBuildable
55  * 
56  * GtkScale supports a custom <marks> element, which can contain multiple
57  * <mark> elements. The “value” and “position” attributes have the same
58  * meaning as gtk_scale_add_mark() parameters of the same name. If the
59  * element is not empty, its content is taken as the markup to show at
60  * the mark. It can be translated with the usual ”translatable” and
61  * “context” attributes.
62  * 
63  * # CSS nodes
64  * 
65  * |[<!-- language="plain" -->
66  * scale[.fine-tune][.marks-before][.marks-after]
67  * ├── marks.top
68  * │   ├── mark
69  * │   ┊    ├── [label]
70  * │   ┊    ╰── indicator
71  * ┊   ┊
72  * │   ╰── mark
73  * ├── [value]
74  * ├── contents
75  * │   ╰── trough
76  * │       ├── slider
77  * │       ├── [highlight]
78  * │       ╰── [fill]
79  * ╰── marks.bottom
80  * ├── mark
81  * ┊    ├── indicator
82  * ┊    ╰── [label]
83  * ╰── mark
84  * ]|
85  * 
86  * GtkScale has a main CSS node with name scale and a subnode for its contents,
87  * with subnodes named trough and slider.
88  * 
89  * The main node gets the style class .fine-tune added when the scale is in
90  * 'fine-tuning' mode.
91  * 
92  * If the scale has an origin (see gtk_scale_set_has_origin()), there is a
93  * subnode with name highlight below the trough node that is used for rendering
94  * the highlighted part of the trough.
95  * 
96  * If the scale is showing a fill level (see gtk_range_set_show_fill_level()),
97  * there is a subnode with name fill below the trough node that is used for
98  * rendering the filled in part of the trough.
99  * 
100  * If marks are present, there is a marks subnode before or after the contents
101  * node, below which each mark gets a node with name mark. The marks nodes get
102  * either the .top or .bottom style class.
103  * 
104  * The mark node has a subnode named indicator. If the mark has text, it also
105  * has a subnode named label. When the mark is either above or left of the
106  * scale, the label subnode is the first when present. Otherwise, the indicator
107  * subnode is the first.
108  * 
109  * The main CSS node gets the 'marks-before' and/or 'marks-after' style classes
110  * added depending on what marks are present.
111  * 
112  * If the scale is displaying the value (see #GtkScale:draw-value), there is
113  * subnode with name value.
114  */
115 public class Scale : Range
116 {
117 	/** the main Gtk struct */
118 	protected GtkScale* gtkScale;
119 
120 	/** Get the main Gtk struct */
121 	public GtkScale* getScaleStruct(bool transferOwnership = false)
122 	{
123 		if (transferOwnership)
124 			ownedRef = false;
125 		return gtkScale;
126 	}
127 
128 	/** the main Gtk struct as a void* */
129 	protected override void* getStruct()
130 	{
131 		return cast(void*)gtkScale;
132 	}
133 
134 	/**
135 	 * Sets our main struct and passes it to the parent class.
136 	 */
137 	public this (GtkScale* gtkScale, bool ownedRef = false)
138 	{
139 		this.gtkScale = gtkScale;
140 		super(cast(GtkRange*)gtkScale, ownedRef);
141 	}
142 
143 
144 	/** */
145 	public static GType getType()
146 	{
147 		return gtk_scale_get_type();
148 	}
149 
150 	/**
151 	 * Creates a new #GtkScale.
152 	 *
153 	 * Params:
154 	 *     orientation = the scale’s orientation.
155 	 *     adjustment = the #GtkAdjustment which sets the range
156 	 *         of the scale, or %NULL to create a new adjustment.
157 	 *
158 	 * Returns: a new #GtkScale
159 	 *
160 	 * Since: 3.0
161 	 *
162 	 * Throws: ConstructionException GTK+ fails to create the object.
163 	 */
164 	public this(GtkOrientation orientation, Adjustment adjustment)
165 	{
166 		auto p = gtk_scale_new(orientation, (adjustment is null) ? null : adjustment.getAdjustmentStruct());
167 
168 		if(p is null)
169 		{
170 			throw new ConstructionException("null returned by new");
171 		}
172 
173 		this(cast(GtkScale*) p);
174 	}
175 
176 	/**
177 	 * Creates a new scale widget with the given orientation that lets the
178 	 * user input a number between @min and @max (including @min and @max)
179 	 * with the increment @step.  @step must be nonzero; it’s the distance
180 	 * the slider moves when using the arrow keys to adjust the scale
181 	 * value.
182 	 *
183 	 * Note that the way in which the precision is derived works best if @step
184 	 * is a power of ten. If the resulting precision is not suitable for your
185 	 * needs, use gtk_scale_set_digits() to correct it.
186 	 *
187 	 * Params:
188 	 *     orientation = the scale’s orientation.
189 	 *     min = minimum value
190 	 *     max = maximum value
191 	 *     step = step increment (tick size) used with keyboard shortcuts
192 	 *
193 	 * Returns: a new #GtkScale
194 	 *
195 	 * Since: 3.0
196 	 *
197 	 * Throws: ConstructionException GTK+ fails to create the object.
198 	 */
199 	public this(GtkOrientation orientation, double min, double max, double step)
200 	{
201 		auto p = gtk_scale_new_with_range(orientation, min, max, step);
202 
203 		if(p is null)
204 		{
205 			throw new ConstructionException("null returned by new_with_range");
206 		}
207 
208 		this(cast(GtkScale*) p);
209 	}
210 
211 	/**
212 	 * Adds a mark at @value.
213 	 *
214 	 * A mark is indicated visually by drawing a tick mark next to the scale,
215 	 * and GTK+ makes it easy for the user to position the scale exactly at the
216 	 * marks value.
217 	 *
218 	 * If @markup is not %NULL, text is shown next to the tick mark.
219 	 *
220 	 * To remove marks from a scale, use gtk_scale_clear_marks().
221 	 *
222 	 * Params:
223 	 *     value = the value at which the mark is placed, must be between
224 	 *         the lower and upper limits of the scales’ adjustment
225 	 *     position = where to draw the mark. For a horizontal scale, #GTK_POS_TOP
226 	 *         and %GTK_POS_LEFT are drawn above the scale, anything else below.
227 	 *         For a vertical scale, #GTK_POS_LEFT and %GTK_POS_TOP are drawn to
228 	 *         the left of the scale, anything else to the right.
229 	 *     markup = Text to be shown at the mark, using [Pango markup][PangoMarkupFormat], or %NULL
230 	 *
231 	 * Since: 2.16
232 	 */
233 	public void addMark(double value, GtkPositionType position, string markup)
234 	{
235 		gtk_scale_add_mark(gtkScale, value, position, Str.toStringz(markup));
236 	}
237 
238 	/**
239 	 * Removes any marks that have been added with gtk_scale_add_mark().
240 	 *
241 	 * Since: 2.16
242 	 */
243 	public void clearMarks()
244 	{
245 		gtk_scale_clear_marks(gtkScale);
246 	}
247 
248 	/**
249 	 * Gets the number of decimal places that are displayed in the value.
250 	 *
251 	 * Returns: the number of decimal places that are displayed
252 	 */
253 	public int getDigits()
254 	{
255 		return gtk_scale_get_digits(gtkScale);
256 	}
257 
258 	/**
259 	 * Returns whether the current value is displayed as a string
260 	 * next to the slider.
261 	 *
262 	 * Returns: whether the current value is displayed as a string
263 	 */
264 	public bool getDrawValue()
265 	{
266 		return gtk_scale_get_draw_value(gtkScale) != 0;
267 	}
268 
269 	/**
270 	 * Returns whether the scale has an origin.
271 	 *
272 	 * Returns: %TRUE if the scale has an origin.
273 	 *
274 	 * Since: 3.4
275 	 */
276 	public bool getHasOrigin()
277 	{
278 		return gtk_scale_get_has_origin(gtkScale) != 0;
279 	}
280 
281 	/**
282 	 * Gets the #PangoLayout used to display the scale. The returned
283 	 * object is owned by the scale so does not need to be freed by
284 	 * the caller.
285 	 *
286 	 * Returns: the #PangoLayout for this scale,
287 	 *     or %NULL if the #GtkScale:draw-value property is %FALSE.
288 	 *
289 	 * Since: 2.4
290 	 */
291 	public PgLayout getLayout()
292 	{
293 		auto p = gtk_scale_get_layout(gtkScale);
294 
295 		if(p is null)
296 		{
297 			return null;
298 		}
299 
300 		return ObjectG.getDObject!(PgLayout)(cast(PangoLayout*) p);
301 	}
302 
303 	/**
304 	 * Obtains the coordinates where the scale will draw the
305 	 * #PangoLayout representing the text in the scale. Remember
306 	 * when using the #PangoLayout function you need to convert to
307 	 * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
308 	 *
309 	 * If the #GtkScale:draw-value property is %FALSE, the return
310 	 * values are undefined.
311 	 *
312 	 * Params:
313 	 *     x = location to store X offset of layout, or %NULL
314 	 *     y = location to store Y offset of layout, or %NULL
315 	 *
316 	 * Since: 2.4
317 	 */
318 	public void getLayoutOffsets(out int x, out int y)
319 	{
320 		gtk_scale_get_layout_offsets(gtkScale, &x, &y);
321 	}
322 
323 	/**
324 	 * Gets the position in which the current value is displayed.
325 	 *
326 	 * Returns: the position in which the current value is displayed
327 	 */
328 	public GtkPositionType getValuePos()
329 	{
330 		return gtk_scale_get_value_pos(gtkScale);
331 	}
332 
333 	/**
334 	 * Sets the number of decimal places that are displayed in the value. Also
335 	 * causes the value of the adjustment to be rounded to this number of digits,
336 	 * so the retrieved value matches the displayed one, if #GtkScale:draw-value is
337 	 * %TRUE when the value changes. If you want to enforce rounding the value when
338 	 * #GtkScale:draw-value is %FALSE, you can set #GtkRange:round-digits instead.
339 	 *
340 	 * Note that rounding to a small number of digits can interfere with
341 	 * the smooth autoscrolling that is built into #GtkScale. As an alternative,
342 	 * you can use the #GtkScale::format-value signal to format the displayed
343 	 * value yourself.
344 	 *
345 	 * Params:
346 	 *     digits = the number of decimal places to display,
347 	 *         e.g. use 1 to display 1.0, 2 to display 1.00, etc
348 	 */
349 	public void setDigits(int digits)
350 	{
351 		gtk_scale_set_digits(gtkScale, digits);
352 	}
353 
354 	/**
355 	 * Specifies whether the current value is displayed as a string next
356 	 * to the slider.
357 	 *
358 	 * Params:
359 	 *     drawValue = %TRUE to draw the value
360 	 */
361 	public void setDrawValue(bool drawValue)
362 	{
363 		gtk_scale_set_draw_value(gtkScale, drawValue);
364 	}
365 
366 	/**
367 	 * If #GtkScale:has-origin is set to %TRUE (the default), the scale will
368 	 * highlight the part of the trough between the origin (bottom or left side)
369 	 * and the current value.
370 	 *
371 	 * Params:
372 	 *     hasOrigin = %TRUE if the scale has an origin
373 	 *
374 	 * Since: 3.4
375 	 */
376 	public void setHasOrigin(bool hasOrigin)
377 	{
378 		gtk_scale_set_has_origin(gtkScale, hasOrigin);
379 	}
380 
381 	/**
382 	 * Sets the position in which the current value is displayed.
383 	 *
384 	 * Params:
385 	 *     pos = the position in which the current value is displayed
386 	 */
387 	public void setValuePos(GtkPositionType pos)
388 	{
389 		gtk_scale_set_value_pos(gtkScale, pos);
390 	}
391 
392 	/**
393 	 * Signal which allows you to change how the scale value is displayed.
394 	 * Connect a signal handler which returns an allocated string representing
395 	 * @value. That string will then be used to display the scale's value.
396 	 *
397 	 * If no user-provided handlers are installed, the value will be displayed on
398 	 * its own, rounded according to the value of the #GtkScale:digits property.
399 	 *
400 	 * Here's an example signal handler which displays a value 1.0 as
401 	 * with "-->1.0<--".
402 	 * |[<!-- language="C" -->
403 	 * static gchar*
404 	 * format_value_callback (GtkScale *scale,
405 	 * gdouble   value)
406 	 * {
407 	 * return g_strdup_printf ("-->\%0.*g<--",
408 	 * gtk_scale_get_digits (scale), value);
409 	 * }
410 	 * ]|
411 	 *
412 	 * Params:
413 	 *     value = the value to format
414 	 *
415 	 * Returns: allocated string representing @value
416 	 */
417 	gulong addOnFormatValue(string delegate(double, Scale) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
418 	{
419 		return Signals.connect(this, "format-value", dlg, connectFlags ^ ConnectFlags.SWAPPED);
420 	}
421 }