ElementDataGrid.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 ROCKETCONTROLSELEMENTDATAGRID_H
  28. #define ROCKETCONTROLSELEMENTDATAGRID_H
  29. #include "Header.h"
  30. #include "DataSourceListener.h"
  31. #include "../Core/Element.h"
  32. namespace Rocket {
  33. namespace Controls {
  34. class DataFormatter;
  35. class ElementDataGridRow;
  36. /**
  37. A table driven from a data source.
  38. @author Robert Curry
  39. */
  40. class ROCKETCONTROLS_API ElementDataGrid : public Core::Element, public DataSourceListener
  41. {
  42. public:
  43. ElementDataGrid(const Rocket::Core::String& tag);
  44. virtual ~ElementDataGrid();
  45. /// Sets a new data source for the contents of the data grid.
  46. /// @param[in] data_source_name The name of the new data source.
  47. void SetDataSource(const Rocket::Core::String& data_source_name);
  48. /**
  49. A column inside a table.
  50. @author Robert Curry
  51. */
  52. struct Column
  53. {
  54. /// The list of fields that this column reads from the data source for
  55. /// each row.
  56. Rocket::Core::StringList fields;
  57. /// The data formatter this is used to process the field information
  58. /// into what is finally displayed in the data grid.
  59. DataFormatter* formatter;
  60. /// The header that is displayed at the top of the column, in the
  61. /// header row.
  62. Core::Element* header;
  63. /// The width of this column.
  64. float current_width;
  65. /// Whether this column has a forced refresh when a child node changes.
  66. /// This is to allow the expand/collapse buttons to be added or removed
  67. /// when a child node is added.
  68. bool refresh_on_child_change;
  69. };
  70. /// Adds a column to the table.
  71. /// @param[in] fields A comma-separated list of fields that this column reads from the data source.
  72. /// @param[in] formatter The name of the data formatter to be used to format the raw column data into RML.
  73. /// @param[in] initial_width The initial width, in pixels, of the column.
  74. /// @param[in] header_rml The RML to use as the column header.
  75. /// @return True if the column was added successfully, false if not.
  76. bool AddColumn(const Rocket::Core::String& fields, const Rocket::Core::String& formatter, float initial_width, const Rocket::Core::String& header_rml);
  77. /// Adds a column to the table.
  78. /// @param[in] fields A comma-separated list of fields that this column reads from the data source.
  79. /// @param[in] formatter The name of the data formatter to be used to format the raw column data into RML.
  80. /// @param[in] initial_width The initial width, in pixels, of the column.
  81. /// @param[in] header_element The element hierarchy to use as the column header.
  82. void AddColumn(const Rocket::Core::String& fields, const Rocket::Core::String& formatter, float initial_width, Core::Element* header_element);
  83. /// Returns the number of columns in this table
  84. int GetNumColumns();
  85. /// Returns the column at the specified index.
  86. const Column* GetColumn(int column_index);
  87. /// Returns a CSV string containing all the fields that each column requires, in order.
  88. const Rocket::Core::String& GetAllColumnFields();
  89. /// Adds a new row to the table. This is only called from child rows.
  90. /// @param[in] parent The parent row that the row is being added under.
  91. /// @param[in] index The index of the child, relative to its parent.
  92. /// @return A pointer to the newly created row.
  93. ElementDataGridRow* AddRow(ElementDataGridRow* parent, int index);
  94. /// Removes a series of rows from the table.
  95. /// @param[in] index The index of the first row, relative to the table.
  96. /// @param[in] num_rows The number of rows to remove. Defaults to one.
  97. void RemoveRows(int index, int num_rows = 1);
  98. /// Returns the number of rows in the table
  99. int GetNumRows() const;
  100. /// Returns the row at the given index in the table.
  101. /// @param[in] index The index of the row, relative to the table.
  102. ElementDataGridRow* GetRow(int index) const;
  103. protected:
  104. virtual void OnUpdate();
  105. virtual void ProcessEvent(Core::Event& event);
  106. /// Gets the markup and content of the element.
  107. /// @param content[out] The content of the element.
  108. virtual void GetInnerRML(Rocket::Core::String& content) const;
  109. private:
  110. typedef std::vector< Column > ColumnList;
  111. typedef std::vector< ElementDataGridRow* > RowList;
  112. ColumnList columns;
  113. Rocket::Core::String column_fields;
  114. // The row that contains the header elements of the table.
  115. ElementDataGridRow* header;
  116. // The root row, all the top level rows are children under this. Not
  117. // actually rendered, has "display: none".
  118. ElementDataGridRow* root;
  119. // If this is non-empty, then in the previous update the data source was set
  120. // and we must set it this update.
  121. Rocket::Core::String new_data_source;
  122. // The block element that contains all our rows. Only used for applying styles.
  123. Core::Element* body;
  124. // Stores if the body has already been made visible by having enough rows added.
  125. bool body_visible;
  126. };
  127. }
  128. }
  129. #endif