XMLNodeHandlerDataGrid.cpp 3.9 KB

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