License conversion from Flora to Apache 2.0
[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 ControlImpl
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::SetLayoutAnimationDuration
171    */
172   void SetLayoutAnimationDuration( float duration );
173
174   /**
175    * @copydoc Toolkit::TableView::GetLayoutAnimationDuration
176    */
177   float GetLayoutAnimationDuration();
178
179
180   /**
181    * @copydoc Toolkit::TableView::GetRows
182    */
183   unsigned int GetRows();
184
185   /**
186    * @copydoc Toolkit::TableView::GetColumns
187    */
188   unsigned int GetColumns();
189
190 private: // From ControlImpl
191
192   /**
193    * @copydoc Toolkit::ControlImpl::OnControlChildAdd(Actor& child)
194    */
195   virtual void OnControlChildAdd(Actor& child);
196
197   /**
198    * @copydoc Toolkit::ControlImpl::OnControlChildRemove(Actor& child)
199    */
200   virtual void OnControlChildRemove(Actor& child);
201
202   /**
203    * @copydoc Toolkit::ControlImpl::OnRelaidOut
204    */
205   virtual void OnRelaidOut( Vector2 size, ActorSizeContainer& container );
206
207   /**
208    * @copydoc Toolkit::ControlImpl::OnInitialize()
209    */
210   virtual void OnInitialize();
211
212   /**
213    * @copydoc Toolkit::ControlImpl::GetNextKeyboardFocusableActor
214    */
215   virtual Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
216
217 private: // Implementation
218
219   /**
220    * Construct a new TableView.
221    */
222   TableView( unsigned int initialRows, unsigned int initialColumns );
223
224   /**
225    * Resizes the data containers to match the new size
226    * @param [in] rows in the table
227    * @param [in] columns in the table
228    */
229   void ResizeContainers( unsigned int rows, unsigned int columns );
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    * @param [out] removed celldata
236    */
237   void ResizeContainers( unsigned int rows, unsigned int columns, std::vector<CellData>& removed );
238
239   /**
240    * Helper to get the list of lost actors in the case when table looses cells.
241    * Also handles the case when actors span multiple cells
242    * @param lost cells
243    * @param removed actors
244    * @param rowsRemoved from table
245    * @param columnsRemoved from table
246    */
247   void RemoveAndGetLostActors( const std::vector<CellData>& lost, std::vector<Actor>& removed,
248       unsigned int rowsRemoved, unsigned int columnsRemoved );
249
250   /**
251    * Helper to remove all instances of the actor
252    * @param child actor to remove
253    * @return true if the actor was found
254    */
255   bool RemoveAllInstances( Actor child );
256
257   /**
258    * Helper to update relative sizes
259    * @param fixedHeightsTotal sum of the fixed height rows
260    * @param fixedWidthsTotal sum of the fixed width columns
261    */
262   void UpdateRelativeSizes( float& fixedHeightsTotal, float& fixedWidthsTotal );
263
264   /**
265    * A reference counted object may only be deleted by calling Unreference()
266    */
267   virtual ~TableView();
268
269   /**
270    * Helper class to prevent child adds and removes from causing relayout
271    * when we're already anyways going to do one in the end
272    */
273   class RelayoutingLock
274   {
275   public: // API
276
277     /**
278      * Constructor, sets the lock boolean
279      */
280     RelayoutingLock( TableView& parent )
281     : mLock( parent.mLayoutingChild )
282     {
283       mLock = true;
284     }
285
286     /**
287      * Destructor, releases lock boolean
288      */
289     ~RelayoutingLock()
290     {
291       mLock = false;
292       // Note, we could also call Relayout here. This would save one line of code
293       // from each method that uses this lock but destructors are not meant to do
294       // big processing so better to not do it here. This destructor would also
295       // be called in case of an exception and we don't definitely want to do Relayout
296       // in that situation
297     }
298
299   private:
300     bool& mLock;
301   };
302
303 private:
304
305   // Undefined copy constructor and assignment operators
306   TableView(const TableView&);
307   TableView& operator=(const TableView& rhs);
308
309 private: // Data
310
311   Array2d<CellData> mCellData;
312   Array2d<Size> mRelativeSizes;
313   std::vector<float> mFixedHeights;
314   std::vector<float> mRelativeHeights;
315   std::vector<float> mFixedWidths;
316   std::vector<float> mRelativeWidths;
317   Size mPadding;
318   bool mLayoutingChild;
319   float mConstraintDuration;
320
321 };
322
323 } // namespace Internal
324
325 // Helpers for public-api forwarding methods
326
327 inline Toolkit::Internal::TableView& GetImpl( Toolkit::TableView& tableView )
328 {
329   DALI_ASSERT_ALWAYS(tableView);
330
331   Dali::RefObject& handle = tableView.GetImplementation();
332
333   return static_cast<Toolkit::Internal::TableView&>(handle);
334 }
335
336 inline const Toolkit::Internal::TableView& GetImpl( const Toolkit::TableView& tableView )
337 {
338   DALI_ASSERT_ALWAYS(tableView);
339
340   const Dali::RefObject& handle = tableView.GetImplementation();
341
342   return static_cast<const Toolkit::Internal::TableView&>(handle);
343 }
344
345 } // namespace Toolkit
346
347 } // namespace Dali
348
349 #endif // __DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H__