1 #ifndef __DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H__
2 #define __DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali-toolkit/public-api/controls/control-impl.h>
23 #include <dali-toolkit/public-api/controls/table-view/table-view.h>
36 * TableView is a custom control for laying out actors in a table layout
37 * @see Dali::Toolkit:TableView for more details
39 class TableView : public Control
44 * Enum for the size policies of rows and columns
48 FILL, ///< Fill up available space, may have a ratio associated with it
49 FIXED, ///< A specific fixed width or height
50 FIT ///< Fit around actors in the row or column
54 * Create a new TableView.
55 * @return A smart-pointer to the newly allocated TableView.
57 static Toolkit::TableView New( unsigned int initialRows, unsigned int initialColumns );
60 * @copydoc Toolkit::TableView::AddChild
62 bool AddChild( Actor& child, const Toolkit::TableView::CellPosition& position );
65 * @copydoc Toolkit::TableView::GetChildAt
67 Actor GetChildAt( const Toolkit::TableView::CellPosition& position );
70 * @copydoc Toolkit::TableView::RemoveChildAt
72 Actor RemoveChildAt( const Toolkit::TableView::CellPosition& position );
75 * @copydoc Toolkit::TableView::FindChildPosition
77 bool FindChildPosition( const Actor& child, Toolkit::TableView::CellPosition& positionOut );
80 * @copydoc Toolkit::TableView::InsertRow
82 void InsertRow( unsigned int rowIndex );
85 * @copydoc Toolkit::TableView::DeleteRow( unsigned int rowIndex )
87 void DeleteRow( unsigned int rowIndex );
90 * @copydoc Toolkit::TableView::DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed )
92 void DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed );
95 * @copydoc Toolkit::TableView::InsertColumn
97 void InsertColumn( unsigned int columnIndex );
100 * @copydoc Toolkit::TableView::DeleteColumn( unsigned int columnIndex )
102 void DeleteColumn( unsigned int columnIndex );
105 * @copydoc Toolkit::TableView::DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed )
107 void DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed );
110 * @copydoc Toolkit::TableView::Resize( unsigned int rows, unsigned int columns )
112 void Resize( unsigned int rows, unsigned int columns );
115 * @copydoc Toolkit::TableView::Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed )
117 void Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed );
120 * @copydoc Toolkit::TableView::SetCellPadding
122 void SetCellPadding( Size padding );
125 * @copydoc Toolkit::TableView::GetCellPadding
127 Size GetCellPadding();
130 * @brief Set a row policy
132 * @param[in] rowIndex The row to set the policy for
133 * @param[in] policy The policy to set
135 void SetRowPolicy( unsigned int rowIndex, CellSizePolicy policy );
138 * @brief Querry a row policy
140 * @param[in] rowIndex The row to querry
141 * @return Return the policy
143 CellSizePolicy GetRowPolicy( unsigned int rowIndex ) const;
146 * @brief Set a column policy
148 * @param[in] columnIndex The column to set the policy for
149 * @param[in] policy The policy to set
151 void SetColumnPolicy( unsigned int columnIndex, CellSizePolicy policy );
154 * @brief Querry a column policy
156 * @param[in] columnIndex The column to querry
157 * @return Return the policy
159 CellSizePolicy GetColumnPolicy( unsigned int columnIndex ) const;
162 * @copydoc Toolkit::TableView::SetFixedWidth
164 void SetFixedWidth( unsigned int columnIndex, float width );
167 * @copydoc Toolkit::TableView::GetFixedWidth
169 float GetFixedWidth( unsigned int columnIndex ) const;
172 * @copydoc Toolkit::TableView::SetFixedHeight
174 void SetFixedHeight( unsigned int rowIndex, float height );
177 * @copydoc Toolkit::TableView::GetFixedHeight
179 float GetFixedHeight( unsigned int rowIndex ) const;
182 * @copydoc Toolkit::TableView::SetRelativeHeight
184 void SetRelativeHeight( unsigned int rowIndex, float heightPercentage );
187 * @copydoc Toolkit::TableView::GetRelativeHeight
189 float GetRelativeHeight( unsigned int rowIndex ) const;
192 * @copydoc Toolkit::TableView::SetRelativeWidth
194 void SetRelativeWidth( unsigned int columnIndex, float widthPercentage );
197 * @copydoc Toolkit::TableView::GetRelativeWidth
199 float GetRelativeWidth( unsigned int columnIndex ) const;
202 * @copydoc Toolkit::TableView::GetRows
204 unsigned int GetRows();
207 * @copydoc Toolkit::TableView::GetColumns
209 unsigned int GetColumns();
212 * @copydoc Toolkit::TableView::SetCellAlignment
214 void SetCellAlignment( Toolkit::TableView::CellPosition position, HorizontalAlignment::Type horizontal, VerticalAlignment::Type vertical );
219 * Called when a property of an object of this type is set.
220 * @param[in] object The object whose property is set.
221 * @param[in] index The property index.
222 * @param[in] value The new property value.
224 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
227 * Called to retrieve a property of an object of this type.
228 * @param[in] object The object whose property is to be retrieved.
229 * @param[in] index The property index.
230 * @return The current value of the property.
232 static Property::Value GetProperty( BaseObject* object, Property::Index index );
234 private: // From Control
237 * @copydoc Control::OnControlChildAdd(Actor& child)
239 virtual void OnControlChildAdd( Actor& child );
242 * @copydoc Control::OnControlChildRemove(Actor& child)
244 virtual void OnControlChildRemove( Actor& child );
247 * @copydoc Control::OnRelayout
249 virtual void OnRelayout( const Vector2& size, RelayoutContainer& container );
252 * @copydoc Control::CalculateChildSize
254 virtual float CalculateChildSize( const Actor& child, Dimension::Type dimension );
257 * @copydoc Control::OnInitialize()
259 virtual void OnInitialize();
262 * @copydoc Control::GetNextKeyboardFocusableActor
264 virtual Actor GetNextKeyboardFocusableActor( Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled );
267 * @copydoc Control::GetNaturalSize()
269 virtual Vector3 GetNaturalSize();
272 * @copydoc Control::RelayoutDependentOnChildren()
274 virtual bool RelayoutDependentOnChildren( Dimension::Type dimension = Dimension::ALL_DIMENSIONS );
277 * @copydoc Control::OnCalculateRelayoutSize
279 virtual void OnCalculateRelayoutSize( Dimension::Type dimension );
282 * @copydoc Control::OnLayoutNegotiated
284 virtual void OnLayoutNegotiated( float size, Dimension::Type dimension );
286 private: // Implementation
289 * Struct to hold data for rows and columns
291 * If sizePolicy is FIXED then size is the absolute size to use.
292 * If sizePolicy is FIT or FILL then size is the calculated value of size.
297 * Default constructor
303 userFillRatio( false )
310 * @param[in] newSize The size to set for this data
311 * @param[in] newSizePolicy The policy used to interpret the size value
313 RowColumnData( float newSize, float newFillRatio, CellSizePolicy newSizePolicy, bool newUserFillRatio )
315 fillRatio( newFillRatio ),
316 sizePolicy( newSizePolicy ),
317 userFillRatio( newUserFillRatio )
321 float size; ///< Set or calculated size
322 float fillRatio; ///< Ratio to fill remaining space
323 CellSizePolicy sizePolicy; ///< The size policy used to interpret the size value
324 bool userFillRatio : 1; ///< FillRatio was set by user
327 typedef Dali::Vector<RowColumnData> RowColumnArray;
332 * Structure for the layout data
337 : horizontalAlignment( HorizontalAlignment::LEFT ),
338 verticalAlignment( VerticalAlignment::TOP )
344 Toolkit::TableView::CellPosition position;
345 HorizontalAlignment::Type horizontalAlignment;
346 VerticalAlignment::Type verticalAlignment;
352 * Construct a new TableView.
354 TableView( unsigned int initialRows, unsigned int initialColumns );
357 * Resizes the data containers to match the new size
358 * @param [in] rows in the table
359 * @param [in] columns in the table
361 void ResizeContainers( unsigned int rows, unsigned int columns );
364 * Resizes the data containers to match the new size
365 * @param [in] rows in the table
366 * @param [in] columns in the table
367 * @param [out] removed celldata
369 void ResizeContainers( unsigned int rows, unsigned int columns, std::vector<CellData>& removed );
372 * Helper to get the list of lost actors in the case when table looses cells.
373 * Also handles the case when actors span multiple cells
375 * @param removed actors
376 * @param rowsRemoved from table
377 * @param columnsRemoved from table
379 void RemoveAndGetLostActors( const std::vector<CellData>& lost, std::vector<Actor>& removed,
380 unsigned int rowsRemoved, unsigned int columnsRemoved );
383 * Helper to remove all instances of the actor
384 * @param child actor to remove
385 * @return true if the actor was found
387 bool RemoveAllInstances( const Actor& child );
390 * @brief Compute relative sizes for an array
392 * @param[in] data The RowColumn data to compute the relative sizes for
394 void ComputeRelativeSizes( RowColumnArray& data );
397 * @brief Calculate the total fixed sizes for a row or column
399 * @param[in] data The row or column data to process
401 float CalculateTotalFixedSize( const RowColumnArray& data );
404 * @brief Calculate the fixed sizes for a row or column
406 * @param[in] data The row or column data to process
407 * @param[in] dimension The dimension being calculated: row == Dimension::HEIGHT, column == Dimension::WIDTH
409 void CalculateFixedSizes( RowColumnArray& data, Dimension::Type dimension );
412 * @brief Calculate the value of the relative sizes
414 * @param[in] data The row or column data to process
415 * @param[in] size The size of the table view in that dimension
417 void CalculateRelativeSizes( RowColumnArray& data, float size );
420 * @brief Search for a FIT cell in the array
422 * @param[in] data The row or column data to process
423 * @return Return if a FIT cell was found or not
425 bool FindFit( const RowColumnArray& data );
428 * @brief Calculate row and column data when it is dirty
430 void CalculateRowColumnData();
433 * @brief Return the cell padding for a given dimension
435 * @param[in] dimension The dimension to return the padding for
436 * @return Return the padding (x = low, y = high)
438 Vector2 GetCellPadding( Dimension::Type dimension );
441 * A reference counted object may only be deleted by calling Unreference()
443 virtual ~TableView();
445 private: // scripting support
448 * Called to set the heights/widths property.
449 * @param[in] tableViewImpl The object whose property is set.
450 * @param[in] funcFixed The set function to call, it can be SetFixedHeight or SetFixedWidths.
451 * @param[in] funcRelative The set function to call, it can be SetRelativeHeight or SetRelativeWidths.
452 * @param[in] value The new property value.
454 static void SetHeightOrWidthProperty( TableView& tableViewImpl,
455 void(TableView::*funcFixed)(unsigned int, float),
456 void(TableView::*funcRelative)(unsigned int, float),
457 const Property::Value& map );
460 * Called to retrieve the property value of row heights.
461 * @return The property value of row heights.
463 Property::Value GetRowHeightsPropertyValue();
466 * Called to retrieve the property value of column widths.
467 * @return The fixed-widths property value.
469 Property::Value GetColumnWidthsPropertyValue();
472 * Generate the map type property value from the size vectors.
473 * @param[in] data The array of row or column data
474 * @param[out] map The property value.
476 void GetMapPropertyValue( const RowColumnArray& data, Property::Map& map );
480 * Helper class to prevent child adds and removes from causing relayout
481 * when we're already anyways going to do one in the end
483 class RelayoutingLock
488 * Constructor, sets the lock boolean
490 RelayoutingLock( TableView& parent )
491 : mLock( parent.mLayoutingChild )
497 * Destructor, releases lock boolean
502 // Note, we could also call Relayout here. This would save one line of code
503 // from each method that uses this lock but destructors are not meant to do
504 // big processing so better to not do it here. This destructor would also
505 // be called in case of an exception and we don't definitely want to do Relayout
515 // Undefined copy constructor and assignment operators
516 TableView(const TableView&);
517 TableView& operator=(const TableView& rhs);
521 Array2d<CellData> mCellData; ///< Data for each cell: Actor, alignment settings etc
523 RowColumnArray mRowData; ///< Data for each row
524 RowColumnArray mColumnData; ///< Data for each column
525 Size mFixedTotals; ///< Accumulated totals for fixed width and height
527 Size mPadding; ///< Padding to apply to each cell
528 bool mLayoutingChild; ///< Can't be a bitfield due to Relayouting lock
529 bool mRowColumnDirty : 1; ///< Flag to indicate the row column data is dirty
532 } // namespace Internal
534 // Helpers for public-api forwarding methods
536 inline Toolkit::Internal::TableView& GetImpl( Toolkit::TableView& tableView )
538 DALI_ASSERT_ALWAYS(tableView);
540 Dali::RefObject& handle = tableView.GetImplementation();
542 return static_cast<Toolkit::Internal::TableView&>(handle);
545 inline const Toolkit::Internal::TableView& GetImpl( const Toolkit::TableView& tableView )
547 DALI_ASSERT_ALWAYS(tableView);
549 const Dali::RefObject& handle = tableView.GetImplementation();
551 return static_cast<const Toolkit::Internal::TableView&>(handle);
554 } // namespace Toolkit
558 #endif // __DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H__