5f7a5d1d5845ddd41d41c90ce8fa178bd5b66693
[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 Flora License, Version 1.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://floralicense.org/license/
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 // INTERNAL INCLUDES
21 #include <dali-toolkit/public-api/controls/control-impl.h>
22 #include <dali-toolkit/public-api/controls/table-view/table-view.h>
23 #include "array-2d.h"
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace Internal
32 {
33
34 /**
35  * TableView is a custom control for laying out actors in a table layout
36  * @see Dali::Toolkit:TableView for more details
37  */
38 class TableView : public ControlImpl
39 {
40 public:
41
42   /**
43    * Structure for the layout data
44    */
45   struct CellData
46   {
47     // data members
48     Actor actor;
49     Toolkit::TableView::CellPosition position;
50   };
51
52   /**
53    * Create a new TableView.
54    * @return A smart-pointer to the newly allocated TableView.
55    */
56   static Toolkit::TableView New( unsigned int initialRows, unsigned int initialColumns );
57
58   /**
59    * @copydoc Toolkit::TableView::AddChild
60    */
61   bool AddChild( Actor child, Toolkit::TableView::CellPosition position );
62
63   /**
64    * @copydoc Toolkit::TableView::GetChildAt
65    */
66   Actor GetChildAt( Toolkit::TableView::CellPosition position );
67
68   /**
69    * @copydoc Toolkit::TableView::RemoveChildAt
70    */
71   Actor RemoveChildAt( Toolkit::TableView::CellPosition position );
72
73   /**
74    * @copydoc Toolkit::TableView::FindChildPosition
75    */
76   bool FindChildPosition( Actor child, Toolkit::TableView::CellPosition& position );
77
78   /**
79    * @copydoc Toolkit::TableView::InsertRow
80    */
81   void InsertRow( unsigned int rowIndex );
82
83   /**
84    * @copydoc Toolkit::TableView::DeleteRow( unsigned int rowIndex )
85    */
86   void DeleteRow( unsigned int rowIndex );
87
88   /**
89    * @copydoc Toolkit::TableView::DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed )
90    */
91   void DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed );
92
93   /**
94    * @copydoc Toolkit::TableView::InsertColumn
95    */
96   void InsertColumn( unsigned int columnIndex );
97
98   /**
99    * @copydoc Toolkit::TableView::DeleteColumn( unsigned int columnIndex )
100    */
101   void DeleteColumn( unsigned int columnIndex );
102
103   /**
104    * @copydoc Toolkit::TableView::DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed )
105    */
106   void DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed );
107
108   /**
109    * @copydoc Toolkit::TableView::Resize( unsigned int rows, unsigned int columns )
110    */
111   void Resize( unsigned int rows, unsigned int columns );
112
113   /**
114    * @copydoc Toolkit::TableView::Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed )
115    */
116   void Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed );
117
118   /**
119    * @copydoc Toolkit::TableView::SetCellPadding
120    */
121   void SetCellPadding( Size padding );
122
123   /**
124    * @copydoc Toolkit::TableView::GetCellPadding
125    */
126   Size GetCellPadding();
127
128   /**
129    * @copydoc Toolkit::TableView::SetFixedHeight
130    */
131   void SetFixedHeight( unsigned int rowIndex, float height );
132
133   /**
134    * @copydoc Toolkit::TableView::GetFixedHeight
135    */
136   float GetFixedHeight( unsigned int rowIndex ) const;
137
138   /**
139    * @copydoc Toolkit::TableView::SetRelativeHeight
140    */
141   void SetRelativeHeight( unsigned int rowIndex, float heightPercentage );
142
143   /**
144    * @copydoc Toolkit::TableView::GetRelativeHeight
145    */
146   float GetRelativeHeight( unsigned int rowIndex ) const;
147
148   /**
149    * @copydoc Toolkit::TableView::SetFixedWidth
150    */
151   void SetFixedWidth( unsigned int columnIndex, float width );
152
153   /**
154    * @copydoc Toolkit::TableView::GetFixedWidth
155    */
156   float GetFixedWidth( unsigned int columnIndex ) const;
157
158   /**
159    * @copydoc Toolkit::TableView::SetRelativeWidth
160    */
161   void SetRelativeWidth( unsigned int columnIndex, float widthPercentage );
162
163   /**
164    * @copydoc Toolkit::TableView::GetRelativeWidth
165    */
166   float GetRelativeWidth( unsigned int columnIndex ) const;
167
168   /**
169    * @copydoc Toolkit::TableView::SetLayoutAnimationDuration
170    */
171   void SetLayoutAnimationDuration( float duration );
172
173   /**
174    * @copydoc Toolkit::TableView::GetLayoutAnimationDuration
175    */
176   float GetLayoutAnimationDuration();
177
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 private: // From ControlImpl
190
191   /**
192    * @copydoc Toolkit::ControlImpl::OnControlChildAdd(Actor& child)
193    */
194   virtual void OnControlChildAdd(Actor& child);
195
196   /**
197    * @copydoc Toolkit::ControlImpl::OnControlChildRemove(Actor& child)
198    */
199   virtual void OnControlChildRemove(Actor& child);
200
201   /**
202    * @copydoc Toolkit::ControlImpl::OnRelaidOut
203    */
204   virtual void OnRelaidOut( Vector2 size, ActorSizeContainer& container );
205
206   /**
207    * @copydoc Toolkit::ControlImpl::OnInitialize()
208    */
209   virtual void OnInitialize();
210
211   /**
212    * @copydoc Toolkit::ControlImpl::GetNextKeyboardFocusableActor
213    */
214   virtual Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
215
216 private: // Implementation
217
218   /**
219    * Construct a new TableView.
220    */
221   TableView( unsigned int initialRows, unsigned int initialColumns );
222
223   /**
224    * Resizes the data containers to match the new size
225    * @param [in] rows in the table
226    * @param [in] columns in the table
227    */
228   void ResizeContainers( unsigned int rows, unsigned int columns );
229
230   /**
231    * Resizes the data containers to match the new size
232    * @param [in] rows in the table
233    * @param [in] columns in the table
234    * @param [out] removed celldata
235    */
236   void ResizeContainers( unsigned int rows, unsigned int columns, std::vector<CellData>& removed );
237
238   /**
239    * Helper to get the list of lost actors in the case when table looses cells.
240    * Also handles the case when actors span multiple cells
241    * @param lost cells
242    * @param removed actors
243    * @param rowsRemoved from table
244    * @param columnsRemoved from table
245    */
246   void RemoveAndGetLostActors( const std::vector<CellData>& lost, std::vector<Actor>& removed,
247       unsigned int rowsRemoved, unsigned int columnsRemoved );
248
249   /**
250    * Helper to remove all instances of the actor
251    * @param child actor to remove
252    * @return true if the actor was found
253    */
254   bool RemoveAllInstances( Actor child );
255
256   /**
257    * Helper to update relative sizes
258    * @param fixedHeightsTotal sum of the fixed height rows
259    * @param fixedWidthsTotal sum of the fixed width columns
260    */
261   void UpdateRelativeSizes( float& fixedHeightsTotal, float& fixedWidthsTotal );
262
263   /**
264    * A reference counted object may only be deleted by calling Unreference()
265    */
266   virtual ~TableView();
267
268   /**
269    * Helper class to prevent child adds and removes from causing relayout
270    * when we're already anyways going to do one in the end
271    */
272   class RelayoutingLock
273   {
274   public: // API
275
276     /**
277      * Constructor, sets the lock boolean
278      */
279     RelayoutingLock( TableView& parent )
280     : mLock( parent.mLayoutingChild )
281     {
282       mLock = true;
283     }
284
285     /**
286      * Destructor, releases lock boolean
287      */
288     ~RelayoutingLock()
289     {
290       mLock = false;
291       // Note, we could also call Relayout here. This would save one line of code
292       // from each method that uses this lock but destructors are not meant to do
293       // big processing so better to not do it here. This destructor would also
294       // be called in case of an exception and we don't definitely want to do Relayout
295       // in that situation
296     }
297
298   private:
299     bool& mLock;
300   };
301
302 private:
303
304   // Undefined copy constructor and assignment operators
305   TableView(const TableView&);
306   TableView& operator=(const TableView& rhs);
307
308 private: // Data
309
310   Array2d<CellData> mCellData;
311   Array2d<Size> mRelativeSizes;
312   std::vector<float> mFixedHeights;
313   std::vector<float> mRelativeHeights;
314   std::vector<float> mFixedWidths;
315   std::vector<float> mRelativeWidths;
316   Size mPadding;
317   bool mLayoutingChild;
318   float mConstraintDuration;
319
320 };
321
322 } // namespace Internal
323
324 // Helpers for public-api forwarding methods
325
326 inline Toolkit::Internal::TableView& GetImpl( Toolkit::TableView& tableView )
327 {
328   DALI_ASSERT_ALWAYS(tableView);
329
330   Dali::RefObject& handle = tableView.GetImplementation();
331
332   return static_cast<Toolkit::Internal::TableView&>(handle);
333 }
334
335 inline const Toolkit::Internal::TableView& GetImpl( const Toolkit::TableView& tableView )
336 {
337   DALI_ASSERT_ALWAYS(tableView);
338
339   const Dali::RefObject& handle = tableView.GetImplementation();
340
341   return static_cast<const Toolkit::Internal::TableView&>(handle);
342 }
343
344 } // namespace Toolkit
345
346 } // namespace Dali
347
348 #endif // __DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H__