nsTableCellFrame.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsTableCellFrame_h__
  6. #define nsTableCellFrame_h__
  7. #include "mozilla/Attributes.h"
  8. #include "celldata.h"
  9. #include "imgIContainer.h"
  10. #include "nsITableCellLayout.h"
  11. #include "nscore.h"
  12. #include "nsContainerFrame.h"
  13. #include "nsStyleContext.h"
  14. #include "nsIPercentBSizeObserver.h"
  15. #include "nsGkAtoms.h"
  16. #include "nsLayoutUtils.h"
  17. #include "nsTArray.h"
  18. #include "nsTableRowFrame.h"
  19. #include "mozilla/WritingModes.h"
  20. /**
  21. * nsTableCellFrame
  22. * data structure to maintain information about a single table cell's frame
  23. *
  24. * NOTE: frames are not ref counted. We expose addref and release here
  25. * so we can change that decsion in the future. Users of nsITableCellLayout
  26. * should refcount correctly as if this object is being ref counted, though
  27. * no actual support is under the hood.
  28. *
  29. * @author sclark
  30. */
  31. class nsTableCellFrame : public nsContainerFrame,
  32. public nsITableCellLayout,
  33. public nsIPercentBSizeObserver
  34. {
  35. typedef mozilla::gfx::DrawTarget DrawTarget;
  36. typedef mozilla::image::DrawResult DrawResult;
  37. protected:
  38. typedef mozilla::WritingMode WritingMode;
  39. typedef mozilla::LogicalSide LogicalSide;
  40. typedef mozilla::LogicalMargin LogicalMargin;
  41. public:
  42. NS_DECL_QUERYFRAME_TARGET(nsTableCellFrame)
  43. NS_DECL_QUERYFRAME
  44. NS_DECL_FRAMEARENA_HELPERS
  45. // default constructor supplied by the compiler
  46. nsTableCellFrame(nsStyleContext* aContext, nsTableFrame* aTableFrame);
  47. ~nsTableCellFrame();
  48. nsTableRowFrame* GetTableRowFrame() const
  49. {
  50. nsIFrame* parent = GetParent();
  51. MOZ_ASSERT(parent && parent->GetType() == nsGkAtoms::tableRowFrame);
  52. return static_cast<nsTableRowFrame*>(parent);
  53. }
  54. nsTableFrame* GetTableFrame() const
  55. {
  56. return GetTableRowFrame()->GetTableFrame();
  57. }
  58. virtual void Init(nsIContent* aContent,
  59. nsContainerFrame* aParent,
  60. nsIFrame* aPrevInFlow) override;
  61. virtual void DestroyFrom(nsIFrame* aDestructRoot) override;
  62. #ifdef ACCESSIBILITY
  63. virtual mozilla::a11y::AccType AccessibleType() override;
  64. #endif
  65. virtual nsresult AttributeChanged(int32_t aNameSpaceID,
  66. nsIAtom* aAttribute,
  67. int32_t aModType) override;
  68. /** @see nsIFrame::DidSetStyleContext */
  69. virtual void DidSetStyleContext(nsStyleContext* aOldStyleContext) override;
  70. #ifdef DEBUG
  71. // Our anonymous block frame is the content insertion frame so these
  72. // methods should never be called:
  73. virtual void AppendFrames(ChildListID aListID,
  74. nsFrameList& aFrameList) override;
  75. virtual void InsertFrames(ChildListID aListID,
  76. nsIFrame* aPrevFrame,
  77. nsFrameList& aFrameList) override;
  78. virtual void RemoveFrame(ChildListID aListID,
  79. nsIFrame* aOldFrame) override;
  80. #endif
  81. virtual nsContainerFrame* GetContentInsertionFrame() override {
  82. return PrincipalChildList().FirstChild()->GetContentInsertionFrame();
  83. }
  84. virtual nsMargin GetUsedMargin() const override;
  85. virtual void NotifyPercentBSize(const ReflowInput& aReflowInput) override;
  86. virtual bool NeedsToObserve(const ReflowInput& aReflowInput) override;
  87. virtual void BuildDisplayList(nsDisplayListBuilder* aBuilder,
  88. const nsDisplayListSet& aLists) override;
  89. virtual nsresult ProcessBorders(nsTableFrame* aFrame,
  90. nsDisplayListBuilder* aBuilder,
  91. const nsDisplayListSet& aLists);
  92. virtual nscoord GetMinISize(nsRenderingContext *aRenderingContext) override;
  93. virtual nscoord GetPrefISize(nsRenderingContext *aRenderingContext) override;
  94. IntrinsicISizeOffsetData IntrinsicISizeOffsets(nscoord aPercentageBasis =
  95. NS_UNCONSTRAINEDSIZE) override;
  96. virtual void Reflow(nsPresContext* aPresContext,
  97. ReflowOutput& aDesiredSize,
  98. const ReflowInput& aReflowInput,
  99. nsReflowStatus& aStatus) override;
  100. /**
  101. * Get the "type" of the frame
  102. *
  103. * @see nsLayoutAtoms::tableCellFrame
  104. */
  105. virtual nsIAtom* GetType() const override;
  106. #ifdef DEBUG_FRAME_DUMP
  107. virtual nsresult GetFrameName(nsAString& aResult) const override;
  108. #endif
  109. // Although the spec doesn't say that writing-mode is not applied to
  110. // table-cells, we still override this method here because we want to
  111. // make effective writing mode of table structure frames consistent
  112. // within a table. The content inside table cells is reflowed by an
  113. // anonymous block, hence their writing mode is not affected.
  114. virtual mozilla::WritingMode GetWritingMode() const override
  115. { return GetTableFrame()->GetWritingMode(); }
  116. void BlockDirAlignChild(mozilla::WritingMode aWM, nscoord aMaxAscent);
  117. /*
  118. * Get the value of vertical-align adjusted for CSS 2's rules for a
  119. * table cell, which means the result is always
  120. * NS_STYLE_VERTICAL_ALIGN_{TOP,MIDDLE,BOTTOM,BASELINE}.
  121. */
  122. virtual uint8_t GetVerticalAlign() const;
  123. bool HasVerticalAlignBaseline() const {
  124. return GetVerticalAlign() == NS_STYLE_VERTICAL_ALIGN_BASELINE;
  125. }
  126. bool CellHasVisibleContent(nscoord aBSize,
  127. nsTableFrame* tableFrame,
  128. nsIFrame* kidFrame);
  129. /**
  130. * Get the first-line baseline of the cell relative to its block-start border
  131. * edge, as if the cell were vertically aligned to the top of the row.
  132. */
  133. nscoord GetCellBaseline() const;
  134. /**
  135. * return the cell's specified row span. this is what was specified in the
  136. * content model or in the style info, and is always >= 0.
  137. * to get the effective row span (the actual value that applies), use GetEffectiveRowSpan()
  138. * @see nsTableFrame::GetEffectiveRowSpan()
  139. */
  140. int32_t GetRowSpan();
  141. // there is no set row index because row index depends on the cell's parent row only
  142. /*---------------- nsITableCellLayout methods ------------------------*/
  143. /**
  144. * return the cell's starting row index (starting at 0 for the first row).
  145. * for continued cell frames the row index is that of the cell's first-in-flow
  146. * and the column index (starting at 0 for the first column
  147. */
  148. NS_IMETHOD GetCellIndexes(int32_t &aRowIndex, int32_t &aColIndex) override;
  149. /** return the mapped cell's row index (starting at 0 for the first row) */
  150. uint32_t RowIndex() const
  151. {
  152. return static_cast<nsTableRowFrame*>(GetParent())->GetRowIndex();
  153. }
  154. /**
  155. * return the cell's specified col span. this is what was specified in the
  156. * content model or in the style info, and is always >= 1.
  157. * to get the effective col span (the actual value that applies), use GetEffectiveColSpan()
  158. * @see nsTableFrame::GetEffectiveColSpan()
  159. */
  160. int32_t GetColSpan();
  161. /** return the cell's column index (starting at 0 for the first column) */
  162. uint32_t ColIndex() const
  163. {
  164. // NOTE: We copy this from previous continuations, and we don't ever have
  165. // dynamic updates when tables split, so our mColIndex always matches our
  166. // first continuation's.
  167. MOZ_ASSERT(static_cast<nsTableCellFrame*>(FirstContinuation())->mColIndex ==
  168. mColIndex,
  169. "mColIndex out of sync with first continuation");
  170. return mColIndex;
  171. }
  172. void SetColIndex(int32_t aColIndex);
  173. /** return the available isize given to this frame during its last reflow */
  174. inline nscoord GetPriorAvailISize();
  175. /** set the available isize given to this frame during its last reflow */
  176. inline void SetPriorAvailISize(nscoord aPriorAvailISize);
  177. /** return the desired size returned by this frame during its last reflow */
  178. inline mozilla::LogicalSize GetDesiredSize();
  179. /** set the desired size returned by this frame during its last reflow */
  180. inline void SetDesiredSize(const ReflowOutput & aDesiredSize);
  181. bool GetContentEmpty() const;
  182. void SetContentEmpty(bool aContentEmpty);
  183. bool HasPctOverBSize();
  184. void SetHasPctOverBSize(bool aValue);
  185. nsTableCellFrame* GetNextCell() const
  186. {
  187. nsIFrame* sibling = GetNextSibling();
  188. #ifdef DEBUG
  189. if (sibling) {
  190. nsTableCellFrame* cellFrame = do_QueryFrame(sibling);
  191. MOZ_ASSERT(cellFrame, "How do we have a non-cell sibling?");
  192. }
  193. #endif // DEBUG
  194. return static_cast<nsTableCellFrame*>(sibling);
  195. }
  196. virtual LogicalMargin GetBorderWidth(WritingMode aWM) const;
  197. virtual DrawResult PaintBackground(nsRenderingContext& aRenderingContext,
  198. const nsRect& aDirtyRect,
  199. nsPoint aPt,
  200. uint32_t aFlags);
  201. void DecorateForSelection(DrawTarget* aDrawTarget, nsPoint aPt);
  202. virtual bool ComputeCustomOverflow(nsOverflowAreas& aOverflowAreas) override;
  203. virtual bool IsFrameOfType(uint32_t aFlags) const override
  204. {
  205. return nsContainerFrame::IsFrameOfType(aFlags & ~(nsIFrame::eTablePart));
  206. }
  207. virtual void InvalidateFrame(uint32_t aDisplayItemKey = 0) override;
  208. virtual void InvalidateFrameWithRect(const nsRect& aRect, uint32_t aDisplayItemKey = 0) override;
  209. virtual void InvalidateFrameForRemoval() override { InvalidateFrameSubtree(); }
  210. bool ShouldPaintBordersAndBackgrounds() const;
  211. bool ShouldPaintBackground(nsDisplayListBuilder* aBuilder);
  212. protected:
  213. virtual LogicalSides
  214. GetLogicalSkipSides(const ReflowInput* aReflowInput = nullptr) const override;
  215. /**
  216. * GetBorderOverflow says how far the cell's own borders extend
  217. * outside its own bounds. In the separated borders model this should
  218. * just be zero (as it is for most frames), but in the collapsed
  219. * borders model (for which nsBCTableCellFrame overrides this virtual
  220. * method), it considers the extents of the collapsed border.
  221. */
  222. virtual nsMargin GetBorderOverflow();
  223. friend class nsTableRowFrame;
  224. uint32_t mColIndex; // the starting column for this cell
  225. nscoord mPriorAvailISize; // the avail isize during the last reflow
  226. mozilla::LogicalSize mDesiredSize; // the last desired inline and block size
  227. };
  228. inline nscoord nsTableCellFrame::GetPriorAvailISize()
  229. { return mPriorAvailISize; }
  230. inline void nsTableCellFrame::SetPriorAvailISize(nscoord aPriorAvailISize)
  231. { mPriorAvailISize = aPriorAvailISize; }
  232. inline mozilla::LogicalSize nsTableCellFrame::GetDesiredSize()
  233. { return mDesiredSize; }
  234. inline void nsTableCellFrame::SetDesiredSize(const ReflowOutput & aDesiredSize)
  235. {
  236. mozilla::WritingMode wm = aDesiredSize.GetWritingMode();
  237. mDesiredSize = aDesiredSize.Size(wm).ConvertTo(GetWritingMode(), wm);
  238. }
  239. inline bool nsTableCellFrame::GetContentEmpty() const
  240. {
  241. return HasAnyStateBits(NS_TABLE_CELL_CONTENT_EMPTY);
  242. }
  243. inline void nsTableCellFrame::SetContentEmpty(bool aContentEmpty)
  244. {
  245. if (aContentEmpty) {
  246. AddStateBits(NS_TABLE_CELL_CONTENT_EMPTY);
  247. } else {
  248. RemoveStateBits(NS_TABLE_CELL_CONTENT_EMPTY);
  249. }
  250. }
  251. inline bool nsTableCellFrame::HasPctOverBSize()
  252. {
  253. return HasAnyStateBits(NS_TABLE_CELL_HAS_PCT_OVER_BSIZE);
  254. }
  255. inline void nsTableCellFrame::SetHasPctOverBSize(bool aValue)
  256. {
  257. if (aValue) {
  258. AddStateBits(NS_TABLE_CELL_HAS_PCT_OVER_BSIZE);
  259. } else {
  260. RemoveStateBits(NS_TABLE_CELL_HAS_PCT_OVER_BSIZE);
  261. }
  262. }
  263. // nsBCTableCellFrame
  264. class nsBCTableCellFrame final : public nsTableCellFrame
  265. {
  266. typedef mozilla::image::DrawResult DrawResult;
  267. public:
  268. NS_DECL_FRAMEARENA_HELPERS
  269. nsBCTableCellFrame(nsStyleContext* aContext, nsTableFrame* aTableFrame);
  270. ~nsBCTableCellFrame();
  271. virtual nsIAtom* GetType() const override;
  272. virtual nsMargin GetUsedBorder() const override;
  273. // Get the *inner half of the border only*, in twips.
  274. virtual LogicalMargin GetBorderWidth(WritingMode aWM) const override;
  275. // Get the *inner half of the border only*, in pixels.
  276. BCPixelSize GetBorderWidth(LogicalSide aSide) const;
  277. // Set the full (both halves) width of the border
  278. void SetBorderWidth(LogicalSide aSide, BCPixelSize aPixelValue);
  279. virtual nsMargin GetBorderOverflow() override;
  280. #ifdef DEBUG_FRAME_DUMP
  281. virtual nsresult GetFrameName(nsAString& aResult) const override;
  282. #endif
  283. virtual DrawResult PaintBackground(nsRenderingContext& aRenderingContext,
  284. const nsRect& aDirtyRect,
  285. nsPoint aPt,
  286. uint32_t aFlags) override;
  287. private:
  288. // These are the entire width of the border (the cell edge contains only
  289. // the inner half, per the macros in nsTablePainter.h).
  290. BCPixelSize mBStartBorder;
  291. BCPixelSize mIEndBorder;
  292. BCPixelSize mBEndBorder;
  293. BCPixelSize mIStartBorder;
  294. };
  295. // Implemented here because that's a sane-ish way to make the includes work out.
  296. inline nsTableCellFrame* nsTableRowFrame::GetFirstCell() const
  297. {
  298. nsIFrame* firstChild = mFrames.FirstChild();
  299. #ifdef DEBUG
  300. if (firstChild) {
  301. nsTableCellFrame* cellFrame = do_QueryFrame(firstChild);
  302. MOZ_ASSERT(cellFrame, "How do we have a non-cell sibling?");
  303. }
  304. #endif // DEBUG
  305. return static_cast<nsTableCellFrame*>(firstChild);
  306. }
  307. #endif