(TextInput) Don't show cut and copy buttons if text not selected
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / builder / tree-node-manipulator.h
1 #ifndef __DALI_SCRIPT_TREE_NODE_MANIPULATOR_H__
2 #define __DALI_SCRIPT_TREE_NODE_MANIPULATOR_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <utility> // pair
22 #include <iterator>
23 #include <vector>
24 #include <cstring>
25
26 // INTERNAL INCLUDES
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali-toolkit/public-api/builder/tree-node.h>
29
30 namespace Dali DALI_IMPORT_API
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal DALI_INTERNAL
37 {
38 typedef std::vector<char> VectorChar;
39 typedef VectorChar::iterator VectorCharIter;
40
41 /*
42  * TreeNodeManipulator performs modification operations on a TreeNode which are
43  * otherwise prohibited on the TreeNode public interface.
44  */
45 class TreeNodeManipulator
46 {
47 public:
48   /*
49    * Constructor
50    * @param node The TreeNode to modify
51    */
52   explicit TreeNodeManipulator(TreeNode* node);
53
54   /*
55    * Create a new TreeNode instance
56    * @return new TreeNode
57    */
58   static TreeNode* NewTreeNode();
59
60   /*
61    * Shallow copy node data
62    * Shallow copy the data but doesnt parent or copy children
63    * @param from Node to copy from
64    * @param to Node to copy to
65    */
66   static void ShallowCopy(const TreeNode* from, TreeNode* to);
67
68   /*
69    * Moves all string data to a new buffer. There must be enough space for all string data.
70    * @param start The buffer start
71    * @param sentinel The end of the buffer
72    */
73   void MoveStrings(VectorCharIter& start, const VectorCharIter& sentinel);
74
75   /*
76    * Remove all children from the node
77    */
78   void RemoveChildren();
79
80   /*
81    * Make a deep copy of the tree.
82    * @param tree The tree to copy
83    * @param numberOfNodes The number of nodes that were copied
84    * @param numberOfChars The size of string data.
85    */
86   static TreeNode* Copy(const TreeNode& tree, int& numberOfNodes, int& numberOfChars);
87
88   /*
89    * Add child to the node
90    * @param child The child to add
91    * @return the added child
92    */
93   TreeNode *AddChild(TreeNode *child);
94
95   /*
96    * Change the type of the Node
97    * NB: If the type changes from a type with children to a value type without children then
98    *     the children are removed
99    * @param type The new type
100    */
101   void SetType( TreeNode::NodeType type);
102
103   /*
104    * Set the name of the node
105    * @param name The name to set
106    */
107   void SetName( const char* name );
108
109   /*
110    * Set the substituion flag
111    * The substitution flag indicates this nodes string value contains a reference to another node
112    * in the tree.
113    * @param on The state
114    */
115   void SetSubstitution( bool on );
116
117   /*
118    * Get the nodes type
119    * @return The nodes type
120    */
121   TreeNode::NodeType GetType() const;
122
123   /*
124    * Get the number of children of the node
125    * @return The number of children
126    */
127   size_t Size() const;
128
129   /*
130    * Set the node as a string value
131    * @param string The string value
132    */
133   void SetString( const char* string );
134
135   /*
136    * Set the node as an integer value
137    * @param i The integer
138    */
139   void SetInteger( int i );
140
141   /*
142    * Set the node as an float value
143    * @param f The float
144    */
145   void SetFloat( float f );
146
147   /*
148    * Set the node as an boolean value
149    * @param b The boolean
150    */
151   void SetBoolean( bool b );
152
153   /*
154    * Get the nodes parent
155    * @return The nodes parent
156    */
157   TreeNode* GetParent() const;
158
159   /*
160    * Get the nodes child by name
161    * @param name The childs name
162    * @return The nodes if found, else NULL
163    */
164   const TreeNode* GetChild(const std::string& name) const;
165
166   /*
167    * @copydoc Dali::Scripting::JsonParser::Write()
168    */
169   void Write(std::ostream& output, int indent) const;
170
171 private:
172   TreeNode *mNode;
173
174   /*
175    * Move the nodes strings to the buffer
176    */
177   void MoveNodeStrings(VectorCharIter& start, const VectorCharIter& sentinel);
178
179   /*
180    * Recursively move child strings to the buffer
181    */
182   void RecurseMoveChildStrings(VectorCharIter& start, const VectorCharIter& sentinel);
183
184   /*
185    * Recursively copy children
186    */
187   static void CopyChildren(const TreeNode* from, TreeNode* to, int& numberNodes, int& numberChars);
188
189   /*
190    * Do write to string stream
191    */
192   void DoWrite(const TreeNode *value, std::ostream& output, int ident) const;
193
194 };
195
196 /*
197  * Collect nodes
198  */
199 struct CollectNodes : public std::unary_function<TreeNode*, void>
200 {
201   CollectNodes() {};
202
203   /*
204    * Call operator to add nodes to the list
205    */
206   result_type operator()(argument_type& n)
207   {
208     DALI_ASSERT_DEBUG(n && "Operation on NULL JSON node");
209     nodes.push_back(n);
210   }
211
212   typedef std::vector<const TreeNode*> VectorNodes;
213   typedef VectorNodes::iterator iterator;
214
215   VectorNodes nodes; ///< List of collected nodes
216 };
217
218 /*
219  * Depth first walk of nodes applying given operation (unary_function)
220  */
221 template <typename Operation>
222 void DepthFirst( TreeNode* node, Operation& operation)
223 {
224   DALI_ASSERT_DEBUG(node && "Operation on NULL JSON node");
225
226   for(TreeNode::ConstIterator iter = node->CBegin(); iter != node->CEnd(); ++iter)
227   {
228     // iterator access is const for external api but were modifying
229     DepthFirst( const_cast<TreeNode*>(&((*iter).second)), operation);
230   }
231
232   operation(node);
233
234 }
235
236 /*
237  * Recursive search on the tree for the child with the given name
238  * @param childName The name to find
239  * @param tree The tree to search
240  * @return the TreeNode if found, else NULL
241  */
242 const TreeNode* FindIt(const std::string& childName, const TreeNode* tree);
243
244 /*
245  * Copy string to a buffer
246  * Raises if there is not enough space in the buffer
247  * @param fromString The string
248  * @param iter The start of the buffer
249  * @param sentinel The buffer sentinel
250  * @return The start of the given buffer
251  */
252 char *CopyString( const char *fromString, VectorCharIter& iter, const VectorCharIter& sentinel);
253
254
255 } // namespace Internal
256
257 } // namespace Toolkit
258
259 } // namespace Dali
260
261 #endif // __DALI_SCRIPT_TREE_NODE_MANIPULATOR_H__