XMLNodeHandlerDataGrid.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "XMLNodeHandlerDataGrid.h"
  28. #include "../../Include/Rocket/Core/StreamMemory.h"
  29. #include "../../Include/Rocket/Core/Log.h"
  30. #include "../../Include/Rocket/Core/Factory.h"
  31. #include "../../Include/Rocket/Core/XMLParser.h"
  32. #include "../../Include/Rocket/Controls/ElementDataGrid.h"
  33. namespace Rocket {
  34. namespace Controls {
  35. XMLNodeHandlerDataGrid::XMLNodeHandlerDataGrid()
  36. {
  37. }
  38. XMLNodeHandlerDataGrid::~XMLNodeHandlerDataGrid()
  39. {
  40. }
  41. Core::Element* XMLNodeHandlerDataGrid::ElementStart(Core::XMLParser* parser, const Rocket::Core::String& name, const Rocket::Core::XMLAttributes& attributes)
  42. {
  43. Core::Element* element = NULL;
  44. Core::Element* parent = parser->GetParseFrame()->element;
  45. ROCKET_ASSERT(name == "datagrid" ||
  46. name == "col");
  47. if (name == "datagrid")
  48. {
  49. // Attempt to instance the grid.
  50. element = Core::Factory::InstanceElement(parent, name, name, attributes);
  51. ElementDataGrid* grid = dynamic_cast< ElementDataGrid* >(element);
  52. if (grid == NULL)
  53. {
  54. if (element != NULL)
  55. element->RemoveReference();
  56. Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Instancer failed to create data grid for tag %s.", name.CString());
  57. return NULL;
  58. }
  59. // Set the data source and table on the data grid.
  60. Rocket::Core::String data_source = attributes.Get< Rocket::Core::String >("source", "");
  61. grid->SetDataSource(data_source);
  62. parent->AppendChild(grid);
  63. grid->RemoveReference();
  64. // Switch to this handler for all columns.
  65. parser->PushHandler("datagrid");
  66. }
  67. else if (name == "col")
  68. {
  69. // Make a new node handler to handle the header elements.
  70. element = Core::Factory::InstanceElement(parent, "datagridcolumn", "datagridcolumn", attributes);
  71. if (element == NULL)
  72. return NULL;
  73. ElementDataGrid* grid = dynamic_cast< ElementDataGrid* >(parent);
  74. if (grid != NULL)
  75. {
  76. grid->AddColumn(attributes.Get< Rocket::Core::String >("fields", ""), attributes.Get< Rocket::Core::String >("formatter", ""), attributes.Get< float >("width", 0), element);
  77. element->RemoveReference();
  78. }
  79. // Switch to element handler for all children.
  80. parser->PushDefaultHandler();
  81. }
  82. else
  83. {
  84. ROCKET_ERROR;
  85. }
  86. return element;
  87. }
  88. bool XMLNodeHandlerDataGrid::ElementEnd(Core::XMLParser* ROCKET_UNUSED_PARAMETER(parser), const Rocket::Core::String& ROCKET_UNUSED_PARAMETER(name))
  89. {
  90. ROCKET_UNUSED(parser);
  91. ROCKET_UNUSED(name);
  92. return true;
  93. }
  94. bool XMLNodeHandlerDataGrid::ElementData(Core::XMLParser* parser, const Rocket::Core::String& data)
  95. {
  96. Core::Element* parent = parser->GetParseFrame()->element;
  97. // Parse the text into the parent element.
  98. return Core::Factory::InstanceElementText(parent, data);
  99. }
  100. void XMLNodeHandlerDataGrid::Release()
  101. {
  102. delete this;
  103. }
  104. }
  105. }