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