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