2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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/public-api/visuals/visual-properties.h>
22 #include <dali-toolkit/internal/builder/style.h>
23 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 extern const Dali::Scripting::StringEnum ControlStateTable[];
34 extern const unsigned int ControlStateTableCount;
38 StylePtr stylePtr( new Style() );
42 void Style::ApplyVisualsAndPropertiesRecursively(
44 const Dictionary<Property::Map>& instancedProperties ) const
46 ApplyVisuals( handle, instancedProperties );
47 ApplyProperties( handle );
49 Toolkit::Control control = Toolkit::Control::DownCast(handle);
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 );
57 if( ! stateName.empty() )
59 // Look up state in states table:
60 const StylePtr* stylePtr = subStates.FindConst( stateName );
63 const StylePtr statePtr(*stylePtr);
65 // We have a state match.
66 statePtr->ApplyVisuals( handle, instancedProperties );
67 statePtr->ApplyProperties( handle );
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() )
74 const StylePtr* stylePtr = statePtr->subStates.FindConst( subStateName );
77 const StylePtr subStatePtr(*stylePtr);
78 // We have a sub-state match.
79 subStatePtr->ApplyVisuals( handle, instancedProperties );
80 subStatePtr->ApplyProperties( handle );
88 void Style::ApplyVisuals(
90 const Dictionary<Property::Map>& instancedProperties ) const
92 ApplyVisuals( handle, visuals, instancedProperties );
95 void Style::ApplyVisuals(
97 const Dictionary<Property::Map>& visualMaps,
98 const Dictionary<Property::Map>& instancedProperties )
100 for( Dictionary<Property::Map>::iterator iter = visualMaps.Begin(); iter != visualMaps.End() ; ++iter )
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 );
109 void Style::ApplyVisual(
111 const std::string& visualName,
112 const Property::Map& visualMap,
113 const Property::Map* instancedProperties )
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 )
119 const Property::Map* applyMap = &visualMap;
120 Property::Map mergedMap;
122 // If there are instanced properties, and the visual types match,
123 // merge them into the visual map
124 if( instancedProperties )
126 Property::Value* instanceTypeValue = instancedProperties->Find( Toolkit::Visual::Property::TYPE);
127 Property::Value* newTypeValue = visualMap.Find( Toolkit::Visual::Property::TYPE, VISUAL_TYPE );
128 if( instanceTypeValue && newTypeValue )
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 );
135 if( instanceVisualType == newVisualType )
137 // Same type - merge remaining instance data
138 mergedMap.Merge( visualMap );
139 mergedMap.Merge( *instancedProperties );
140 applyMap = &mergedMap;
145 // Apply the visual property map to the handle
146 const Property::Value value(const_cast<Property::Map&>(*applyMap));
147 handle.SetProperty( index, value );
151 void Style::ApplyProperties( Handle handle ) const
153 for( Property::Map::SizeType i=0; i<properties.Count(); ++i )
155 KeyValuePair keyValue = properties.GetKeyValue( i );
156 if( keyValue.first.type == Property::Key::INDEX )
158 handle.SetProperty( keyValue.first.indexKey, keyValue.second );