1 #ifndef DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H
2 #define DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H
5 * Copyright (c) 2019 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/public-api/object/weak-handle.h>
25 #include <dali-toolkit/public-api/controls/control-impl.h>
26 #include <dali-toolkit/public-api/controls/table-view/table-view.h>
39 * TableView is a custom control for laying out actors in a table layout
40 * @see Dali::Toolkit:TableView for more details
42 class TableView : public Control
47 * Create a new TableView.
48 * @return A smart-pointer to the newly allocated TableView.
50 static Toolkit::TableView New( unsigned int initialRows, unsigned int initialColumns );
53 * @copydoc Toolkit::TableView::AddChild
55 bool AddChild( Actor& child, const Toolkit::TableView::CellPosition& position );
58 * @copydoc Toolkit::TableView::GetChildAt
60 Actor GetChildAt( const Toolkit::TableView::CellPosition& position );
63 * @copydoc Toolkit::TableView::RemoveChildAt
65 Actor RemoveChildAt( const Toolkit::TableView::CellPosition& position );
68 * @copydoc Toolkit::TableView::FindChildPosition
70 bool FindChildPosition( const Actor& child, Toolkit::TableView::CellPosition& positionOut );
73 * @copydoc Toolkit::TableView::InsertRow
75 void InsertRow( unsigned int rowIndex );
78 * @copydoc Toolkit::TableView::DeleteRow( unsigned int rowIndex )
80 void DeleteRow( unsigned int rowIndex );
83 * @copydoc Toolkit::TableView::DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed )
85 void DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed );
88 * @copydoc Toolkit::TableView::InsertColumn
90 void InsertColumn( unsigned int columnIndex );
93 * @copydoc Toolkit::TableView::DeleteColumn( unsigned int columnIndex )
95 void DeleteColumn( unsigned int columnIndex );
98 * @copydoc Toolkit::TableView::DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed )
100 void DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed );
103 * @copydoc Toolkit::TableView::Resize( unsigned int rows, unsigned int columns )
105 void Resize( unsigned int rows, unsigned int columns );
108 * @copydoc Toolkit::TableView::Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed )
110 void Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed );
113 * @copydoc Toolkit::TableView::SetCellPadding
115 void SetCellPadding( Size padding );
118 * @copydoc Toolkit::TableView::GetCellPadding
120 Size GetCellPadding();
123 * @copydoc Toolkit::TableView::SetFitHeight
125 void SetFitHeight( unsigned int rowIndex );
128 * @copydoc Toolkit::TableView::IsFitHeight
130 bool IsFitHeight( unsigned int rowIndex ) const;
133 * @copydoc Toolkit::TableView::SetFitWidth
135 void SetFitWidth( unsigned int columnIndex );
138 * @copydoc Toolkit::TableView::IsFitWidth
140 bool IsFitWidth( unsigned int columnIndex ) const;
143 * @copydoc Toolkit::TableView::SetFixedWidth
145 void SetFixedWidth( unsigned int columnIndex, float width );
148 * @copydoc Toolkit::TableView::GetFixedWidth
150 float GetFixedWidth( unsigned int columnIndex ) const;
153 * @copydoc Toolkit::TableView::SetFixedHeight
155 void SetFixedHeight( unsigned int rowIndex, float height );
158 * @copydoc Toolkit::TableView::GetFixedHeight
160 float GetFixedHeight( unsigned int rowIndex ) const;
163 * @copydoc Toolkit::TableView::SetRelativeHeight
165 void SetRelativeHeight( unsigned int rowIndex, float heightPercentage );
168 * @copydoc Toolkit::TableView::GetRelativeHeight
170 float GetRelativeHeight( unsigned int rowIndex ) const;
173 * @copydoc Toolkit::TableView::SetRelativeWidth
175 void SetRelativeWidth( unsigned int columnIndex, float widthPercentage );
178 * @copydoc Toolkit::TableView::GetRelativeWidth
180 float GetRelativeWidth( unsigned int columnIndex ) const;
183 * @copydoc Toolkit::TableView::GetRows
185 unsigned int GetRows();
188 * @copydoc Toolkit::TableView::GetColumns
190 unsigned int GetColumns();
193 * @copydoc Toolkit::TableView::SetCellAlignment
195 void SetCellAlignment( Toolkit::TableView::CellPosition position, HorizontalAlignment::Type horizontal, VerticalAlignment::Type vertical );
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.
205 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
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.
213 static Property::Value GetProperty( BaseObject* object, Property::Index index );
215 private: // From Control
218 * @copydoc Control::OnChildAdd(Actor& child)
220 virtual void OnChildAdd( Actor& child );
223 * @copydoc Control::OnChildRemove(Actor& child)
225 virtual void OnChildRemove( Actor& child );
228 * @copydoc Control::OnRelayout
230 virtual void OnRelayout( const Vector2& size, RelayoutContainer& container );
233 * @copydoc Control::CalculateChildSize
235 virtual float CalculateChildSize( const Actor& child, Dimension::Type dimension );
238 * @copydoc Control::OnInitialize()
240 virtual void OnInitialize();
243 * @copydoc Control::GetNextKeyboardFocusableActor
245 virtual Actor GetNextKeyboardFocusableActor( Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled );
248 * @copydoc Control::GetNaturalSize()
250 virtual Vector3 GetNaturalSize();
253 * @copydoc Control::RelayoutDependentOnChildren()
255 virtual bool RelayoutDependentOnChildren( Dimension::Type dimension = Dimension::ALL_DIMENSIONS );
258 * @copydoc Control::OnCalculateRelayoutSize
260 virtual void OnCalculateRelayoutSize( Dimension::Type dimension );
263 * @copydoc Control::OnLayoutNegotiated
265 virtual void OnLayoutNegotiated( float size, Dimension::Type dimension );
268 * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
270 virtual void OnSizeSet( const Vector3& size );
272 private: // Implementation
275 * Struct to hold data for rows and columns
277 * If sizePolicy is FIXED then size is the absolute size to use.
278 * If sizePolicy is FIT, RELATIVE or FILL then size is the calculated value of size.
283 * Default constructor
289 sizePolicy( Toolkit::TableView::FILL )
296 * @param[in] newSize The size to set for this data
297 * @param[in] newSizePolicy The policy used to interpret the size value
299 RowColumnData( float newSize, float newFillRatio, Toolkit::TableView::LayoutPolicy newSizePolicy )
301 fillRatio( newFillRatio ),
303 sizePolicy( newSizePolicy )
307 float size; ///< Set or calculated size
308 float fillRatio; ///< Ratio to fill remaining space, only valid with RELATIVE or FILL policy
309 float position; ///< Position of the row/column, this value is updated during every Relayout round
310 Toolkit::TableView::LayoutPolicy sizePolicy; ///< The size policy used to interpret the size value
313 typedef Dali::Vector<RowColumnData> RowColumnArray;
318 * Structure for the layout data
323 : horizontalAlignment( HorizontalAlignment::LEFT ),
324 verticalAlignment( VerticalAlignment::TOP )
330 Toolkit::TableView::CellPosition position;
331 HorizontalAlignment::Type horizontalAlignment;
332 VerticalAlignment::Type verticalAlignment;
338 * Construct a new TableView.
340 TableView( unsigned int initialRows, unsigned int initialColumns );
343 * Resizes the data containers to match the new size
344 * @param [in] rows in the table
345 * @param [in] columns in the table
347 void ResizeContainers( unsigned int rows, unsigned int columns );
350 * Resizes the data containers to match the new size
351 * @param [in] rows in the table
352 * @param [in] columns in the table
353 * @param [out] removed celldata
355 void ResizeContainers( unsigned int rows, unsigned int columns, std::vector<CellData>& removed );
358 * Helper to get the list of lost actors in the case when table looses cells.
359 * Also handles the case when actors span multiple cells
361 * @param removed actors
362 * @param rowsRemoved from table
363 * @param columnsRemoved from table
365 void RemoveAndGetLostActors( const std::vector<CellData>& lost, std::vector<Actor>& removed,
366 unsigned int rowsRemoved, unsigned int columnsRemoved );
369 * Helper to remove all instances of the actor
370 * @param child actor to remove
371 * @return true if the actor was found
373 bool RemoveAllInstances( const Actor& child );
376 * @brief Calculate the ratio of FILL rows/columns
378 * @param[in] data The RowColumn data to compute the relative sizes for
380 void CalculateFillSizes( RowColumnArray& data );
383 * @brief Calculate the total fixed sizes for a row or column
385 * @param[in] data The row or column data to process
387 float CalculateTotalFixedSize( const RowColumnArray& data );
390 * @brief Calculate the sizes of FIT rows/columns
392 * @param[in] data The row or column data to process
393 * @param[in] dimension The dimension being calculated: row == Dimension::HEIGHT, column == Dimension::WIDTH
395 void CalculateFitSizes( RowColumnArray& data, Dimension::Type dimension );
398 * @brief Search for a FIT cell in the array
400 * @param[in] data The row or column data to process
401 * @return Return if a FIT cell was found or not
403 bool FindFit( const RowColumnArray& data );
406 * @brief Return the cell padding for a given dimension
408 * @param[in] dimension The dimension to return the padding for
409 * @return Return the padding (x = low, y = high)
411 Vector2 GetCellPadding( Dimension::Type dimension );
414 * A reference counted object may only be deleted by calling Unreference()
416 virtual ~TableView();
418 private: // scripting support
421 * Called to set the heights/widths property.
422 * @param[in] tableViewImpl The object whose property is set.
423 * @param[in] funcFixed The set function to call, it can be SetFixedHeight or SetFixedWidth.
424 * @param[in] funcRelative The set function to call, it can be SetRelativeHeight or SetRelativeWidth.
425 * @param[in] funcFit The set function to call, it can be SetFitHeight or SetFiltWidth.
426 * @param[in] value The new property value.
428 static void SetHeightOrWidthProperty( TableView& tableViewImpl,
429 void(TableView::*funcFixed)(unsigned int, float),
430 void(TableView::*funcRelative)(unsigned int, float),
431 void(TableView::*funcFit)(unsigned int),
432 const Property::Value& map );
435 * Called to retrieve the property value of row heights.
436 * @return The property value of row heights.
438 Property::Value GetRowHeightsPropertyValue();
441 * Called to retrieve the property value of column widths.
442 * @return The fixed-widths property value.
444 Property::Value GetColumnWidthsPropertyValue();
447 * Generate the map type property value from the size vectors.
448 * @param[in] data The array of row or column data
449 * @param[out] map The property value.
451 void GetMapPropertyValue( const RowColumnArray& data, Property::Map& map );
455 * Helper class to prevent child adds and removes from causing relayout
456 * when we're already anyways going to do one in the end
458 class RelayoutingLock
463 * Constructor, sets the lock boolean
465 RelayoutingLock( TableView& parent )
466 : mLock( parent.mLayoutingChild )
472 * Destructor, releases lock boolean
477 // Note, we could also call Relayout here. This would save one line of code
478 // from each method that uses this lock but destructors are not meant to do
479 // big processing so better to not do it here. This destructor would also
480 // be called in case of an exception and we don't definitely want to do Relayout
490 // Undefined copy constructor and assignment operators
491 TableView(const TableView&);
492 TableView& operator=(const TableView& rhs);
496 Array2d<CellData> mCellData; ///< Data for each cell: Actor, alignment settings etc
498 RowColumnArray mRowData; ///< Data for each row
499 RowColumnArray mColumnData; ///< Data for each column
500 Size mFixedTotals; ///< Accumulated totals for fixed width and height
502 Size mPadding; ///< Padding to apply to each cell
504 WeakHandle<Actor> mPreviousFocusedActor; ///< Perviously focused actor
505 bool mLayoutingChild; ///< Can't be a bitfield due to Relayouting lock
506 bool mRowDirty : 1; ///< Flag to indicate the row data is dirty
507 bool mColumnDirty : 1; ///< Flag to indicate the column data is dirty
510 } // namespace Internal
512 // Helpers for public-api forwarding methods
514 inline Toolkit::Internal::TableView& GetImpl( Toolkit::TableView& tableView )
516 DALI_ASSERT_ALWAYS(tableView);
518 Dali::RefObject& handle = tableView.GetImplementation();
520 return static_cast<Toolkit::Internal::TableView&>(handle);
523 inline const Toolkit::Internal::TableView& GetImpl( const Toolkit::TableView& tableView )
525 DALI_ASSERT_ALWAYS(tableView);
527 const Dali::RefObject& handle = tableView.GetImplementation();
529 return static_cast<const Toolkit::Internal::TableView&>(handle);
532 } // namespace Toolkit
536 #endif // DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H