XMLNodeHandlerTabSet.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "XMLNodeHandlerTabSet.h"
  28. #include "../../Include/Rocket/Core/Log.h"
  29. #include "../../Include/Rocket/Core/Factory.h"
  30. #include "../../Include/Rocket/Core/XMLParser.h"
  31. #include "../../Include/Rocket/Controls/ElementTabSet.h"
  32. namespace Rocket {
  33. namespace Controls {
  34. XMLNodeHandlerTabSet::XMLNodeHandlerTabSet()
  35. {
  36. }
  37. XMLNodeHandlerTabSet::~XMLNodeHandlerTabSet()
  38. {
  39. }
  40. Core::Element* XMLNodeHandlerTabSet::ElementStart(Core::XMLParser* parser, const Rocket::Core::String& name, const Rocket::Core::XMLAttributes& attributes)
  41. {
  42. ROCKET_ASSERT(name == "tabset" ||
  43. name == "tabs" ||
  44. name == "tab" ||
  45. name == "panels" ||
  46. name == "panel");
  47. if (name == "tabset")
  48. {
  49. // Call this node handler for all children
  50. parser->PushHandler("tabset");
  51. // Attempt to instance the tabset
  52. Core::Element* element = Core::Factory::InstanceElement(parser->GetParseFrame()->element, name, name, attributes);
  53. ElementTabSet* tabset = dynamic_cast< ElementTabSet* >(element);
  54. if (!tabset)
  55. {
  56. if (element)
  57. element->RemoveReference();
  58. Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Instancer failed to create element for tag %s.", name.CString());
  59. return NULL;
  60. }
  61. // Add the TabSet into the document
  62. parser->GetParseFrame()->element->AppendChild(element);
  63. element->RemoveReference();
  64. return element;
  65. }
  66. else if (name == "tab")
  67. {
  68. // Call default element handler for all children.
  69. parser->PushDefaultHandler();
  70. Core::Element* tab_element = Core::Factory::InstanceElement(parser->GetParseFrame()->element, "*", "tab", attributes);
  71. ElementTabSet* tabset = dynamic_cast< ElementTabSet* >(parser->GetParseFrame()->element);
  72. if (tabset)
  73. {
  74. tabset->SetTab(-1, tab_element);
  75. tab_element->RemoveReference();
  76. }
  77. return tab_element;
  78. }
  79. else if (name == "panel")
  80. {
  81. // Call default element handler for all children.
  82. parser->PushDefaultHandler();
  83. Core::Element* panel_element = Core::Factory::InstanceElement(parser->GetParseFrame()->element, "*", "panel", attributes);
  84. ElementTabSet* tabset = dynamic_cast< ElementTabSet* >(parser->GetParseFrame()->element);
  85. if (tabset)
  86. {
  87. tabset->SetPanel(-1, panel_element);
  88. panel_element->RemoveReference();
  89. }
  90. return panel_element;
  91. }
  92. else if (name == "tabs" || name == "panels")
  93. {
  94. // Use the element handler to add the tabs and panels elements to the the tabset (this allows users to
  95. // style them nicely), but don't return the new element, as we still want the tabset to be the top of the
  96. // parser's node stack.
  97. Core::Element* parent = parser->GetParseFrame()->element;
  98. // Attempt to instance the element with the instancer.
  99. Core::Element* element = Core::Factory::InstanceElement(parent, name, name, attributes);
  100. if (!element)
  101. {
  102. Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Instancer failed to create element for tag %s.", name.CString());
  103. return NULL;
  104. }
  105. // Add the element to its parent and remove the initial reference.
  106. parent->AppendChild(element);
  107. element->RemoveReference();
  108. }
  109. return NULL;
  110. }
  111. bool XMLNodeHandlerTabSet::ElementEnd(Core::XMLParser* ROCKET_UNUSED_PARAMETER(parser), const Rocket::Core::String& ROCKET_UNUSED_PARAMETER(name))
  112. {
  113. ROCKET_UNUSED(parser);
  114. ROCKET_UNUSED(name);
  115. return true;
  116. }
  117. bool XMLNodeHandlerTabSet::ElementData(Core::XMLParser* parser, const Rocket::Core::String& data)
  118. {
  119. return Core::Factory::InstanceElementText(parser->GetParseFrame()->element, data);
  120. }
  121. void XMLNodeHandlerTabSet::Release()
  122. {
  123. delete this;
  124. }
  125. }
  126. }