Property refactor in dali-toolkit: Toolkit changes
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / table-view / table-view.h
1 #ifndef __DALI_TOOLKIT_TABLE_VIEW_H__
2 #define __DALI_TOOLKIT_TABLE_VIEW_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 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/vector-wrapper.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/controls/control.h>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 namespace Internal DALI_INTERNAL
34 {
35 class TableView;
36 }
37
38 /**
39  * TableView is a layout container for aligning child actors in a grid like layout.
40  * TableView constrains the x and y position and width and height of the child actors.
41  * z position and depth are left intact so that 3D model actors can also be laid out
42  * in a grid without loosing their depth scaling.
43  */
44 class DALI_IMPORT_API TableView : public Control
45 {
46 public:
47
48   /**
49    * @brief The start and end property ranges for this control.
50    */
51   enum PropertyRange
52   {
53     PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
54     PROPERTY_END_INDEX =   PROPERTY_START_INDEX + 1000              ///< Reserve property indices
55   };
56
57   /**
58    * @brief An enumeration of properties belonging to the TableView class.
59    *
60    * LayoutRows: set the height of the rows.
61    * It has the format as follows in script:
62    * @code
63    * "layout-rows":
64       {
65         "0": { "policy": "fixed", "value": 40 },       //@see SetFixedHight
66         "2": { "policy": "relative", "value": 0.33 }   //@see SetRelativeHeight
67       }
68    * @endcode
69    *
70    * LayoutColumns: set the height of the rows.
71    * It has the format as follows in script:
72    * @code
73    * "layout-columns":
74       {
75         "0": { "policy": "fixed", "value": 40 },       //@see SetFixedWidth
76         "2": { "policy": "relative", "value": 0.33 }   //@see SetRelativeWidth
77       }
78    * @endcode
79    */
80   struct Property
81   {
82     enum
83     {
84       ROWS = PROPERTY_START_INDEX, ///< name "rows",           type UNSIGNED_INTEGER
85       COLUMNS,                     ///< name "columns",        type UNSIGNED_INTEGER
86       CELL_PADDING,                ///< name "cell-padding",   type VECTOR2
87       LAYOUT_ROWS,                 ///< name "layout-rows",    type MAP
88       LAYOUT_COLUMNS,              ///< name "layout-columns", type MAP
89     };
90   };
91
92   // Custom properties for where to put the actor, these properties should be registered to the child which would be added to the table
93   static const std::string CELL_INDICES_PROPERTY_NAME;           ///< Property, name "cell-indices", type VECTOR2
94   static const std::string ROW_SPAN_PROPERTY_NAME;               ///< Property, name "row-span",     type FLOAT (Currently builder unable to differentiate integer and float from Json string)
95   static const std::string COLUMN_SPAN_PROPERTY_NAME;            ///< Property, name "column-span",  type FLOAT (Currently builder unable to differentiate integer and float from Json string)
96
97   /**
98    * @brief Describes how the size of a row / column been set
99    */
100   enum LayoutPolicy
101   {
102     Fixed,      ///< Fixed with the given value.
103     Relative,   ///< Calculated as percentage of the remainder after subtracting Padding and Fixed height/width
104     Fill        ///< Get the remainder of the 100% (after subtracting Padding, Fixed and Relative height/ width) divided evenly between 'fill' rows/columns
105   };
106
107   /**
108    * Structure to specify layout position for child actor
109    */
110   struct CellPosition
111   {
112     /**
113      * Constructor to initialise values to defaults for convenience
114      */
115     CellPosition( unsigned int rowIndex = 0, unsigned int columnIndex = 0,
116                     unsigned int rowSpan = 1, unsigned int columnSpan = 1 )
117     : rowIndex( rowIndex ), columnIndex( columnIndex ),
118       rowSpan( rowSpan ), columnSpan( columnSpan )
119     { }
120
121     unsigned int rowIndex;
122     unsigned int columnIndex;
123     unsigned int rowSpan;
124     unsigned int columnSpan;
125   };
126
127   /**
128    * Create a TableView handle; this can be initialised with TableView::New()
129    * Calling member functions with an uninitialised handle is not allowed.
130    */
131   TableView();
132
133   /**
134    * Copy constructor. Creates another handle that points to the same real object
135    * @param handle to copy from
136    */
137   TableView( const TableView& handle );
138
139   /**
140    * Assignment operator. Changes this handle to point to another real object
141    */
142   TableView& operator=( const TableView& handle );
143
144   /**
145    * @brief Destructor
146    *
147    * This is non-virtual since derived Handle types must not contain data or virtual methods.
148    */
149   ~TableView();
150
151   /**
152    * Create the TableView control.
153    * @param[in] initialRows for the table
154    * @param[in] initialColumns for the table
155    * @return A handle to the TableView control.
156    */
157   static TableView New( unsigned int initialRows, unsigned int initialColumns );
158
159   /**
160    * Downcast an Object handle to TableView. If handle points to a TableView the
161    * downcast produces valid handle. If not the returned handle is left uninitialized.
162    * @param[in] handle Handle to an object
163    * @return handle to a TableView or an uninitialized handle
164    */
165   static TableView DownCast( BaseHandle handle );
166
167   /**
168    * Adds a child to the table
169    * If the row or column index is outside the table, the table gets resized bigger
170    * @pre The child actor has been initialized.
171    * @param[in] child to add
172    * @param[in] position for the child
173    * @return true if the addition succeeded, false if the cell is already occupied
174    */
175   bool AddChild( Actor child, CellPosition position );
176
177   /**
178    * Returns a child from the given layout position
179    * Note! if there is no child in this position this method returns an uninitialized
180    * Actor handle
181    * @param[in] position in the table
182    * @return child that was in the cell or an uninitialized handle
183    */
184   Actor GetChildAt( CellPosition position );
185
186   /**
187    * Removes a child from the given layout position
188    * Note! if there is no child in this position this method does nothing
189    * @param[in] position for the child to remove
190    * @return child that was removed or an uninitialized handle
191    */
192   Actor RemoveChildAt( CellPosition position );
193
194   /**
195    * Finds the childs layout position
196    * @param[in] child to search for
197    * @param[out] position for the child
198    * @return true if the child was included in this TableView
199    */
200   bool FindChildPosition( Actor child, CellPosition& position );
201
202   /**
203    * Insert a new row to given index
204    * @param [in] rowIndex of the new row
205    */
206   void InsertRow( unsigned int rowIndex );
207
208   /**
209    * Delete a row from given index
210    * Removed elements are deleted
211    * @param [in] rowIndex of the row to delete
212    */
213   void DeleteRow( unsigned int rowIndex );
214
215   /**
216    * Delete a row from given index
217    * @param [in] rowIndex of the row to delete
218    * @param [out] removed elements
219    */
220   void DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed );
221
222   /**
223    * Insert a new column to given index
224    * @param [in] columnIndex of the new column
225    */
226   void InsertColumn( unsigned int columnIndex );
227
228   /**
229    * Delete a column from given index.
230    * Removed elements are deleted
231    * @param [in] columnIndex of the column to delete
232    */
233   void DeleteColumn( unsigned int columnIndex );
234
235   /**
236    * Delete a column from given index
237    * @param [in] columnIndex of the column to delete
238    * @param [out] removed elements
239    */
240   void DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed );
241
242   /**
243    * Resize the TableView. Note! if the new size is smaller than old,
244    * superfluous actors get removed. If you want to relayout removed children,
245    * use the variant that returns the removed Actors and reinsert them into the table
246    * If an actor spans to a removed row or column it gets removed from the table
247    * @param[in] rows for the table
248    * @param[in] columns for the table
249    */
250   void Resize( unsigned int rows, unsigned int columns );
251
252   /**
253    * Resize the TableView. Note! if the new size is smaller than old,
254    * superfluous actors get removed.
255    * If an actor spans to a removed row or column it gets removed from the table
256    * @param[in] rows for the table
257    * @param[in] columns for the table
258    * @param[out] removed actor handles
259    */
260   void Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed );
261
262   /**
263    * Set horizontal and vertical padding between cells
264    * @param[in] padding width and height
265    */
266   void SetCellPadding( Size padding );
267
268   /**
269    * @return the current padding as width and height
270    */
271   Size GetCellPadding();
272
273   /**
274    * Sets a row to have fixed height
275    * Setting a fixed height of 0 has no effect
276    * @pre The row rowIndex must exist.
277    * @param rowIndex for row with fixed height
278    * @param height in world coordinate units
279    */
280   void SetFixedHeight( unsigned int rowIndex, float height );
281
282   /**
283    * Gets a row's fixed height.
284    * Note! The returned value is valid if it has been set before.
285    * @pre The row rowIndex must exist.
286    * @return height in world coordinate units.
287    */
288   float GetFixedHeight( unsigned int rowIndex ) const;
289
290   /**
291    * Sets a row to have relative height. Relative height means percentage of
292    * the remainder of the table height after subtracting Padding and Fixed height rows
293    * Setting a relative height of 0 has no effect
294    * @pre The row rowIndex must exist.
295    * @param rowIndex for row with relative height
296    * @param heightPercentage between 0.0f and 1.0f
297    */
298   void SetRelativeHeight( unsigned int rowIndex, float heightPercentage );
299
300   /**
301    * Gets a row's relative height.
302    * Note! The returned value is valid if it has been set before.
303    * @pre The row rowIndex must exist.
304    * @return height in percentage units, between 0.0f and 1.0f.
305    */
306   float GetRelativeHeight( unsigned int rowIndex ) const;
307
308   /**
309    * Sets a column to have fixed width
310    * Setting a fixed width of 0 has no effect
311    * @pre The column columnIndex must exist.
312    * @param columnIndex for column with fixed width
313    * @param width in world coordinate units
314    */
315   void SetFixedWidth( unsigned int columnIndex, float width );
316
317   /**
318    * Gets a column's fixed width.
319    * Note! The returned value is valid if it has been set before.
320    * @pre The column columnIndex must exist.
321    * @return width in world coordinate units.
322    */
323   float GetFixedWidth( unsigned int columnIndex ) const;
324
325   /**
326    * Sets a column to have relative width. Relative width means percentage of
327    * the remainder of table width after subtracting Padding and Fixed width columns
328    * Setting a relative width of 0 has no effect
329    * @pre The column columnIndex must exist.
330    * @param columnIndex for column with fixed width
331    * @param widthPercentage between 0.0f and 1.0f
332    */
333   void SetRelativeWidth( unsigned int columnIndex, float widthPercentage );
334
335   /**
336    * Gets a column's relative width.
337    * Note! The returned value is valid if it has been set before.
338    * @pre The column columnIndex must exist.
339    * @return width in percentage units, between 0.0f and 1.0f.
340    */
341   float GetRelativeWidth( unsigned int columnIndex ) const;
342
343   /**
344    * @return the amount of rows in the table
345    */
346   unsigned int GetRows();
347
348   /**
349    * @return the amount of columns in the table
350    */
351   unsigned int GetColumns();
352
353 public: // Not intended for application developers
354
355   /**
356    * Creates a handle using the Toolkit::Internal implementation.
357    * @param[in]  implementation  The Control implementation.
358    */
359   DALI_INTERNAL TableView(Internal::TableView& implementation);
360
361   /**
362    * Allows the creation of this Control from an Internal::CustomActor pointer.
363    * @param[in]  internal  A pointer to the internal CustomActor.
364    */
365   explicit DALI_INTERNAL TableView( Dali::Internal::CustomActor* internal );
366 };
367
368 } // namespace Toolkit
369
370 } // namespace Dali
371
372 #endif // __DALI_TOOLKIT_TABLE_VIEW_H__