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