ElementDataGrid.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #include "../../Include/Rocket/Controls/ElementDataGrid.h"
  28. #include "../../Include/Rocket/Controls/DataSource.h"
  29. #include "../../Include/Rocket/Core/Math.h"
  30. #include "../../Include/Rocket/Core/XMLParser.h"
  31. #include "../../Include/Rocket/Core/Event.h"
  32. #include "../../Include/Rocket/Core/ElementDocument.h"
  33. #include "../../Include/Rocket/Core/Factory.h"
  34. #include "../../Include/Rocket/Core/Property.h"
  35. #include "../../Include/Rocket/Controls/DataFormatter.h"
  36. #include "../../Include/Rocket/Controls/ElementDataGridRow.h"
  37. namespace Rocket {
  38. namespace Controls {
  39. ElementDataGrid::ElementDataGrid(const Rocket::Core::String& tag) : Core::Element(tag)
  40. {
  41. Rocket::Core::XMLAttributes attributes;
  42. // Create the row for the column headers:
  43. header = static_cast< ElementDataGridRow* >(Core::Factory::InstanceElement(this, "#rktctl_datagridrow", "datagridheader", attributes));
  44. header->SetProperty("display", Core::Property(Core::Style::Display::Block));
  45. header->Initialise(this);
  46. AppendChild(header);
  47. header->RemoveReference();
  48. body = Core::Factory::InstanceElement(this, "*", "datagridbody", attributes);
  49. body->SetProperty("display", Core::Property(Core::Style::Display::None));
  50. body->SetProperty("width", Core::Property(Core::Style::Width::Auto));
  51. AppendChild(body);
  52. body->RemoveReference();
  53. body_visible = false;
  54. root = static_cast< ElementDataGridRow* >(Core::Factory::InstanceElement(this, "#rktctl_datagridrow", "datagridroot", attributes));
  55. root->SetProperty("display", Core::Property(Core::Style::Display::None));
  56. root->Initialise(this);
  57. SetProperty("overflow", Core::Property(Core::Style::Overflow::Auto));
  58. new_data_source = "";
  59. }
  60. ElementDataGrid::~ElementDataGrid()
  61. {
  62. root->RemoveReference();
  63. }
  64. void ElementDataGrid::SetDataSource(const Rocket::Core::String& data_source_name)
  65. {
  66. new_data_source = data_source_name;
  67. }
  68. // Adds a column to the table.
  69. bool ElementDataGrid::AddColumn(const Rocket::Core::String& fields, const Rocket::Core::String& formatter, float initial_width, const Rocket::Core::String& header_rml)
  70. {
  71. Core::Element* header_element = Core::Factory::InstanceElement(this, "datagridcolumn", "datagridcolumn", Rocket::Core::XMLAttributes());
  72. if (header_element == NULL)
  73. return false;
  74. if (!Core::Factory::InstanceElementText(header_element, header_rml))
  75. {
  76. header_element->RemoveReference();
  77. return false;
  78. }
  79. AddColumn(fields, formatter, initial_width, header_element);
  80. header_element->RemoveReference();
  81. return true;
  82. }
  83. // Adds a column to the table.
  84. void ElementDataGrid::AddColumn(const Rocket::Core::String& fields, const Rocket::Core::String& formatter, float initial_width, Core::Element* header_element)
  85. {
  86. Column column;
  87. Rocket::Core::StringUtilities::ExpandString(column.fields, fields);
  88. column.formatter = DataFormatter::GetDataFormatter(formatter);
  89. column.header = header;
  90. column.current_width = initial_width;
  91. column.refresh_on_child_change = false;
  92. // The header elements are added to the header row at the top of the table.
  93. if (header_element)
  94. {
  95. header_element->SetProperty("display", Core::Property(Core::Style::Display::InlineBlock));
  96. // Push all the width properties from the column onto the header element.
  97. Rocket::Core::String width = header_element->GetAttribute<Rocket::Core::String>("width", "100%");
  98. header_element->SetProperty("width", width);
  99. header->AppendChild(header_element);
  100. }
  101. // Add the fields that this column requires to the concatenated string of all column fields.
  102. for (size_t i = 0; i < column.fields.size(); i++)
  103. {
  104. // Don't append the depth or num_children fields, as they're handled by the row.
  105. if (column.fields[i] == Rocket::Controls::DataSource::NUM_CHILDREN)
  106. {
  107. column.refresh_on_child_change = true;
  108. }
  109. else if (column.fields[i] != Rocket::Controls::DataSource::DEPTH)
  110. {
  111. if (!column_fields.empty())
  112. {
  113. column_fields += ",";
  114. }
  115. column_fields += column.fields[i];
  116. }
  117. }
  118. columns.push_back(column);
  119. Rocket::Core::Dictionary parameters;
  120. parameters["index"] = (int)(columns.size() - 1);
  121. if (DispatchEvent(Core::EventId::Columnadd, parameters))
  122. {
  123. root->RefreshRows();
  124. DirtyLayout();
  125. }
  126. }
  127. // Returns the number of columns in this table
  128. int ElementDataGrid::GetNumColumns()
  129. {
  130. return (int)columns.size();
  131. }
  132. // Returns the column at the specified index.
  133. const ElementDataGrid::Column* ElementDataGrid::GetColumn(int column_index)
  134. {
  135. if (column_index < 0 || column_index >= (int)columns.size())
  136. {
  137. ROCKET_ERROR;
  138. return NULL;
  139. }
  140. return &columns[column_index];
  141. }
  142. /// Returns a CSV string containing all the fields that each column requires, in order.
  143. const Rocket::Core::String& ElementDataGrid::GetAllColumnFields()
  144. {
  145. return column_fields;
  146. }
  147. // Adds a new row to the table - usually only called by child rows.
  148. ElementDataGridRow* ElementDataGrid::AddRow(ElementDataGridRow* parent, int index)
  149. {
  150. // Now we make a new row at the right place then return it.
  151. Rocket::Core::XMLAttributes attributes;
  152. ElementDataGridRow* new_row = dynamic_cast< ElementDataGridRow* >(Core::Factory::InstanceElement(this, "#rktctl_datagridrow", "datagridrow", attributes));
  153. new_row->Initialise(this, parent, index, header, parent->GetDepth() + 1);
  154. // We need to work out the table-specific row.
  155. int table_relative_index = parent->GetChildTableRelativeIndex(index);
  156. Core::Element* child_to_insert_before = NULL;
  157. if (table_relative_index < body->GetNumChildren())
  158. {
  159. child_to_insert_before = body->GetChild(table_relative_index);
  160. }
  161. body->InsertBefore(new_row, child_to_insert_before);
  162. new_row->RemoveReference();
  163. // As the rows have changed, we need to lay out the document again.
  164. DirtyLayout();
  165. return new_row;
  166. }
  167. // Removes a row from the table.
  168. void ElementDataGrid::RemoveRows(int index, int num_rows)
  169. {
  170. for (int i = 0; i < num_rows; i++)
  171. {
  172. ElementDataGridRow* row = GetRow(index);
  173. row->SetDataSource("");
  174. body->RemoveChild(row);
  175. }
  176. // As the rows have changed, we need to lay out the document again.
  177. DirtyLayout();
  178. }
  179. // Returns the number of rows in the table
  180. int ElementDataGrid::GetNumRows() const
  181. {
  182. return body->GetNumChildren();
  183. }
  184. // Returns the row at the given index in the table.
  185. ElementDataGridRow* ElementDataGrid::GetRow(int index) const
  186. {
  187. // We need to add two to the index, to skip the header row.
  188. ElementDataGridRow* row = dynamic_cast< ElementDataGridRow* >(body->GetChild(index));
  189. return row;
  190. }
  191. void ElementDataGrid::OnUpdate()
  192. {
  193. if (!new_data_source.empty())
  194. {
  195. root->SetDataSource(new_data_source);
  196. new_data_source = "";
  197. }
  198. bool any_new_children = root->UpdateChildren();
  199. if (any_new_children)
  200. {
  201. // @performance: Does anyone really use this?
  202. DispatchEvent(Core::EventId::Rowupdate, Rocket::Core::Dictionary());
  203. }
  204. if (!body_visible && (!any_new_children || root->GetNumLoadedChildren() >= GetAttribute("min-rows", 0)))
  205. {
  206. body->SetProperty("display", Core::Property(Core::Style::Display::Block));
  207. body_visible = true;
  208. }
  209. }
  210. void ElementDataGrid::OnResize()
  211. {
  212. SetScrollTop(GetScrollHeight() - GetClientHeight());
  213. for (int i = 0; i < header->GetNumChildren(); i++)
  214. {
  215. Core::Element* child = header->GetChild(i);
  216. columns[i].current_width = child->GetBox().GetSize(Core::Box::MARGIN).x;
  217. }
  218. }
  219. // Gets the markup and content of the element.
  220. void ElementDataGrid::GetInnerRML(Rocket::Core::String& content) const
  221. {
  222. // The only content we have is the columns, and inside them the header elements.
  223. for (size_t i = 0; i < columns.size(); i++)
  224. {
  225. Core::Element* header_element = header->GetChild((int)i);
  226. Rocket::Core::String column_fields;
  227. for (size_t j = 0; j < columns[i].fields.size(); j++)
  228. {
  229. if (j != columns[i].fields.size() - 1)
  230. {
  231. column_fields += ",";
  232. }
  233. column_fields += columns[i].fields[j];
  234. }
  235. Rocket::Core::String width_attribute = header_element->GetAttribute<Rocket::Core::String>("width", "");
  236. content += Core::CreateString(column_fields.size() + 32, "<col fields=\"%s\"", column_fields.c_str());
  237. if (!width_attribute.empty())
  238. content += Core::CreateString(width_attribute.size() + 32, " width=\"%s\"", width_attribute.c_str());
  239. content += ">";
  240. header_element->GetInnerRML(content);
  241. content += "</col>";
  242. }
  243. }
  244. }
  245. }