fixing some of the comments and changing copy-by-value to by reference in size negoti...
[platform/core/uifw/dali-toolkit.git] / base / 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::SetLayoutAnimationDuration
178    */
179   void SetLayoutAnimationDuration( float duration );
180
181   /**
182    * @copydoc Toolkit::TableView::GetLayoutAnimationDuration
183    */
184   float GetLayoutAnimationDuration();
185
186
187   /**
188    * @copydoc Toolkit::TableView::GetRows
189    */
190   unsigned int GetRows();
191
192   /**
193    * @copydoc Toolkit::TableView::GetColumns
194    */
195   unsigned int GetColumns();
196
197   // Properties
198
199   /**
200    * Called when a property of an object of this type is set.
201    * @param[in] object The object whose property is set.
202    * @param[in] index The property index.
203    * @param[in] value The new property value.
204    */
205   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
206
207   /**
208    * Called to retrieve a property of an object of this type.
209    * @param[in] object The object whose property is to be retrieved.
210    * @param[in] index The property index.
211    * @return The current value of the property.
212    */
213   static Property::Value GetProperty( BaseObject* object, Property::Index index );
214
215 private: // From Control
216
217   /**
218    * @copydoc Control::OnControlChildAdd(Actor& child)
219    */
220   virtual void OnControlChildAdd(Actor& child);
221
222   /**
223    * @copydoc Control::OnControlChildRemove(Actor& child)
224    */
225   virtual void OnControlChildRemove(Actor& child);
226
227   /**
228    * @copydoc Control::OnRelayout
229    */
230   virtual void OnRelayout( const Vector2& size, ActorSizeContainer& container );
231
232   /**
233    * @copydoc Control::OnInitialize()
234    */
235   virtual void OnInitialize();
236
237   /**
238    * @copydoc Control::GetNextKeyboardFocusableActor
239    */
240   virtual Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
241
242 private: // Implementation
243
244   /**
245    * Construct a new TableView.
246    */
247   TableView( unsigned int initialRows, unsigned int initialColumns );
248
249   /**
250    * Resizes the data containers to match the new size
251    * @param [in] rows in the table
252    * @param [in] columns in the table
253    */
254   void ResizeContainers( unsigned int rows, unsigned int columns );
255
256   /**
257    * Resizes the data containers to match the new size
258    * @param [in] rows in the table
259    * @param [in] columns in the table
260    * @param [out] removed celldata
261    */
262   void ResizeContainers( unsigned int rows, unsigned int columns, std::vector<CellData>& removed );
263
264   /**
265    * Helper to get the list of lost actors in the case when table looses cells.
266    * Also handles the case when actors span multiple cells
267    * @param lost cells
268    * @param removed actors
269    * @param rowsRemoved from table
270    * @param columnsRemoved from table
271    */
272   void RemoveAndGetLostActors( const std::vector<CellData>& lost, std::vector<Actor>& removed,
273       unsigned int rowsRemoved, unsigned int columnsRemoved );
274
275   /**
276    * Helper to remove all instances of the actor
277    * @param child actor to remove
278    * @return true if the actor was found
279    */
280   bool RemoveAllInstances( Actor child );
281
282   /**
283    * Helper to update relative sizes
284    * @param fixedHeightsTotal sum of the fixed height rows
285    * @param fixedWidthsTotal sum of the fixed width columns
286    */
287   void UpdateRelativeSizes( float& fixedHeightsTotal, float& fixedWidthsTotal );
288
289   /**
290    * A reference counted object may only be deleted by calling Unreference()
291    */
292   virtual ~TableView();
293
294 private: // scripting support
295
296   /**
297    * Called to set the heights/widths property.
298    * @param[in] tableViewImpl The object whose property is set.
299    * @param[in] funcFixed The set function to call, it can be SetFixedHeight or SetFixedWidths.
300    * @param[in] funcRelative The set function to call, it can be SetRelativeHeight or SetRelativeWidths.
301    * @param[in] value The new property value.
302    */
303   static void SetHeightOrWidthProperty( TableView& tableViewImpl,
304                                         void(TableView::*funcFixed)(unsigned int, float),
305                                         void(TableView::*funcRelative)(unsigned int, float),
306                                         const Property::Value& map );
307
308   /**
309    * Called to retrieve the property value of row heights.
310    * @return The property value of row heights.
311    */
312   Property::Value GetRowHeightsPropertyValue();
313
314   /**
315    * Called to retrieve the property value of column widths.
316    * @return The fixed-widths property value.
317    */
318   Property::Value GetColumnWidthsPropertyValue();
319
320   /**
321    * Generate the map type property value from the size vectors.
322    * @param[in] fixedSize The vector of fixed heights or widths.
323    * @param[in] relativeSize The vector of relative heights or widths.
324    * @param[out] map The property value.
325    */
326   void GetMapPropertyValue( const std::vector<float>& fixedSize, const std::vector<float>& relativeSize, Property::Map& map );
327
328
329   /**
330    * Helper class to prevent child adds and removes from causing relayout
331    * when we're already anyways going to do one in the end
332    */
333   class RelayoutingLock
334   {
335   public: // API
336
337     /**
338      * Constructor, sets the lock boolean
339      */
340     RelayoutingLock( TableView& parent )
341     : mLock( parent.mLayoutingChild )
342     {
343       mLock = true;
344     }
345
346     /**
347      * Destructor, releases lock boolean
348      */
349     ~RelayoutingLock()
350     {
351       mLock = false;
352       // Note, we could also call Relayout here. This would save one line of code
353       // from each method that uses this lock but destructors are not meant to do
354       // big processing so better to not do it here. This destructor would also
355       // be called in case of an exception and we don't definitely want to do Relayout
356       // in that situation
357     }
358
359   private:
360     bool& mLock;
361   };
362
363 private:
364
365   // Undefined copy constructor and assignment operators
366   TableView(const TableView&);
367   TableView& operator=(const TableView& rhs);
368
369 private: // Data
370
371   Array2d<CellData> mCellData;
372   Array2d<Size> mRelativeSizes;
373   std::vector<float> mFixedHeights;
374   std::vector<float> mRelativeHeights;
375   std::vector<float> mFixedWidths;
376   std::vector<float> mRelativeWidths;
377   Size mPadding;
378   bool mLayoutingChild;
379   float mConstraintDuration;
380
381 };
382
383 } // namespace Internal
384
385 // Helpers for public-api forwarding methods
386
387 inline Toolkit::Internal::TableView& GetImpl( Toolkit::TableView& tableView )
388 {
389   DALI_ASSERT_ALWAYS(tableView);
390
391   Dali::RefObject& handle = tableView.GetImplementation();
392
393   return static_cast<Toolkit::Internal::TableView&>(handle);
394 }
395
396 inline const Toolkit::Internal::TableView& GetImpl( const Toolkit::TableView& tableView )
397 {
398   DALI_ASSERT_ALWAYS(tableView);
399
400   const Dali::RefObject& handle = tableView.GetImplementation();
401
402   return static_cast<const Toolkit::Internal::TableView&>(handle);
403 }
404
405 } // namespace Toolkit
406
407 } // namespace Dali
408
409 #endif // __DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H__