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