Fix feedback style error message.
[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/devel-api/visuals/visual-properties-devel.h>
22 #include <dali-toolkit/internal/builder/style.h>
23 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
24
25 namespace Dali
26 {
27 namespace Toolkit
28 {
29
30 namespace Internal
31 {
32
33 extern const Dali::Scripting::StringEnum ControlStateTable[];
34 extern const unsigned int ControlStateTableCount;
35
36 StylePtr Style::New()
37 {
38   StylePtr stylePtr( new Style() );
39   return stylePtr;
40 }
41
42 void Style::ApplyVisualsAndPropertiesRecursively(
43   Handle handle,
44   const Dictionary<Property::Map>& instancedProperties ) const
45 {
46   ApplyVisuals( handle, instancedProperties );
47   ApplyProperties( handle );
48
49   Toolkit::Control control = Toolkit::Control::DownCast(handle);
50   if( control )
51   {
52     std::string stateName;
53     Property::Value value = control.GetProperty(DevelControl::Property::STATE);
54     Dali::Toolkit::DevelControl::State state = static_cast<Dali::Toolkit::DevelControl::State>(value.Get<int>());
55     stateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( state, ControlStateTable, ControlStateTableCount );
56
57     if( ! stateName.empty() )
58     {
59       // Look up state in states table:
60       const StylePtr* stylePtr = subStates.FindConst( stateName );
61       if( stylePtr )
62       {
63         const StylePtr statePtr(*stylePtr);
64
65         // We have a state match.
66         statePtr->ApplyVisuals( handle, instancedProperties );
67         statePtr->ApplyProperties( handle );
68
69         // Apply substate visuals
70         Property::Value value = control.GetProperty(DevelControl::Property::SUB_STATE);
71         std::string subStateName;
72         if( value.Get( subStateName ) && ! subStateName.empty() )
73         {
74           const StylePtr* stylePtr = statePtr->subStates.FindConst( subStateName );
75           if( stylePtr )
76           {
77             const StylePtr subStatePtr(*stylePtr);
78             // We have a sub-state match.
79             subStatePtr->ApplyVisuals( handle, instancedProperties );
80             subStatePtr->ApplyProperties( handle );
81           }
82         }
83       }
84     }
85   }
86 }
87
88 void Style::ApplyVisuals(
89   Handle handle,
90   const Dictionary<Property::Map>& instancedProperties ) const
91 {
92   ApplyVisuals( handle, visuals, instancedProperties );
93 }
94
95 void Style::ApplyVisuals(
96   Handle handle,
97   const Dictionary<Property::Map>& visualMaps,
98   const Dictionary<Property::Map>& instancedProperties )
99 {
100   for( Dictionary<Property::Map>::iterator iter = visualMaps.Begin(); iter != visualMaps.End() ; ++iter )
101   {
102     const std::string& visualName = (*iter).key;
103     Property::Map map = (*iter).entry;
104     Property::Map* instancedMap = instancedProperties.Find( visualName );
105     ApplyVisual( handle, visualName, map, instancedMap );
106   }
107 }
108
109 void Style::ApplyVisual(
110   Handle handle,
111   const std::string& visualName,
112   const Property::Map& visualMap,
113   const Property::Map* instancedProperties )
114 {
115   // Check if this visual name is a valid property of handle
116   Dali::Property::Index index = handle.GetPropertyIndex( visualName );
117   if( index != Property::INVALID_INDEX )
118   {
119     const Property::Map* applyMap = &visualMap;
120     Property::Map mergedMap;
121
122     // If there are instanced properties, and the visual types match,
123     // merge them into the visual map
124     if( instancedProperties )
125     {
126       Property::Value* instanceTypeValue = instancedProperties->Find( Toolkit::DevelVisual::Property::TYPE);
127       Property::Value* newTypeValue = visualMap.Find( Toolkit::DevelVisual::Property::TYPE, VISUAL_TYPE );
128       if( instanceTypeValue && newTypeValue )
129       {
130         int instanceVisualType=-1;
131         int newVisualType=-1;
132         Scripting::GetEnumerationProperty( *instanceTypeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, instanceVisualType );
133         Scripting::GetEnumerationProperty( *newTypeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, newVisualType );
134
135         if( instanceVisualType == newVisualType )
136         {
137           // Same type - merge remaining instance data
138           mergedMap.Merge( visualMap );
139           mergedMap.Merge( *instancedProperties );
140           applyMap = &mergedMap;
141         }
142       }
143     }
144
145     // Apply the visual property map to the handle
146     const Property::Value value(const_cast<Property::Map&>(*applyMap));
147     handle.SetProperty( index, value );
148   }
149 }
150
151 void Style::ApplyProperties( Handle handle ) const
152 {
153   for( Property::Map::SizeType i=0; i<properties.Count(); ++i )
154   {
155     KeyValuePair keyValue = properties.GetKeyValue( i );
156     if( keyValue.first.type == Property::Key::INDEX )
157     {
158       handle.SetProperty( keyValue.first.indexKey, keyValue.second );
159     }
160   }
161 }
162
163 Style::Style()
164 {
165 }
166 Style::~Style()
167 {
168 }
169
170
171 } // Internal
172 } // Toolkit
173 } // Dali