[dali_1.0.47] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / table-view / table-view-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H__
2 #define __DALI_TOOLKIT_INTERNAL_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 // INTERNAL INCLUDES
22 #include <dali-toolkit/public-api/controls/control-impl.h>
23 #include <dali-toolkit/public-api/controls/table-view/table-view.h>
24 #include "array-2d.h"
25
26 namespace Dali
27 {
28
29 namespace Toolkit
30 {
31
32 namespace Internal
33 {
34
35 /**
36  * TableView is a custom control for laying out actors in a table layout
37  * @see Dali::Toolkit:TableView for more details
38  */
39 class TableView : public Control
40 {
41 public:
42
43   /**
44    * Create a new TableView.
45    * @return A smart-pointer to the newly allocated TableView.
46    */
47   static Toolkit::TableView New( unsigned int initialRows, unsigned int initialColumns );
48
49   /**
50    * @copydoc Toolkit::TableView::AddChild
51    */
52   bool AddChild( Actor& child, const Toolkit::TableView::CellPosition& position );
53
54   /**
55    * @copydoc Toolkit::TableView::GetChildAt
56    */
57   Actor GetChildAt( const Toolkit::TableView::CellPosition& position );
58
59   /**
60    * @copydoc Toolkit::TableView::RemoveChildAt
61    */
62   Actor RemoveChildAt( const Toolkit::TableView::CellPosition& position );
63
64   /**
65    * @copydoc Toolkit::TableView::FindChildPosition
66    */
67   bool FindChildPosition( const Actor& child, Toolkit::TableView::CellPosition& positionOut );
68
69   /**
70    * @copydoc Toolkit::TableView::InsertRow
71    */
72   void InsertRow( unsigned int rowIndex );
73
74   /**
75    * @copydoc Toolkit::TableView::DeleteRow( unsigned int rowIndex )
76    */
77   void DeleteRow( unsigned int rowIndex );
78
79   /**
80    * @copydoc Toolkit::TableView::DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed )
81    */
82   void DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed );
83
84   /**
85    * @copydoc Toolkit::TableView::InsertColumn
86    */
87   void InsertColumn( unsigned int columnIndex );
88
89   /**
90    * @copydoc Toolkit::TableView::DeleteColumn( unsigned int columnIndex )
91    */
92   void DeleteColumn( unsigned int columnIndex );
93
94   /**
95    * @copydoc Toolkit::TableView::DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed )
96    */
97   void DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed );
98
99   /**
100    * @copydoc Toolkit::TableView::Resize( unsigned int rows, unsigned int columns )
101    */
102   void Resize( unsigned int rows, unsigned int columns );
103
104   /**
105    * @copydoc Toolkit::TableView::Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed )
106    */
107   void Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed );
108
109   /**
110    * @copydoc Toolkit::TableView::SetCellPadding
111    */
112   void SetCellPadding( Size padding );
113
114   /**
115    * @copydoc Toolkit::TableView::GetCellPadding
116    */
117   Size GetCellPadding();
118
119   /**
120    * @copydoc Toolkit::TableView::SetFitHeight
121    */
122   void SetFitHeight( unsigned int rowIndex );
123
124   /**
125    * @copydoc Toolkit::TableView::IsFitHeight
126    */
127   bool IsFitHeight( unsigned int rowIndex ) const;
128
129   /**
130    * @copydoc Toolkit::TableView::SetFitWidth
131    */
132   void SetFitWidth( unsigned int columnIndex );
133
134   /**
135    * @copydoc Toolkit::TableView::IsFitWidth
136    */
137   bool IsFitWidth( unsigned int columnIndex ) const;
138
139   /**
140    * @copydoc Toolkit::TableView::SetFixedWidth
141    */
142   void SetFixedWidth( unsigned int columnIndex, float width );
143
144   /**
145    * @copydoc Toolkit::TableView::GetFixedWidth
146    */
147   float GetFixedWidth( unsigned int columnIndex ) const;
148
149   /**
150    * @copydoc Toolkit::TableView::SetFixedHeight
151    */
152   void SetFixedHeight( unsigned int rowIndex, float height );
153
154   /**
155    * @copydoc Toolkit::TableView::GetFixedHeight
156    */
157   float GetFixedHeight( unsigned int rowIndex ) const;
158
159   /**
160    * @copydoc Toolkit::TableView::SetRelativeHeight
161    */
162   void SetRelativeHeight( unsigned int rowIndex, float heightPercentage );
163
164   /**
165    * @copydoc Toolkit::TableView::GetRelativeHeight
166    */
167   float GetRelativeHeight( unsigned int rowIndex ) const;
168
169   /**
170    * @copydoc Toolkit::TableView::SetRelativeWidth
171    */
172   void SetRelativeWidth( unsigned int columnIndex, float widthPercentage );
173
174   /**
175    * @copydoc Toolkit::TableView::GetRelativeWidth
176    */
177   float GetRelativeWidth( unsigned int columnIndex ) const;
178
179   /**
180    * @copydoc Toolkit::TableView::GetRows
181    */
182   unsigned int GetRows();
183
184   /**
185    * @copydoc Toolkit::TableView::GetColumns
186    */
187   unsigned int GetColumns();
188
189   /**
190    * @copydoc Toolkit::TableView::SetCellAlignment
191    */
192   void SetCellAlignment( Toolkit::TableView::CellPosition position, HorizontalAlignment::Type horizontal, VerticalAlignment::Type vertical );
193
194   // Properties
195
196   /**
197    * Called when a property of an object of this type is set.
198    * @param[in] object The object whose property is set.
199    * @param[in] index The property index.
200    * @param[in] value The new property value.
201    */
202   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
203
204   /**
205    * Called to retrieve a property of an object of this type.
206    * @param[in] object The object whose property is to be retrieved.
207    * @param[in] index The property index.
208    * @return The current value of the property.
209    */
210   static Property::Value GetProperty( BaseObject* object, Property::Index index );
211
212 private: // From Control
213
214   /**
215    * @copydoc Control::OnControlChildAdd(Actor& child)
216    */
217   virtual void OnControlChildAdd( Actor& child );
218
219   /**
220    * @copydoc Control::OnControlChildRemove(Actor& child)
221    */
222   virtual void OnControlChildRemove( Actor& child );
223
224   /**
225    * @copydoc Control::OnRelayout
226    */
227   virtual void OnRelayout( const Vector2& size, RelayoutContainer& container );
228
229   /**
230    * @copydoc Control::CalculateChildSize
231    */
232   virtual float CalculateChildSize( const Actor& child, Dimension::Type dimension );
233
234   /**
235    * @copydoc Control::OnInitialize()
236    */
237   virtual void OnInitialize();
238
239   /**
240    * @copydoc Control::GetNextKeyboardFocusableActor
241    */
242   virtual Actor GetNextKeyboardFocusableActor( Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled );
243
244   /**
245    * @copydoc Control::GetNaturalSize()
246    */
247   virtual Vector3 GetNaturalSize();
248
249   /**
250    * @copydoc Control::RelayoutDependentOnChildren()
251    */
252   virtual bool RelayoutDependentOnChildren( Dimension::Type dimension = Dimension::ALL_DIMENSIONS );
253
254   /**
255    * @copydoc Control::OnCalculateRelayoutSize
256    */
257   virtual void OnCalculateRelayoutSize( Dimension::Type dimension );
258
259   /**
260    * @copydoc Control::OnLayoutNegotiated
261    */
262   virtual void OnLayoutNegotiated( float size, Dimension::Type dimension );
263
264 private: // Implementation
265
266   /**
267    * Struct to hold data for rows and columns
268    *
269    * If sizePolicy is FIXED then size is the absolute size to use.
270    * If sizePolicy is FIT, RELATIVE or FILL then size is the calculated value of size.
271    */
272   struct RowColumnData
273   {
274     /**
275      * Default constructor
276      */
277     RowColumnData()
278     : size( 0.0f ),
279       fillRatio( 0.0f ),
280       position( 0.0f ),
281       sizePolicy( Toolkit::TableView::FILL )
282     {
283     }
284
285     /**
286      * Constructor
287      *
288      * @param[in] newSize The size to set for this data
289      * @param[in] newSizePolicy The policy used to interpret the size value
290      */
291     RowColumnData( float newSize, float newFillRatio, Toolkit::TableView::LayoutPolicy newSizePolicy )
292     : size( newSize ),
293       fillRatio( newFillRatio ),
294       position( 0.0f ),
295       sizePolicy( newSizePolicy )
296     {
297     }
298
299     float size;                                  ///< Set or calculated size
300     float fillRatio;                             ///< Ratio to fill remaining space, only valid with RELATIVE or FILL policy
301     float position;                              ///< Position of the row/column, this value is updated during every Relayout round
302     Toolkit::TableView::LayoutPolicy sizePolicy; ///< The size policy used to interpret the size value
303   };
304
305   typedef Dali::Vector<RowColumnData> RowColumnArray;
306
307 public:
308
309   /**
310    * Structure for the layout data
311    */
312   struct CellData
313   {
314     CellData()
315     : horizontalAlignment( HorizontalAlignment::LEFT ),
316       verticalAlignment( VerticalAlignment::TOP )
317     {
318     }
319
320     // data members
321     Dali::Actor actor;
322     Toolkit::TableView::CellPosition position;
323     HorizontalAlignment::Type horizontalAlignment;
324     VerticalAlignment::Type verticalAlignment;
325   };
326
327 private:
328
329   /**
330    * Construct a new TableView.
331    */
332   TableView( unsigned int initialRows, unsigned int initialColumns );
333
334   /**
335    * Resizes the data containers to match the new size
336    * @param [in] rows in the table
337    * @param [in] columns in the table
338    */
339   void ResizeContainers( unsigned int rows, unsigned int columns );
340
341   /**
342    * Resizes the data containers to match the new size
343    * @param [in] rows in the table
344    * @param [in] columns in the table
345    * @param [out] removed celldata
346    */
347   void ResizeContainers( unsigned int rows, unsigned int columns, std::vector<CellData>& removed );
348
349   /**
350    * Helper to get the list of lost actors in the case when table looses cells.
351    * Also handles the case when actors span multiple cells
352    * @param lost cells
353    * @param removed actors
354    * @param rowsRemoved from table
355    * @param columnsRemoved from table
356    */
357   void RemoveAndGetLostActors( const std::vector<CellData>& lost, std::vector<Actor>& removed,
358       unsigned int rowsRemoved, unsigned int columnsRemoved );
359
360   /**
361    * Helper to remove all instances of the actor
362    * @param child actor to remove
363    * @return true if the actor was found
364    */
365   bool RemoveAllInstances( const Actor& child );
366
367   /**
368    * @brief Calculate the ratio of FILL rows/columns
369    *
370    * @param[in] data The RowColumn data to compute the relative sizes for
371    */
372   void CalculateFillSizes( RowColumnArray& data );
373
374   /**
375    * @brief Calculate the total fixed sizes for a row or column
376    *
377    * @param[in] data The row or column data to process
378    */
379   float CalculateTotalFixedSize( const RowColumnArray& data );
380
381   /**
382    * @brief Calculate the sizes of FIT rows/columns
383    *
384    * @param[in] data The row or column data to process
385    * @param[in] dimension The dimension being calculated: row == Dimension::HEIGHT, column == Dimension::WIDTH
386    */
387   void CalculateFitSizes( RowColumnArray& data, Dimension::Type dimension );
388
389   /**
390    * @brief Search for a FIT cell in the array
391    *
392    * @param[in] data The row or column data to process
393    * @return Return if a FIT cell was found or not
394    */
395   bool FindFit( const RowColumnArray& data );
396
397   /**
398    * @brief Return the cell padding for a given dimension
399    *
400    * @param[in] dimension The dimension to return the padding for
401    * @return Return the padding (x = low, y = high)
402    */
403   Vector2 GetCellPadding( Dimension::Type dimension );
404
405   /**
406    * A reference counted object may only be deleted by calling Unreference()
407    */
408   virtual ~TableView();
409
410 private: // scripting support
411
412   /**
413    * Called to set the heights/widths property.
414    * @param[in] tableViewImpl The object whose property is set.
415    * @param[in] funcFixed The set function to call, it can be SetFixedHeight or SetFixedWidth.
416    * @param[in] funcRelative The set function to call, it can be SetRelativeHeight or SetRelativeWidth.
417    * @param[in] funcFit The set function to call, it can be SetFitHeight or SetFiltWidth.
418    * @param[in] value The new property value.
419    */
420   static void SetHeightOrWidthProperty( TableView& tableViewImpl,
421                                         void(TableView::*funcFixed)(unsigned int, float),
422                                         void(TableView::*funcRelative)(unsigned int, float),
423                                         void(TableView::*funcFit)(unsigned int),
424                                         const Property::Value& map );
425
426   /**
427    * Called to retrieve the property value of row heights.
428    * @return The property value of row heights.
429    */
430   Property::Value GetRowHeightsPropertyValue();
431
432   /**
433    * Called to retrieve the property value of column widths.
434    * @return The fixed-widths property value.
435    */
436   Property::Value GetColumnWidthsPropertyValue();
437
438   /**
439    * Generate the map type property value from the size vectors.
440    * @param[in] data The array of row or column data
441    * @param[out] map The property value.
442    */
443   void GetMapPropertyValue( const RowColumnArray& data, Property::Map& map );
444
445
446   /**
447    * Helper class to prevent child adds and removes from causing relayout
448    * when we're already anyways going to do one in the end
449    */
450   class RelayoutingLock
451   {
452   public: // API
453
454     /**
455      * Constructor, sets the lock boolean
456      */
457     RelayoutingLock( TableView& parent )
458     : mLock( parent.mLayoutingChild )
459     {
460       mLock = true;
461     }
462
463     /**
464      * Destructor, releases lock boolean
465      */
466     ~RelayoutingLock()
467     {
468       mLock = false;
469       // Note, we could also call Relayout here. This would save one line of code
470       // from each method that uses this lock but destructors are not meant to do
471       // big processing so better to not do it here. This destructor would also
472       // be called in case of an exception and we don't definitely want to do Relayout
473       // in that situation
474     }
475
476   private:
477     bool& mLock;
478   };
479
480 private:
481
482   // Undefined copy constructor and assignment operators
483   TableView(const TableView&);
484   TableView& operator=(const TableView& rhs);
485
486 private: // Data
487
488   Array2d<CellData> mCellData;   ///< Data for each cell: Actor, alignment settings etc
489
490   RowColumnArray mRowData;       ///< Data for each row
491   RowColumnArray mColumnData;    ///< Data for each column
492   Size mFixedTotals;             ///< Accumulated totals for fixed width and height
493
494   Size mPadding;                 ///< Padding to apply to each cell
495   bool mLayoutingChild;          ///< Can't be a bitfield due to Relayouting lock
496   bool mRowDirty : 1;            ///< Flag to indicate the row data is dirty
497   bool mColumnDirty : 1;         ///< Flag to indicate the column data is dirty
498 };
499
500 } // namespace Internal
501
502 // Helpers for public-api forwarding methods
503
504 inline Toolkit::Internal::TableView& GetImpl( Toolkit::TableView& tableView )
505 {
506   DALI_ASSERT_ALWAYS(tableView);
507
508   Dali::RefObject& handle = tableView.GetImplementation();
509
510   return static_cast<Toolkit::Internal::TableView&>(handle);
511 }
512
513 inline const Toolkit::Internal::TableView& GetImpl( const Toolkit::TableView& tableView )
514 {
515   DALI_ASSERT_ALWAYS(tableView);
516
517   const Dali::RefObject& handle = tableView.GetImplementation();
518
519   return static_cast<const Toolkit::Internal::TableView&>(handle);
520 }
521
522 } // namespace Toolkit
523
524 } // namespace Dali
525
526 #endif // __DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H__