LayoutBlockBox.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 "../../Include/RmlUi/Core/Element.h"
  32. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  33. #include "../../Include/RmlUi/Core/ElementScroll.h"
  34. #include "../../Include/RmlUi/Core/Property.h"
  35. #include "../../Include/RmlUi/Core/Profiling.h"
  36. #include <float.h>
  37. namespace Rml {
  38. namespace Core {
  39. // Creates a new block box for rendering a block element.
  40. LayoutBlockBox::LayoutBlockBox(LayoutEngine* _layout_engine, LayoutBlockBox* _parent, Element* _element) : position(0, 0)
  41. {
  42. RMLUI_ZoneScoped;
  43. space = new LayoutBlockBoxSpace(this);
  44. layout_engine = _layout_engine;
  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. 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. layout_engine->BuildBox(box, min_height, max_height, parent, element);
  77. // Position ourselves within our containing block (if we have a valid offset parent).
  78. if (parent->GetElement() != nullptr)
  79. {
  80. if (self_offset_parent != this)
  81. {
  82. // Get the next position within our offset parent's containing block.
  83. parent->PositionBlockBox(position, box, element ? element->GetComputedValues().clear : Style::Clear::None);
  84. element->SetOffset(position - (self_offset_parent->GetPosition() - offset_root->GetPosition()), self_offset_parent->GetElement());
  85. }
  86. else
  87. element->SetOffset(position, nullptr);
  88. }
  89. }
  90. if (element != nullptr)
  91. {
  92. const auto& computed = element->GetComputedValues();
  93. wrap_content = computed.white_space != Style::WhiteSpace::Nowrap;
  94. // Determine if this element should have scrollbars or not, and create them if so.
  95. overflow_x_property = computed.overflow_x;
  96. overflow_y_property = computed.overflow_y;
  97. if (overflow_x_property == Style::Overflow::Scroll)
  98. element->GetElementScroll()->EnableScrollbar(ElementScroll::HORIZONTAL, box.GetSize(Box::PADDING).x);
  99. else
  100. element->GetElementScroll()->DisableScrollbar(ElementScroll::HORIZONTAL);
  101. if (overflow_y_property == Style::Overflow::Scroll)
  102. element->GetElementScroll()->EnableScrollbar(ElementScroll::VERTICAL, box.GetSize(Box::PADDING).x);
  103. else
  104. element->GetElementScroll()->DisableScrollbar(ElementScroll::VERTICAL);
  105. }
  106. else
  107. {
  108. wrap_content = true;
  109. overflow_x_property = Style::Overflow::Visible;
  110. overflow_y_property = Style::Overflow::Visible;
  111. }
  112. }
  113. // Creates a new block box in an inline context.
  114. LayoutBlockBox::LayoutBlockBox(LayoutEngine* _layout_engine, LayoutBlockBox* _parent) : position(-1, -1)
  115. {
  116. layout_engine = _layout_engine;
  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(new LayoutLineBox(this));
  123. wrap_content = parent->wrap_content;
  124. element = nullptr;
  125. interrupted_chain = nullptr;
  126. box_cursor = 0;
  127. vertical_overflow = false;
  128. layout_engine->BuildBox(box, min_height, max_height, parent, nullptr);
  129. parent->PositionBlockBox(position, box, Style::Clear::None);
  130. box.SetContent(Vector2f(box.GetSize(Box::CONTENT).x, -1));
  131. // Reset the min and max heights; they're not valid for inline block boxes.
  132. min_height = 0;
  133. max_height = FLT_MAX;
  134. }
  135. // Releases the block box.
  136. LayoutBlockBox::~LayoutBlockBox()
  137. {
  138. for (size_t i = 0; i < block_boxes.size(); i++)
  139. delete block_boxes[i];
  140. for (size_t i = 0; i < line_boxes.size(); i++)
  141. delete line_boxes[i];
  142. if (context == BLOCK)
  143. delete space;
  144. }
  145. // Closes the box.
  146. LayoutBlockBox::CloseResult LayoutBlockBox::Close()
  147. {
  148. // If the last child of this block box is an inline box, then we haven't closed it; close it now!
  149. if (context == BLOCK)
  150. {
  151. CloseResult result = CloseInlineBlockBox();
  152. if (result != OK)
  153. return LAYOUT_SELF;
  154. }
  155. // Otherwise, we're an inline context box; so close our last line, which will still be open.
  156. else
  157. {
  158. line_boxes.back()->Close();
  159. // Expand our content area if any line boxes had to push themselves out.
  160. Vector2f content_area = box.GetSize();
  161. for (size_t i = 0; i < line_boxes.size(); i++)
  162. content_area.x = Math::Max(content_area.x, line_boxes[i]->GetDimensions().x);
  163. box.SetContent(content_area);
  164. }
  165. // Set this box's height, if necessary.
  166. if (box.GetSize(Box::CONTENT).y < 0)
  167. {
  168. Vector2f content_area = box.GetSize();
  169. content_area.y = Math::Clamp(box_cursor, min_height, max_height);
  170. if (element != nullptr)
  171. content_area.y = Math::Max(content_area.y, space->GetDimensions().y);
  172. box.SetContent(content_area);
  173. }
  174. // Set the computed box on the element.
  175. if (element != nullptr)
  176. {
  177. if (context == BLOCK)
  178. {
  179. // Calculate the dimensions of the box's *internal* content; this is the tightest-fitting box around all of the
  180. // internal elements, plus this element's padding.
  181. Vector2f content_box(0, 0);
  182. for (size_t i = 0; i < block_boxes.size(); i++)
  183. content_box.x = Math::Max(content_box.x, block_boxes[i]->GetBox().GetSize(Box::MARGIN).x);
  184. // Check how big our floated area is.
  185. Vector2f space_box = space->GetDimensions();
  186. content_box.x = Math::Max(content_box.x, space_box.x);
  187. // If our content is larger than our window, we can enable the horizontal scrollbar if
  188. // we're set to auto-scrollbars. If we're set to always use scrollbars, then the horiontal
  189. // scrollbar will already have been enabled in the constructor.
  190. if (content_box.x > box.GetSize().x)
  191. {
  192. if (overflow_x_property == Style::Overflow::Auto)
  193. {
  194. element->GetElementScroll()->EnableScrollbar(ElementScroll::HORIZONTAL, box.GetSize(Box::PADDING).x);
  195. if (!CatchVerticalOverflow())
  196. return LAYOUT_SELF;
  197. }
  198. }
  199. content_box.x += (box.GetEdge(Box::PADDING, Box::LEFT) + box.GetEdge(Box::PADDING, Box::RIGHT));
  200. content_box.y = box_cursor;
  201. content_box.y = Math::Max(content_box.y, space_box.y);
  202. if (!CatchVerticalOverflow(content_box.y))
  203. return LAYOUT_SELF;
  204. content_box.y += (box.GetEdge(Box::PADDING, Box::TOP) + box.GetEdge(Box::PADDING, Box::BOTTOM));
  205. element->SetBox(box);
  206. element->SetContentBox(space->GetOffset(), content_box);
  207. // Format any scrollbars which were enabled on this element.
  208. element->GetElementScroll()->FormatScrollbars();
  209. }
  210. else
  211. element->SetBox(box);
  212. }
  213. // Increment the parent's cursor.
  214. if (parent != nullptr)
  215. {
  216. // If this close fails, it means this block box has caused our parent block box to generate an automatic
  217. // vertical scrollbar.
  218. if (!parent->CloseBlockBox(this))
  219. return LAYOUT_PARENT;
  220. }
  221. // If we represent a positioned element, then we can now (as we've been sized) act as the containing block for all
  222. // the absolutely-positioned elements of our descendants.
  223. if (context == BLOCK &&
  224. element != nullptr)
  225. {
  226. if (element->GetPosition() != Style::Position::Static)
  227. CloseAbsoluteElements();
  228. }
  229. return OK;
  230. }
  231. // Called by a closing block box child.
  232. bool LayoutBlockBox::CloseBlockBox(LayoutBlockBox* child)
  233. {
  234. RMLUI_ASSERT(context == BLOCK);
  235. box_cursor = (child->GetPosition().y - child->box.GetEdge(Box::MARGIN, Box::TOP) - (box.GetPosition().y + position.y)) + child->GetBox().GetSize(Box::MARGIN).y;
  236. return CatchVerticalOverflow();
  237. }
  238. // Called by a closing line box child.
  239. LayoutInlineBox* LayoutBlockBox::CloseLineBox(LayoutLineBox* child, LayoutInlineBox* overflow, LayoutInlineBox* overflow_chain)
  240. {
  241. RMLUI_ZoneScoped;
  242. RMLUI_ASSERT(context == INLINE);
  243. if (child->GetDimensions().x > 0)
  244. box_cursor = (child->GetPosition().y - (box.GetPosition().y + position.y)) + child->GetDimensions().y;
  245. // If we have any pending floating elements for our parent, then this would be an ideal time to position them.
  246. if (!float_elements.empty())
  247. {
  248. for (size_t i = 0; i < float_elements.size(); ++i)
  249. parent->PositionFloat(float_elements[i], box_cursor);
  250. float_elements.clear();
  251. }
  252. // Add a new line box.
  253. line_boxes.push_back(new LayoutLineBox(this));
  254. if (overflow_chain != nullptr)
  255. line_boxes.back()->AddChainedBox(overflow_chain);
  256. if (overflow != nullptr)
  257. return line_boxes.back()->AddBox(overflow);
  258. return nullptr;
  259. }
  260. // Adds a new block element to this block box.
  261. LayoutBlockBox* LayoutBlockBox::AddBlockElement(Element* element)
  262. {
  263. RMLUI_ZoneScoped;
  264. RMLUI_ASSERT(context == BLOCK);
  265. // Check if our most previous block box is rendering in an inline context.
  266. if (!block_boxes.empty() &&
  267. block_boxes.back()->context == INLINE)
  268. {
  269. LayoutBlockBox* inline_block_box = block_boxes.back();
  270. LayoutInlineBox* open_inline_box = inline_block_box->line_boxes.back()->GetOpenInlineBox();
  271. if (open_inline_box != nullptr)
  272. {
  273. // There's an open inline box chain, which means this block element is parented to it. The chain needs to
  274. // be positioned (if it hasn't already), closed and duplicated after this block box closes. Also, this
  275. // block needs to be aware of its parentage, so it can correctly compute its relative position. First of
  276. // all, we need to close the inline box; this will position the last line if necessary, but it will also
  277. // create a new line in the inline block box; we want this line to be in an inline box after our block
  278. // element.
  279. if (inline_block_box->Close() != OK)
  280. return nullptr;
  281. interrupted_chain = open_inline_box;
  282. }
  283. else
  284. {
  285. // There are no open inline boxes, so this inline box just needs to be closed.
  286. if (CloseInlineBlockBox() != OK)
  287. return nullptr;
  288. }
  289. }
  290. block_boxes.push_back(new LayoutBlockBox(layout_engine, this, element));
  291. return block_boxes.back();
  292. }
  293. // Adds a new inline element to this inline box.
  294. LayoutInlineBox* LayoutBlockBox::AddInlineElement(Element* element, const Box& box)
  295. {
  296. RMLUI_ZoneScoped;
  297. if (context == BLOCK)
  298. {
  299. LayoutInlineBox* inline_box;
  300. // If we have an open child rendering in an inline context, we can add this element into it.
  301. if (!block_boxes.empty() &&
  302. block_boxes.back()->context == INLINE)
  303. inline_box = block_boxes.back()->AddInlineElement(element, box);
  304. // No dice! Ah well, nothing for it but to open a new inline context block box.
  305. else
  306. {
  307. block_boxes.push_back(new LayoutBlockBox(layout_engine, this));
  308. if (interrupted_chain != nullptr)
  309. {
  310. block_boxes.back()->line_boxes.back()->AddChainedBox(interrupted_chain);
  311. interrupted_chain = nullptr;
  312. }
  313. inline_box = block_boxes.back()->AddInlineElement(element, box);
  314. }
  315. return inline_box;
  316. }
  317. else
  318. {
  319. // We're an inline context box, so we'll add this new inline element into our line boxes.
  320. return line_boxes.back()->AddElement(element, box);
  321. }
  322. }
  323. // Adds a line-break to this block box.
  324. void LayoutBlockBox::AddBreak()
  325. {
  326. float line_height = element->GetLineHeight();
  327. // Check for an inline box as our last child; if so, we can simply end its line and bail.
  328. if (!block_boxes.empty())
  329. {
  330. LayoutBlockBox* block_box = block_boxes.back();
  331. if (block_box->context == INLINE)
  332. {
  333. LayoutLineBox* last_line = block_box->line_boxes.back();
  334. if (last_line->GetDimensions().y < 0)
  335. block_box->box_cursor += line_height;
  336. else
  337. last_line->Close();
  338. return;
  339. }
  340. }
  341. // No inline box as our last child; no problem, just increment the cursor by the line height of this element.
  342. box_cursor += line_height;
  343. }
  344. // Adds an element to this block box to be handled as a floating element.
  345. bool LayoutBlockBox::AddFloatElement(Element* element)
  346. {
  347. // If we have an open inline block box, then we have to position the box a little differently.
  348. if (!block_boxes.empty() &&
  349. block_boxes.back()->context == INLINE)
  350. block_boxes.back()->float_elements.push_back(element);
  351. // Nope ... just place it!
  352. else
  353. PositionFloat(element);
  354. return true;
  355. }
  356. // Adds an element to this block box to be handled as an absolutely-positioned element.
  357. void LayoutBlockBox::AddAbsoluteElement(Element* element)
  358. {
  359. RMLUI_ASSERT(context == BLOCK);
  360. AbsoluteElement absolute_element;
  361. absolute_element.element = element;
  362. PositionBox(absolute_element.position, 0);
  363. // If we have an open inline-context block box as our last child, then the absolute element must appear after it,
  364. // but not actually close the box.
  365. if (!block_boxes.empty()
  366. && block_boxes.back()->context == INLINE)
  367. {
  368. LayoutBlockBox* inline_context_box = block_boxes.back();
  369. float last_line_height = inline_context_box->line_boxes.back()->GetDimensions().y;
  370. absolute_element.position.y += (inline_context_box->box_cursor + Math::Max(0.0f, last_line_height));
  371. }
  372. // Find the positioned parent for this element.
  373. LayoutBlockBox* absolute_parent = this;
  374. while (absolute_parent != absolute_parent->offset_parent)
  375. absolute_parent = absolute_parent->parent;
  376. absolute_parent->absolute_elements.push_back(absolute_element);
  377. }
  378. // Lays out, sizes, and positions all absolute elements in this block relative to the containing block.
  379. void LayoutBlockBox::CloseAbsoluteElements()
  380. {
  381. if (!absolute_elements.empty())
  382. {
  383. // The size of the containing box, including the padding. This is used to resolve relative offsets.
  384. Vector2f containing_block = GetBox().GetSize(Box::PADDING);
  385. for (size_t i = 0; i < absolute_elements.size(); i++)
  386. {
  387. Element* absolute_element = absolute_elements[i].element;
  388. Vector2f absolute_position = absolute_elements[i].position;
  389. absolute_position -= position - offset_root->GetPosition();
  390. // Lay out the element.
  391. LayoutEngine layout_engine;
  392. layout_engine.FormatElement(absolute_element, containing_block);
  393. // Now that the element's box has been built, we can offset the position we determined was appropriate for
  394. // it by the element's margin. This is necessary because the coordinate system for the box begins at the
  395. // border, not the margin.
  396. absolute_position.x += absolute_element->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  397. absolute_position.y += absolute_element->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  398. // Set the offset of the element; the element itself will take care of any RCSS-defined positional offsets.
  399. absolute_element->SetOffset(absolute_position, element);
  400. }
  401. absolute_elements.clear();
  402. }
  403. }
  404. // Returns the offset from the top-left corner of this box that the next child box will be positioned at.
  405. void LayoutBlockBox::PositionBox(Vector2f& box_position, float top_margin, Style::Clear clear_property) const
  406. {
  407. // If our element is establishing a new offset hierarchy, then any children of ours don't inherit our offset.
  408. box_position = GetPosition();
  409. box_position += box.GetPosition();
  410. box_position.y += box_cursor;
  411. float clear_margin = space->ClearBoxes(box_position.y + top_margin, clear_property) - (box_position.y + top_margin);
  412. if (clear_margin > 0)
  413. box_position.y += clear_margin;
  414. else
  415. {
  416. // Check for a collapsing vertical margin.
  417. if (!block_boxes.empty() &&
  418. block_boxes.back()->context == BLOCK)
  419. {
  420. float bottom_margin = block_boxes.back()->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM);
  421. box_position.y -= Math::Min(top_margin, bottom_margin);
  422. }
  423. }
  424. }
  425. // Returns the offset from the top-left corner of this box's offset element the next child block box, of the given
  426. // dimensions, will be positioned at. This will include the margins on the new block box.
  427. void LayoutBlockBox::PositionBlockBox(Vector2f& box_position, const Box& box, Style::Clear clear_property) const
  428. {
  429. PositionBox(box_position, box.GetEdge(Box::MARGIN, Box::TOP), clear_property);
  430. box_position.x += box.GetEdge(Box::MARGIN, Box::LEFT);
  431. box_position.y += box.GetEdge(Box::MARGIN, Box::TOP);
  432. }
  433. // Returns the offset from the top-left corner of this box for the next line.
  434. void LayoutBlockBox::PositionLineBox(Vector2f& box_position, float& box_width, bool& _wrap_content, const Vector2f& dimensions) const
  435. {
  436. Vector2f cursor;
  437. PositionBox(cursor);
  438. space->PositionBox(box_position, box_width, cursor.y, dimensions);
  439. // Also, probably shouldn't check for widths when positioning the box?
  440. _wrap_content = wrap_content;
  441. }
  442. // Calculate the dimensions of the box's internal width; i.e. the size of the largest line, plus this element's padding.
  443. float LayoutBlockBox::InternalContentWidth() const
  444. {
  445. float content_width = 0.0f;
  446. if (context == BLOCK)
  447. {
  448. for (size_t i = 0; i < block_boxes.size(); i++)
  449. {
  450. content_width = Math::Max(content_width, block_boxes[i]->InternalContentWidth());
  451. }
  452. // Work-around for supporting 'width' specification of 'display:block' elements inside 'display:inline-block'.
  453. // Alternative solution: Add some 'intrinsic_width' property to every 'LayoutBlockBox' and have that propagate up to the nearest 'inline-block'.
  454. if (element)
  455. {
  456. auto& computed = element->GetComputedValues();
  457. const float block_width = box.GetSize(Box::CONTENT).x;
  458. if(computed.width.type != Style::Width::Auto)
  459. {
  460. float w_value = ResolveValue(computed.width, block_width);
  461. content_width = Math::Max(content_width, w_value);
  462. }
  463. float min_width = ResolveValue(computed.min_width, block_width);
  464. content_width = Math::Max(content_width, min_width);
  465. if (computed.max_width.value >= 0.f)
  466. {
  467. float value = ResolveValue(computed.max_width, block_width);
  468. content_width = Math::Min(content_width, value);
  469. }
  470. }
  471. content_width += (box.GetEdge(Box::PADDING, Box::LEFT) + box.GetEdge(Box::PADDING, Box::RIGHT));
  472. content_width += (box.GetEdge(Box::MARGIN, Box::LEFT) + box.GetEdge(Box::MARGIN, Box::RIGHT));
  473. }
  474. else
  475. {
  476. // Find the largest line in this layout block
  477. for (size_t i = 0; i < line_boxes.size(); i++)
  478. {
  479. // Perhaps a more robust solution is to modify how we set the line box dimension on 'line_box->close()'
  480. // and use that, or add another value in the line_box ... but seems to work for now.
  481. LayoutLineBox* line_box = line_boxes[i];
  482. content_width = Math::Max(content_width, line_box->GetBoxCursor());
  483. }
  484. content_width = Math::Min(content_width, box.GetSize(Box::CONTENT).x);
  485. }
  486. return content_width;
  487. }
  488. // Returns the block box's element.
  489. Element* LayoutBlockBox::GetElement() const
  490. {
  491. return element;
  492. }
  493. // Returns the block box's parent.
  494. LayoutBlockBox* LayoutBlockBox::GetParent() const
  495. {
  496. return parent;
  497. }
  498. // Returns the position of the block box, relative to its parent's content area.
  499. const Vector2f& LayoutBlockBox::GetPosition() const
  500. {
  501. return position;
  502. }
  503. // Returns the element against which all positions of boxes in the hierarchy are calculated relative to.
  504. LayoutBlockBox* LayoutBlockBox::GetOffsetParent() const
  505. {
  506. return offset_parent;
  507. }
  508. // Returns the block box against which all positions of boxes in the hierarchy are calculated relative to.
  509. LayoutBlockBox* LayoutBlockBox::GetOffsetRoot() const
  510. {
  511. return offset_root;
  512. }
  513. // Returns the block box's dimension box.
  514. Box& LayoutBlockBox::GetBox()
  515. {
  516. return box;
  517. }
  518. // Returns the block box's dimension box.
  519. const Box& LayoutBlockBox::GetBox() const
  520. {
  521. return box;
  522. }
  523. void* LayoutBlockBox::operator new(size_t size)
  524. {
  525. void* memory = LayoutEngine::AllocateLayoutChunk(size);
  526. return memory;
  527. }
  528. void LayoutBlockBox::operator delete(void* chunk)
  529. {
  530. LayoutEngine::DeallocateLayoutChunk(chunk);
  531. }
  532. // Closes our last block box, if it is an open inline block box.
  533. LayoutBlockBox::CloseResult LayoutBlockBox::CloseInlineBlockBox()
  534. {
  535. if (!block_boxes.empty() &&
  536. block_boxes.back()->context == INLINE)
  537. return block_boxes.back()->Close();
  538. return OK;
  539. }
  540. // Positions a floating element within this block box.
  541. void LayoutBlockBox::PositionFloat(Element* element, float offset)
  542. {
  543. Vector2f box_position;
  544. PositionBox(box_position);
  545. space->PositionBox(box_position.y + offset, element);
  546. }
  547. // Checks if we have a new vertical overflow on an auto-scrolling element.
  548. bool LayoutBlockBox::CatchVerticalOverflow(float cursor)
  549. {
  550. if (cursor == -1)
  551. cursor = box_cursor;
  552. float box_height = box.GetSize().y;
  553. if (box_height < 0)
  554. box_height = max_height;
  555. // If we're auto-scrolling and our height is fixed, we have to check if this box has exceeded our client height.
  556. if (!vertical_overflow &&
  557. box_height >= 0 &&
  558. overflow_y_property == Style::Overflow::Auto)
  559. {
  560. if (cursor > box_height - element->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL))
  561. {
  562. RMLUI_ZoneScopedC(0xDD3322);
  563. vertical_overflow = true;
  564. element->GetElementScroll()->EnableScrollbar(ElementScroll::VERTICAL, box.GetSize(Box::PADDING).x);
  565. for (size_t i = 0; i < block_boxes.size(); i++)
  566. delete block_boxes[i];
  567. block_boxes.clear();
  568. delete space;
  569. space = new LayoutBlockBoxSpace(this);
  570. box_cursor = 0;
  571. interrupted_chain = nullptr;
  572. return false;
  573. }
  574. }
  575. return true;
  576. }
  577. }
  578. }