LayoutBlockBox.cpp 22 KB

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