ElementDataGrid.cpp 9.1 KB

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