515ddd9205cca0b8f73473d5e47d5c06a3496fbc
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / style.cpp
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 #include <dali/public-api/object/handle.h>
18 #include <dali/devel-api/scripting/scripting.h>
19 #include <dali-toolkit/public-api/controls/control.h>
20 #include <dali-toolkit/devel-api/controls/control-devel.h>
21 #include <dali-toolkit/internal/builder/style.h>
22
23 namespace Dali
24 {
25 namespace Toolkit
26 {
27
28 namespace Internal
29 {
30
31 extern const Dali::Scripting::StringEnum ControlStateTable[];
32 extern const unsigned int ControlStateTableCount;
33
34 StylePtr Style::New()
35 {
36   StylePtr stylePtr( new Style() );
37   return stylePtr;
38 }
39
40 void Style::ApplyVisualsAndPropertiesRecursively( Handle handle ) const
41 {
42   ApplyVisuals( handle );
43   ApplyProperties( handle );
44
45   Toolkit::Control control = Toolkit::Control::DownCast(handle);
46   if( control )
47   {
48     std::string stateName;
49     Property::Value value = control.GetProperty(DevelControl::Property::STATE);
50     Dali::Toolkit::DevelControl::State state = static_cast<Dali::Toolkit::DevelControl::State>(value.Get<int>());
51     stateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( state, ControlStateTable, ControlStateTableCount );
52
53     if( ! stateName.empty() )
54     {
55       // Look up state in states table:
56       const StylePtr* stylePtr = subStates.FindConst( stateName );
57       if( stylePtr )
58       {
59         const StylePtr statePtr(*stylePtr);
60
61         // We have a state match.
62         statePtr->ApplyVisuals( handle );
63         statePtr->ApplyProperties( handle );
64
65         // Apply substate visuals
66         Property::Value value = control.GetProperty(DevelControl::Property::SUB_STATE);
67         std::string subStateName;
68         if( value.Get( subStateName ) && ! subStateName.empty() )
69         {
70           const StylePtr* stylePtr = statePtr->subStates.FindConst( subStateName );
71           if( stylePtr )
72           {
73             const StylePtr subStatePtr(*stylePtr);
74             // We have a sub-state match.
75             subStatePtr->ApplyVisuals( handle );
76             subStatePtr->ApplyProperties( handle );
77           }
78         }
79       }
80     }
81   }
82 }
83
84 void Style::ApplyVisuals( Handle handle ) const
85 {
86   for( Dictionary<Property::Map>::iterator iter = visuals.Begin(); iter != visuals.End() ; ++iter )
87   {
88     const std::string& visualName = (*iter).key;
89     const Property::Map& map = (*iter).entry;
90     Dali::Property::Index index = handle.GetPropertyIndex( visualName );
91     if( index != Property::INVALID_INDEX )
92     {
93       const Property::Value value(const_cast<Property::Map&>(map));
94       handle.SetProperty( index, value );
95     }
96   }
97 }
98
99 void Style::ApplyProperties( Handle handle ) const
100 {
101   for( Property::Map::SizeType i=0; i<properties.Count(); ++i )
102   {
103     KeyValuePair keyValue = properties.GetKeyValue( i );
104     if( keyValue.first.type == Property::Key::INDEX )
105     {
106       handle.SetProperty( keyValue.first.indexKey, keyValue.second );
107     }
108   }
109 }
110
111 Style::Style()
112 {
113 }
114 Style::~Style()
115 {
116 }
117
118
119 } // Internal
120 } // Toolkit
121 } // Dali