LayoutBlockBox.cpp 22 KB

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