ElementForm.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/ElementForm.h"
  28. #include "../../Include/Rocket/Core/Dictionary.h"
  29. #include "../../Include/Rocket/Core/ElementUtilities.h"
  30. #include "../../Include/Rocket/Controls/ElementFormControl.h"
  31. namespace Rocket {
  32. namespace Controls {
  33. // Constructs a new ElementForm. This should not be called directly; use the Factory instead.
  34. ElementForm::ElementForm(const Rocket::Core::String& tag) : Core::Element(tag)
  35. {
  36. }
  37. ElementForm::~ElementForm()
  38. {
  39. }
  40. // Submits the form.
  41. void ElementForm::Submit(const Rocket::Core::String& name, const Rocket::Core::String& submit_value)
  42. {
  43. Rocket::Core::Dictionary values;
  44. if (name.Empty())
  45. values.Set("submit", submit_value);
  46. else
  47. values.Set(name, submit_value);
  48. Core::ElementList form_controls;
  49. Core::ElementUtilities::GetElementsByTagName(form_controls, this, "input");
  50. Core::ElementUtilities::GetElementsByTagName(form_controls, this, "textarea");
  51. Core::ElementUtilities::GetElementsByTagName(form_controls, this, "select");
  52. Core::ElementUtilities::GetElementsByTagName(form_controls, this, "dataselect");
  53. for (size_t i = 0; i < form_controls.size(); i++)
  54. {
  55. ElementFormControl* control = dynamic_cast< ElementFormControl* >(form_controls[i]);
  56. if (!control)
  57. continue;
  58. // Skip disabled controls.
  59. if (control->IsDisabled())
  60. continue;
  61. // Only process controls that should be submitted.
  62. if (!control->IsSubmitted())
  63. continue;
  64. Rocket::Core::String control_name = control->GetName();
  65. Rocket::Core::String control_value = control->GetValue();
  66. // Skip over unnamed form controls.
  67. if (control_name.Empty())
  68. continue;
  69. // If the item already exists, append to it.
  70. Rocket::Core::Variant* value = values.Get(control_name);
  71. if (value != NULL)
  72. value->Set(value->Get< Rocket::Core::String >() + ", " + control_value);
  73. else
  74. values.Set< Rocket::Core::String >(control_name, control_value);
  75. }
  76. DispatchEvent("submit", values);
  77. }
  78. }
  79. }