Updating Builder and Control to handle styled States
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / builder / tree-node.cpp
1 /*
2  * Copyright (c) 2016 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 // EXTERNAL INCLUDES
19 #include <cstring>
20 #include <algorithm>
21
22 // INTERNAL INCLUDES
23 #include "dali-toolkit/devel-api/builder/tree-node.h"
24 #include "dali-toolkit/internal/builder/tree-node-manipulator.h"
25
26
27 namespace Dali
28 {
29
30 bool CaseInsensitiveCharacterCompare( unsigned char a, unsigned char b )
31 {
32   // Converts to lower case in the current locale.
33   return std::tolower( a ) == std::tolower( b );
34 }
35
36 /**
37  * return true if the lower cased ASCII strings are equal.
38  */
39 bool CaseInsensitiveStringCompare( const std::string& a, const std::string& b )
40 {
41   bool result = false;
42   if( a.length() == b.length() )
43   {
44     result = std::equal( a.begin(), a.end(), b.begin(), CaseInsensitiveCharacterCompare );
45   }
46   return result;
47 }
48
49 namespace Toolkit
50 {
51
52 TreeNode::TreeNode()
53   : mName(NULL),
54     mParent(NULL),
55     mNextSibling(NULL),
56     mFirstChild(NULL),
57     mLastChild(NULL),
58     mStringValue(NULL),
59     mType(TreeNode::IS_NULL),
60     mSubstituion(false)
61 {
62 }
63
64 TreeNode::~TreeNode()
65 {
66
67 }
68
69 const char* TreeNode::GetName() const
70 {
71   return mName;
72 }
73
74 TreeNode::NodeType TreeNode::GetType() const
75 {
76   return mType;
77 }
78
79 const char* TreeNode::GetString() const
80 {
81   return mStringValue;
82 }
83
84 bool TreeNode::HasSubstitution() const
85 {
86   return mSubstituion;
87 }
88
89 float TreeNode::GetFloat() const
90 {
91   return mFloatValue;
92 }
93
94 int TreeNode::GetInteger() const
95 {
96   return mIntValue;
97 }
98
99 bool TreeNode::GetBoolean() const
100 {
101   return mIntValue == 1 ? true : false;
102 }
103
104 size_t TreeNode::Size() const
105 {
106   size_t c = 0;
107   TreeNode* p = mFirstChild;
108   while(p)
109   {
110     c++;
111     p = p->mNextSibling;
112   }
113   return c;
114 }
115
116 size_t TreeNode::Count(const std::string& childName) const
117 {
118   const TreeNode* c = GetChild(childName);
119   if(c)
120   {
121     return c->Size();
122   }
123   else
124   {
125     return 0;
126   }
127 }
128
129 const TreeNode* TreeNode::GetChild(const std::string& childName) const
130 {
131   const TreeNode* p = mFirstChild;
132   while(p)
133   {
134     if(p->mName && (std::string(p->mName) == childName) )
135     {
136       return p;
137     }
138     p = p->mNextSibling;
139   }
140   return NULL;
141 }
142
143
144 const TreeNode* TreeNode::GetChildIgnoreCase(const std::string& childName) const
145 {
146   const TreeNode* p = mFirstChild;
147   while(p)
148   {
149     if(p->mName)
150     {
151       std::string nodeName(p->mName);
152       if( CaseInsensitiveStringCompare( nodeName, childName) )
153       {
154         return p;
155       }
156     }
157     p = p->mNextSibling;
158   }
159   return NULL;
160 }
161
162 const TreeNode* TreeNode::Find(const std::string& childName) const
163 {
164   if(mName && std::string(mName) == childName)
165   {
166     return this;
167   }
168   else
169   {
170     return Internal::FindIt(childName, this);
171   }
172 }
173
174 TreeNode::ConstIterator TreeNode::CBegin() const
175 {
176   return ConstIterator(mFirstChild);
177 }
178
179
180 TreeNode::ConstIterator TreeNode::CEnd() const
181 {
182   return ConstIterator(NULL);
183 }
184
185
186 TreeNode::ConstIterator::ConstIterator(TreeNode* v) : mNode(v)
187 {
188
189 }
190
191 TreeNode::ConstIterator& TreeNode::ConstIterator::operator ++()
192 {
193   if(mNode)
194   {
195     mNode = mNode->mNextSibling;
196   }
197   else
198   {
199     mNode = NULL;
200   }
201   return *this;
202 }
203
204 TreeNode::ConstIterator TreeNode::ConstIterator::operator ++(int)
205 {
206   TreeNode::ConstIterator ret(mNode);
207
208   if(mNode)
209   {
210     mNode = mNode->mNextSibling;
211   }
212   else
213   {
214     mNode = NULL;
215   }
216   return ret;
217 }
218
219 TreeNode::KeyNodePair TreeNode::ConstIterator::operator *()
220 {
221   return KeyNodePair(mNode->mName, *mNode);
222 }
223
224 bool TreeNode::ConstIterator::operator!=( const TreeNode::ConstIterator& rhs ) const
225 {
226   return mNode != rhs.mNode;
227 }
228
229 } // namespace Toolkit
230
231 } // namespace Dali