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