Bug 1874684 - Part 37: Fix unified compilation. r=allstarschh
[gecko.git] / layout / forms / nsListControlFrame.h
blob7c43eb6f2e623f315864badf372a852236e4cb80
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsListControlFrame_h___
7 #define nsListControlFrame_h___
9 #ifdef DEBUG_evaughan
10 // #define DEBUG_rods
11 #endif
13 #ifdef DEBUG_rods
14 // #define DO_REFLOW_DEBUG
15 // #define DO_REFLOW_COUNTER
16 // #define DO_UNCONSTRAINED_CHECK
17 // #define DO_PIXELS
18 #endif
20 #include "mozilla/Attributes.h"
21 #include "mozilla/StaticPtr.h"
22 #include "nsGfxScrollFrame.h"
23 #include "nsIFormControlFrame.h"
24 #include "nsISelectControlFrame.h"
25 #include "nsSelectsAreaFrame.h"
27 class nsComboboxControlFrame;
28 class nsPresContext;
30 namespace mozilla {
31 class PresShell;
32 class HTMLSelectEventListener;
34 namespace dom {
35 class Event;
36 class HTMLOptionElement;
37 class HTMLSelectElement;
38 class HTMLOptionsCollection;
39 } // namespace dom
40 } // namespace mozilla
42 /**
43 * Frame-based listbox.
46 class nsListControlFrame final : public nsHTMLScrollFrame,
47 public nsIFormControlFrame,
48 public nsISelectControlFrame {
49 public:
50 typedef mozilla::dom::HTMLOptionElement HTMLOptionElement;
52 friend nsListControlFrame* NS_NewListControlFrame(
53 mozilla::PresShell* aPresShell, ComputedStyle* aStyle);
55 NS_DECL_QUERYFRAME
56 NS_DECL_FRAMEARENA_HELPERS(nsListControlFrame)
58 Maybe<nscoord> GetNaturalBaselineBOffset(
59 mozilla::WritingMode aWM, BaselineSharingGroup aBaselineGroup,
60 BaselineExportContext) const override;
62 // nsIFrame
63 nsresult HandleEvent(nsPresContext* aPresContext,
64 mozilla::WidgetGUIEvent* aEvent,
65 nsEventStatus* aEventStatus) final;
67 void SetInitialChildList(ChildListID aListID, nsFrameList&& aChildList) final;
69 nscoord GetPrefISize(gfxContext* aRenderingContext) final;
70 nscoord GetMinISize(gfxContext* aRenderingContext) final;
72 void Reflow(nsPresContext* aCX, ReflowOutput& aDesiredSize,
73 const ReflowInput& aReflowInput, nsReflowStatus& aStatus) final;
75 void Init(nsIContent* aContent, nsContainerFrame* aParent,
76 nsIFrame* aPrevInFlow) final;
78 void DidReflow(nsPresContext* aPresContext,
79 const ReflowInput* aReflowInput) final;
80 void Destroy(DestroyContext&) override;
82 void BuildDisplayList(nsDisplayListBuilder* aBuilder,
83 const nsDisplayListSet& aLists) final;
85 nsContainerFrame* GetContentInsertionFrame() final;
87 int32_t GetEndSelectionIndex() const { return mEndSelectionIndex; }
89 mozilla::dom::HTMLOptionElement* GetCurrentOption() const;
91 #ifdef DEBUG_FRAME_DUMP
92 nsresult GetFrameName(nsAString& aResult) const final;
93 #endif
95 // nsIFormControlFrame
96 nsresult SetFormProperty(nsAtom* aName, const nsAString& aValue) final;
97 MOZ_CAN_RUN_SCRIPT_BOUNDARY
98 void SetFocus(bool aOn = true, bool aRepaint = false) final;
100 bool ShouldPropagateComputedBSizeToScrolledContent() const final;
102 // for accessibility purposes
103 #ifdef ACCESSIBILITY
104 mozilla::a11y::AccType AccessibleType() final;
105 #endif
107 int32_t GetSelectedIndex();
110 * Gets the text of the currently selected item.
111 * If the there are zero items then an empty string is returned
112 * If there is nothing selected, then the 0th item's text is returned.
114 void GetOptionText(uint32_t aIndex, nsAString& aStr);
116 void CaptureMouseEvents(bool aGrabMouseEvents);
117 nscoord GetBSizeOfARow();
118 uint32_t GetNumberOfOptions();
120 MOZ_CAN_RUN_SCRIPT_BOUNDARY void OnContentReset();
122 // nsISelectControlFrame
123 NS_IMETHOD AddOption(int32_t index) final;
124 NS_IMETHOD RemoveOption(int32_t index) final;
125 MOZ_CAN_RUN_SCRIPT_BOUNDARY
126 NS_IMETHOD DoneAddingChildren(bool aIsDone) final;
129 * Gets the content (an option) by index and then set it as
130 * being selected or not selected.
132 MOZ_CAN_RUN_SCRIPT_BOUNDARY
133 NS_IMETHOD OnOptionSelected(int32_t aIndex, bool aSelected) final;
134 MOZ_CAN_RUN_SCRIPT_BOUNDARY
135 NS_IMETHOD_(void)
136 OnSetSelectedIndex(int32_t aOldIndex, int32_t aNewIndex) final;
139 * Mouse event listeners.
140 * @note These methods might destroy the frame, pres shell and other objects.
142 MOZ_CAN_RUN_SCRIPT
143 nsresult HandleLeftButtonMouseDown(mozilla::dom::Event* aMouseEvent);
144 MOZ_CAN_RUN_SCRIPT
145 nsresult HandleLeftButtonMouseUp(mozilla::dom::Event* aMouseEvent);
146 MOZ_CAN_RUN_SCRIPT
147 nsresult DragMove(mozilla::dom::Event* aMouseEvent);
148 MOZ_CAN_RUN_SCRIPT
150 MOZ_CAN_RUN_SCRIPT
151 bool PerformSelection(int32_t aClickedIndex, bool aIsShift, bool aIsControl);
152 MOZ_CAN_RUN_SCRIPT
153 void UpdateSelectionAfterKeyEvent(int32_t aNewIndex, uint32_t aCharCode,
154 bool aIsShift, bool aIsControlOrMeta,
155 bool aIsControlSelectMode);
158 * Returns the options collection for mContent, if any.
160 mozilla::dom::HTMLOptionsCollection* GetOptions() const;
162 * Returns the HTMLOptionElement for a given index in mContent's collection.
164 HTMLOptionElement* GetOption(uint32_t aIndex) const;
166 // Helper
167 bool IsFocused() { return this == mFocused; }
170 * Function to paint the focus rect when our nsSelectsAreaFrame is painting.
171 * @param aPt the offset of this frame, relative to the rendering reference
172 * frame
174 void PaintFocus(mozilla::gfx::DrawTarget* aDrawTarget, nsPoint aPt);
177 * If this frame IsFocused(), invalidates an area that includes anything
178 * that PaintFocus will or could have painted --- basically the whole
179 * GetOptionsContainer, plus some extra stuff if there are no options. This
180 * must be called every time mEndSelectionIndex changes.
182 void InvalidateFocus();
185 * Function to calculate the block size of a row, for use with the
186 * "size" attribute.
187 * Can't be const because GetNumberOfOptions() isn't const.
189 nscoord CalcBSizeOfARow();
192 * Function to ask whether we're currently in what might be the
193 * first pass of a two-pass reflow.
195 bool MightNeedSecondPass() const { return mMightNeedSecondPass; }
197 void SetSuppressScrollbarUpdate(bool aSuppress) {
198 nsHTMLScrollFrame::SetSuppressScrollbarUpdate(aSuppress);
202 * Return the number of displayed rows in the list.
204 uint32_t GetNumDisplayRows() const { return mNumDisplayRows; }
206 #ifdef ACCESSIBILITY
208 * Post a custom DOM event for the change, so that accessibility can
209 * fire a native focus event for accessibility
210 * (Some 3rd party products need to track our focus)
212 void FireMenuItemActiveEvent(
213 nsIContent* aPreviousOption); // Inform assistive tech what got focused
214 #endif
216 protected:
218 * Updates the selected text in a combobox and then calls FireOnChange().
219 * @note This method might destroy the frame, pres shell and other objects.
220 * Returns false if calling it destroyed |this|.
222 MOZ_CAN_RUN_SCRIPT
223 bool UpdateSelection();
226 * Returns whether mContent supports multiple selection.
228 bool GetMultiple() const {
229 return mContent->AsElement()->HasAttr(nsGkAtoms::multiple);
232 mozilla::dom::HTMLSelectElement& Select() const;
235 * @return true if the <option> at aIndex is selectable by the user.
237 bool IsOptionInteractivelySelectable(int32_t aIndex) const;
239 * @return true if aOption in aSelect is selectable by the user.
241 static bool IsOptionInteractivelySelectable(
242 mozilla::dom::HTMLSelectElement* aSelect,
243 mozilla::dom::HTMLOptionElement* aOption);
245 MOZ_CAN_RUN_SCRIPT void ScrollToFrame(HTMLOptionElement& aOptElement);
247 MOZ_CAN_RUN_SCRIPT void ScrollToIndex(int32_t anIndex);
249 public:
251 * Resets the select back to it's original default values;
252 * those values as determined by the original HTML
254 MOZ_CAN_RUN_SCRIPT void ResetList(bool aAllowScrolling);
256 protected:
257 explicit nsListControlFrame(ComputedStyle* aStyle,
258 nsPresContext* aPresContext);
259 virtual ~nsListControlFrame();
262 * Sets the mSelectedIndex and mOldSelectedIndex from figuring out what
263 * item was selected using content
264 * @param aPoint the event point, in listcontrolframe coordinates
265 * @return NS_OK if it successfully found the selection
267 nsresult GetIndexFromDOMEvent(mozilla::dom::Event* aMouseEvent,
268 int32_t& aCurIndex);
270 bool CheckIfAllFramesHere();
272 // guess at a row block size based on our own style.
273 nscoord CalcFallbackRowBSize(float aFontSizeInflation);
275 // CalcIntrinsicBSize computes our intrinsic block size (taking the
276 // "size" attribute into account). This should only be called in
277 // non-dropdown mode.
278 nscoord CalcIntrinsicBSize(nscoord aBSizeOfARow, int32_t aNumberOfOptions);
280 // Dropped down stuff
281 void SetComboboxItem(int32_t aIndex);
283 // Selection
284 bool SetOptionsSelectedFromFrame(int32_t aStartIndex, int32_t aEndIndex,
285 bool aValue, bool aClearAll);
286 bool ToggleOptionSelectedFromFrame(int32_t aIndex);
288 MOZ_CAN_RUN_SCRIPT
289 bool SingleSelection(int32_t aClickedIndex, bool aDoToggle);
290 bool ExtendedSelection(int32_t aStartIndex, int32_t aEndIndex,
291 bool aClearAll);
292 MOZ_CAN_RUN_SCRIPT
293 bool HandleListSelection(mozilla::dom::Event* aDOMEvent,
294 int32_t selectedIndex);
295 void InitSelectionRange(int32_t aClickedIndex);
297 public:
298 nsSelectsAreaFrame* GetOptionsContainer() const {
299 return static_cast<nsSelectsAreaFrame*>(GetScrolledFrame());
302 static constexpr int32_t kNothingSelected = -1;
304 protected:
305 nscoord BSizeOfARow() { return GetOptionsContainer()->BSizeOfARow(); }
308 * @return how many displayable options/optgroups this frame has.
310 uint32_t GetNumberOfRows();
312 // Data Members
313 int32_t mStartSelectionIndex;
314 int32_t mEndSelectionIndex;
316 uint32_t mNumDisplayRows;
317 bool mChangesSinceDragStart : 1;
319 // Has the user selected a visible item since we showed the dropdown?
320 bool mItemSelectionStarted : 1;
322 bool mIsAllContentHere : 1;
323 bool mIsAllFramesHere : 1;
324 bool mHasBeenInitialized : 1;
325 bool mNeedToReset : 1;
326 bool mPostChildrenLoadedReset : 1;
328 // True if we're in the middle of a reflow and might need a second
329 // pass. This only happens for auto heights.
330 bool mMightNeedSecondPass : 1;
333 * Set to aPresContext->HasPendingInterrupt() at the start of Reflow.
334 * Set to false at the end of DidReflow.
336 bool mHasPendingInterruptAtStartOfReflow : 1;
338 // True if the selection can be set to nothing or disabled options.
339 bool mForceSelection : 1;
341 RefPtr<mozilla::HTMLSelectEventListener> mEventListener;
343 static nsListControlFrame* mFocused;
345 #ifdef DO_REFLOW_COUNTER
346 int32_t mReflowId;
347 #endif
350 #endif /* nsListControlFrame_h___ */