LayoutStyle.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Optional.h>
  28. #include <LibWeb/CSS/LengthBox.h>
  29. #include <LibWeb/CSS/StyleValue.h>
  30. namespace Web {
  31. class InitialValues {
  32. public:
  33. static CSS::Float float_() { return CSS::Float::None; }
  34. static CSS::WhiteSpace white_space() { return CSS::WhiteSpace::Normal; }
  35. };
  36. struct BorderData {
  37. public:
  38. Color color { Color::Transparent };
  39. CSS::LineStyle line_style { CSS::LineStyle::None };
  40. float width { 0 };
  41. };
  42. class LayoutStyle {
  43. public:
  44. CSS::Float float_() const { return m_float; }
  45. Optional<int> z_index() const { return m_z_index; }
  46. CSS::TextAlign text_align() const { return m_text_align; }
  47. CSS::Position position() const { return m_position; }
  48. CSS::WhiteSpace white_space() const { return m_white_space; }
  49. const CSS::Length& width() const { return m_width; }
  50. const CSS::Length& min_width() const { return m_min_width; }
  51. const CSS::Length& max_width() const { return m_max_width; }
  52. const CSS::Length& height() const { return m_height; }
  53. const CSS::Length& min_height() const { return m_min_height; }
  54. const CSS::Length& max_height() const { return m_max_height; }
  55. const CSS::LengthBox& offset() const { return m_offset; }
  56. const CSS::LengthBox& margin() const { return m_margin; }
  57. const CSS::LengthBox& padding() const { return m_padding; }
  58. const BorderData& border_left() const { return m_border_left; }
  59. const BorderData& border_top() const { return m_border_top; }
  60. const BorderData& border_right() const { return m_border_right; }
  61. const BorderData& border_bottom() const { return m_border_bottom; }
  62. protected:
  63. CSS::Float m_float { InitialValues::float_() };
  64. Optional<int> m_z_index;
  65. CSS::TextAlign m_text_align;
  66. CSS::Position m_position;
  67. CSS::WhiteSpace m_white_space { InitialValues::white_space() };
  68. CSS::Length m_width;
  69. CSS::Length m_min_width;
  70. CSS::Length m_max_width;
  71. CSS::Length m_height;
  72. CSS::Length m_min_height;
  73. CSS::Length m_max_height;
  74. CSS::LengthBox m_offset;
  75. CSS::LengthBox m_margin;
  76. CSS::LengthBox m_padding;
  77. BorderData m_border_left;
  78. BorderData m_border_top;
  79. BorderData m_border_right;
  80. BorderData m_border_bottom;
  81. };
  82. class ImmutableLayoutStyle final : public LayoutStyle {
  83. };
  84. class MutableLayoutStyle final : public LayoutStyle {
  85. public:
  86. void set_float(CSS::Float value) { m_float = value; }
  87. void set_z_index(Optional<int> value) { m_z_index = value; }
  88. void set_text_align(CSS::TextAlign text_align) { m_text_align = text_align; }
  89. void set_position(CSS::Position position) { m_position = position; }
  90. void set_white_space(CSS::WhiteSpace value) { m_white_space = value; }
  91. void set_width(const CSS::Length& width) { m_width = width; }
  92. void set_min_width(const CSS::Length& width) { m_min_width = width; }
  93. void set_max_width(const CSS::Length& width) { m_max_width = width; }
  94. void set_height(const CSS::Length& height) { m_height = height; }
  95. void set_min_height(const CSS::Length& height) { m_min_height = height; }
  96. void set_max_height(const CSS::Length& height) { m_max_height = height; }
  97. void set_offset(const CSS::LengthBox& offset) { m_offset = offset; }
  98. void set_margin(const CSS::LengthBox& margin) { m_margin = margin; }
  99. void set_padding(const CSS::LengthBox& padding) { m_padding = padding; }
  100. BorderData& border_left() { return m_border_left; }
  101. BorderData& border_top() { return m_border_top; }
  102. BorderData& border_right() { return m_border_right; }
  103. BorderData& border_bottom() { return m_border_bottom; }
  104. };
  105. }