2 * Copyright (c) 2021 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-toolkit/devel-api/controls/control-devel.h>
18 #include <dali-toolkit/internal/builder/style.h>
19 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
20 #include <dali-toolkit/public-api/controls/control.h>
21 #include <dali-toolkit/public-api/visuals/visual-properties.h>
22 #include <dali/devel-api/scripting/scripting.h>
23 #include <dali/public-api/object/handle.h>
31 extern const Dali::Scripting::StringEnum ControlStateTable[];
32 extern const unsigned int ControlStateTableCount;
36 StylePtr stylePtr(new Style());
40 void Style::ApplyVisualsAndPropertiesRecursively(
42 const Dictionary<Property::Map>& instancedProperties) const
44 ApplyVisuals(handle, instancedProperties);
45 ApplyProperties(handle);
47 Toolkit::Control control = Toolkit::Control::DownCast(handle);
50 std::string stateName;
51 Property::Value value = control.GetProperty(DevelControl::Property::STATE);
52 Dali::Toolkit::DevelControl::State state = static_cast<Dali::Toolkit::DevelControl::State>(value.Get<int>());
53 stateName = Scripting::GetEnumerationName<Toolkit::DevelControl::State>(state, ControlStateTable, ControlStateTableCount);
55 if(!stateName.empty())
57 // Look up state in states table:
58 const StylePtr* stylePtr = subStates.FindConst(stateName);
61 const StylePtr statePtr(*stylePtr);
63 // We have a state match.
64 statePtr->ApplyVisuals(handle, instancedProperties);
65 statePtr->ApplyProperties(handle);
67 // Apply substate visuals
68 Property::Value value = control.GetProperty(DevelControl::Property::SUB_STATE);
69 std::string subStateName;
70 if(value.Get(subStateName) && !subStateName.empty())
72 const StylePtr* stylePtr = statePtr->subStates.FindConst(subStateName);
75 const StylePtr subStatePtr(*stylePtr);
76 // We have a sub-state match.
77 subStatePtr->ApplyVisuals(handle, instancedProperties);
78 subStatePtr->ApplyProperties(handle);
86 void Style::ApplyVisuals(
88 const Dictionary<Property::Map>& instancedProperties) const
90 ApplyVisuals(handle, visuals, instancedProperties);
93 void Style::ApplyVisuals(
95 const Dictionary<Property::Map>& visualMaps,
96 const Dictionary<Property::Map>& instancedProperties)
98 for(Dictionary<Property::Map>::iterator iter = visualMaps.Begin(); iter != visualMaps.End(); ++iter)
100 const std::string& visualName = (*iter).key;
101 Property::Map map = (*iter).entry;
102 Property::Map* instancedMap = instancedProperties.Find(visualName);
103 ApplyVisual(handle, visualName, map, instancedMap);
107 void Style::ApplyVisual(
109 const std::string& visualName,
110 const Property::Map& visualMap,
111 const Property::Map* instancedProperties)
113 // Check if this visual name is a valid property of handle
114 Dali::Property::Index index = handle.GetPropertyIndex(visualName);
115 if(index != Property::INVALID_INDEX)
117 const Property::Map* applyMap = &visualMap;
118 Property::Map mergedMap;
120 // If there are instanced properties, and the visual types match,
121 // merge them into the visual map
122 if(instancedProperties)
124 Property::Value* instanceTypeValue = instancedProperties->Find(Toolkit::Visual::Property::TYPE);
125 Property::Value* newTypeValue = visualMap.Find(Toolkit::Visual::Property::TYPE, VISUAL_TYPE);
126 if(instanceTypeValue && newTypeValue)
128 int instanceVisualType = -1;
129 int newVisualType = -1;
130 Scripting::GetEnumerationProperty(*instanceTypeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, instanceVisualType);
131 Scripting::GetEnumerationProperty(*newTypeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, newVisualType);
133 if(instanceVisualType == newVisualType)
135 // Same type - merge remaining instance data
136 mergedMap.Merge(visualMap);
137 mergedMap.Merge(*instancedProperties);
138 applyMap = &mergedMap;
143 // Apply the visual property map to the handle
144 const Property::Value value(const_cast<Property::Map&>(*applyMap));
145 handle.SetProperty(index, value);
149 void Style::ApplyProperties(Handle handle) const
151 for(Property::Map::SizeType i = 0; i < properties.Count(); ++i)
153 KeyValuePair keyValue = properties.GetKeyValue(i);
154 if(keyValue.first.type == Property::Key::INDEX)
156 handle.SetProperty(keyValue.first.indexKey, keyValue.second);
168 } // namespace Internal
169 } // namespace Toolkit