Manual merge for nui v0.2.35.
[platform/core/csapi/nui.git] / Tizen.NUI / src / public / CustomView / Spin.cs
1 // Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 //
16 // This File has been auto-generated by SWIG and then modified using DALi Ruby Scripts
17 // Some have been manually changed
18
19 using System;
20 using System.Runtime.InteropServices;
21 using Tizen.NUI;
22 using Tizen.NUI.UIComponents;
23
24 // A spin control (for continously changing values when users can easily predict a set of values)
25
26 namespace Tizen.NUI
27 {
28     ///<summary>
29     ///Spin CustomView class
30     /// </summary>
31     public class Spin : CustomView
32     {
33         private VisualBase _arrowVisual;
34         private TextField _textField;
35         private int _arrowVisualPropertyIndex;
36         private string _arrowImage;
37         private int _currentValue;
38         private int _minValue;
39         private int _maxValue;
40         private int _singleStep;
41         private bool _wrappingEnabled;
42         private string _fontFamily;
43         private string _fontStyle;
44         private int _pointSize;
45         private Color _textColor;
46         private Color _textBackgroundColor;
47         private int _maxTextLength;
48
49         // Called by DALi Builder if it finds a Spin control in a JSON file
50         static CustomView CreateInstance()
51         {
52             return new Spin();
53         }
54
55         // static constructor registers the control type (only runs once)
56         static Spin()
57         {
58             // ViewRegistry registers control type with DALi type registery
59             // also uses introspection to find any properties that need to be registered with type registry
60             ViewRegistry.Instance.Register(CreateInstance, typeof(Spin));
61         }
62
63         /// <summary>
64         /// Creates an initialized Spin.
65         /// </summary>
66         public Spin() : base(typeof(Spin).Name, CustomViewBehaviour.RequiresKeyboardNavigationSupport)
67         {
68         }
69
70         /// <summary>
71         /// Override method of OnInitialize() for CustomView class.<br>
72         /// This method is called after the Control has been initialized.<br>
73         /// Derived classes should do any second phase initialization by overriding this method.<br>
74         /// </summary>
75         public override void OnInitialize()
76         {
77             // Initialize the propertiesControl
78             _arrowImage = "/home/owner/apps_rw/NUISamples.TizenTV/res/images/arrow.png";
79             _textBackgroundColor = new Color(0.6f, 0.6f, 0.6f, 1.0f);
80             _currentValue = 0;
81             _minValue = 0;
82             _maxValue = 0;
83             _singleStep = 1;
84             _maxTextLength = 0;
85
86             // Create image visual for the arrow keys
87             _arrowVisualPropertyIndex = RegisterProperty("ArrowImage", new PropertyValue(_arrowImage), Tizen.NUI.PropertyAccessMode.ReadWrite);
88             _arrowVisual = VisualFactory.Get().CreateVisual(_arrowImage, new Uint16Pair(150, 150));
89             RegisterVisual(_arrowVisualPropertyIndex, _arrowVisual);
90
91             // Create a text field
92             _textField = new TextField();
93             _textField.AnchorPoint = Tizen.NUI.AnchorPoint.Center;
94             _textField.WidthResizePolicy = ResizePolicyType.SizeRelativeToParent;
95             _textField.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent;
96             _textField.SizeModeFactor = new Vector3(1.0f, 0.45f, 1.0f);
97             _textField.PlaceholderText = "----";
98             _textField.BackgroundColor = _textBackgroundColor;
99             _textField.HorizontalAlignment = HorizontalAlignment.HorizontalAlignCenter;
100             _textField.VerticalAlignment = VerticalAlignment.VerticalAlignCenter;
101             _textField.Focusable = (true);
102             _textField.Name = "_textField";
103             _textField.Position2D = new Position2D(0, 40);
104
105             this.Add(_textField);
106
107             _textField.FocusGained += TextFieldKeyInputFocusGained;
108             _textField.FocusLost += TextFieldKeyInputFocusLost;
109         }
110
111         /// <summary>
112         /// Override method of GetNaturalSize() for CustomView class.<br>
113         /// Return the natural size of the actor.<br>
114         /// </summary>
115         /// <returns> Natural size of this Spin itself</returns>
116         public override Size GetNaturalSize()
117         {
118             return new Size(150.0f, 150.0f, 0.0f);
119         }
120
121         /// <summary>
122         /// Event handler when the TextField in Spin gets the Key focus.<br>
123         /// Make sure when the current spin that takes input focus also takes the keyboard focus.<br>
124         /// For example, when you tap the spin directly.<br>
125         /// </summary>
126         /// <param name="source">Sender of this event</param>
127         /// <param name="e">Event arguments</param>
128         public void TextFieldKeyInputFocusGained(object source, EventArgs e)
129         {
130             FocusManager.Instance.SetCurrentFocusView(_textField);
131         }
132
133         /// <summary>
134         /// Event handler when the TextField in Spin looses it's Key focus
135         /// </summary>
136         /// <param name="source"></param>
137         /// <param name="e"></param>
138         public void TextFieldKeyInputFocusLost(object source, EventArgs e)
139         {
140             int previousValue = _currentValue;
141
142             // If the input value is invalid, change it back to the previous valid value
143             if (int.TryParse(_textField.Text, out _currentValue))
144             {
145                 if (_currentValue < _minValue || _currentValue > _maxValue)
146                 {
147                     _currentValue = previousValue;
148                 }
149             }
150             else
151             {
152                 _currentValue = previousValue;
153             }
154
155             // Otherwise take the new value
156             this.Value = _currentValue;
157         }
158
159         /// <summary>
160         /// Override method of GetNextKeyboardFocusableView() for CustomView class.<br>
161         /// Gets the next key focusable view in this View towards the given direction.<br>
162         /// A View needs to override this function in order to support two dimensional key navigation.<br>
163         /// </summary>
164         /// <param name="currentFocusedView">The current focused view</param>
165         /// <param name="direction">The direction to move the focus towards</param>
166         /// <param name="loopEnabled">Whether the focus movement should be looped within the control</param>
167         /// <returns>The next keyboard focusable view in this control or an empty handle if no view can be focused</returns>
168         public override View GetNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
169         {
170             // Respond to Up/Down keys to change the value while keeping the current spin focused
171             View nextFocusedView = currentFocusedView;
172             if (direction == View.FocusDirection.Up)
173             {
174                 this.Value += this.Step;
175                 nextFocusedView = _textField;
176             }
177             else if (direction == View.FocusDirection.Down)
178             {
179                 this.Value -= this.Step;
180                 nextFocusedView = _textField;
181             }
182             else
183             {
184                 // Return a native empty handle as nothing can be focused in the left or right
185                 nextFocusedView = new View();
186                 nextFocusedView.Reset();
187             }
188
189             return nextFocusedView;
190         }
191
192         /// <summary>
193         /// Value to be set in Spin.
194         /// </summary>
195         [ScriptableProperty()]
196         public int Value
197         {
198             get
199             {
200                 return _currentValue;
201             }
202             set
203             {
204
205                 Tizen.Log.Debug("NUI", "Value set to " + value);
206                 _currentValue = value;
207
208                 // Make sure no invalid value is accepted
209                 if (_currentValue < _minValue)
210                 {
211                     _currentValue = _minValue;
212                 }
213
214                 if (_currentValue > _maxValue)
215                 {
216                     _currentValue = _maxValue;
217                 }
218
219                 _textField.Text = _currentValue.ToString();
220             }
221         }
222         
223         /// <summary>
224         /// Minimum Value of Spin Value.
225         /// </summary>
226         // MinValue property of type int:
227         [ScriptableProperty()]
228         public int MinValue
229         {
230             get
231             {
232                 return _minValue;
233             }
234             set
235             {
236                 _minValue = value;
237             }
238         }
239
240         /// <summary>
241         /// Maximum Value of Spin Value.
242         /// </summary>
243         // MaxValue property of type int:
244         [ScriptableProperty()]
245         public int MaxValue
246         {
247             get
248             {
249                 return _maxValue;
250             }
251             set
252             {
253                 _maxValue = value;
254             }
255         }
256
257         /// <summary>
258         ///  Increasing, decresing step of Spin Value when Up or Down key is pressed.
259         /// </summary>
260         // Step property of type int:
261         [ScriptableProperty()]
262         public int Step
263         {
264             get
265             {
266                 return _singleStep;
267             }
268             set
269             {
270                 _singleStep = value;
271             }
272         }
273
274         /// <summary>
275         /// Wrapping enabled status.
276         /// </summary>
277         // WrappingEnabled property of type bool:
278         [ScriptableProperty()]
279         public bool WrappingEnabled
280         {
281             get
282             {
283                 return _wrappingEnabled;
284             }
285             set
286             {
287                 _wrappingEnabled = value;
288             }
289         }
290
291         /// <summary>
292         ///  Text point size of Spin Value.
293         /// </summary>
294         // TextPointSize property of type int:
295         [ScriptableProperty()]
296         public int TextPointSize
297         {
298             get
299             {
300                 return _pointSize;
301             }
302             set
303             {
304                 _pointSize = value;
305                 _textField.PointSize = _pointSize;
306             }
307         }
308
309         /// <summary>
310         /// The color of Spin Value.
311         /// </summary>
312         // TextColor property of type Color:
313         [ScriptableProperty()]
314         public Color TextColor
315         {
316             get
317             {
318                 return _textColor;
319             }
320             set
321             {
322                 Tizen.Log.Debug("NUI", "TextColor set to " + value.R + "," + value.G + "," + value.B);
323
324                 _textColor = value;
325                 _textField.TextColor = _textColor;
326             }
327         }
328
329         /// <summary>
330         /// Maximum text lengh of Spin Value.
331         /// </summary>
332         // MaxTextLength property of type int:
333         [ScriptableProperty()]
334         public int MaxTextLength
335         {
336             get
337             {
338                 return _maxTextLength;
339             }
340             set
341             {
342                 _maxTextLength = value;
343                 _textField.MaxLength = _maxTextLength;
344             }
345         }
346
347         /// <summary>
348         /// Reference of TextField of Spin.
349         /// </summary>
350         public TextField SpinText
351         {
352             get
353             {
354                 return _textField;
355             }
356             set
357             {
358                 _textField = value;
359             }
360         }
361
362         /// <summary>
363         /// Show indicator image, for example Up/Down Arrow image.
364         /// </summary>
365         // Indicator property of type string:
366         public string IndicatorImage
367         {
368             get
369             {
370                 return _arrowImage;
371             }
372             set
373             {
374                 _arrowImage = value;
375                 _arrowVisual = VisualFactory.Get().CreateVisual(_arrowImage, new Uint16Pair(150, 150));
376                 RegisterVisual(_arrowVisualPropertyIndex, _arrowVisual);
377             }
378         }
379     }
380 }