Merge "Refactoring Button: remove painter" into tizen
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / toolkit / focus-manager / keyboard-focus-manager-api.cpp
1 /*
2  * Copyright (c) 2015 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 // CLASS HEADER
19 #include "keyboard-focus-manager-api.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <actors/actor-wrapper.h>
26
27 namespace Dali
28 {
29
30 namespace V8Plugin
31 {
32
33 namespace // un named namespace
34 {
35
36
37 Toolkit::Control::KeyboardFocusNavigationDirection  GetDirection( std::string name,  v8::Isolate* isolate )
38 {
39   if( name == "left")
40   {
41     return Dali::Toolkit::Control::Left;
42   }
43   if( name == "right")
44   {
45     return  Dali::Toolkit::Control::Right;
46   }
47   if( name == "up")
48   {
49     return  Dali::Toolkit::Control::Up;
50   }
51   if( name == "down")
52   {
53     return  Dali::Toolkit::Control::Down;
54   }
55
56   DALI_SCRIPT_EXCEPTION( isolate,  "direction not found ( wanted left,right,up,down)" );
57
58
59   return Dali::Toolkit::Control::Up;
60
61 }
62 }; //un-named namespace
63
64 /**
65  * Move the keyboard focus to the given actor.
66  * Only one actor can be focused at the same time.  The actor must
67  * be in the stage already and be keyboard focusable.
68  *
69  * @method setCurrentFocusActor
70  * @for KeyboardFocusManager
71  * @param {Object} Actor
72  */
73 void KeyboardFocusManagerApi::SetCurrentFocusActor( const v8::FunctionCallbackInfo< v8::Value >& args )
74 {
75   v8::Isolate* isolate = args.GetIsolate();
76   v8::HandleScope handleScope( isolate );
77   bool found( false );
78   Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
79   if( ! found )
80   {
81     DALI_SCRIPT_EXCEPTION( isolate,  "no actor found" );
82     return;
83   }
84
85   Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor( actor );
86 }
87
88 /**
89  * Get the current focused actor.
90  *
91  * @method getCurrentFocusActor
92  * @for KeyboardFocusManager
93  * @return {Object} Actor
94  */
95 void KeyboardFocusManagerApi::GetCurrentFocusActor( const v8::FunctionCallbackInfo< v8::Value >& args )
96 {
97   v8::Isolate* isolate = args.GetIsolate();
98   v8::HandleScope handleScope( isolate );
99
100   v8::Handle < v8::Object > wrappedActor = ActorWrapper::WrapActor( isolate, Toolkit::KeyboardFocusManager::Get().GetCurrentFocusActor() );
101   args.GetReturnValue().Set( wrappedActor );
102 }
103
104 /**
105  * Move the focus to the next focusable actor in the focus
106  * chain in the given direction (according to the focus traversal
107  * order).
108  *
109  * @method moveFocus
110  * @for KeyboardFocusManager
111  * @param {String} direction The direction of focus movement ( left, right, up, down)
112  */
113 void KeyboardFocusManagerApi::MoveFocus( const v8::FunctionCallbackInfo< v8::Value >& args )
114 {
115   v8::Isolate* isolate = args.GetIsolate();
116   v8::HandleScope handleScope( isolate );
117
118   bool found(false);
119   std::string direction = V8Utils::GetStringParameter( PARAMETER_0, found,isolate, args);
120
121   if( ! found )
122   {
123     DALI_SCRIPT_EXCEPTION( isolate,  "no direction found" );
124     return;
125   }
126
127   Toolkit::Control::KeyboardFocusNavigationDirection dir = GetDirection( direction, isolate );
128
129   Toolkit::KeyboardFocusManager::Get().MoveFocus( dir );
130 }
131 /**
132  * Clear the focus from the current focused actor if any, so
133  * that no actor is focused in the focus chain.
134  * It will emit focus changed signal without current focused actor
135  * @method clearFocus
136  * @for KeyboardFocusManager
137  */
138 void KeyboardFocusManagerApi::ClearFocus( const v8::FunctionCallbackInfo< v8::Value >& args )
139 {
140   Toolkit::KeyboardFocusManager::Get().ClearFocus();
141 }
142
143 /**
144  * Set whether an actor is a focus group that can limit the
145  * scope of focus movement to its child actors in the focus chain.
146  *
147  * @method setAsFocusGroup
148  * @param {Boolean} enabled Whether the focus movement should be looped
149  * @for KeyboardFocusManager
150  */
151 void KeyboardFocusManagerApi::SetAsFocusGroup( const v8::FunctionCallbackInfo< v8::Value >& args )
152 {
153   //  void SetAsFocusGroup(Actor actor, bool isFocusGroup);
154   v8::Isolate* isolate = args.GetIsolate();
155   v8::HandleScope handleScope( isolate );
156   bool found( false );
157
158   Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
159
160   if( !found )
161   {
162     DALI_SCRIPT_EXCEPTION( isolate,  "missing actor param" );
163     return;
164   }
165
166   bool isFocusGroup = V8Utils::GetBooleanParameter( PARAMETER_1, found,isolate, args);
167   if( ! found )
168   {
169     DALI_SCRIPT_EXCEPTION( isolate,  "boolean param missing" );
170     return;
171   }
172
173   Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup( actor,isFocusGroup );
174
175 }
176 /**
177  * Check whether the actor is set as a focus group or not.
178  * @method isFocusGroup
179  * @param {Object} Actor  The actor to be checked.
180  * @for KeyboardFocusManager
181  */
182 void KeyboardFocusManagerApi::IsFocusGroup( const v8::FunctionCallbackInfo< v8::Value >& args )
183 {
184   v8::Isolate* isolate = args.GetIsolate();
185   v8::HandleScope handleScope( isolate );
186   bool found( false );
187   Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
188
189   if( !found )
190   {
191     DALI_SCRIPT_EXCEPTION( isolate, "Missing actor parameter");
192     return;
193   }
194   args.GetReturnValue().Set( v8::Boolean::New( isolate,   Toolkit::KeyboardFocusManager::Get().IsFocusGroup(actor) ) );
195 }
196
197 /**
198  * Returns the closest ancestor of the given actor that is a focus group.
199  * @method getFocusGroup
200  * @param {Object} Actor  The actor to be checked.
201  * @for KeyboardFocusManager
202  */
203 void KeyboardFocusManagerApi::GetFocusGroup( const v8::FunctionCallbackInfo< v8::Value >& args )
204 {
205   v8::Isolate* isolate = args.GetIsolate();
206   v8::HandleScope handleScope( isolate );
207   bool found( false );
208
209   Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
210   if( !found )
211   {
212     DALI_SCRIPT_EXCEPTION( isolate, "Missing actor parameter");
213     return;
214   }
215   Actor retActor  = Toolkit::KeyboardFocusManager::Get().GetFocusGroup( actor );
216   v8::Handle < v8::Object > wrappedActor = ActorWrapper::WrapActor( isolate, retActor );
217   args.GetReturnValue().Set( wrappedActor );
218
219 }
220
221 /**
222  * Get whether the focus movement should be looped within the same focus group.
223  * @method setFocusGroupLoop
224  * @param {Object} Actor  he actor to be set as a focus group.
225  * @param {Boolean} isFocusGroup  Whether to set the actor as a focus group or not.
226  * @for KeyboardFocusManager
227  */
228 void KeyboardFocusManagerApi::SetFocusGroupLoop( const v8::FunctionCallbackInfo< v8::Value >& args )
229 {
230   v8::Isolate* isolate = args.GetIsolate();
231   v8::HandleScope handleScope( isolate );
232
233   bool found(false);
234   bool enable = V8Utils::GetBooleanParameter( PARAMETER_0, found,isolate, args);
235   if( !found )
236   {
237     DALI_SCRIPT_EXCEPTION( isolate,  "boolean param missing" );
238     return;
239   }
240   Toolkit::KeyboardFocusManager::Get().SetFocusGroupLoop( enable );
241 }
242
243 /**
244  * Get whether the focus movement should be looped within the same focus group.
245  * @method getFocusGroupLoop
246  * @return  {Boolean} Whether the focus movement should be looped
247  * @for KeyboardFocusManager
248  */
249 void KeyboardFocusManagerApi::GetFocusGroupLoop( const v8::FunctionCallbackInfo< v8::Value >& args )
250 {
251   v8::Isolate* isolate = args.GetIsolate();
252   v8::HandleScope handleScope( isolate );
253
254   args.GetReturnValue().Set( v8::Boolean::New( isolate,   Toolkit::KeyboardFocusManager::Get().GetFocusGroupLoop()) );
255 }
256
257 /**
258  * Set the focus indicator actor.
259  *
260  * This will replace the default focus indicator actor in
261  * KeyboardFocusManager and will be added to the focused actor as a
262  * highlight.
263  * @method setFocusIndicatorActor
264  * @param {Object} Actor The indicator actor to be added
265  * @for KeyboardFocusManager
266  */
267 void KeyboardFocusManagerApi::SetFocusIndicatorActor( const v8::FunctionCallbackInfo< v8::Value >& args )
268 {
269   v8::Isolate* isolate = args.GetIsolate();
270   v8::HandleScope handleScope( isolate );
271   bool found(false);
272
273   Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
274
275   if( !found )
276   {
277     DALI_SCRIPT_EXCEPTION( isolate, "Missing actor parameter");
278     return;
279   }
280
281   // the actor may be an empty handle
282   Toolkit::KeyboardFocusManager::Get().SetFocusIndicatorActor( actor );
283 }
284
285
286 /**
287  *  Get the focus indicator actor.
288  *
289  * @method getFocusIndicatorActor
290  * @param {Object} Actor The indicator actor to be added
291  * @for KeyboardFocusManager
292  */
293 void KeyboardFocusManagerApi::GetFocusIndicatorActor( const v8::FunctionCallbackInfo< v8::Value >& args )
294 {
295   v8::Isolate* isolate = args.GetIsolate();
296   v8::HandleScope handleScope( isolate );
297
298   v8::Handle < v8::Object > wrappedActor = ActorWrapper::WrapActor( isolate, Toolkit::KeyboardFocusManager::Get().GetFocusIndicatorActor() );
299   args.GetReturnValue().Set( wrappedActor );
300
301 }
302
303 } // namespace V8Plugin
304
305 } // namespace Dali