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