nsTreeColumns.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 nsTreeColumns_h__
  6. #define nsTreeColumns_h__
  7. #include "nsITreeColumns.h"
  8. #include "nsITreeBoxObject.h"
  9. #include "mozilla/Attributes.h"
  10. #include "nsCoord.h"
  11. #include "nsCycleCollectionParticipant.h"
  12. #include "nsWrapperCache.h"
  13. #include "nsString.h"
  14. class nsTreeBodyFrame;
  15. class nsTreeColumns;
  16. class nsIFrame;
  17. class nsIContent;
  18. struct nsRect;
  19. namespace mozilla {
  20. class ErrorResult;
  21. namespace dom {
  22. class Element;
  23. class TreeBoxObject;
  24. } // namespace dom
  25. } // namespace mozilla
  26. #define NS_TREECOLUMN_IMPL_CID \
  27. { /* 02cd1963-4b5d-4a6c-9223-814d3ade93a3 */ \
  28. 0x02cd1963, \
  29. 0x4b5d, \
  30. 0x4a6c, \
  31. {0x92, 0x23, 0x81, 0x4d, 0x3a, 0xde, 0x93, 0xa3} \
  32. }
  33. // This class is our column info. We use it to iterate our columns and to obtain
  34. // information about each column.
  35. class nsTreeColumn final : public nsITreeColumn
  36. , public nsWrapperCache
  37. {
  38. public:
  39. nsTreeColumn(nsTreeColumns* aColumns, nsIContent* aContent);
  40. NS_DECLARE_STATIC_IID_ACCESSOR(NS_TREECOLUMN_IMPL_CID)
  41. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  42. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsTreeColumn)
  43. NS_DECL_NSITREECOLUMN
  44. // WebIDL
  45. nsIContent* GetParentObject() const;
  46. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  47. mozilla::dom::Element* GetElement(mozilla::ErrorResult& aRv);
  48. nsTreeColumns* GetColumns() const { return mColumns; }
  49. int32_t GetX(mozilla::ErrorResult& aRv);
  50. int32_t GetWidth(mozilla::ErrorResult& aRv);
  51. // GetId is fine
  52. int32_t Index() const { return mIndex; }
  53. bool Primary() const { return mIsPrimary; }
  54. bool Cycler() const { return mIsCycler; }
  55. bool Editable() const { return mIsEditable; }
  56. bool Selectable() const { return mIsSelectable; }
  57. int16_t Type() const { return mType; }
  58. nsTreeColumn* GetNext() const { return mNext; }
  59. nsTreeColumn* GetPrevious() const { return mPrevious; }
  60. void Invalidate(mozilla::ErrorResult& aRv);
  61. friend class nsTreeBodyFrame;
  62. friend class nsTreeColumns;
  63. protected:
  64. ~nsTreeColumn();
  65. nsIFrame* GetFrame();
  66. nsIFrame* GetFrame(nsTreeBodyFrame* aBodyFrame);
  67. // Don't call this if GetWidthInTwips or GetRect fails
  68. bool IsLastVisible(nsTreeBodyFrame* aBodyFrame);
  69. /**
  70. * Returns a rect with x and width taken from the frame's rect and specified
  71. * y and height. May fail in case there's no frame for the column.
  72. */
  73. nsresult GetRect(nsTreeBodyFrame* aBodyFrame, nscoord aY, nscoord aHeight,
  74. nsRect* aResult);
  75. nsresult GetXInTwips(nsTreeBodyFrame* aBodyFrame, nscoord* aResult);
  76. nsresult GetWidthInTwips(nsTreeBodyFrame* aBodyFrame, nscoord* aResult);
  77. void SetColumns(nsTreeColumns* aColumns) { mColumns = aColumns; }
  78. const nsAString& GetId() { return mId; }
  79. nsIAtom* GetAtom() { return mAtom; }
  80. int32_t GetIndex() { return mIndex; }
  81. bool IsPrimary() { return mIsPrimary; }
  82. bool IsCycler() { return mIsCycler; }
  83. bool IsEditable() { return mIsEditable; }
  84. bool IsSelectable() { return mIsSelectable; }
  85. bool Overflow() { return mOverflow; }
  86. int16_t GetType() { return mType; }
  87. int8_t GetCropStyle() { return mCropStyle; }
  88. int32_t GetTextAlignment() { return mTextAlignment; }
  89. void SetNext(nsTreeColumn* aNext) {
  90. NS_ASSERTION(!mNext, "already have a next sibling");
  91. mNext = aNext;
  92. }
  93. void SetPrevious(nsTreeColumn* aPrevious) { mPrevious = aPrevious; }
  94. private:
  95. /**
  96. * Non-null nsIContent for the associated <treecol> element.
  97. */
  98. nsCOMPtr<nsIContent> mContent;
  99. nsTreeColumns* mColumns;
  100. nsString mId;
  101. nsCOMPtr<nsIAtom> mAtom;
  102. int32_t mIndex;
  103. bool mIsPrimary;
  104. bool mIsCycler;
  105. bool mIsEditable;
  106. bool mIsSelectable;
  107. bool mOverflow;
  108. int16_t mType;
  109. int8_t mCropStyle;
  110. int8_t mTextAlignment;
  111. RefPtr<nsTreeColumn> mNext;
  112. nsTreeColumn* mPrevious;
  113. };
  114. NS_DEFINE_STATIC_IID_ACCESSOR(nsTreeColumn, NS_TREECOLUMN_IMPL_CID)
  115. class nsTreeColumns final : public nsITreeColumns
  116. , public nsWrapperCache
  117. {
  118. private:
  119. ~nsTreeColumns();
  120. public:
  121. explicit nsTreeColumns(nsTreeBodyFrame* aTree);
  122. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  123. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsTreeColumns)
  124. NS_DECL_NSITREECOLUMNS
  125. nsIContent* GetParentObject() const;
  126. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  127. // WebIDL
  128. mozilla::dom::TreeBoxObject* GetTree() const;
  129. uint32_t Count();
  130. uint32_t Length()
  131. {
  132. return Count();
  133. }
  134. nsTreeColumn* GetFirstColumn() { EnsureColumns(); return mFirstColumn; }
  135. nsTreeColumn* GetLastColumn();
  136. nsTreeColumn* GetPrimaryColumn();
  137. nsTreeColumn* GetSortedColumn();
  138. nsTreeColumn* GetKeyColumn();
  139. nsTreeColumn* GetColumnFor(mozilla::dom::Element* aElement);
  140. nsTreeColumn* IndexedGetter(uint32_t aIndex, bool& aFound);
  141. nsTreeColumn* GetColumnAt(uint32_t aIndex);
  142. nsTreeColumn* NamedGetter(const nsAString& aId, bool& aFound);
  143. nsTreeColumn* GetNamedColumn(const nsAString& aId);
  144. void GetSupportedNames(nsTArray<nsString>& aNames);
  145. // Uses XPCOM InvalidateColumns().
  146. // Uses XPCOM RestoreNaturalOrder().
  147. friend class nsTreeBodyFrame;
  148. protected:
  149. void SetTree(nsTreeBodyFrame* aTree) { mTree = aTree; }
  150. // Builds our cache of column info.
  151. void EnsureColumns();
  152. private:
  153. nsTreeBodyFrame* mTree;
  154. /**
  155. * The first column in the list of columns. All of the columns are supposed
  156. * to be "alive", i.e. have a frame. This is achieved by clearing the columns
  157. * list each time an nsTreeColFrame is destroyed.
  158. *
  159. * XXX this means that new nsTreeColumn objects are unnecessarily created
  160. * for untouched columns.
  161. */
  162. RefPtr<nsTreeColumn> mFirstColumn;
  163. };
  164. #endif // nsTreeColumns_h__