LayoutBlockBox.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "LayoutBlockBox.h"
  29. #include "LayoutBlockBoxSpace.h"
  30. #include "LayoutEngine.h"
  31. #include "LayoutDetails.h"
  32. #include "../../Include/RmlUi/Core/Element.h"
  33. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  34. #include "../../Include/RmlUi/Core/ElementScroll.h"
  35. #include "../../Include/RmlUi/Core/Property.h"
  36. #include "../../Include/RmlUi/Core/Profiling.h"
  37. #include <float.h>
  38. namespace Rml {
  39. // Creates a new block box for rendering a block element.
  40. LayoutBlockBox::LayoutBlockBox(LayoutBlockBox* _parent, Element* _element, bool allow_shrink) : position(0), visible_outer_width(0)
  41. {
  42. RMLUI_ZoneScoped;
  43. space_owner = MakeUnique<LayoutBlockBoxSpace>(this);
  44. space = space_owner.get();
  45. parent = _parent;
  46. context = BLOCK;
  47. element = _element;
  48. interrupted_chain = nullptr;
  49. box_cursor = 0;
  50. vertical_overflow = false;
  51. // Get our offset root from our parent, if it has one; otherwise, our element is the offset parent.
  52. if (parent != nullptr &&
  53. parent->offset_root->GetElement() != nullptr)
  54. offset_root = parent->offset_root;
  55. else
  56. offset_root = this;
  57. // Determine the offset parent for this element.
  58. const LayoutBlockBox* self_offset_parent;
  59. if (parent != nullptr &&
  60. parent->offset_parent->GetElement() != nullptr)
  61. self_offset_parent = parent->offset_parent;
  62. else
  63. self_offset_parent = this;
  64. // Determine the offset parent for our children.
  65. if (parent != nullptr &&
  66. parent->offset_parent->GetElement() != nullptr &&
  67. (element == nullptr || element->GetPosition() == Style::Position::Static))
  68. offset_parent = parent->offset_parent;
  69. else
  70. offset_parent = this;
  71. // Build the box for our element, and position it if we can.
  72. if (parent != nullptr)
  73. {
  74. space->ImportSpace(*parent->space);
  75. // Build our box if possible; if not, it will have to be set up manually.
  76. // TODO: Calculate outside and pass the box into the constructor.
  77. LayoutDetails::BuildBox(box, min_height, max_height, parent, element, false, allow_shrink);
  78. // Position ourselves within our containing block (if we have a valid offset parent).
  79. if (parent->GetElement() != nullptr)
  80. {
  81. if (self_offset_parent != this)
  82. {
  83. // Get the next position within our offset parent's containing block.
  84. parent->PositionBlockBox(position, box, element ? element->GetComputedValues().clear : Style::Clear::None);
  85. element->SetOffset(position - (self_offset_parent->GetPosition() - offset_root->GetPosition()), self_offset_parent->GetElement());
  86. }
  87. else
  88. element->SetOffset(position, nullptr);
  89. }
  90. }
  91. if (element != nullptr)
  92. {
  93. const auto& computed = element->GetComputedValues();
  94. wrap_content = computed.white_space != Style::WhiteSpace::Nowrap;
  95. // Determine if this element should have scrollbars or not, and create them if so.
  96. overflow_x_property = computed.overflow_x;
  97. overflow_y_property = computed.overflow_y;
  98. if (overflow_x_property == Style::Overflow::Scroll)
  99. element->GetElementScroll()->EnableScrollbar(ElementScroll::HORIZONTAL, box.GetSize(Box::PADDING).x);
  100. else
  101. element->GetElementScroll()->DisableScrollbar(ElementScroll::HORIZONTAL);
  102. if (overflow_y_property == Style::Overflow::Scroll)
  103. element->GetElementScroll()->EnableScrollbar(ElementScroll::VERTICAL, box.GetSize(Box::PADDING).x);
  104. else
  105. element->GetElementScroll()->DisableScrollbar(ElementScroll::VERTICAL);
  106. }
  107. else
  108. {
  109. wrap_content = true;
  110. overflow_x_property = Style::Overflow::Visible;
  111. overflow_y_property = Style::Overflow::Visible;
  112. }
  113. }
  114. // Creates a new block box in an inline context.
  115. LayoutBlockBox::LayoutBlockBox(LayoutBlockBox* _parent, bool allow_shrink) : position(-1, -1)
  116. {
  117. parent = _parent;
  118. offset_parent = parent->offset_parent;
  119. offset_root = parent->offset_root;
  120. space = _parent->space;
  121. context = INLINE;
  122. line_boxes.push_back(MakeUnique<LayoutLineBox>(this));
  123. wrap_content = parent->wrap_content;
  124. element = nullptr;
  125. interrupted_chain = nullptr;
  126. box_cursor = 0;
  127. vertical_overflow = false;
  128. // TODO: Calculate outside and pass the box into the constructor.
  129. LayoutDetails::BuildBox(box, min_height, max_height, parent, nullptr, false, allow_shrink);
  130. parent->PositionBlockBox(position, box, Style::Clear::None);
  131. box.SetContent(Vector2f(box.GetSize(Box::CONTENT).x, -1));
  132. // Reset the min and max heights; they're not valid for inline block boxes.
  133. min_height = 0;
  134. max_height = FLT_MAX;
  135. }
  136. // Releases the block box.
  137. LayoutBlockBox::~LayoutBlockBox()
  138. {
  139. }
  140. // Closes the box.
  141. LayoutBlockBox::CloseResult LayoutBlockBox::Close()
  142. {
  143. // If the last child of this block box is an inline box, then we haven't closed it; close it now!
  144. if (context == BLOCK)
  145. {
  146. CloseResult result = CloseInlineBlockBox();
  147. if (result != OK)
  148. return LAYOUT_SELF;
  149. }
  150. // Otherwise, we're an inline context box; so close our last line, which will still be open.
  151. else
  152. {
  153. line_boxes.back()->Close();
  154. // Expand our content area if any line boxes had to push themselves out.
  155. Vector2f content_area = box.GetSize();
  156. for (size_t i = 0; i < line_boxes.size(); i++)
  157. content_area.x = Math::Max(content_area.x, line_boxes[i]->GetDimensions().x);
  158. box.SetContent(content_area);
  159. }
  160. // Set this box's height, if necessary.
  161. if (box.GetSize(Box::CONTENT).y < 0)
  162. {
  163. Vector2f content_area = box.GetSize();
  164. content_area.y = Math::Clamp(box_cursor, min_height, max_height);
  165. if (element != nullptr)
  166. content_area.y = Math::Max(content_area.y, space->GetDimensions().y);
  167. box.SetContent(content_area);
  168. }
  169. // Set the computed box on the element.
  170. if (element != nullptr)
  171. {
  172. if (context == BLOCK)
  173. {
  174. // Calculate the dimensions of the box's *internal* content; this is the tightest-fitting box around all of the
  175. // internal elements, plus this element's padding.
  176. Vector2f content_box(0, 0);
  177. for (size_t i = 0; i < block_boxes.size(); i++)
  178. {
  179. // TODO: Only if the containing block is not an ancestor of us (ie. we are the containing block?).
  180. content_box.x = Math::Max(content_box.x, block_boxes[i]->visible_outer_width);
  181. }
  182. // Check how big our floated area is.
  183. Vector2f space_box = space->GetDimensions();
  184. content_box.x = Math::Max(content_box.x, space_box.x);
  185. // If our content is larger than our window, we can enable the horizontal scrollbar if
  186. // we're set to auto-scrollbars. If we're set to always use scrollbars, then the horiontal
  187. // scrollbar will already have been enabled in the constructor.
  188. if (content_box.x > box.GetSize().x)
  189. {
  190. if (overflow_x_property == Style::Overflow::Auto)
  191. {
  192. element->GetElementScroll()->EnableScrollbar(ElementScroll::HORIZONTAL, box.GetSize(Box::PADDING).x);
  193. if (!CatchVerticalOverflow())
  194. return LAYOUT_SELF;
  195. }
  196. }
  197. content_box.x += (box.GetEdge(Box::PADDING, Box::LEFT) + box.GetEdge(Box::PADDING, Box::RIGHT));
  198. content_box.y = box_cursor;
  199. content_box.y = Math::Max(content_box.y, space_box.y);
  200. if (!CatchVerticalOverflow(content_box.y))
  201. return LAYOUT_SELF;
  202. content_box.y += (box.GetEdge(Box::PADDING, Box::TOP) + box.GetEdge(Box::PADDING, Box::BOTTOM));
  203. element->SetBox(box);
  204. element->SetContentBox(space->GetOffset(), content_box);
  205. const float margin_width = GetBox().GetSize(Box::MARGIN).x;
  206. // Set the visible outer width so that ancestors can catch any overflow produced by us. That is, hiding it or providing a scrolling mechanism.
  207. // If we catch our own overflow here, then just use the normal margin box as that will effectively remove the overflow from our ancestor's perspective.
  208. if (overflow_x_property != Style::Overflow::Visible)
  209. visible_outer_width = margin_width;
  210. else
  211. visible_outer_width = Math::Max(margin_width, space->GetOffset().x + content_box.x + box.GetEdge(Box::MARGIN, Box::LEFT) + box.GetEdge(Box::MARGIN, Box::RIGHT));
  212. // Format any scrollbars which were enabled on this element.
  213. element->GetElementScroll()->FormatScrollbars();
  214. }
  215. else
  216. element->SetBox(box);
  217. }
  218. // Increment the parent's cursor.
  219. if (parent != nullptr)
  220. {
  221. // If this close fails, it means this block box has caused our parent block box to generate an automatic
  222. // vertical scrollbar.
  223. if (!parent->CloseBlockBox(this))
  224. return LAYOUT_PARENT;
  225. }
  226. // If we represent a positioned element, then we can now (as we've been sized) act as the containing block for all
  227. // the absolutely-positioned elements of our descendants.
  228. if (context == BLOCK &&
  229. element != nullptr)
  230. {
  231. if (element->GetPosition() != Style::Position::Static)
  232. CloseAbsoluteElements();
  233. }
  234. return OK;
  235. }
  236. // Called by a closing block box child.
  237. bool LayoutBlockBox::CloseBlockBox(LayoutBlockBox* child)
  238. {
  239. RMLUI_ASSERT(context == BLOCK);
  240. box_cursor = (child->GetPosition().y - child->box.GetEdge(Box::MARGIN, Box::TOP) - (box.GetPosition().y + position.y)) + child->GetBox().GetSize(Box::MARGIN).y;
  241. return CatchVerticalOverflow();
  242. }
  243. // Called by a closing line box child.
  244. LayoutInlineBox* LayoutBlockBox::CloseLineBox(LayoutLineBox* child, UniquePtr<LayoutInlineBox> overflow, LayoutInlineBox* overflow_chain)
  245. {
  246. RMLUI_ZoneScoped;
  247. RMLUI_ASSERT(context == INLINE);
  248. if (child->GetDimensions().x > 0)
  249. box_cursor = (child->GetPosition().y - (box.GetPosition().y + position.y)) + child->GetDimensions().y;
  250. // If we have any pending floating elements for our parent, then this would be an ideal time to position them.
  251. if (!float_elements.empty())
  252. {
  253. for (size_t i = 0; i < float_elements.size(); ++i)
  254. parent->PositionFloat(float_elements[i], box_cursor);
  255. float_elements.clear();
  256. }
  257. // Add a new line box.
  258. line_boxes.push_back(MakeUnique<LayoutLineBox>(this));
  259. if (overflow_chain != nullptr)
  260. line_boxes.back()->AddChainedBox(overflow_chain);
  261. if (overflow != nullptr)
  262. return line_boxes.back()->AddBox(std::move(overflow));
  263. return nullptr;
  264. }
  265. // Adds a new block element to this block box.
  266. LayoutBlockBox* LayoutBlockBox::AddBlockElement(Element* element, bool allow_shrink)
  267. {
  268. RMLUI_ZoneScoped;
  269. RMLUI_ASSERT(context == BLOCK);
  270. // Check if our most previous block box is rendering in an inline context.
  271. if (!block_boxes.empty() &&
  272. block_boxes.back()->context == INLINE)
  273. {
  274. LayoutBlockBox* inline_block_box = block_boxes.back().get();
  275. LayoutInlineBox* open_inline_box = inline_block_box->line_boxes.back()->GetOpenInlineBox();
  276. if (open_inline_box != nullptr)
  277. {
  278. // There's an open inline box chain, which means this block element is parented to it. The chain needs to
  279. // be positioned (if it hasn't already), closed and duplicated after this block box closes. Also, this
  280. // block needs to be aware of its parentage, so it can correctly compute its relative position. First of
  281. // all, we need to close the inline box; this will position the last line if necessary, but it will also
  282. // create a new line in the inline block box; we want this line to be in an inline box after our block
  283. // element.
  284. if (inline_block_box->Close() != OK)
  285. return nullptr;
  286. interrupted_chain = open_inline_box;
  287. }
  288. else
  289. {
  290. // There are no open inline boxes, so this inline box just needs to be closed.
  291. if (CloseInlineBlockBox() != OK)
  292. return nullptr;
  293. }
  294. }
  295. block_boxes.push_back(MakeUnique<LayoutBlockBox>(this, element, allow_shrink));
  296. return block_boxes.back().get();
  297. }
  298. // Adds a new inline element to this inline box.
  299. LayoutInlineBox* LayoutBlockBox::AddInlineElement(Element* element, const Box& box)
  300. {
  301. RMLUI_ZoneScoped;
  302. if (context == BLOCK)
  303. {
  304. LayoutInlineBox* inline_box;
  305. // If we have an open child rendering in an inline context, we can add this element into it.
  306. if (!block_boxes.empty() &&
  307. block_boxes.back()->context == INLINE)
  308. inline_box = block_boxes.back()->AddInlineElement(element, box);
  309. // No dice! Ah well, nothing for it but to open a new inline context block box.
  310. else
  311. {
  312. block_boxes.push_back(MakeUnique<LayoutBlockBox>(this));
  313. if (interrupted_chain != nullptr)
  314. {
  315. block_boxes.back()->line_boxes.back()->AddChainedBox(interrupted_chain);
  316. interrupted_chain = nullptr;
  317. }
  318. inline_box = block_boxes.back()->AddInlineElement(element, box);
  319. }
  320. return inline_box;
  321. }
  322. else
  323. {
  324. // We're an inline context box, so we'll add this new inline element into our line boxes.
  325. return line_boxes.back()->AddElement(element, box);
  326. }
  327. }
  328. // Adds a line-break to this block box.
  329. void LayoutBlockBox::AddBreak()
  330. {
  331. float line_height = element->GetLineHeight();
  332. // Check for an inline box as our last child; if so, we can simply end its line and bail.
  333. if (!block_boxes.empty())
  334. {
  335. LayoutBlockBox* block_box = block_boxes.back().get();
  336. if (block_box->context == INLINE)
  337. {
  338. LayoutLineBox* last_line = block_box->line_boxes.back().get();
  339. if (last_line->GetDimensions().y < 0)
  340. block_box->box_cursor += line_height;
  341. else
  342. last_line->Close();
  343. return;
  344. }
  345. }
  346. // No inline box as our last child; no problem, just increment the cursor by the line height of this element.
  347. box_cursor += line_height;
  348. }
  349. // Adds an element to this block box to be handled as a floating element.
  350. bool LayoutBlockBox::AddFloatElement(Element* element)
  351. {
  352. // If we have an open inline block box, then we have to position the box a little differently.
  353. if (!block_boxes.empty() &&
  354. block_boxes.back()->context == INLINE)
  355. block_boxes.back()->float_elements.push_back(element);
  356. // Nope ... just place it!
  357. else
  358. PositionFloat(element);
  359. return true;
  360. }
  361. // Adds an element to this block box to be handled as an absolutely-positioned element.
  362. void LayoutBlockBox::AddAbsoluteElement(Element* element)
  363. {
  364. RMLUI_ASSERT(context == BLOCK);
  365. AbsoluteElement absolute_element;
  366. absolute_element.element = element;
  367. PositionBox(absolute_element.position, 0);
  368. // If we have an open inline-context block box as our last child, then the absolute element must appear after it,
  369. // but not actually close the box.
  370. if (!block_boxes.empty()
  371. && block_boxes.back()->context == INLINE)
  372. {
  373. LayoutBlockBox* inline_context_box = block_boxes.back().get();
  374. float last_line_height = inline_context_box->line_boxes.back()->GetDimensions().y;
  375. absolute_element.position.y += (inline_context_box->box_cursor + Math::Max(0.0f, last_line_height));
  376. }
  377. // Find the positioned parent for this element.
  378. LayoutBlockBox* absolute_parent = this;
  379. while (absolute_parent != absolute_parent->offset_parent)
  380. absolute_parent = absolute_parent->parent;
  381. absolute_parent->absolute_elements.push_back(absolute_element);
  382. }
  383. // Lays out, sizes, and positions all absolute elements in this block relative to the containing block.
  384. void LayoutBlockBox::CloseAbsoluteElements()
  385. {
  386. if (!absolute_elements.empty())
  387. {
  388. // The size of the containing box, including the padding. This is used to resolve relative offsets.
  389. Vector2f containing_block = GetBox().GetSize(Box::PADDING);
  390. for (size_t i = 0; i < absolute_elements.size(); i++)
  391. {
  392. Element* absolute_element = absolute_elements[i].element;
  393. Vector2f absolute_position = absolute_elements[i].position;
  394. absolute_position -= position - offset_root->GetPosition();
  395. // Lay out the element.
  396. LayoutEngine::FormatElement(absolute_element, containing_block);
  397. // Now that the element's box has been built, we can offset the position we determined was appropriate for
  398. // it by the element's margin. This is necessary because the coordinate system for the box begins at the
  399. // border, not the margin.
  400. absolute_position.x += absolute_element->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  401. absolute_position.y += absolute_element->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  402. // Set the offset of the element; the element itself will take care of any RCSS-defined positional offsets.
  403. absolute_element->SetOffset(absolute_position, element);
  404. }
  405. absolute_elements.clear();
  406. }
  407. }
  408. // Returns the offset from the top-left corner of this box that the next child box will be positioned at.
  409. void LayoutBlockBox::PositionBox(Vector2f& box_position, float top_margin, Style::Clear clear_property) const
  410. {
  411. // If our element is establishing a new offset hierarchy, then any children of ours don't inherit our offset.
  412. box_position = GetPosition();
  413. box_position += box.GetPosition();
  414. box_position.y += box_cursor;
  415. float clear_margin = space->ClearBoxes(box_position.y + top_margin, clear_property) - (box_position.y + top_margin);
  416. if (clear_margin > 0)
  417. box_position.y += clear_margin;
  418. else
  419. {
  420. // Check for a collapsing vertical margin.
  421. if (!block_boxes.empty() &&
  422. block_boxes.back()->context == BLOCK)
  423. {
  424. float bottom_margin = block_boxes.back()->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM);
  425. box_position.y -= Math::Min(top_margin, bottom_margin);
  426. }
  427. }
  428. }
  429. // Returns the offset from the top-left corner of this box's offset element the next child block box, of the given
  430. // dimensions, will be positioned at. This will include the margins on the new block box.
  431. void LayoutBlockBox::PositionBlockBox(Vector2f& box_position, const Box& box, Style::Clear clear_property) const
  432. {
  433. PositionBox(box_position, box.GetEdge(Box::MARGIN, Box::TOP), clear_property);
  434. box_position.x += box.GetEdge(Box::MARGIN, Box::LEFT);
  435. box_position.y += box.GetEdge(Box::MARGIN, Box::TOP);
  436. }
  437. // Returns the offset from the top-left corner of this box for the next line.
  438. void LayoutBlockBox::PositionLineBox(Vector2f& box_position, float& box_width, bool& _wrap_content, const Vector2f& dimensions) const
  439. {
  440. Vector2f cursor;
  441. PositionBox(cursor);
  442. space->PositionBox(box_position, box_width, cursor.y, dimensions);
  443. // Also, probably shouldn't check for widths when positioning the box?
  444. _wrap_content = wrap_content;
  445. }
  446. // Calculate the dimensions of the box's internal content width; i.e. the size of the largest line.
  447. float LayoutBlockBox::GetShrinkToFitWidth() const
  448. {
  449. float content_width = 0.0f;
  450. if (context == BLOCK)
  451. {
  452. auto get_content_width_from_children = [this, &content_width]() {
  453. for (size_t i = 0; i < block_boxes.size(); i++)
  454. {
  455. const Box& box = block_boxes[i]->GetBox();
  456. const float edge_size = box.GetCumulativeEdge(Box::PADDING, Box::LEFT) + box.GetCumulativeEdge(Box::PADDING, Box::RIGHT);
  457. content_width = Math::Max(content_width, block_boxes[i]->GetShrinkToFitWidth() + edge_size);
  458. }
  459. };
  460. // Block boxes with definite sizes should use that size. Otherwise, find the maximum content width of our children.
  461. // Alternative solution: Add some 'intrinsic_width' property to every 'LayoutBlockBox' and have that propagate up.
  462. if (element)
  463. {
  464. auto& computed = element->GetComputedValues();
  465. const float block_width = box.GetSize(Box::CONTENT).x;
  466. if(computed.width.type == Style::Width::Auto)
  467. {
  468. get_content_width_from_children();
  469. }
  470. else
  471. {
  472. float width_value = ResolveValue(computed.width, block_width);
  473. content_width = Math::Max(content_width, width_value);
  474. }
  475. content_width = LayoutDetails::ClampWidth(content_width, computed, block_width);
  476. }
  477. else
  478. {
  479. get_content_width_from_children();
  480. }
  481. // Can add the dimensions of floating elements here if we want to support that.
  482. }
  483. else
  484. {
  485. // Find the largest line in this layout block
  486. for (size_t i = 0; i < line_boxes.size(); i++)
  487. {
  488. // Perhaps a more robust solution is to modify how we set the line box dimension on 'line_box->close()'
  489. // and use that, or add another value in the line_box ... but seems to work for now.
  490. LayoutLineBox* line_box = line_boxes[i].get();
  491. content_width = Math::Max(content_width, line_box->GetBoxCursor());
  492. }
  493. content_width = Math::Min(content_width, box.GetSize(Box::CONTENT).x);
  494. }
  495. return content_width;
  496. }
  497. // Returns the block box's element.
  498. Element* LayoutBlockBox::GetElement() const
  499. {
  500. return element;
  501. }
  502. // Returns the block box's parent.
  503. LayoutBlockBox* LayoutBlockBox::GetParent() const
  504. {
  505. return parent;
  506. }
  507. // Returns the position of the block box, relative to its parent's content area.
  508. const Vector2f& LayoutBlockBox::GetPosition() const
  509. {
  510. return position;
  511. }
  512. // Returns the element against which all positions of boxes in the hierarchy are calculated relative to.
  513. const LayoutBlockBox* LayoutBlockBox::GetOffsetParent() const
  514. {
  515. return offset_parent;
  516. }
  517. // Returns the block box against which all positions of boxes in the hierarchy are calculated relative to.
  518. const LayoutBlockBox* LayoutBlockBox::GetOffsetRoot() const
  519. {
  520. return offset_root;
  521. }
  522. // Returns the block box's dimension box.
  523. Box& LayoutBlockBox::GetBox()
  524. {
  525. return box;
  526. }
  527. // Returns the block box's dimension box.
  528. const Box& LayoutBlockBox::GetBox() const
  529. {
  530. return box;
  531. }
  532. void* LayoutBlockBox::operator new(size_t size)
  533. {
  534. void* memory = LayoutEngine::AllocateLayoutChunk(size);
  535. return memory;
  536. }
  537. void LayoutBlockBox::operator delete(void* chunk)
  538. {
  539. LayoutEngine::DeallocateLayoutChunk(chunk);
  540. }
  541. // Closes our last block box, if it is an open inline block box.
  542. LayoutBlockBox::CloseResult LayoutBlockBox::CloseInlineBlockBox()
  543. {
  544. if (!block_boxes.empty() &&
  545. block_boxes.back()->context == INLINE)
  546. return block_boxes.back()->Close();
  547. return OK;
  548. }
  549. // Positions a floating element within this block box.
  550. void LayoutBlockBox::PositionFloat(Element* element, float offset)
  551. {
  552. Vector2f box_position;
  553. PositionBox(box_position);
  554. space->PositionBox(box_position.y + offset, element);
  555. }
  556. // Checks if we have a new vertical overflow on an auto-scrolling element.
  557. bool LayoutBlockBox::CatchVerticalOverflow(float cursor)
  558. {
  559. if (cursor == -1)
  560. cursor = box_cursor;
  561. float box_height = box.GetSize().y;
  562. if (box_height < 0)
  563. box_height = max_height;
  564. // If we're auto-scrolling and our height is fixed, we have to check if this box has exceeded our client height.
  565. if (!vertical_overflow &&
  566. box_height >= 0 &&
  567. overflow_y_property == Style::Overflow::Auto)
  568. {
  569. if (cursor > box_height - element->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL))
  570. {
  571. RMLUI_ZoneScopedC(0xDD3322);
  572. vertical_overflow = true;
  573. element->GetElementScroll()->EnableScrollbar(ElementScroll::VERTICAL, box.GetSize(Box::PADDING).x);
  574. block_boxes.clear();
  575. space_owner = MakeUnique<LayoutBlockBoxSpace>(this);
  576. space = space_owner.get();
  577. box_cursor = 0;
  578. interrupted_chain = nullptr;
  579. return false;
  580. }
  581. }
  582. return true;
  583. }
  584. } // namespace Rml