ElementDataGrid.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Controls/ElementDataGrid.h"
  29. #include "../../Include/RmlUi/Controls/DataSource.h"
  30. #include "../../Include/RmlUi/Core/Math.h"
  31. #include "../../Include/RmlUi/Core/XMLParser.h"
  32. #include "../../Include/RmlUi/Core/Event.h"
  33. #include "../../Include/RmlUi/Core/ElementDocument.h"
  34. #include "../../Include/RmlUi/Core/Factory.h"
  35. #include "../../Include/RmlUi/Core/Property.h"
  36. #include "../../Include/RmlUi/Controls/DataFormatter.h"
  37. #include "../../Include/RmlUi/Controls/ElementDataGridRow.h"
  38. namespace Rml {
  39. namespace Controls {
  40. ElementDataGrid::ElementDataGrid(const Rml::Core::String& tag) : Core::Element(tag)
  41. {
  42. Rml::Core::XMLAttributes attributes;
  43. // Create the row for the column headers:
  44. Core::ElementPtr element = Core::Factory::InstanceElement(this, "#rmlctl_datagridrow", "datagridheader", attributes);
  45. header = static_cast<ElementDataGridRow*>(element.get());
  46. header->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::Block));
  47. header->Initialise(this);
  48. AppendChild(std::move(element));
  49. element = Core::Factory::InstanceElement(this, "*", "datagridbody", attributes);
  50. body = element.get();
  51. body->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::None));
  52. body->SetProperty(Core::PropertyId::Width, Core::Property(Core::Style::Width::Auto));
  53. AppendChild(std::move(element));
  54. body_visible = false;
  55. element = Core::Factory::InstanceElement(this, "#rmlctl_datagridrow", "datagridroot", attributes);
  56. root = static_cast<ElementDataGridRow*>(element.get());
  57. root->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::None));
  58. root->Initialise(this);
  59. AppendChild(std::move(element), false);
  60. SetProperty(Core::PropertyId::OverflowX, Core::Property(Core::Style::Overflow::Auto));
  61. SetProperty(Core::PropertyId::OverflowY, Core::Property(Core::Style::Overflow::Auto));
  62. new_data_source = "";
  63. }
  64. ElementDataGrid::~ElementDataGrid()
  65. {
  66. }
  67. void ElementDataGrid::SetDataSource(const Rml::Core::String& data_source_name)
  68. {
  69. new_data_source = data_source_name;
  70. }
  71. // Adds a column to the table.
  72. bool ElementDataGrid::AddColumn(const Rml::Core::String& fields, const Rml::Core::String& formatter, float initial_width, const Rml::Core::String& header_rml)
  73. {
  74. Core::ElementPtr header_element = Core::Factory::InstanceElement(this, "datagridcolumn", "datagridcolumn", Rml::Core::XMLAttributes());
  75. if (!header_element)
  76. return false;
  77. if (!Core::Factory::InstanceElementText(header_element.get(), header_rml))
  78. {
  79. return false;
  80. }
  81. AddColumn(fields, formatter, initial_width, std::move(header_element));
  82. return true;
  83. }
  84. // Adds a column to the table.
  85. void ElementDataGrid::AddColumn(const Rml::Core::String& fields, const Rml::Core::String& formatter, float initial_width, Core::ElementPtr header_element)
  86. {
  87. Column column;
  88. Rml::Core::StringUtilities::ExpandString(column.fields, fields);
  89. column.formatter = DataFormatter::GetDataFormatter(formatter);
  90. column.header = header;
  91. column.current_width = initial_width;
  92. column.refresh_on_child_change = false;
  93. // The header elements are added to the header row at the top of the table.
  94. if (header_element)
  95. {
  96. header_element->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::InlineBlock));
  97. // Push all the width properties from the column onto the header element.
  98. Rml::Core::String width = header_element->GetAttribute<Rml::Core::String>("width", "100%");
  99. header_element->SetProperty("width", width);
  100. header->AppendChild(std::move(header_element));
  101. }
  102. // Add the fields that this column requires to the concatenated string of all column fields.
  103. for (size_t i = 0; i < column.fields.size(); i++)
  104. {
  105. // Don't append the depth or num_children fields, as they're handled by the row.
  106. if (column.fields[i] == Rml::Controls::DataSource::NUM_CHILDREN)
  107. {
  108. column.refresh_on_child_change = true;
  109. }
  110. else if (column.fields[i] != Rml::Controls::DataSource::DEPTH)
  111. {
  112. if (!column_fields.empty())
  113. {
  114. column_fields += ",";
  115. }
  116. column_fields += column.fields[i];
  117. }
  118. }
  119. columns.push_back(column);
  120. Rml::Core::Dictionary parameters;
  121. parameters["index"] = (int)(columns.size() - 1);
  122. if (DispatchEvent(Core::EventId::Columnadd, parameters))
  123. {
  124. root->RefreshRows();
  125. DirtyLayout();
  126. }
  127. }
  128. // Returns the number of columns in this table
  129. int ElementDataGrid::GetNumColumns()
  130. {
  131. return (int)columns.size();
  132. }
  133. // Returns the column at the specified index.
  134. const ElementDataGrid::Column* ElementDataGrid::GetColumn(int column_index)
  135. {
  136. if (column_index < 0 || column_index >= (int)columns.size())
  137. {
  138. RMLUI_ERROR;
  139. return nullptr;
  140. }
  141. return &columns[column_index];
  142. }
  143. /// Returns a CSV string containing all the fields that each column requires, in order.
  144. const Rml::Core::String& ElementDataGrid::GetAllColumnFields()
  145. {
  146. return column_fields;
  147. }
  148. // Adds a new row to the table - usually only called by child rows.
  149. ElementDataGridRow* ElementDataGrid::AddRow(ElementDataGridRow* parent, int index)
  150. {
  151. // Now we make a new row at the right place then return it.
  152. Rml::Core::XMLAttributes attributes;
  153. Core::ElementPtr element = Core::Factory::InstanceElement(this, "#rmlctl_datagridrow", "datagridrow", attributes);
  154. ElementDataGridRow* new_row = dynamic_cast< ElementDataGridRow* >(element.get());
  155. new_row->Initialise(this, parent, index, header, parent->GetDepth() + 1);
  156. // We need to work out the table-specific row.
  157. int table_relative_index = parent->GetChildTableRelativeIndex(index);
  158. Core::Element* child_to_insert_before = nullptr;
  159. if (table_relative_index < body->GetNumChildren())
  160. {
  161. child_to_insert_before = body->GetChild(table_relative_index);
  162. }
  163. body->InsertBefore(std::move(element), child_to_insert_before);
  164. // As the rows have changed, we need to lay out the document again.
  165. DirtyLayout();
  166. return new_row;
  167. }
  168. // Removes a row from the table.
  169. void ElementDataGrid::RemoveRows(int index, int num_rows)
  170. {
  171. for (int i = 0; i < num_rows; i++)
  172. {
  173. ElementDataGridRow* row = GetRow(index);
  174. row->SetDataSource("");
  175. body->RemoveChild(row);
  176. }
  177. // As the rows have changed, we need to lay out the document again.
  178. DirtyLayout();
  179. }
  180. // Returns the number of rows in the table
  181. int ElementDataGrid::GetNumRows() const
  182. {
  183. return body->GetNumChildren();
  184. }
  185. // Returns the row at the given index in the table.
  186. ElementDataGridRow* ElementDataGrid::GetRow(int index) const
  187. {
  188. // We need to add two to the index, to skip the header row.
  189. ElementDataGridRow* row = dynamic_cast< ElementDataGridRow* >(body->GetChild(index));
  190. return row;
  191. }
  192. void ElementDataGrid::OnUpdate()
  193. {
  194. if (!new_data_source.empty())
  195. {
  196. root->SetDataSource(new_data_source);
  197. new_data_source = "";
  198. }
  199. bool any_new_children = root->UpdateChildren();
  200. if (any_new_children)
  201. {
  202. // @performance: Does anyone really use this?
  203. DispatchEvent(Core::EventId::Rowupdate, Rml::Core::Dictionary());
  204. }
  205. if (!body_visible && (!any_new_children || root->GetNumLoadedChildren() >= GetAttribute("min-rows", 0)))
  206. {
  207. body->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::Block));
  208. body_visible = true;
  209. }
  210. }
  211. void ElementDataGrid::OnResize()
  212. {
  213. SetScrollTop(GetScrollHeight() - GetClientHeight());
  214. for (int i = 0; i < header->GetNumChildren(); i++)
  215. {
  216. Core::Element* child = header->GetChild(i);
  217. columns[i].current_width = child->GetBox().GetSize(Core::Box::MARGIN).x;
  218. }
  219. }
  220. // Gets the markup and content of the element.
  221. void ElementDataGrid::GetInnerRML(Rml::Core::String& content) const
  222. {
  223. // The only content we have is the columns, and inside them the header elements.
  224. for (size_t i = 0; i < columns.size(); i++)
  225. {
  226. Core::Element* header_element = header->GetChild((int)i);
  227. Rml::Core::String column_fields;
  228. for (size_t j = 0; j < columns[i].fields.size(); j++)
  229. {
  230. if (j != columns[i].fields.size() - 1)
  231. {
  232. column_fields += ",";
  233. }
  234. column_fields += columns[i].fields[j];
  235. }
  236. Rml::Core::String width_attribute = header_element->GetAttribute<Rml::Core::String>("width", "");
  237. content += Core::CreateString(column_fields.size() + 32, "<col fields=\"%s\"", column_fields.c_str());
  238. if (!width_attribute.empty())
  239. content += Core::CreateString(width_attribute.size() + 32, " width=\"%s\"", width_attribute.c_str());
  240. content += ">";
  241. header_element->GetInnerRML(content);
  242. content += "</col>";
  243. }
  244. }
  245. }
  246. }