HTMLInputElement.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <LibGUI/GButton.h>
  2. #include <LibGUI/GTextBox.h>
  3. #include <LibHTML/DOM/Document.h>
  4. #include <LibHTML/DOM/HTMLFormElement.h>
  5. #include <LibHTML/DOM/HTMLInputElement.h>
  6. #include <LibHTML/Frame.h>
  7. #include <LibHTML/HtmlView.h>
  8. #include <LibHTML/Layout/LayoutWidget.h>
  9. HTMLInputElement::HTMLInputElement(Document& document, const String& tag_name)
  10. : HTMLElement(document, tag_name)
  11. {
  12. }
  13. HTMLInputElement::~HTMLInputElement()
  14. {
  15. }
  16. RefPtr<LayoutNode> HTMLInputElement::create_layout_node(const StyleProperties*) const
  17. {
  18. ASSERT(document().frame());
  19. auto& frame = *document().frame();
  20. ASSERT(frame.html_view());
  21. auto& html_view = const_cast<HtmlView&>(*frame.html_view());
  22. RefPtr<GWidget> widget;
  23. if (type() == "submit") {
  24. auto button = GButton::construct(value(), &html_view);
  25. int text_width = Font::default_font().width(value());
  26. button->set_relative_rect(0, 0, text_width + 20, 20);
  27. button->on_click = [this](auto&) {
  28. if (auto* form = first_ancestor_of_type<HTMLFormElement>()) {
  29. // FIXME: Remove this const_cast once we have a non-const first_ancestor_of_type.
  30. const_cast<HTMLFormElement*>(form)->submit();
  31. }
  32. };
  33. widget = button;
  34. } else {
  35. auto text_box = GTextBox::construct(&html_view);
  36. text_box->set_text(value());
  37. text_box->on_change = [this] {
  38. auto& widget = to<LayoutWidget>(layout_node())->widget();
  39. const_cast<HTMLInputElement*>(this)->set_attribute("value", static_cast<const GTextBox&>(widget).text());
  40. };
  41. int text_width = Font::default_font().width(value());
  42. text_box->set_relative_rect(0, 0, text_width + 20, 20);
  43. widget = text_box;
  44. }
  45. return adopt(*new LayoutWidget(*this, *widget));
  46. }