Merge "Remove constraints from Popup, TableView, Alignment & ScrollBar" into tizen
[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   // Properties
44   enum
45   {
46     TABLEVIEW_PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
47     TABLEVIEW_PROPERTY_END_INDEX = TABLEVIEW_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
48   };
49
50   /**
51    * Structure for the layout data
52    */
53   struct CellData
54   {
55     // data members
56     Actor actor;
57     Toolkit::TableView::CellPosition position;
58   };
59
60   /**
61    * Create a new TableView.
62    * @return A smart-pointer to the newly allocated TableView.
63    */
64   static Toolkit::TableView New( unsigned int initialRows, unsigned int initialColumns );
65
66   /**
67    * @copydoc Toolkit::TableView::AddChild
68    */
69   bool AddChild( Actor child, Toolkit::TableView::CellPosition position );
70
71   /**
72    * @copydoc Toolkit::TableView::GetChildAt
73    */
74   Actor GetChildAt( Toolkit::TableView::CellPosition position );
75
76   /**
77    * @copydoc Toolkit::TableView::RemoveChildAt
78    */
79   Actor RemoveChildAt( Toolkit::TableView::CellPosition position );
80
81   /**
82    * @copydoc Toolkit::TableView::FindChildPosition
83    */
84   bool FindChildPosition( Actor child, Toolkit::TableView::CellPosition& position );
85
86   /**
87    * @copydoc Toolkit::TableView::InsertRow
88    */
89   void InsertRow( unsigned int rowIndex );
90
91   /**
92    * @copydoc Toolkit::TableView::DeleteRow( unsigned int rowIndex )
93    */
94   void DeleteRow( unsigned int rowIndex );
95
96   /**
97    * @copydoc Toolkit::TableView::DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed )
98    */
99   void DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed );
100
101   /**
102    * @copydoc Toolkit::TableView::InsertColumn
103    */
104   void InsertColumn( unsigned int columnIndex );
105
106   /**
107    * @copydoc Toolkit::TableView::DeleteColumn( unsigned int columnIndex )
108    */
109   void DeleteColumn( unsigned int columnIndex );
110
111   /**
112    * @copydoc Toolkit::TableView::DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed )
113    */
114   void DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed );
115
116   /**
117    * @copydoc Toolkit::TableView::Resize( unsigned int rows, unsigned int columns )
118    */
119   void Resize( unsigned int rows, unsigned int columns );
120
121   /**
122    * @copydoc Toolkit::TableView::Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed )
123    */
124   void Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed );
125
126   /**
127    * @copydoc Toolkit::TableView::SetCellPadding
128    */
129   void SetCellPadding( Size padding );
130
131   /**
132    * @copydoc Toolkit::TableView::GetCellPadding
133    */
134   Size GetCellPadding();
135
136   /**
137    * @copydoc Toolkit::TableView::SetFixedHeight
138    */
139   void SetFixedHeight( unsigned int rowIndex, float height );
140
141   /**
142    * @copydoc Toolkit::TableView::GetFixedHeight
143    */
144   float GetFixedHeight( unsigned int rowIndex ) const;
145
146   /**
147    * @copydoc Toolkit::TableView::SetRelativeHeight
148    */
149   void SetRelativeHeight( unsigned int rowIndex, float heightPercentage );
150
151   /**
152    * @copydoc Toolkit::TableView::GetRelativeHeight
153    */
154   float GetRelativeHeight( unsigned int rowIndex ) const;
155
156   /**
157    * @copydoc Toolkit::TableView::SetFixedWidth
158    */
159   void SetFixedWidth( unsigned int columnIndex, float width );
160
161   /**
162    * @copydoc Toolkit::TableView::GetFixedWidth
163    */
164   float GetFixedWidth( unsigned int columnIndex ) const;
165
166   /**
167    * @copydoc Toolkit::TableView::SetRelativeWidth
168    */
169   void SetRelativeWidth( unsigned int columnIndex, float widthPercentage );
170
171   /**
172    * @copydoc Toolkit::TableView::GetRelativeWidth
173    */
174   float GetRelativeWidth( unsigned int columnIndex ) const;
175
176   /**
177    * @copydoc Toolkit::TableView::GetRows
178    */
179   unsigned int GetRows();
180
181   /**
182    * @copydoc Toolkit::TableView::GetColumns
183    */
184   unsigned int GetColumns();
185
186   // Properties
187
188   /**
189    * Called when a property of an object of this type is set.
190    * @param[in] object The object whose property is set.
191    * @param[in] index The property index.
192    * @param[in] value The new property value.
193    */
194   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
195
196   /**
197    * Called to retrieve a property of an object of this type.
198    * @param[in] object The object whose property is to be retrieved.
199    * @param[in] index The property index.
200    * @return The current value of the property.
201    */
202   static Property::Value GetProperty( BaseObject* object, Property::Index index );
203
204 private: // From Control
205
206   /**
207    * @copydoc Control::OnControlChildAdd(Actor& child)
208    */
209   virtual void OnControlChildAdd(Actor& child);
210
211   /**
212    * @copydoc Control::OnControlChildRemove(Actor& child)
213    */
214   virtual void OnControlChildRemove(Actor& child);
215
216   /**
217    * @copydoc Control::OnRelayout
218    */
219   virtual void OnRelayout( const Vector2& size, ActorSizeContainer& container );
220
221   /**
222    * @copydoc Control::OnInitialize()
223    */
224   virtual void OnInitialize();
225
226   /**
227    * @copydoc Control::GetNextKeyboardFocusableActor
228    */
229   virtual Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
230
231 private: // Implementation
232
233   /**
234    * Construct a new TableView.
235    */
236   TableView( unsigned int initialRows, unsigned int initialColumns );
237
238   /**
239    * Resizes the data containers to match the new size
240    * @param [in] rows in the table
241    * @param [in] columns in the table
242    */
243   void ResizeContainers( unsigned int rows, unsigned int columns );
244
245   /**
246    * Resizes the data containers to match the new size
247    * @param [in] rows in the table
248    * @param [in] columns in the table
249    * @param [out] removed celldata
250    */
251   void ResizeContainers( unsigned int rows, unsigned int columns, std::vector<CellData>& removed );
252
253   /**
254    * Helper to get the list of lost actors in the case when table looses cells.
255    * Also handles the case when actors span multiple cells
256    * @param lost cells
257    * @param removed actors
258    * @param rowsRemoved from table
259    * @param columnsRemoved from table
260    */
261   void RemoveAndGetLostActors( const std::vector<CellData>& lost, std::vector<Actor>& removed,
262       unsigned int rowsRemoved, unsigned int columnsRemoved );
263
264   /**
265    * Helper to remove all instances of the actor
266    * @param child actor to remove
267    * @return true if the actor was found
268    */
269   bool RemoveAllInstances( Actor child );
270
271   /**
272    * Helper to update relative sizes
273    * @param fixedHeightsTotal sum of the fixed height rows
274    * @param fixedWidthsTotal sum of the fixed width columns
275    */
276   void UpdateRelativeSizes( float& fixedHeightsTotal, float& fixedWidthsTotal );
277
278   /**
279    * A reference counted object may only be deleted by calling Unreference()
280    */
281   virtual ~TableView();
282
283 private: // scripting support
284
285   /**
286    * Called to set the heights/widths property.
287    * @param[in] tableViewImpl The object whose property is set.
288    * @param[in] funcFixed The set function to call, it can be SetFixedHeight or SetFixedWidths.
289    * @param[in] funcRelative The set function to call, it can be SetRelativeHeight or SetRelativeWidths.
290    * @param[in] value The new property value.
291    */
292   static void SetHeightOrWidthProperty( TableView& tableViewImpl,
293                                         void(TableView::*funcFixed)(unsigned int, float),
294                                         void(TableView::*funcRelative)(unsigned int, float),
295                                         const Property::Value& map );
296
297   /**
298    * Called to retrieve the property value of row heights.
299    * @return The property value of row heights.
300    */
301   Property::Value GetRowHeightsPropertyValue();
302
303   /**
304    * Called to retrieve the property value of column widths.
305    * @return The fixed-widths property value.
306    */
307   Property::Value GetColumnWidthsPropertyValue();
308
309   /**
310    * Generate the map type property value from the size vectors.
311    * @param[in] fixedSize The vector of fixed heights or widths.
312    * @param[in] relativeSize The vector of relative heights or widths.
313    * @param[out] map The property value.
314    */
315   void GetMapPropertyValue( const std::vector<float>& fixedSize, const std::vector<float>& relativeSize, Property::Map& map );
316
317
318   /**
319    * Helper class to prevent child adds and removes from causing relayout
320    * when we're already anyways going to do one in the end
321    */
322   class RelayoutingLock
323   {
324   public: // API
325
326     /**
327      * Constructor, sets the lock boolean
328      */
329     RelayoutingLock( TableView& parent )
330     : mLock( parent.mLayoutingChild )
331     {
332       mLock = true;
333     }
334
335     /**
336      * Destructor, releases lock boolean
337      */
338     ~RelayoutingLock()
339     {
340       mLock = false;
341       // Note, we could also call Relayout here. This would save one line of code
342       // from each method that uses this lock but destructors are not meant to do
343       // big processing so better to not do it here. This destructor would also
344       // be called in case of an exception and we don't definitely want to do Relayout
345       // in that situation
346     }
347
348   private:
349     bool& mLock;
350   };
351
352 private:
353
354   // Undefined copy constructor and assignment operators
355   TableView(const TableView&);
356   TableView& operator=(const TableView& rhs);
357
358 private: // Data
359
360   Array2d<CellData> mCellData;
361   Array2d<Size> mRelativeSizes;
362   std::vector<float> mFixedHeights;
363   std::vector<float> mRelativeHeights;
364   std::vector<float> mFixedWidths;
365   std::vector<float> mRelativeWidths;
366   Size mPadding;
367   bool mLayoutingChild;
368
369 };
370
371 } // namespace Internal
372
373 // Helpers for public-api forwarding methods
374
375 inline Toolkit::Internal::TableView& GetImpl( Toolkit::TableView& tableView )
376 {
377   DALI_ASSERT_ALWAYS(tableView);
378
379   Dali::RefObject& handle = tableView.GetImplementation();
380
381   return static_cast<Toolkit::Internal::TableView&>(handle);
382 }
383
384 inline const Toolkit::Internal::TableView& GetImpl( const Toolkit::TableView& tableView )
385 {
386   DALI_ASSERT_ALWAYS(tableView);
387
388   const Dali::RefObject& handle = tableView.GetImplementation();
389
390   return static_cast<const Toolkit::Internal::TableView&>(handle);
391 }
392
393 } // namespace Toolkit
394
395 } // namespace Dali
396
397 #endif // __DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H__