1 #ifndef DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H
2 #define DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H
5 * Copyright (c) 2021 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/devel-api/controls/table-view/table-view.h>
26 #include <dali-toolkit/public-api/controls/control-impl.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
43 * Create a new TableView.
44 * @return A smart-pointer to the newly allocated TableView.
46 static Toolkit::TableView New(unsigned int initialRows, unsigned int initialColumns);
49 * @copydoc Toolkit::TableView::AddChild
51 bool AddChild(Actor& child, const Toolkit::TableView::CellPosition& position);
54 * @copydoc Toolkit::TableView::GetChildAt
56 Actor GetChildAt(const Toolkit::TableView::CellPosition& position);
59 * @copydoc Toolkit::TableView::RemoveChildAt
61 Actor RemoveChildAt(const Toolkit::TableView::CellPosition& position);
64 * @copydoc Toolkit::TableView::FindChildPosition
66 bool FindChildPosition(const Actor& child, Toolkit::TableView::CellPosition& positionOut);
69 * @copydoc Toolkit::TableView::InsertRow
71 void InsertRow(unsigned int rowIndex);
74 * @copydoc Toolkit::TableView::DeleteRow( unsigned int rowIndex )
76 void DeleteRow(unsigned int rowIndex);
79 * @copydoc Toolkit::TableView::DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed )
81 void DeleteRow(unsigned int rowIndex, std::vector<Actor>& removed);
84 * @copydoc Toolkit::TableView::InsertColumn
86 void InsertColumn(unsigned int columnIndex);
89 * @copydoc Toolkit::TableView::DeleteColumn( unsigned int columnIndex )
91 void DeleteColumn(unsigned int columnIndex);
94 * @copydoc Toolkit::TableView::DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed )
96 void DeleteColumn(unsigned int columnIndex, std::vector<Actor>& removed);
99 * @copydoc Toolkit::TableView::Resize( unsigned int rows, unsigned int columns )
101 void Resize(unsigned int rows, unsigned int columns);
104 * @copydoc Toolkit::TableView::Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed )
106 void Resize(unsigned int rows, unsigned int columns, std::vector<Actor>& removed);
109 * @copydoc Toolkit::TableView::SetCellPadding
111 void SetCellPadding(Size padding);
114 * @copydoc Toolkit::TableView::GetCellPadding
116 Size GetCellPadding();
119 * @copydoc Toolkit::TableView::SetFitHeight
121 void SetFitHeight(unsigned int rowIndex);
124 * @copydoc Toolkit::TableView::IsFitHeight
126 bool IsFitHeight(unsigned int rowIndex) const;
129 * @copydoc Toolkit::TableView::SetFitWidth
131 void SetFitWidth(unsigned int columnIndex);
134 * @copydoc Toolkit::TableView::IsFitWidth
136 bool IsFitWidth(unsigned int columnIndex) const;
139 * @copydoc Toolkit::TableView::SetFixedWidth
141 void SetFixedWidth(unsigned int columnIndex, float width);
144 * @copydoc Toolkit::TableView::GetFixedWidth
146 float GetFixedWidth(unsigned int columnIndex) const;
149 * @copydoc Toolkit::TableView::SetFixedHeight
151 void SetFixedHeight(unsigned int rowIndex, float height);
154 * @copydoc Toolkit::TableView::GetFixedHeight
156 float GetFixedHeight(unsigned int rowIndex) const;
159 * @copydoc Toolkit::TableView::SetRelativeHeight
161 void SetRelativeHeight(unsigned int rowIndex, float heightPercentage);
164 * @copydoc Toolkit::TableView::GetRelativeHeight
166 float GetRelativeHeight(unsigned int rowIndex) const;
169 * @copydoc Toolkit::TableView::SetRelativeWidth
171 void SetRelativeWidth(unsigned int columnIndex, float widthPercentage);
174 * @copydoc Toolkit::TableView::GetRelativeWidth
176 float GetRelativeWidth(unsigned int columnIndex) const;
179 * @copydoc Toolkit::TableView::GetRows
181 unsigned int GetRows();
184 * @copydoc Toolkit::TableView::GetColumns
186 unsigned int GetColumns();
189 * @copydoc Toolkit::TableView::SetCellAlignment
191 void SetCellAlignment(Toolkit::TableView::CellPosition position, HorizontalAlignment::Type horizontal, VerticalAlignment::Type vertical);
196 * Called when a property of an object of this type is set.
197 * @param[in] object The object whose property is set.
198 * @param[in] index The property index.
199 * @param[in] value The new property value.
201 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
204 * Called to retrieve a property of an object of this type.
205 * @param[in] object The object whose property is to be retrieved.
206 * @param[in] index The property index.
207 * @return The current value of the property.
209 static Property::Value GetProperty(BaseObject* object, Property::Index index);
211 private: // From Control
213 * @copydoc Control::OnChildAdd(Actor& child)
215 void OnChildAdd(Actor& child) override;
218 * @copydoc Control::OnChildRemove(Actor& child)
220 void OnChildRemove(Actor& child) override;
223 * @copydoc Control::OnRelayout
225 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
228 * @copydoc Control::CalculateChildSize
230 float CalculateChildSize(const Actor& child, Dimension::Type dimension) override;
233 * @copydoc Control::OnInitialize()
235 void OnInitialize() override;
238 * @copydoc Control::GetNextKeyboardFocusableActor
240 Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled) override;
243 * @copydoc Control::GetNaturalSize()
245 Vector3 GetNaturalSize() override;
248 * @copydoc Control::RelayoutDependentOnChildren()
250 bool RelayoutDependentOnChildren(Dimension::Type dimension = Dimension::ALL_DIMENSIONS) override;
253 * @copydoc Control::OnCalculateRelayoutSize
255 void OnCalculateRelayoutSize(Dimension::Type dimension) override;
258 * @copydoc Control::OnLayoutNegotiated
260 void OnLayoutNegotiated(float size, Dimension::Type dimension) override;
263 * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
265 void OnSizeSet(const Vector3& size) override;
267 private: // Implementation
269 * Struct to hold data for rows and columns
271 * If sizePolicy is FIXED then size is the absolute size to use.
272 * If sizePolicy is FIT, RELATIVE or FILL then size is the calculated value of size.
277 * Default constructor
283 sizePolicy(Toolkit::TableView::FILL)
290 * @param[in] newSize The size to set for this data
291 * @param[in] newSizePolicy The policy used to interpret the size value
293 RowColumnData(float newSize, float newFillRatio, Toolkit::TableView::LayoutPolicy newSizePolicy)
295 fillRatio(newFillRatio),
297 sizePolicy(newSizePolicy)
301 float size; ///< Set or calculated size
302 float fillRatio; ///< Ratio to fill remaining space, only valid with RELATIVE or FILL policy
303 float position; ///< Position of the row/column, this value is updated during every Relayout round
304 Toolkit::TableView::LayoutPolicy sizePolicy; ///< The size policy used to interpret the size value
307 typedef Dali::Vector<RowColumnData> RowColumnArray;
311 * Structure for the layout data
316 : horizontalAlignment(HorizontalAlignment::LEFT),
317 verticalAlignment(VerticalAlignment::TOP)
323 Toolkit::TableView::CellPosition position;
324 HorizontalAlignment::Type horizontalAlignment;
325 VerticalAlignment::Type verticalAlignment;
330 * Construct a new TableView.
332 TableView(unsigned int initialRows, unsigned int initialColumns);
335 * Resizes the data containers to match the new size
336 * @param [in] rows in the table
337 * @param [in] columns in the table
339 void ResizeContainers(unsigned int rows, unsigned int columns);
342 * Resizes the data containers to match the new size
343 * @param [in] rows in the table
344 * @param [in] columns in the table
345 * @param [out] removed celldata
347 void ResizeContainers(unsigned int rows, unsigned int columns, std::vector<CellData>& removed);
350 * Helper to get the list of lost actors in the case when table looses cells.
351 * Also handles the case when actors span multiple cells
353 * @param removed actors
354 * @param rowsRemoved from table
355 * @param columnsRemoved from table
357 void RemoveAndGetLostActors(const std::vector<CellData>& lost, std::vector<Actor>& removed, unsigned int rowsRemoved, unsigned int columnsRemoved);
360 * Helper to remove all instances of the actor
361 * @param child actor to remove
362 * @return true if the actor was found
364 bool RemoveAllInstances(const Actor& child);
367 * @brief Calculate the ratio of FILL rows/columns
369 * @param[in] data The RowColumn data to compute the relative sizes for
371 void CalculateFillSizes(RowColumnArray& data);
374 * @brief Calculate the total fixed sizes for a row or column
376 * @param[in] data The row or column data to process
378 float CalculateTotalFixedSize(const RowColumnArray& data);
381 * @brief Calculate the sizes of FIT rows/columns
383 * @param[in] data The row or column data to process
384 * @param[in] dimension The dimension being calculated: row == Dimension::HEIGHT, column == Dimension::WIDTH
386 void CalculateFitSizes(RowColumnArray& data, Dimension::Type dimension);
389 * @brief Search for a FIT cell in the array
391 * @param[in] data The row or column data to process
392 * @return Return if a FIT cell was found or not
394 bool FindFit(const RowColumnArray& data);
397 * @brief Return the cell padding for a given dimension
399 * @param[in] dimension The dimension to return the padding for
400 * @return Return the padding (x = low, y = high)
402 Vector2 GetCellPadding(Dimension::Type dimension);
405 * A reference counted object may only be deleted by calling Unreference()
407 virtual ~TableView();
409 private: // scripting support
411 * Called to set the heights/widths property.
412 * @param[in] tableViewImpl The object whose property is set.
413 * @param[in] funcFixed The set function to call, it can be SetFixedHeight or SetFixedWidth.
414 * @param[in] funcRelative The set function to call, it can be SetRelativeHeight or SetRelativeWidth.
415 * @param[in] funcFit The set function to call, it can be SetFitHeight or SetFiltWidth.
416 * @param[in] value The new property value.
418 static void SetHeightOrWidthProperty(TableView& tableViewImpl,
419 void (TableView::*funcFixed)(unsigned int, float),
420 void (TableView::*funcRelative)(unsigned int, float),
421 void (TableView::*funcFit)(unsigned int),
422 const Property::Value& map);
425 * Called to retrieve the property value of row heights.
426 * @return The property value of row heights.
428 Property::Value GetRowHeightsPropertyValue();
431 * Called to retrieve the property value of column widths.
432 * @return The fixed-widths property value.
434 Property::Value GetColumnWidthsPropertyValue();
437 * Generate the map type property value from the size vectors.
438 * @param[in] data The array of row or column data
439 * @param[out] map The property value.
441 void GetMapPropertyValue(const RowColumnArray& data, Property::Map& map);
444 * Helper class to prevent child adds and removes from causing relayout
445 * when we're already anyways going to do one in the end
447 class RelayoutingLock
451 * Constructor, sets the lock boolean
453 RelayoutingLock(TableView& parent)
454 : mLock(parent.mLayoutingChild)
460 * Destructor, releases lock boolean
465 // Note, we could also call Relayout here. This would save one line of code
466 // from each method that uses this lock but destructors are not meant to do
467 // big processing so better to not do it here. This destructor would also
468 // be called in case of an exception and we don't definitely want to do Relayout
477 // Undefined copy constructor and assignment operators
478 TableView(const TableView&);
479 TableView& operator=(const TableView& rhs);
482 Array2d<CellData> mCellData; ///< Data for each cell: Actor, alignment settings etc
484 RowColumnArray mRowData; ///< Data for each row
485 RowColumnArray mColumnData; ///< Data for each column
486 Size mFixedTotals; ///< Accumulated totals for fixed width and height
488 Size mPadding; ///< Padding to apply to each cell
490 WeakHandle<Actor> mPreviousFocusedActor; ///< Perviously focused actor
491 bool mLayoutingChild; ///< Can't be a bitfield due to Relayouting lock
492 bool mRowDirty : 1; ///< Flag to indicate the row data is dirty
493 bool mColumnDirty : 1; ///< Flag to indicate the column data is dirty
496 } // namespace Internal
498 // Helpers for public-api forwarding methods
500 inline Toolkit::Internal::TableView& GetImpl(Toolkit::TableView& tableView)
502 DALI_ASSERT_ALWAYS(tableView);
504 Dali::RefObject& handle = tableView.GetImplementation();
506 return static_cast<Toolkit::Internal::TableView&>(handle);
509 inline const Toolkit::Internal::TableView& GetImpl(const Toolkit::TableView& tableView)
511 DALI_ASSERT_ALWAYS(tableView);
513 const Dali::RefObject& handle = tableView.GetImplementation();
515 return static_cast<const Toolkit::Internal::TableView&>(handle);
518 } // namespace Toolkit
522 #endif // DALI_TOOLKIT_INTERNAL_TABLE_VIEW_H