TableView: Cleanup
[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) 2015 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 #include <dali/public-api/actors/actor-enumerations.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/public-api/controls/control.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal DALI_INTERNAL
35 {
36 class TableView;
37 }
38
39 /**
40  * @brief TableView is a layout container for aligning child actors in a grid like layout.
41  *
42  * TableView constrains the x and y position and width and height of the child actors.
43  * z position and depth are left intact so that 3D model actors can also be laid out
44  * in a grid without loosing their depth scaling.
45  *
46  * @nosubgrouping
47  * <h3>Per-child Custom properties for script supporting:</h3>
48  *
49  * When an actor is add to the tableView through Actor::Add() instead of TableView::AddChild,
50  * the following custom properties of the actor are checked to decide the actor position inside the table.
51  *
52  * These properties are registered dynamically to the child and is non-animatable.
53  *
54  * | %Property Name            | Type        |
55  * |---------------------------|-------------|
56  * | cell-index                | Vector2     |
57  * | row-span                  | float       |
58  * | column-span               | float       |
59  * | cell-horizontal-alignment | string      |
60  * | cell-vertical-alignment   | string      |
61  *
62  * The row-span or column span has integer value, but its type is float here due to the limitation of the builder's ability to differentiate integer and float from Json string.
63  * The available values for cell-horizontal-alignment are: left, center, right.
64  * The available values for cell-vertical-alignment are: top, center, bottom.
65  *
66  * @code
67  * "name":"gallery-1",
68  * "type":"ImageActor",
69  * "image": {
70  *    "filename": "{DALI_IMAGE_DIR}gallery-small-1.jpg"
71  *  },
72  *  "custom-properties": {
73  *     "cell-index":[1,1],  // property to specify the top-left cell this child occupies, if not set, the first available cell is used
74  *     "row-span":3,        // property to specify how many rows this child occupies, if not set, default value is 1
75  *     "column-span": 2,    // property to specify how many columns this child occupies, if nor set, default value is 1
76  *     "cell-horizontal-alignment": "left", // property to specify how to align horizontally inside the cells, if not set, default value is 'left'
77  *     "cell-vertical-alignment": "center"  // property to specify how to align vertically inside the cells, if not set, default value is 'top'
78  *   }
79  * @endcode
80  */
81 class DALI_IMPORT_API TableView : public Control
82 {
83 public:
84
85   /**
86    * @brief The start and end property ranges for this control.
87    */
88   enum PropertyRange
89   {
90     PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
91     PROPERTY_END_INDEX =   PROPERTY_START_INDEX + 1000              ///< Reserve property indices
92   };
93
94   /**
95    * @brief An enumeration of properties belonging to the TableView class.
96    *
97    * LayoutRows: set the height of the rows.
98    * It has the format as follows in script:
99    * @code
100    * "layout-rows":
101       {
102         "0": { "policy": "fixed", "value": 40 },       //@see SetFixedHight
103         "2": { "policy": "relative", "value": 0.33 },  //@see SetRelativeHeight
104         "3": { "policy": "fit", "value":0.0 }          //@see SetFitHeight, the value is not used, its height is decided by the children in this row
105       }
106    * @endcode
107    *
108    * LayoutColumns: set the height of the rows.
109    * It has the format as follows in script:
110    * @code
111    * "layout-columns":
112       {
113         "0": { "policy": "fixed", "value": 40 },       //@see SetFixedWidth
114         "1": { "policy": "fit", "value":0.0 }          //@see SetFitHeight, the value is not used, its width is decided by the children in this column
115         "2": { "policy": "relative", "value": 0.33 }   //@see SetRelativeWidth
116       }
117    * @endcode
118    */
119   struct Property
120   {
121     enum
122     {
123       ROWS = PROPERTY_START_INDEX, ///< name "rows",           type unsigned int
124       COLUMNS,                     ///< name "columns",        type unsigned int
125       CELL_PADDING,                ///< name "cell-padding",   type Vector2
126       LAYOUT_ROWS,                 ///< name "layout-rows",    type Map
127       LAYOUT_COLUMNS,              ///< name "layout-columns", type Map
128     };
129   };
130
131   /**
132    * @brief Describes how the size of a row / column been set
133    */
134   enum LayoutPolicy
135   {
136     FIXED,      ///< Fixed with the given value.
137     RELATIVE,   ///< Calculated as percentage of the remainder after subtracting Padding and Fixed height/width
138     FILL,       ///< Default policy, get the remainder of the 100% (after subtracting Fixed, Fit and Relative height/ width) divided evenly between 'fill' rows/columns
139     FIT         ///< Fit around its children.
140   };
141
142   /**
143    * Structure to specify layout position for child actor
144    */
145   struct CellPosition
146   {
147     /**
148      * Constructor to initialise values to defaults for convenience
149      */
150     CellPosition( unsigned int rowIndex = 0, unsigned int columnIndex = 0,
151                     unsigned int rowSpan = 1, unsigned int columnSpan = 1 )
152     : rowIndex( rowIndex ), columnIndex( columnIndex ),
153       rowSpan( rowSpan ), columnSpan( columnSpan )
154     { }
155
156     unsigned int rowIndex;
157     unsigned int columnIndex;
158     unsigned int rowSpan;
159     unsigned int columnSpan;
160   };
161
162   /**
163    * Create a TableView handle; this can be initialised with TableView::New()
164    * Calling member functions with an uninitialised handle is not allowed.
165    */
166   TableView();
167
168   /**
169    * Copy constructor. Creates another handle that points to the same real object
170    * @param handle to copy from
171    */
172   TableView( const TableView& handle );
173
174   /**
175    * Assignment operator. Changes this handle to point to another real object
176    */
177   TableView& operator=( const TableView& handle );
178
179   /**
180    * @brief Destructor
181    *
182    * This is non-virtual since derived Handle types must not contain data or virtual methods.
183    */
184   ~TableView();
185
186   /**
187    * Create the TableView control.
188    * @param[in] initialRows for the table
189    * @param[in] initialColumns for the table
190    * @return A handle to the TableView control.
191    */
192   static TableView New( unsigned int initialRows, unsigned int initialColumns );
193
194   /**
195    * Downcast an Object handle to TableView. If handle points to a TableView the
196    * downcast produces valid handle. If not the returned handle is left uninitialized.
197    * @param[in] handle Handle to an object
198    * @return handle to a TableView or an uninitialized handle
199    */
200   static TableView DownCast( BaseHandle handle );
201
202   /**
203    * Adds a child to the table
204    * If the row or column index is outside the table, the table gets resized bigger
205    * @pre The child actor has been initialized.
206    * @param[in] child to add
207    * @param[in] position for the child
208    * @return true if the addition succeeded, false if the cell is already occupied
209    */
210   bool AddChild( Actor child, CellPosition position );
211
212   /**
213    * Returns a child from the given layout position
214    * Note! if there is no child in this position this method returns an uninitialized
215    * Actor handle
216    * @param[in] position in the table
217    * @return child that was in the cell or an uninitialized handle
218    */
219   Actor GetChildAt( CellPosition position );
220
221   /**
222    * Removes a child from the given layout position
223    * Note! if there is no child in this position this method does nothing
224    * @param[in] position for the child to remove
225    * @return child that was removed or an uninitialized handle
226    */
227   Actor RemoveChildAt( CellPosition position );
228
229   /**
230    * Finds the childs layout position
231    * @param[in] child to search for
232    * @param[out] position for the child
233    * @return true if the child was included in this TableView
234    */
235   bool FindChildPosition( Actor child, CellPosition& position );
236
237   /**
238    * Insert a new row to given index
239    * @param [in] rowIndex of the new row
240    */
241   void InsertRow( unsigned int rowIndex );
242
243   /**
244    * Delete a row from given index
245    * Removed elements are deleted
246    * @param [in] rowIndex of the row to delete
247    */
248   void DeleteRow( unsigned int rowIndex );
249
250   /**
251    * Delete a row from given index
252    * @param [in] rowIndex of the row to delete
253    * @param [out] removed elements
254    */
255   void DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed );
256
257   /**
258    * Insert a new column to given index
259    * @param [in] columnIndex of the new column
260    */
261   void InsertColumn( unsigned int columnIndex );
262
263   /**
264    * Delete a column from given index.
265    * Removed elements are deleted
266    * @param [in] columnIndex of the column to delete
267    */
268   void DeleteColumn( unsigned int columnIndex );
269
270   /**
271    * Delete a column from given index
272    * @param [in] columnIndex of the column to delete
273    * @param [out] removed elements
274    */
275   void DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed );
276
277   /**
278    * Resize the TableView. Note! if the new size is smaller than old,
279    * superfluous actors get removed. If you want to relayout removed children,
280    * use the variant that returns the removed Actors and reinsert them into the table
281    * If an actor spans to a removed row or column it gets removed from the table
282    * @param[in] rows for the table
283    * @param[in] columns for the table
284    */
285   void Resize( unsigned int rows, unsigned int columns );
286
287   /**
288    * Resize the TableView. Note! if the new size is smaller than old,
289    * superfluous actors get removed.
290    * If an actor spans to a removed row or column it gets removed from the table
291    * @param[in] rows for the table
292    * @param[in] columns for the table
293    * @param[out] removed actor handles
294    */
295   void Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed );
296
297   /**
298    * Set horizontal and vertical padding between cells
299    * @param[in] padding width and height
300    */
301   void SetCellPadding( Size padding );
302
303   /**
304    * @return the current padding as width and height
305    */
306   Size GetCellPadding();
307
308   /**
309    * @brief Specify this row as fitting its height to its children
310    *
311    * @param[in] rowIndex The row to set
312    */
313   void SetFitHeight( unsigned int rowIndex );
314
315   /**
316    * @brief Is the row a fit row
317    *
318    * @param[in] rowIndex The row to check
319    * @return Return true if the row is fit
320    */
321   bool IsFitHeight( unsigned int rowIndex ) const;
322
323   /**
324    * @brief Specify this column as fitting its width to its children
325    *
326    * @param[in] columnIndex The column to set
327    */
328   void SetFitWidth( unsigned int columnIndex );
329
330   /**
331    * @brief Is the column a fit column
332    *
333    * @param[in] columnIndex The column to check
334    * @return Return true if the column is fit
335    */
336   bool IsFitWidth( unsigned int columnIndex ) const;
337
338   /**
339    * Sets a row to have fixed height
340    * Setting a fixed height of 0 has no effect
341    * @pre The row rowIndex must exist.
342    * @param rowIndex for row with fixed height
343    * @param height in world coordinate units
344    */
345   void SetFixedHeight( unsigned int rowIndex, float height );
346
347   /**
348    * Gets a row's fixed height.
349    * Note! The returned value is valid if it has been set before.
350    * @pre The row rowIndex must exist.
351    * @return height in world coordinate units.
352    */
353   float GetFixedHeight( unsigned int rowIndex ) const;
354
355   /**
356    * Sets a row to have relative height. Relative height means percentage of
357    * the remainder of the table height after subtracting Padding and Fixed height rows
358    * Setting a relative height of 0 has no effect
359    * @pre The row rowIndex must exist.
360    * @param rowIndex for row with relative height
361    * @param heightPercentage between 0.0f and 1.0f
362    */
363   void SetRelativeHeight( unsigned int rowIndex, float heightPercentage );
364
365   /**
366    * Gets a row's relative height.
367    * Note! The returned value is valid if it has been set before.
368    * @pre The row rowIndex must exist.
369    * @return height in percentage units, between 0.0f and 1.0f.
370    */
371   float GetRelativeHeight( unsigned int rowIndex ) const;
372
373   /**
374    * Sets a column to have fixed width
375    * Setting a fixed width of 0 has no effect
376    * @pre The column columnIndex must exist.
377    * @param columnIndex for column with fixed width
378    * @param width in world coordinate units
379    */
380   void SetFixedWidth( unsigned int columnIndex, float width );
381
382   /**
383    * Gets a column's fixed width.
384    * Note! The returned value is valid if it has been set before.
385    * @pre The column columnIndex must exist.
386    * @return width in world coordinate units.
387    */
388   float GetFixedWidth( unsigned int columnIndex ) const;
389
390   /**
391    * Sets a column to have relative width. Relative width means percentage of
392    * the remainder of table width after subtracting Padding and Fixed width columns
393    * Setting a relative width of 0 has no effect
394    * @pre The column columnIndex must exist.
395    * @param columnIndex for column with fixed width
396    * @param widthPercentage between 0.0f and 1.0f
397    */
398   void SetRelativeWidth( unsigned int columnIndex, float widthPercentage );
399
400   /**
401    * Gets a column's relative width.
402    * Note! The returned value is valid if it has been set before.
403    * @pre The column columnIndex must exist.
404    * @return width in percentage units, between 0.0f and 1.0f.
405    */
406   float GetRelativeWidth( unsigned int columnIndex ) const;
407
408   /**
409    * @return the amount of rows in the table
410    */
411   unsigned int GetRows();
412
413   /**
414    * @return the amount of columns in the table
415    */
416   unsigned int GetColumns();
417
418   /**
419    * @brief Set the alignment on a cell.
420    *
421    * Cells without calling this function have the default values of LEFT and TOP respectively.
422    *
423    * @param[in] position The cell to set alignment on.
424    * @param[in] horizontal The horizontal alignment.
425    * @param[in] vertical The vertical alignment.
426    */
427   void SetCellAlignment( CellPosition position, HorizontalAlignment::Type horizontal, VerticalAlignment::Type vertical );
428
429 public: // Not intended for application developers
430
431   /**
432    * Creates a handle using the Toolkit::Internal implementation.
433    * @param[in]  implementation  The Control implementation.
434    */
435   DALI_INTERNAL TableView(Internal::TableView& implementation);
436
437   /**
438    * Allows the creation of this Control from an Internal::CustomActor pointer.
439    * @param[in]  internal  A pointer to the internal CustomActor.
440    */
441   explicit DALI_INTERNAL TableView( Dali::Internal::CustomActor* internal );
442 };
443
444 } // namespace Toolkit
445
446 } // namespace Dali
447
448 #endif // __DALI_TOOLKIT_TABLE_VIEW_H__