00c327a5a90b38c722569d56433912d001545043
[platform/core/csapi/nui.git] / NUISamples / NUISamples / NUISamples.TizenTV / examples / date-picker.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using System.Runtime.InteropServices;
20 using Tizen.NUI;
21 using Tizen.NUI.BaseComponents;
22
23
24 namespace DatePickerTest
25 {
26     // A spin control (for continously changing values when users can easily predict a set of values)
27
28     class Example : NUIApplication
29     {
30         private FlexContainer _container;   // Flex container to hold spin controls
31         private Spin _spinYear;  // spin control for year
32         private Spin _spinMonth; // spin control for month
33         private Spin _spinDay;   // spin control for day
34
35         public Example() : base()
36         {
37
38         }
39
40         protected override void OnCreate()
41         {
42             base.OnCreate();
43             Initialize();
44         }
45
46         public void Initialize()
47         {
48             Stage stage = Stage.Instance;
49             stage.BackgroundColor = Color.White;
50
51             // Create a container for the spins
52             _container = new FlexContainer();
53
54             //_container.ParentOrigin = ParentOrigin.Center;
55             _container.AnchorPoint = AnchorPoint.Center;
56             _container.FlexDirection = FlexContainer.FlexDirectionType.Row;
57             _container.Size = new Vector3(480.0f, 150.0f, 0.0f);
58             _container.Position2D = new Position2D(400, 400);
59
60             stage.GetDefaultLayer().Add(_container);
61
62             // Create a Spin control for year
63             _spinYear = new Spin();
64             _spinYear.AnchorPoint = AnchorPoint.Center;
65             _spinYear.Flex = 0.3f;
66             _spinYear.FlexMargin = new Vector4(5.0f, 0.0f, 5.0f, 0.0f);
67             _container.Add(_spinYear);
68
69             _spinYear.MinValue = 1900;
70             _spinYear.MaxValue = 2100;
71             _spinYear.Value = 2016;
72             _spinYear.Step = 1;
73             _spinYear.MaxTextLength = 4;
74             _spinYear.TextPointSize = 15;
75             _spinYear.TextColor = Color.Red;
76             _spinYear.Focusable = (true);
77             _spinYear.Name = "_spinYear";
78
79             // Create a Spin control for month
80             _spinMonth = new Spin();
81             _spinMonth.AnchorPoint = AnchorPoint.Center;
82             _spinMonth.Flex = 0.3f;
83             _spinMonth.FlexMargin = new Vector4(5.0f, 0.0f, 5.0f, 0.0f);
84             _container.Add(_spinMonth);
85
86             _spinMonth.MinValue = 1;
87             _spinMonth.MaxValue = 12;
88             _spinMonth.Value = 10;
89             _spinMonth.Step = 1;
90             _spinMonth.MaxTextLength = 2;
91             _spinMonth.TextPointSize = 15;
92             _spinMonth.TextColor = Color.Green;
93             _spinMonth.Focusable = (true);
94             _spinMonth.Name = "_spinMonth";
95
96             // Create a Spin control for day
97             _spinDay = new Spin();
98             _spinDay.AnchorPoint = AnchorPoint.Center;
99             _spinDay.Flex = 0.3f;
100             _spinDay.FlexMargin = new Vector4(5.0f, 0.0f, 5.0f, 0.0f);
101             _container.Add(_spinDay);
102
103             _spinDay.MinValue = 1;
104             _spinDay.MaxValue = 31;
105             _spinDay.Value = 26;
106             _spinDay.Step = 1;
107             _spinDay.MaxTextLength = 2;
108             _spinDay.TextPointSize = 15;
109             _spinDay.TextColor = Color.Blue;
110             _spinDay.Focusable = (true);
111             _spinDay.Name = "_spinDay";
112
113             FocusManager keyboardFocusManager = FocusManager.Instance;
114             keyboardFocusManager.PreFocusChange += OnKeyboardPreFocusChange;
115             keyboardFocusManager.FocusedViewEnterKeyPressed += OnFocusedActorEnterKeyPressed;
116
117         }
118
119         private View OnKeyboardPreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
120         {
121             View nextFocusView = e.ProposedView;
122
123             // When nothing has been focused initially, focus the text field in the first spin
124             if (!e.CurrentView && !e.ProposedView)
125             {
126                 nextFocusView = _spinYear.SpinText;
127             }
128             else if(e.Direction == View.FocusDirection.Left)
129             {
130                 // Move the focus to the spin in the left of the current focused spin
131                 if(e.CurrentView == _spinMonth.SpinText)
132                 {
133                     nextFocusView = _spinYear.SpinText;
134                 }
135                 else if(e.CurrentView == _spinDay.SpinText)
136                 {
137                     nextFocusView = _spinMonth.SpinText;
138                 }
139             }
140             else if(e.Direction == View.FocusDirection.Right)
141             {
142                 // Move the focus to the spin in the right of the current focused spin
143                 if(e.CurrentView == _spinYear.SpinText)
144                 {
145                     nextFocusView = _spinMonth.SpinText;
146                 }
147                 else if(e.CurrentView == _spinMonth.SpinText)
148                 {
149                     nextFocusView = _spinDay.SpinText;
150                 }
151             }
152
153             return nextFocusView;
154         }
155
156         private void OnFocusedActorEnterKeyPressed(object source, FocusManager.FocusedViewEnterKeyEventArgs e)
157         {
158             // Make the text field in the current focused spin to take the key input
159             KeyInputFocusManager manager = KeyInputFocusManager.Get();
160
161             if (e.View == _spinYear.SpinText)
162             {
163                 if (manager.GetCurrentFocusControl() != _spinYear.SpinText)
164                 {
165                     manager.SetFocus(_spinYear.SpinText);
166                 }
167             }
168             else if (e.View == _spinMonth.SpinText)
169             {
170                 if (manager.GetCurrentFocusControl() != _spinMonth.SpinText)
171                 {
172                     manager.SetFocus(_spinMonth.SpinText);
173                 }
174             }
175             else if (e.View == _spinDay.SpinText)
176             {
177                 if (manager.GetCurrentFocusControl() != _spinDay.SpinText)
178                 {
179                     manager.SetFocus(_spinDay.SpinText);
180                 }
181             }
182         }
183
184         /// <summary>
185         /// The main entry point for the application.
186         /// </summary>
187         [STAThread]
188         static void _Main(string[] args)
189         {
190             Example example = new Example();
191             example.Run(args);
192         }
193     }
194 }