Commit manual merge for dali-csharp.
[platform/core/csapi/nui.git] / NUISamples / NUISamples / NUISamples.TizenTV / examples / date-picker-using-json / date-picker-using-json.cs
1 /*
2  * Copyright (c) 2017 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 namespace DatePickerUsingJson
24 {
25     // A spin control (for continously changing values when users can easily predict a set of values)
26
27     class Example : NUIApplication
28     {
29         private Spin _spinYear;  // spin control for year
30         private Spin _spinMonth; // spin control for month
31         private Spin _spinDay;   // spin control for day
32         private Builder _builder; // DALi Builder
33
34         public Example() : base()
35         {
36         }
37
38         protected override void OnCreate()
39         {
40             base.OnCreate();
41             Initialize();
42         }
43
44         public void Initialize()
45         {
46             Window window = Window.Instance;
47             window.BackgroundColor = Color.White;
48
49             //This is required for the Application which uses JSON theme and style of Dali builder
50             ViewRegistryHelper.Initialize();
51
52             // load date JSON template...
53             _builder = new Builder ();
54
55             // Optional constant to see logging information coming out
56             // of DALi JSON parser (builder)
57             PropertyMap constants = new  PropertyMap();
58             constants.Insert( "CONFIG_SCRIPT_LOG_LEVEL",  new PropertyValue( "Verbose") );
59             _builder.AddConstants( constants );
60
61             _builder.LoadFromFile("/home/owner/apps_rw/NUISamples.TizenTV/res/json/date-picker-template.json" );
62
63             // create the date-picker from the template in the json file
64             BaseHandle handle =  _builder.Create( "date-picker");
65
66             View actorTree =  View.DownCast( handle );
67
68             window.GetDefaultLayer().Add( actorTree );
69
70             View year  = actorTree.FindChildByName("Year");
71             View month  =  actorTree.FindChildByName("Month" );
72             View day  = actorTree.FindChildByName("Day");
73
74             // need to get the actual C# Spin object associated with the actor,
75             _spinYear = View.DownCast<Spin>( year );
76             _spinMonth = View.DownCast<Spin>( month );
77             _spinDay = View.DownCast<Spin>( day );
78
79             _spinYear.Value = 2099;
80             _spinMonth.Value = 5;
81             _spinDay.Value = 23;
82
83             _spinYear.Focusable = (true);
84             _spinMonth.Focusable = (true);
85             _spinDay.Focusable = (true);
86
87             FocusManager keyboardFocusManager = FocusManager.Instance;
88             keyboardFocusManager.PreFocusChange += OnKeyboardPreFocusChange;
89             keyboardFocusManager.FocusedViewEnterKeyPressed += OnFocusedViewEnterKeyPressed;
90
91             StyleManager.Get().ApplyTheme("/home/owner/apps_rw/NUISamples.TizenTV/res/json/date-picker-theme.json");
92         }
93
94         private View OnKeyboardPreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
95         {
96             View nextFocusView = e.ProposedView;
97
98             // When nothing has been focused initially, focus the text field in the first spin
99             if (!e.CurrentView && !e.ProposedView)
100             {
101                 nextFocusView = _spinYear.SpinText;
102             }
103             else if(e.Direction == View.FocusDirection.Left)
104             {
105                 // Move the focus to the spin in the left of the current focused spin
106                 if(e.CurrentView == _spinMonth.SpinText)
107                 {
108                     nextFocusView = _spinYear.SpinText;
109                 }
110                 else if(e.CurrentView == _spinDay.SpinText)
111                 {
112                     nextFocusView = _spinMonth.SpinText;
113                 }
114             }
115             else if(e.Direction == View.FocusDirection.Right)
116             {
117                 // Move the focus to the spin in the right of the current focused spin
118                 if(e.CurrentView == _spinYear.SpinText)
119                 {
120                     nextFocusView = _spinMonth.SpinText;
121                 }
122                 else if(e.CurrentView == _spinMonth.SpinText)
123                 {
124                     nextFocusView = _spinDay.SpinText;
125                 }
126             }
127
128             return nextFocusView;
129         }
130
131         private void OnFocusedViewEnterKeyPressed(object source, FocusManager.FocusedViewEnterKeyEventArgs e)
132         {
133             // Make the text field in the current focused spin to take the key input
134             KeyInputFocusManager manager = KeyInputFocusManager.Get();
135
136             if (e.View == _spinYear.SpinText)
137             {
138                 if (manager.GetCurrentFocusControl() != _spinYear.SpinText)
139                 {
140                     manager.SetFocus(_spinYear.SpinText);
141                 }
142             }
143             else if (e.View == _spinMonth.SpinText)
144             {
145                 if (manager.GetCurrentFocusControl() != _spinMonth.SpinText)
146                 {
147                     manager.SetFocus(_spinMonth.SpinText);
148                 }
149             }
150             else if (e.View == _spinDay.SpinText)
151             {
152                 if (manager.GetCurrentFocusControl() != _spinDay.SpinText)
153                 {
154                     manager.SetFocus(_spinDay.SpinText);
155                 }
156             }
157         }
158
159
160         /// <summary>
161         /// The main entry point for the application.
162         /// </summary>
163         [STAThread]
164         static void _Main(string[] args)
165         {
166             Example example = new Example();
167             example.Run(args);
168         }
169     }
170 }