ElementDataGridRow.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCONTROLSELEMENTDATAGRIDROW_H
  28. #define ROCKETCONTROLSELEMENTDATAGRIDROW_H
  29. #include "Header.h"
  30. #include "DataSourceListener.h"
  31. #include "DataQuery.h"
  32. #include "../Core/Element.h"
  33. #include <queue>
  34. namespace Rocket {
  35. namespace Controls {
  36. class ElementDataGrid;
  37. /**
  38. Class for rows inside a data table. Used for both the header and the individual rows.
  39. @author Robert Curry
  40. */
  41. class ROCKETCONTROLS_API ElementDataGridRow : public Core::Element, public DataSourceListener
  42. {
  43. friend class ElementDataGrid;
  44. public:
  45. ElementDataGridRow(const Rocket::Core::String& tag);
  46. virtual ~ElementDataGridRow();
  47. void Initialise(ElementDataGrid* parent_grid, ElementDataGridRow* parent_row = NULL, int child_index = -1, ElementDataGridRow* header_row = NULL, int depth = -1);
  48. void SetChildIndex(int child_index);
  49. int GetDepth();
  50. void SetDataSource(const Rocket::Core::String& data_source_name);
  51. /// Checks dirty children and cells, and loads them if necessary.
  52. /// @return True if any children were updated.
  53. bool UpdateChildren();
  54. /// Returns the number of children that aren't dirty (have been loaded)
  55. int GetNumLoadedChildren();
  56. /// Removes all the child cells and fetches them again from the data source.
  57. void RefreshRows();
  58. /// Returns whether this row is expanded or not.
  59. bool IsRowExpanded();
  60. /// Shows all of this row's descendants.
  61. void ExpandRow();
  62. /// Hides all of this row's descendants.
  63. void CollapseRow();
  64. /// Expands the row if collapsed, or collapses the row if expanded.
  65. void ToggleRow();
  66. /// Returns the index of this row, relative to its parent.
  67. int GetParentRelativeIndex();
  68. /// Returns the index of this row, relative to the table rather than its parent.
  69. int GetTableRelativeIndex();
  70. /// Returns the parent row of this row.
  71. ElementDataGridRow* GetParentRow();
  72. /// Returns the grid that this row belongs to.
  73. ElementDataGrid* GetParentGrid();
  74. protected:
  75. virtual void OnDataSourceDestroy(DataSource* data_source);
  76. virtual void OnRowAdd(DataSource* data_source, const Rocket::Core::String& table, int first_row_added, int num_rows_added);
  77. virtual void OnRowRemove(DataSource* data_source, const Rocket::Core::String& table, int first_row_removed, int num_rows_removed);
  78. virtual void OnRowChange(DataSource* data_source, const Rocket::Core::String& table, int first_row_changed, int num_rows_changed);
  79. virtual void OnRowChange(DataSource* data_source, const Rocket::Core::String& table);
  80. private:
  81. typedef std::queue< ElementDataGridRow* > RowQueue;
  82. typedef std::vector< ElementDataGridRow* > RowList;
  83. // Called when a row change (addition or removal) occurs in one of our
  84. // children. Causes the table row index to be dirtied on all following
  85. // children.
  86. void ChildChanged(int child_index);
  87. // Checks if any columns are dependent on the number of children
  88. // present, and refreshes them from the data source if they are.
  89. void RefreshChildDependentCells();
  90. // Forces the row to recalculate its relative table index the next time
  91. // it is requested.
  92. void DirtyTableRelativeIndex();
  93. // Works out what the table relative index is for a given child.
  94. int GetChildTableRelativeIndex(int child_index);
  95. // Adds children underneath this row, and fetches their contents (and
  96. // possible children) from the row's data source. If first_row is left
  97. // as the default -1, the rows are appended at the end of the list.
  98. void AddChildren(int first_row_added = -1, int num_rows_added = 1);
  99. // Removes this rows children, and their children, etc, from the table.
  100. // If the num_rows_removed parameter is left as the -1 default, it'll
  101. // default to the rest of the children after the first row.
  102. void RemoveChildren(int first_row_removed = 0, int num_rows_removed = -1);
  103. // Marks children as dirty and dispatches the event.
  104. void ChangeChildren(int first_row_changed = 0, int num_rows_changed = -1);
  105. // Returns the number of rows under this row (children, grandchildren, etc)
  106. int GetNumDescendants();
  107. // Adds or refreshes the cell contents, and undirties the row's cells.
  108. void Load(const Rocket::Controls::DataQuery& row_information);
  109. // Finds all children that have cell information missing (either though being
  110. // refreshed or not being loaded yet) and reloads them.
  111. void LoadChildren(float time_slice);
  112. // Loads a specific set of children. Called by the above function.
  113. void LoadChildren(int first_row_to_load, int num_rows_to_load, Rocket::Core::Time time_slice);
  114. // If the cells need reloading, this takes care of it. If any children
  115. // need updating, they are added to the queue.
  116. void UpdateCellsAndChildren(RowQueue& dirty_rows);
  117. // Sets the dirty_cells flag on this row, and lets our ancestors know.
  118. void DirtyCells();
  119. // Sets the dirty children flag on this row and the row's ancestors.
  120. void DirtyRow();
  121. // This row has one or more cells that need loading.
  122. bool dirty_cells;
  123. // This row has one or more children that have either dirty flag set.
  124. bool dirty_children;
  125. // Shows this row, and, if this was was expanded before it was hidden, its children as well.
  126. void Show();
  127. // Hides this row and all descendants.
  128. void Hide();
  129. bool row_expanded;
  130. int table_relative_index;
  131. bool table_relative_index_dirty;
  132. ElementDataGrid* parent_grid;
  133. ElementDataGridRow* parent_row;
  134. int child_index;
  135. int depth;
  136. RowList children;
  137. // The data source and table that the children are fetched from.
  138. DataSource* data_source;
  139. Rocket::Core::String data_table;
  140. };
  141. }
  142. }
  143. #endif