summaryrefslogtreecommitdiff
path: root/ElmSharp/ElmSharp/Slider.cs
blob: 3122a55b5b687b273a3860fd0f9b415a5517a772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;

namespace ElmSharp
{
    /// <summary>
    /// Enumeration for the Slider's indicator visiblity mode.
    /// </summary>
    public enum SliderIndicatorVisibleMode
    {
        /// <summary>
        /// Show indicator on mouse down or change in slider value.
        /// </summary>
        Default,

        /// <summary>
        /// Always show the indicator.
        /// </summary>
        Always,

        /// <summary>
        /// Show the indicator on focus.
        /// </summary>
        OnFocus,

        /// <summary>
        /// Never show the indicator.
        /// </summary>
        None,
    }

    /// <summary>
    /// The Slider is a widget that adds a draggable slider widget for selecting the value of something within a range.
    /// </summary>
    public class Slider : Layout
    {
        double _minimum = 0.0;
        double _maximum = 1.0;

        SmartEvent _changed;
        SmartEvent _delayedChanged;
        SmartEvent _dragStarted;
        SmartEvent _dragStopped;

        /// <summary>
        /// Creates and initializes a new instance of the Slider class.
        /// </summary>
        /// <param name="parent">The <see cref="EvasObject"/> to which the new Slider will be attached as a child.</param>
        public Slider(EvasObject parent) : base(parent)
        {
            _changed = new SmartEvent(this, this.RealHandle, "changed");
            _changed.On += (s, e) => ValueChanged?.Invoke(this, EventArgs.Empty);

            _delayedChanged = new SmartEvent(this, this.RealHandle, "delay,changed");
            _delayedChanged.On += (s, e) => DelayedValueChanged?.Invoke(this, EventArgs.Empty);

            _dragStarted = new SmartEvent(this, this.RealHandle, "slider,drag,start");
            _dragStarted.On += (s, e) => DragStarted?.Invoke(this, EventArgs.Empty);

            _dragStopped = new SmartEvent(this, this.RealHandle, "slider,drag,stop");
            _dragStopped.On += (s, e) => DragStopped?.Invoke(this, EventArgs.Empty);
        }

        /// <summary>
        /// ValueChanged will be triggered when the Slider value is changed by the user.
        /// </summary>
        public event EventHandler ValueChanged;

        /// <summary>
        /// DelayedValueChanged will be triggered when a short time after the value is changed by the user.
        /// This will be called only when the user stops dragging for a very short period or when they release their finger/mouse,
        /// so it avoids possibly expensive reactions to the value change.
        /// </summary>
        public event EventHandler DelayedValueChanged;

        /// <summary>
        /// DragStarted will be triggered when dragging the Slider indicator around has started.
        /// </summary>
        public event EventHandler DragStarted;

        /// <summary>
        /// DragStopped will be triggered when dragging the Slider indicator around has stopped.
        /// </summary>
        public event EventHandler DragStopped;

        /// <summary>
        /// Sets or gets the (exact) length of the bar region of a given Slider widget.
        /// </summary>
        /// <remarks>
        /// This sets the minimum width (when in the horizontal mode) or height (when in the vertical mode)
        /// of the actual bar area of the slider obj. This in turn affects the object's minimum size.
        /// Use this when you're not setting other size hints expanding on the given direction
        /// (like weight and alignment hints), and you would like it to have a specific size.
        /// </remarks>
        public int SpanSize
        {
            get
            {
                return Interop.Elementary.elm_slider_span_size_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_slider_span_size_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Sets or gets the format string for the unit label.
        /// </summary>
        /// <remarks>
        /// Unit label is displayed all the time, if set, after the slider's bar.
        /// In the horizontal mode, on the right and in the vertical mode, at the bottom.If NULL,
        /// the unit label won't be visible. If not, it sets the format string for the label text.
        /// For the label text a floating point value is provided,
        /// so the label text can display up to 1 floating point value.
        /// Note that this is optional.Use a format string such as "%1.2f meters" for example,
        /// and it displays values like: "3.14 meters" for a value equal to 3.14159.By default, unit label is disabled.
        /// </remarks>
        public string UnitFormat
        {
            get
            {
                return Interop.Elementary.elm_slider_unit_format_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_slider_unit_format_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Sets or gets the format string for the indicator label.
        /// </summary>
        /// <remarks>
        /// The slider may display its value somewhere other than the unit label,
        /// for example, above the slider knob that is dragged around. This function sets the format string
        /// used for this.If NULL, the indicator label won't be visible. If not, it sets the format string
        /// for the label text. For the label text floating point value is provided, so the label text can
        /// display up to 1 floating point value. Note that this is optional.Use a format string
        /// such as "%1.2f meters" for example, and it displays values like: "3.14 meters" for a value
        /// equal to 3.14159.By default, the indicator label is disabled.
        /// </remarks>
        public string IndicatorFormat
        {
            get
            {
                return Interop.Elementary.elm_slider_indicator_format_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_slider_indicator_format_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Sets or gets the orientation of a given slider widget.
        /// </summary>
        /// <remarks>
        /// The orientation may be vertically or horizontally.By default, it's displayed horizontally.
        /// </remarks>
        public bool IsHorizontal
        {
            get
            {
                return Interop.Elementary.elm_slider_horizontal_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_slider_horizontal_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Sets or gets the minimum values for the slider.
        /// </summary>
        /// <remarks>
        /// This defines the allowed minimum values to be selected by the user.
        /// If the actual value is less than min, it is updated to min.
        /// Actual value can be obtained with Value.By default, min is equal to 0.0.
        /// </remarks>
        public double Minimum
        {
            get
            {
                return _minimum;
            }
            set
            {
                _minimum = value;
                Interop.Elementary.elm_slider_min_max_set(RealHandle, _minimum, _maximum);
            }
        }

        /// <summary>
        /// Sets or gets the maximum values for the slider.
        /// </summary>
        /// <remarks>
        /// This defines the allowed maximum values to be selected by the user.
        /// If the actual value is bigger then max, it is updated to max.
        /// Actual value can be obtained with Value.By default, min is equal to 0.0, and max is equal to 1.0.
        /// Maximum must be greater than minimum, otherwise the behavior is undefined.
        /// </remarks>
        public double Maximum
        {
            get
            {
                return _maximum;
            }
            set
            {
                _maximum = value;
                Interop.Elementary.elm_slider_min_max_set(RealHandle, _minimum, _maximum);
            }
        }

        /// <summary>
        /// Gets or sets the value displayed by the slider.
        /// </summary>
        /// <remarks>
        /// Value will be presented on the unit label following format specified with UnitFormat and
        /// on indicator with IndicatorFormat.The value must to be between Minimum and Maximum values.
        /// </remarks>
        public double Value
        {
            get
            {
                return Interop.Elementary.elm_slider_value_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_slider_value_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Sets or gets the step by which the slider indicator moves.
        /// </summary>
        /// <remarks>
        /// This value is used when the draggable object is moved automatically i.e.,
        /// in case of a key event when up/down/left/right key is pressed or in case accessibility
        /// is set and the flick event is used to inc/dec slider values.
        /// By default, the step value is equal to 0.05.
        /// </remarks>
        public double Step
        {
            get
            {
                return Interop.Elementary.elm_slider_step_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_slider_step_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Gets or sets whether a given slider widget's displaying values are inverted.
        /// </summary>
        /// <remarks>
        /// A slider may be inverted, in which case it gets its values inverted,
        /// with high values being on the left or top and low values on the right or bottom,
        /// as opposed to normally have the low values on the former and high values on the latter,
        /// respectively, for the horizontal and vertical modes.
        /// </remarks>
        public bool IsInverted
        {
            get
            {
                return Interop.Elementary.elm_slider_inverted_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_slider_inverted_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Sets or gets whether to enlarge the slider indicator (augmented knob).
        /// </summary>
        /// <remarks>
        /// By default, the indicator is bigger when dragged by the user.
        /// It won't display values set with IndicatorFormat if you disable the indicator.
        /// </remarks>
        public bool IsIndicatorVisible
        {
            get
            {
                return Interop.Elementary.elm_slider_indicator_show_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_slider_indicator_show_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Sets or gets the visible mode of slider indicator.
        /// </summary>
        public SliderIndicatorVisibleMode IndicatorVisibleMode
        {
            get
            {
                return (SliderIndicatorVisibleMode)Interop.Elementary.elm_slider_indicator_visible_mode_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_slider_indicator_visible_mode_set(RealHandle, (int)value);
            }
        }

        /// <summary>
        /// Sets or gets whether to Show the indicator of slider on focus.
        /// </summary>
        public bool IsIndicatorFocusable
        {
            get
            {
                return Interop.Elementary.elm_slider_indicator_show_on_focus_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_slider_indicator_show_on_focus_set(RealHandle, value);
            }
        }

        protected override IntPtr CreateHandle(EvasObject parent)
        {
            IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
            Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");

            RealHandle = Interop.Elementary.elm_slider_add(handle);
            Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);

            return handle;
        }
    }
}