Blame layout/forms/nsComboboxControlFrame.cpp

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit f0b94e
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
#include "nsComboboxControlFrame.h"
Packit f0b94e
Packit f0b94e
#include "gfxContext.h"
Packit f0b94e
#include "gfxUtils.h"
Packit f0b94e
#include "mozilla/gfx/2D.h"
Packit f0b94e
#include "mozilla/gfx/PathHelpers.h"
Packit f0b94e
#include "nsCOMPtr.h"
Packit f0b94e
#include "nsFocusManager.h"
Packit f0b94e
#include "nsCheckboxRadioFrame.h"
Packit f0b94e
#include "nsGkAtoms.h"
Packit f0b94e
#include "nsCSSAnonBoxes.h"
Packit f0b94e
#include "nsHTMLParts.h"
Packit f0b94e
#include "nsIFormControl.h"
Packit f0b94e
#include "nsNameSpaceManager.h"
Packit f0b94e
#include "nsIListControlFrame.h"
Packit f0b94e
#include "nsPIDOMWindow.h"
Packit f0b94e
#include "nsIPresShell.h"
Packit f0b94e
#include "nsPresState.h"
Packit f0b94e
#include "nsView.h"
Packit f0b94e
#include "nsViewManager.h"
Packit f0b94e
#include "nsIContentInlines.h"
Packit f0b94e
#include "nsIDOMEventListener.h"
Packit f0b94e
#include "nsIDOMNode.h"
Packit f0b94e
#include "nsISelectControlFrame.h"
Packit f0b94e
#include "nsContentUtils.h"
Packit f0b94e
#include "mozilla/dom/HTMLSelectElement.h"
Packit f0b94e
#include "nsIDocument.h"
Packit f0b94e
#include "nsIScrollableFrame.h"
Packit f0b94e
#include "nsListControlFrame.h"
Packit f0b94e
#include "mozilla/StyleSetHandle.h"
Packit f0b94e
#include "mozilla/StyleSetHandleInlines.h"
Packit f0b94e
#include "nsNodeInfoManager.h"
Packit f0b94e
#include "nsContentCreatorFunctions.h"
Packit f0b94e
#include "nsLayoutUtils.h"
Packit f0b94e
#include "nsDisplayList.h"
Packit f0b94e
#include "nsITheme.h"
Packit f0b94e
#include "nsThemeConstants.h"
Packit f0b94e
#include "mozilla/Likely.h"
Packit f0b94e
#include <algorithm>
Packit f0b94e
#include "nsTextNode.h"
Packit f0b94e
#include "mozilla/AsyncEventDispatcher.h"
Packit f0b94e
#include "mozilla/EventStates.h"
Packit f0b94e
#include "mozilla/LookAndFeel.h"
Packit f0b94e
#include "mozilla/MouseEvents.h"
Packit f0b94e
#include "mozilla/Unused.h"
Packit f0b94e
#include "gfx2DGlue.h"
Packit f0b94e
#include "mozilla/widget/nsAutoRollup.h"
Packit f0b94e
Packit f0b94e
#ifdef XP_WIN
Packit f0b94e
#define COMBOBOX_ROLLUP_CONSUME_EVENT 0
Packit f0b94e
#else
Packit f0b94e
#define COMBOBOX_ROLLUP_CONSUME_EVENT 1
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
using namespace mozilla;
Packit f0b94e
using namespace mozilla::gfx;
Packit f0b94e
Packit f0b94e
NS_IMETHODIMP
Packit f0b94e
nsComboboxControlFrame::RedisplayTextEvent::Run() {
Packit f0b94e
  if (mControlFrame) mControlFrame->HandleRedisplayTextEvent();
Packit f0b94e
  return NS_OK;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
class nsPresState;
Packit f0b94e
Packit f0b94e
#define FIX_FOR_BUG_53259
Packit f0b94e
Packit f0b94e
// Drop down list event management.
Packit f0b94e
// The combo box uses the following strategy for managing the drop-down list.
Packit f0b94e
// If the combo box or its arrow button is clicked on the drop-down list is
Packit f0b94e
// displayed If mouse exits the combo box with the drop-down list displayed the
Packit f0b94e
// drop-down list is asked to capture events The drop-down list will capture all
Packit f0b94e
// events including mouse down and up and will always return with
Packit f0b94e
// ListWasSelected method call regardless of whether an item in the list was
Packit f0b94e
// actually selected.
Packit f0b94e
// The ListWasSelected code will turn off mouse-capture for the drop-down list.
Packit f0b94e
// The drop-down list does not explicitly set capture when it is in the
Packit f0b94e
// drop-down mode.
Packit f0b94e
Packit f0b94e
/**
Packit f0b94e
 * Helper class that listens to the combo boxes button. If the button is pressed
Packit f0b94e
 * the combo box is toggled to open or close. this is used by Accessibility
Packit f0b94e
 * which presses that button Programmatically.
Packit f0b94e
 */
Packit f0b94e
class nsComboButtonListener : public nsIDOMEventListener {
Packit f0b94e
 private:
Packit f0b94e
  virtual ~nsComboButtonListener() {}
Packit f0b94e
Packit f0b94e
 public:
Packit f0b94e
  NS_DECL_ISUPPORTS
Packit f0b94e
Packit f0b94e
  NS_IMETHOD HandleEvent(nsIDOMEvent*) override {
Packit f0b94e
    mComboBox->ShowDropDown(!mComboBox->IsDroppedDown());
Packit f0b94e
    return NS_OK;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  explicit nsComboButtonListener(nsComboboxControlFrame* aCombobox) {
Packit f0b94e
    mComboBox = aCombobox;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  nsComboboxControlFrame* mComboBox;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
NS_IMPL_ISUPPORTS(nsComboButtonListener, nsIDOMEventListener)
Packit f0b94e
Packit f0b94e
// static class data member for Bug 32920
Packit f0b94e
nsComboboxControlFrame* nsComboboxControlFrame::sFocused = nullptr;
Packit f0b94e
Packit f0b94e
nsComboboxControlFrame* NS_NewComboboxControlFrame(nsIPresShell* aPresShell,
Packit f0b94e
                                                   nsStyleContext* aContext,
Packit f0b94e
                                                   nsFrameState aStateFlags) {
Packit f0b94e
  nsComboboxControlFrame* it =
Packit f0b94e
      new (aPresShell) nsComboboxControlFrame(aContext);
Packit f0b94e
Packit f0b94e
  if (it) {
Packit f0b94e
    // set the state flags (if any are provided)
Packit f0b94e
    it->AddStateBits(aStateFlags);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  return it;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
NS_IMPL_FRAMEARENA_HELPERS(nsComboboxControlFrame)
Packit f0b94e
Packit f0b94e
//-----------------------------------------------------------
Packit f0b94e
// Reflow Debugging Macros
Packit f0b94e
// These let us "see" how many reflow counts are happening
Packit f0b94e
//-----------------------------------------------------------
Packit f0b94e
#ifdef DO_REFLOW_COUNTER
Packit f0b94e
Packit f0b94e
#define MAX_REFLOW_CNT 1024
Packit f0b94e
static int32_t gTotalReqs = 0;
Packit f0b94e
;
Packit f0b94e
static int32_t gTotalReflows = 0;
Packit f0b94e
;
Packit f0b94e
static int32_t gReflowControlCntRQ[MAX_REFLOW_CNT];
Packit f0b94e
static int32_t gReflowControlCnt[MAX_REFLOW_CNT];
Packit f0b94e
static int32_t gReflowInx = -1;
Packit f0b94e
Packit f0b94e
#define REFLOW_COUNTER() \
Packit f0b94e
  if (mReflowId > -1) gReflowControlCnt[mReflowId]++;
Packit f0b94e
Packit f0b94e
#define REFLOW_COUNTER_REQUEST() \
Packit f0b94e
  if (mReflowId > -1) gReflowControlCntRQ[mReflowId]++;
Packit f0b94e
Packit f0b94e
#define REFLOW_COUNTER_DUMP(__desc)                                            \
Packit f0b94e
  if (mReflowId > -1) {                                                        \
Packit f0b94e
    gTotalReqs += gReflowControlCntRQ[mReflowId];                              \
Packit f0b94e
    gTotalReflows += gReflowControlCnt[mReflowId];                             \
Packit f0b94e
    printf("** Id:%5d %s RF: %d RQ: %d   %d/%d  %5.2f\n", mReflowId, (__desc), \
Packit f0b94e
           gReflowControlCnt[mReflowId], gReflowControlCntRQ[mReflowId],       \
Packit f0b94e
           gTotalReflows, gTotalReqs,                                          \
Packit f0b94e
           float(gTotalReflows) / float(gTotalReqs) * 100.0f);                 \
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
#define REFLOW_COUNTER_INIT()           \
Packit f0b94e
  if (gReflowInx < MAX_REFLOW_CNT) {    \
Packit f0b94e
    gReflowInx++;                       \
Packit f0b94e
    mReflowId = gReflowInx;             \
Packit f0b94e
    gReflowControlCnt[mReflowId] = 0;   \
Packit f0b94e
    gReflowControlCntRQ[mReflowId] = 0; \
Packit f0b94e
  } else {                              \
Packit f0b94e
    mReflowId = -1;                     \
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
// reflow messages
Packit f0b94e
#define REFLOW_DEBUG_MSG(_msg1) printf((_msg1))
Packit f0b94e
#define REFLOW_DEBUG_MSG2(_msg1, _msg2) printf((_msg1), (_msg2))
Packit f0b94e
#define REFLOW_DEBUG_MSG3(_msg1, _msg2, _msg3) printf((_msg1), (_msg2), (_msg3))
Packit f0b94e
#define REFLOW_DEBUG_MSG4(_msg1, _msg2, _msg3, _msg4) \
Packit f0b94e
  printf((_msg1), (_msg2), (_msg3), (_msg4))
Packit f0b94e
Packit f0b94e
#else  //-------------
Packit f0b94e
Packit f0b94e
#define REFLOW_COUNTER_REQUEST()
Packit f0b94e
#define REFLOW_COUNTER()
Packit f0b94e
#define REFLOW_COUNTER_DUMP(__desc)
Packit f0b94e
#define REFLOW_COUNTER_INIT()
Packit f0b94e
Packit f0b94e
#define REFLOW_DEBUG_MSG(_msg)
Packit f0b94e
#define REFLOW_DEBUG_MSG2(_msg1, _msg2)
Packit f0b94e
#define REFLOW_DEBUG_MSG3(_msg1, _msg2, _msg3)
Packit f0b94e
#define REFLOW_DEBUG_MSG4(_msg1, _msg2, _msg3, _msg4)
Packit f0b94e
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
//------------------------------------------
Packit f0b94e
// This is for being VERY noisy
Packit f0b94e
//------------------------------------------
Packit f0b94e
#ifdef DO_VERY_NOISY
Packit f0b94e
#define REFLOW_NOISY_MSG(_msg1) printf((_msg1))
Packit f0b94e
#define REFLOW_NOISY_MSG2(_msg1, _msg2) printf((_msg1), (_msg2))
Packit f0b94e
#define REFLOW_NOISY_MSG3(_msg1, _msg2, _msg3) printf((_msg1), (_msg2), (_msg3))
Packit f0b94e
#define REFLOW_NOISY_MSG4(_msg1, _msg2, _msg3, _msg4) \
Packit f0b94e
  printf((_msg1), (_msg2), (_msg3), (_msg4))
Packit f0b94e
#else
Packit f0b94e
#define REFLOW_NOISY_MSG(_msg)
Packit f0b94e
#define REFLOW_NOISY_MSG2(_msg1, _msg2)
Packit f0b94e
#define REFLOW_NOISY_MSG3(_msg1, _msg2, _msg3)
Packit f0b94e
#define REFLOW_NOISY_MSG4(_msg1, _msg2, _msg3, _msg4)
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
//------------------------------------------
Packit f0b94e
// Displays value in pixels or twips
Packit f0b94e
//------------------------------------------
Packit f0b94e
#ifdef DO_PIXELS
Packit f0b94e
#define PX(__v) __v / 15
Packit f0b94e
#else
Packit f0b94e
#define PX(__v) __v
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
//------------------------------------------------------
Packit f0b94e
//-- Done with macros
Packit f0b94e
//------------------------------------------------------
Packit f0b94e
Packit f0b94e
nsComboboxControlFrame::nsComboboxControlFrame(nsStyleContext* aContext)
Packit f0b94e
    : nsBlockFrame(aContext, kClassID),
Packit f0b94e
      mDisplayFrame(nullptr),
Packit f0b94e
      mButtonFrame(nullptr),
Packit f0b94e
      mDropdownFrame(nullptr),
Packit f0b94e
      mListControlFrame(nullptr),
Packit f0b94e
      mDisplayISize(0),
Packit f0b94e
      mRecentSelectedIndex(NS_SKIP_NOTIFY_INDEX),
Packit f0b94e
      mDisplayedIndex(-1),
Packit f0b94e
      mLastDropDownBeforeScreenBCoord(nscoord_MIN),
Packit f0b94e
      mLastDropDownAfterScreenBCoord(nscoord_MIN),
Packit f0b94e
      mDroppedDown(false),
Packit f0b94e
      mInRedisplayText(false),
Packit f0b94e
      mDelayedShowDropDown(false),
Packit f0b94e
      mIsOpenInParentProcess(false){REFLOW_COUNTER_INIT()}
Packit f0b94e
Packit f0b94e
      //--------------------------------------------------------------
Packit f0b94e
      nsComboboxControlFrame::~nsComboboxControlFrame() {
Packit f0b94e
  REFLOW_COUNTER_DUMP("nsCCF");
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
//--------------------------------------------------------------
Packit f0b94e
Packit f0b94e
NS_QUERYFRAME_HEAD(nsComboboxControlFrame)
Packit f0b94e
NS_QUERYFRAME_ENTRY(nsComboboxControlFrame)
Packit f0b94e
NS_QUERYFRAME_ENTRY(nsIComboboxControlFrame)
Packit f0b94e
NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
Packit f0b94e
NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
Packit f0b94e
NS_QUERYFRAME_ENTRY(nsISelectControlFrame)
Packit f0b94e
NS_QUERYFRAME_ENTRY(nsIStatefulFrame)
Packit f0b94e
NS_QUERYFRAME_TAIL_INHERITING(nsBlockFrame)
Packit f0b94e
Packit f0b94e
#ifdef ACCESSIBILITY
Packit f0b94e
a11y::AccType nsComboboxControlFrame::AccessibleType() {
Packit f0b94e
  return a11y::eHTMLComboboxType;
Packit f0b94e
}
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::SetFocus(bool aOn, bool aRepaint) {
Packit f0b94e
  AutoWeakFrame weakFrame(this);
Packit f0b94e
  if (aOn) {
Packit f0b94e
    nsListControlFrame::ComboboxFocusSet();
Packit f0b94e
    sFocused = this;
Packit f0b94e
    if (mDelayedShowDropDown) {
Packit f0b94e
      ShowDropDown(true);  // might destroy us
Packit f0b94e
      if (!weakFrame.IsAlive()) {
Packit f0b94e
        return;
Packit f0b94e
      }
Packit f0b94e
    }
Packit f0b94e
  } else {
Packit f0b94e
    sFocused = nullptr;
Packit f0b94e
    mDelayedShowDropDown = false;
Packit f0b94e
    if (mDroppedDown) {
Packit f0b94e
      mListControlFrame->ComboboxFinish(mDisplayedIndex);  // might destroy us
Packit f0b94e
      if (!weakFrame.IsAlive()) {
Packit f0b94e
        return;
Packit f0b94e
      }
Packit f0b94e
    }
Packit f0b94e
    // May delete |this|.
Packit f0b94e
    mListControlFrame->FireOnInputAndOnChange();
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  if (!weakFrame.IsAlive()) {
Packit f0b94e
    return;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // This is needed on a temporary basis. It causes the focus
Packit f0b94e
  // rect to be drawn. This is much faster than ReResolvingStyle
Packit f0b94e
  // Bug 32920
Packit f0b94e
  InvalidateFrame();
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::ShowPopup(bool aShowPopup) {
Packit f0b94e
  // TODO(kuoe0) Remove this function when content-select is enabled.
Packit f0b94e
Packit f0b94e
  nsView* view = mDropdownFrame->GetView();
Packit f0b94e
  nsViewManager* viewManager = view->GetViewManager();
Packit f0b94e
Packit f0b94e
  if (aShowPopup) {
Packit f0b94e
    nsRect rect = mDropdownFrame->GetRect();
Packit f0b94e
    rect.x = rect.y = 0;
Packit f0b94e
    viewManager->ResizeView(view, rect);
Packit f0b94e
    viewManager->SetViewVisibility(view, nsViewVisibility_kShow);
Packit f0b94e
  } else {
Packit f0b94e
    viewManager->SetViewVisibility(view, nsViewVisibility_kHide);
Packit f0b94e
    nsRect emptyRect(0, 0, 0, 0);
Packit f0b94e
    viewManager->ResizeView(view, emptyRect);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // fire a popup dom event if it is safe to do so
Packit f0b94e
  nsCOMPtr<nsIPresShell> shell = PresContext()->GetPresShell();
Packit f0b94e
  if (shell && nsContentUtils::IsSafeToRunScript()) {
Packit f0b94e
    nsEventStatus status = nsEventStatus_eIgnore;
Packit f0b94e
    WidgetMouseEvent event(true,
Packit f0b94e
                           aShowPopup ? eXULPopupShowing : eXULPopupHiding,
Packit f0b94e
                           nullptr, WidgetMouseEvent::eReal);
Packit f0b94e
Packit f0b94e
    shell->HandleDOMEventWithTarget(mContent, &event, &status);
Packit f0b94e
  }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
bool nsComboboxControlFrame::ShowList(bool aShowList) {
Packit f0b94e
  // TODO(kuoe0) Remove this function when content-select is enabled.
Packit f0b94e
  //
Packit f0b94e
  // This function is used to handle the widget/view stuff, so we just return
Packit f0b94e
  // when content-select is enabled. And the following callee, ShowPopup(), will
Packit f0b94e
  // also be ignored, it is only used to show and hide the widget.
Packit f0b94e
  if (nsLayoutUtils::IsContentSelectEnabled()) {
Packit f0b94e
    return true;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  nsView* view = mDropdownFrame->GetView();
Packit f0b94e
  if (aShowList) {
Packit f0b94e
    NS_ASSERTION(
Packit f0b94e
        !view->HasWidget(),
Packit f0b94e
        "We shouldn't have a widget before we need to display the popup");
Packit f0b94e
Packit f0b94e
    // Create the widget for the drop-down list
Packit f0b94e
    view->GetViewManager()->SetViewFloating(view, true);
Packit f0b94e
Packit f0b94e
    nsWidgetInitData widgetData;
Packit f0b94e
    widgetData.mWindowType = eWindowType_popup;
Packit f0b94e
    widgetData.mBorderStyle = eBorderStyle_default;
Packit f0b94e
    view->CreateWidgetForPopup(&widgetData);
Packit f0b94e
  } else {
Packit f0b94e
    nsIWidget* widget = view->GetWidget();
Packit f0b94e
    if (widget) {
Packit f0b94e
      // We must do this before ShowPopup in case it destroys us (bug 813442).
Packit f0b94e
      widget->CaptureRollupEvents(this, false);
Packit f0b94e
    }
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  AutoWeakFrame weakFrame(this);
Packit f0b94e
  ShowPopup(aShowList);  // might destroy us
Packit f0b94e
  if (!weakFrame.IsAlive()) {
Packit f0b94e
    return false;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  mDroppedDown = aShowList;
Packit f0b94e
  nsIWidget* widget = view->GetWidget();
Packit f0b94e
  if (mDroppedDown) {
Packit f0b94e
    // The listcontrol frame will call back to the nsComboboxControlFrame's
Packit f0b94e
    // ListWasSelected which will stop the capture.
Packit f0b94e
    mListControlFrame->AboutToDropDown();
Packit f0b94e
    mListControlFrame->CaptureMouseEvents(true);
Packit f0b94e
    if (widget) {
Packit f0b94e
      widget->CaptureRollupEvents(this, true);
Packit f0b94e
    }
Packit f0b94e
  } else {
Packit f0b94e
    if (widget) {
Packit f0b94e
      view->DestroyWidget();
Packit f0b94e
    }
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  return weakFrame.IsAlive();
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
class nsResizeDropdownAtFinalPosition final : public nsIReflowCallback,
Packit f0b94e
                                              public Runnable {
Packit f0b94e
 public:
Packit f0b94e
  explicit nsResizeDropdownAtFinalPosition(nsComboboxControlFrame* aFrame)
Packit f0b94e
      : mozilla::Runnable("nsResizeDropdownAtFinalPosition"), mFrame(aFrame) {}
Packit f0b94e
Packit f0b94e
 protected:
Packit f0b94e
  ~nsResizeDropdownAtFinalPosition() {}
Packit f0b94e
Packit f0b94e
 public:
Packit f0b94e
  virtual bool ReflowFinished() override {
Packit f0b94e
    Run();
Packit f0b94e
    NS_RELEASE_THIS();
Packit f0b94e
    return false;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  virtual void ReflowCallbackCanceled() override { NS_RELEASE_THIS(); }
Packit f0b94e
Packit f0b94e
  NS_IMETHOD Run() override {
Packit f0b94e
    if (mFrame.IsAlive()) {
Packit f0b94e
      static_cast<nsComboboxControlFrame*>(mFrame.GetFrame())
Packit f0b94e
          ->AbsolutelyPositionDropDown();
Packit f0b94e
    }
Packit f0b94e
    return NS_OK;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  WeakFrame mFrame;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::ReflowDropdown(nsPresContext* aPresContext,
Packit f0b94e
                                            const ReflowInput& aReflowInput) {
Packit f0b94e
  // All we want out of it later on, really, is the block size of a row, so we
Packit f0b94e
  // don't even need to cache mDropdownFrame's ascent or anything.  If we don't
Packit f0b94e
  // need to reflow it, just bail out here.
Packit f0b94e
  if (!aReflowInput.ShouldReflowAllKids() &&
Packit f0b94e
      !NS_SUBTREE_DIRTY(mDropdownFrame)) {
Packit f0b94e
    return;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // XXXbz this will, for small-block-size dropdowns, have extra space
Packit f0b94e
  // on the appropriate edge for the scrollbar we don't show... but
Packit f0b94e
  // that's the best we can do here for now.
Packit f0b94e
  WritingMode wm = mDropdownFrame->GetWritingMode();
Packit f0b94e
  LogicalSize availSize = aReflowInput.AvailableSize(wm);
Packit f0b94e
  availSize.BSize(wm) = NS_UNCONSTRAINEDSIZE;
Packit f0b94e
  ReflowInput kidReflowInput(aPresContext, aReflowInput, mDropdownFrame,
Packit f0b94e
                             availSize);
Packit f0b94e
Packit f0b94e
  // If the dropdown's intrinsic inline size is narrower than our
Packit f0b94e
  // specified inline size, then expand it out.  We want our border-box
Packit f0b94e
  // inline size to end up the same as the dropdown's so account for
Packit f0b94e
  // both sets of mComputedBorderPadding.
Packit f0b94e
  nscoord forcedISize =
Packit f0b94e
      aReflowInput.ComputedISize() +
Packit f0b94e
      aReflowInput.ComputedLogicalBorderPadding().IStartEnd(wm) -
Packit f0b94e
      kidReflowInput.ComputedLogicalBorderPadding().IStartEnd(wm);
Packit f0b94e
  kidReflowInput.SetComputedISize(
Packit f0b94e
      std::max(kidReflowInput.ComputedISize(), forcedISize));
Packit f0b94e
Packit f0b94e
  // ensure we start off hidden
Packit f0b94e
  if (!nsLayoutUtils::IsContentSelectEnabled() && !mDroppedDown &&
Packit f0b94e
      GetStateBits() & NS_FRAME_FIRST_REFLOW) {
Packit f0b94e
    nsView* view = mDropdownFrame->GetView();
Packit f0b94e
    nsViewManager* viewManager = view->GetViewManager();
Packit f0b94e
    viewManager->SetViewVisibility(view, nsViewVisibility_kHide);
Packit f0b94e
    nsRect emptyRect(0, 0, 0, 0);
Packit f0b94e
    viewManager->ResizeView(view, emptyRect);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // Allow the child to move/size/change-visibility its view if it's currently
Packit f0b94e
  // dropped down
Packit f0b94e
  int32_t flags = mDroppedDown
Packit f0b94e
                      ? 0
Packit f0b94e
                      : NS_FRAME_NO_MOVE_FRAME | NS_FRAME_NO_VISIBILITY |
Packit f0b94e
                            NS_FRAME_NO_SIZE_VIEW;
Packit f0b94e
Packit f0b94e
  // XXX Can this be different from the dropdown's writing mode?
Packit f0b94e
  // That would be odd!
Packit f0b94e
  // Note that we don't need to pass the true frame position or container size
Packit f0b94e
  // to ReflowChild or FinishReflowChild here; it will be positioned as needed
Packit f0b94e
  // by AbsolutelyPositionDropDown().
Packit f0b94e
  WritingMode outerWM = GetWritingMode();
Packit f0b94e
  const nsSize dummyContainerSize;
Packit f0b94e
  ReflowOutput desiredSize(aReflowInput);
Packit f0b94e
  nsReflowStatus ignoredStatus;
Packit f0b94e
  ReflowChild(mDropdownFrame, aPresContext, desiredSize, kidReflowInput,
Packit f0b94e
              outerWM, LogicalPoint(outerWM), dummyContainerSize, flags,
Packit f0b94e
              ignoredStatus);
Packit f0b94e
Packit f0b94e
  // Set the child's width and height to its desired size
Packit f0b94e
  FinishReflowChild(mDropdownFrame, aPresContext, desiredSize, &kidReflowInput,
Packit f0b94e
                    outerWM, LogicalPoint(outerWM), dummyContainerSize, flags);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
nsPoint nsComboboxControlFrame::GetCSSTransformTranslation() {
Packit f0b94e
  nsIFrame* frame = this;
Packit f0b94e
  bool is3DTransform = false;
Packit f0b94e
  Matrix transform;
Packit f0b94e
  while (frame) {
Packit f0b94e
    nsIFrame* parent;
Packit f0b94e
    Matrix4x4Flagged ctm = frame->GetTransformMatrix(nullptr, &parent);
Packit f0b94e
    Matrix matrix;
Packit f0b94e
    if (ctm.Is2D(&matrix)) {
Packit f0b94e
      transform = transform * matrix;
Packit f0b94e
    } else {
Packit f0b94e
      is3DTransform = true;
Packit f0b94e
      break;
Packit f0b94e
    }
Packit f0b94e
    frame = parent;
Packit f0b94e
  }
Packit f0b94e
  nsPoint translation;
Packit f0b94e
  if (!is3DTransform && !transform.HasNonTranslation()) {
Packit f0b94e
    nsPresContext* pc = PresContext();
Packit f0b94e
    // To get the translation introduced only by transforms we subtract the
Packit f0b94e
    // regular non-transform translation.
Packit f0b94e
    nsRootPresContext* rootPC = pc->GetRootPresContext();
Packit f0b94e
    if (rootPC) {
Packit f0b94e
      int32_t apd = pc->AppUnitsPerDevPixel();
Packit f0b94e
      translation.x = NSFloatPixelsToAppUnits(transform._31, apd);
Packit f0b94e
      translation.y = NSFloatPixelsToAppUnits(transform._32, apd);
Packit f0b94e
      translation -= GetOffsetToCrossDoc(rootPC->PresShell()->GetRootFrame());
Packit f0b94e
    }
Packit f0b94e
  }
Packit f0b94e
  return translation;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
class nsAsyncRollup : public Runnable {
Packit f0b94e
 public:
Packit f0b94e
  explicit nsAsyncRollup(nsComboboxControlFrame* aFrame)
Packit f0b94e
      : mozilla::Runnable("nsAsyncRollup"), mFrame(aFrame) {}
Packit f0b94e
  NS_IMETHOD Run() override {
Packit f0b94e
    if (mFrame.IsAlive()) {
Packit f0b94e
      static_cast<nsComboboxControlFrame*>(mFrame.GetFrame())->RollupFromList();
Packit f0b94e
    }
Packit f0b94e
    return NS_OK;
Packit f0b94e
  }
Packit f0b94e
  WeakFrame mFrame;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
class nsAsyncResize : public Runnable {
Packit f0b94e
 public:
Packit f0b94e
  explicit nsAsyncResize(nsComboboxControlFrame* aFrame)
Packit f0b94e
      : mozilla::Runnable("nsAsyncResize"), mFrame(aFrame) {}
Packit f0b94e
  NS_IMETHOD Run() override {
Packit f0b94e
    if (mFrame.IsAlive()) {
Packit f0b94e
      nsComboboxControlFrame* combo =
Packit f0b94e
          static_cast<nsComboboxControlFrame*>(mFrame.GetFrame());
Packit f0b94e
      static_cast<nsListControlFrame*>(combo->mDropdownFrame)
Packit f0b94e
          ->SetSuppressScrollbarUpdate(true);
Packit f0b94e
      nsCOMPtr<nsIPresShell> shell = mFrame->PresShell();
Packit f0b94e
      shell->FrameNeedsReflow(combo->mDropdownFrame, nsIPresShell::eResize,
Packit f0b94e
                              NS_FRAME_IS_DIRTY);
Packit f0b94e
      shell->FlushPendingNotifications(FlushType::Layout);
Packit f0b94e
      if (mFrame.IsAlive()) {
Packit f0b94e
        combo = static_cast<nsComboboxControlFrame*>(mFrame.GetFrame());
Packit f0b94e
        static_cast<nsListControlFrame*>(combo->mDropdownFrame)
Packit f0b94e
            ->SetSuppressScrollbarUpdate(false);
Packit f0b94e
        if (combo->mDelayedShowDropDown) {
Packit f0b94e
          combo->ShowDropDown(true);
Packit f0b94e
        }
Packit f0b94e
      }
Packit f0b94e
    }
Packit f0b94e
    return NS_OK;
Packit f0b94e
  }
Packit f0b94e
  WeakFrame mFrame;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::GetAvailableDropdownSpace(
Packit f0b94e
    WritingMode aWM, nscoord* aBefore, nscoord* aAfter,
Packit f0b94e
    LogicalPoint* aTranslation) {
Packit f0b94e
  MOZ_ASSERT(!XRE_IsContentProcess());
Packit f0b94e
  // Note: At first glance, it appears that you could simply get the
Packit f0b94e
  // absolute bounding box for the dropdown list by first getting its
Packit f0b94e
  // view, then getting the view's nsIWidget, then asking the nsIWidget
Packit f0b94e
  // for its AbsoluteBounds.
Packit f0b94e
  // The problem with this approach, is that the dropdown list's bcoord
Packit f0b94e
  // location can change based on whether the dropdown is placed after
Packit f0b94e
  // or before the display frame.  The approach taken here is to get the
Packit f0b94e
  // absolute position of the display frame and use its location to
Packit f0b94e
  // determine if the dropdown will go offscreen.
Packit f0b94e
Packit f0b94e
  // Normal frame geometry (eg GetOffsetTo, mRect) doesn't include transforms.
Packit f0b94e
  // In the special case that our transform is only a 2D translation we
Packit f0b94e
  // introduce this hack so that the dropdown will show up in the right place.
Packit f0b94e
  // Use null container size when converting a vector from logical to physical.
Packit f0b94e
  const nsSize nullContainerSize;
Packit f0b94e
  *aTranslation =
Packit f0b94e
      LogicalPoint(aWM, GetCSSTransformTranslation(), nullContainerSize);
Packit f0b94e
  *aBefore = 0;
Packit f0b94e
  *aAfter = 0;
Packit f0b94e
Packit f0b94e
  nsRect screen = nsCheckboxRadioFrame::GetUsableScreenRect(PresContext());
Packit f0b94e
  nsSize containerSize = screen.Size();
Packit f0b94e
  LogicalRect logicalScreen(aWM, screen, containerSize);
Packit f0b94e
  if (mLastDropDownAfterScreenBCoord == nscoord_MIN) {
Packit f0b94e
    LogicalRect thisScreenRect(aWM, GetScreenRectInAppUnits(), containerSize);
Packit f0b94e
    mLastDropDownAfterScreenBCoord =
Packit f0b94e
        thisScreenRect.BEnd(aWM) + aTranslation->B(aWM);
Packit f0b94e
    mLastDropDownBeforeScreenBCoord =
Packit f0b94e
        thisScreenRect.BStart(aWM) + aTranslation->B(aWM);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  nscoord minBCoord;
Packit f0b94e
  nsPresContext* pc = PresContext()->GetToplevelContentDocumentPresContext();
Packit f0b94e
  nsIFrame* root = pc ? pc->PresShell()->GetRootFrame() : nullptr;
Packit f0b94e
  if (root) {
Packit f0b94e
    minBCoord = LogicalRect(aWM, root->GetScreenRectInAppUnits(), containerSize)
Packit f0b94e
                    .BStart(aWM);
Packit f0b94e
    if (mLastDropDownAfterScreenBCoord < minBCoord) {
Packit f0b94e
      // Don't allow the drop-down to be placed before the content area.
Packit f0b94e
      return;
Packit f0b94e
    }
Packit f0b94e
  } else {
Packit f0b94e
    minBCoord = logicalScreen.BStart(aWM);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  nscoord after = logicalScreen.BEnd(aWM) - mLastDropDownAfterScreenBCoord;
Packit f0b94e
  nscoord before = mLastDropDownBeforeScreenBCoord - minBCoord;
Packit f0b94e
Packit f0b94e
  // If the difference between the space before and after is less
Packit f0b94e
  // than a row-block-size, then we favor the space after.
Packit f0b94e
  if (before >= after) {
Packit f0b94e
    nsListControlFrame* lcf = static_cast<nsListControlFrame*>(mDropdownFrame);
Packit f0b94e
    nscoord rowBSize = lcf->GetBSizeOfARow();
Packit f0b94e
    if (before < after + rowBSize) {
Packit f0b94e
      before -= rowBSize;
Packit f0b94e
    }
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  *aAfter = after;
Packit f0b94e
  *aBefore = before;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
nsComboboxControlFrame::DropDownPositionState
Packit f0b94e
nsComboboxControlFrame::AbsolutelyPositionDropDown() {
Packit f0b94e
  if (XRE_IsContentProcess()) {
Packit f0b94e
    return eDropDownPositionSuppressed;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  WritingMode wm = GetWritingMode();
Packit f0b94e
  LogicalPoint translation(wm);
Packit f0b94e
  nscoord before, after;
Packit f0b94e
  mLastDropDownAfterScreenBCoord = nscoord_MIN;
Packit f0b94e
  GetAvailableDropdownSpace(wm, &before, &after, &translation);
Packit f0b94e
  if (before <= 0 && after <= 0) {
Packit f0b94e
    if (!nsLayoutUtils::IsContentSelectEnabled() && IsDroppedDown()) {
Packit f0b94e
      // Hide the view immediately to minimize flicker.
Packit f0b94e
      nsView* view = mDropdownFrame->GetView();
Packit f0b94e
      view->GetViewManager()->SetViewVisibility(view, nsViewVisibility_kHide);
Packit f0b94e
      NS_DispatchToCurrentThread(new nsAsyncRollup(this));
Packit f0b94e
    }
Packit f0b94e
    return eDropDownPositionSuppressed;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  LogicalSize dropdownSize = mDropdownFrame->GetLogicalSize(wm);
Packit f0b94e
  nscoord bSize = std::max(before, after);
Packit f0b94e
  nsListControlFrame* lcf = static_cast<nsListControlFrame*>(mDropdownFrame);
Packit f0b94e
  if (bSize < dropdownSize.BSize(wm)) {
Packit f0b94e
    if (lcf->GetNumDisplayRows() > 1) {
Packit f0b94e
      // The drop-down doesn't fit and currently shows more than 1 row -
Packit f0b94e
      // schedule a resize to show fewer rows.
Packit f0b94e
      NS_DispatchToCurrentThread(new nsAsyncResize(this));
Packit f0b94e
      return eDropDownPositionPendingResize;
Packit f0b94e
    }
Packit f0b94e
  } else if (bSize > (dropdownSize.BSize(wm) + lcf->GetBSizeOfARow() * 1.5) &&
Packit f0b94e
             lcf->GetDropdownCanGrow()) {
Packit f0b94e
    // The drop-down fits but there is room for at least 1.5 more rows -
Packit f0b94e
    // schedule a resize to show more rows if it has more rows to show.
Packit f0b94e
    // (1.5 rows for good measure to avoid any rounding issues that would
Packit f0b94e
    // lead to a loop of reflow requests)
Packit f0b94e
    NS_DispatchToCurrentThread(new nsAsyncResize(this));
Packit f0b94e
    return eDropDownPositionPendingResize;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // Position the drop-down after if there is room, otherwise place it before
Packit f0b94e
  // if there is room.  If there is no room for it on either side then place
Packit f0b94e
  // it after (to avoid overlapping UI like the URL bar).
Packit f0b94e
  bool b = dropdownSize.BSize(wm) <= after || dropdownSize.BSize(wm) > before;
Packit f0b94e
  LogicalPoint dropdownPosition(wm, 0, b ? BSize(wm) : -dropdownSize.BSize(wm));
Packit f0b94e
Packit f0b94e
  // Don't position the view unless the position changed since it might cause
Packit f0b94e
  // a call to NotifyGeometryChange() and an infinite loop here.
Packit f0b94e
  nsSize containerSize = GetSize();
Packit f0b94e
  const LogicalPoint currentPos =
Packit f0b94e
      mDropdownFrame->GetLogicalPosition(containerSize);
Packit f0b94e
  const LogicalPoint newPos = dropdownPosition + translation;
Packit f0b94e
  if (currentPos != newPos) {
Packit f0b94e
    mDropdownFrame->SetPosition(wm, newPos, containerSize);
Packit f0b94e
    nsContainerFrame::PositionFrameView(mDropdownFrame);
Packit f0b94e
  }
Packit f0b94e
  return eDropDownPositionFinal;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::NotifyGeometryChange() {
Packit f0b94e
  if (XRE_IsContentProcess()) {
Packit f0b94e
    return;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // We don't need to resize if we're not dropped down since ShowDropDown
Packit f0b94e
  // does that, or if we're dirty then the reflow callback does it,
Packit f0b94e
  // or if we have a delayed ShowDropDown pending.
Packit f0b94e
  if (IsDroppedDown() && !(GetStateBits() & NS_FRAME_IS_DIRTY) &&
Packit f0b94e
      !mDelayedShowDropDown) {
Packit f0b94e
    // Async because we're likely in a middle of a scroll here so
Packit f0b94e
    // frame/view positions are in flux.
Packit f0b94e
    RefPtr<nsResizeDropdownAtFinalPosition> resize =
Packit f0b94e
        new nsResizeDropdownAtFinalPosition(this);
Packit f0b94e
    NS_DispatchToCurrentThread(resize);
Packit f0b94e
  }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
//----------------------------------------------------------
Packit f0b94e
//
Packit f0b94e
//----------------------------------------------------------
Packit f0b94e
#ifdef DO_REFLOW_DEBUG
Packit f0b94e
static int myCounter = 0;
Packit f0b94e
Packit f0b94e
static void printSize(char* aDesc, nscoord aSize) {
Packit f0b94e
  printf(" %s: ", aDesc);
Packit f0b94e
  if (aSize == NS_UNCONSTRAINEDSIZE) {
Packit f0b94e
    printf("UC");
Packit f0b94e
  } else {
Packit f0b94e
    printf("%d", PX(aSize));
Packit f0b94e
  }
Packit f0b94e
}
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
//-------------------------------------------------------------------
Packit f0b94e
//-- Main Reflow for the Combobox
Packit f0b94e
//-------------------------------------------------------------------
Packit f0b94e
Packit f0b94e
bool nsComboboxControlFrame::HasDropDownButton() const {
Packit f0b94e
  const nsStyleDisplay* disp = StyleDisplay();
Packit f0b94e
  return disp->mAppearance == NS_THEME_MENULIST &&
Packit f0b94e
         (!IsThemed(disp) ||
Packit f0b94e
          PresContext()->GetTheme()->ThemeNeedsComboboxDropmarker());
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
nscoord nsComboboxControlFrame::GetIntrinsicISize(
Packit f0b94e
    gfxContext* aRenderingContext, nsLayoutUtils::IntrinsicISizeType aType) {
Packit f0b94e
  // get the scrollbar width, we'll use this later
Packit f0b94e
  nscoord scrollbarWidth = 0;
Packit f0b94e
  nsPresContext* presContext = PresContext();
Packit f0b94e
  if (mListControlFrame) {
Packit f0b94e
    nsIScrollableFrame* scrollable = do_QueryFrame(mListControlFrame);
Packit f0b94e
    NS_ASSERTION(scrollable, "List must be a scrollable frame");
Packit f0b94e
    scrollbarWidth = scrollable->GetNondisappearingScrollbarWidth(
Packit f0b94e
        presContext, aRenderingContext, GetWritingMode());
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  nscoord displayISize = 0;
Packit f0b94e
  if (MOZ_LIKELY(mDisplayFrame)) {
Packit f0b94e
    displayISize = nsLayoutUtils::IntrinsicForContainer(aRenderingContext,
Packit f0b94e
                                                        mDisplayFrame, aType);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  if (mDropdownFrame) {
Packit f0b94e
    nscoord dropdownContentISize;
Packit f0b94e
    bool isUsingOverlayScrollbars =
Packit f0b94e
        LookAndFeel::GetInt(LookAndFeel::eIntID_UseOverlayScrollbars) != 0;
Packit f0b94e
    if (aType == nsLayoutUtils::MIN_ISIZE) {
Packit f0b94e
      dropdownContentISize = mDropdownFrame->GetMinISize(aRenderingContext);
Packit f0b94e
      if (isUsingOverlayScrollbars) {
Packit f0b94e
        dropdownContentISize += scrollbarWidth;
Packit f0b94e
      }
Packit f0b94e
    } else {
Packit f0b94e
      NS_ASSERTION(aType == nsLayoutUtils::PREF_ISIZE, "Unexpected type");
Packit f0b94e
      dropdownContentISize = mDropdownFrame->GetPrefISize(aRenderingContext);
Packit f0b94e
      if (isUsingOverlayScrollbars) {
Packit f0b94e
        dropdownContentISize += scrollbarWidth;
Packit f0b94e
      }
Packit f0b94e
    }
Packit f0b94e
    dropdownContentISize = NSCoordSaturatingSubtract(
Packit f0b94e
        dropdownContentISize, scrollbarWidth, nscoord_MAX);
Packit f0b94e
Packit f0b94e
    displayISize = std::max(dropdownContentISize, displayISize);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // add room for the dropmarker button if there is one
Packit f0b94e
  if (HasDropDownButton()) {
Packit f0b94e
    displayISize += scrollbarWidth;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  return displayISize;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
nscoord nsComboboxControlFrame::GetMinISize(gfxContext* aRenderingContext) {
Packit f0b94e
  nscoord minISize;
Packit f0b94e
  DISPLAY_MIN_WIDTH(this, minISize);
Packit f0b94e
  minISize = GetIntrinsicISize(aRenderingContext, nsLayoutUtils::MIN_ISIZE);
Packit f0b94e
  return minISize;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
nscoord nsComboboxControlFrame::GetPrefISize(gfxContext* aRenderingContext) {
Packit f0b94e
  nscoord prefISize;
Packit f0b94e
  DISPLAY_PREF_WIDTH(this, prefISize);
Packit f0b94e
  prefISize = GetIntrinsicISize(aRenderingContext, nsLayoutUtils::PREF_ISIZE);
Packit f0b94e
  return prefISize;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::Reflow(nsPresContext* aPresContext,
Packit f0b94e
                                    ReflowOutput& aDesiredSize,
Packit f0b94e
                                    const ReflowInput& aReflowInput,
Packit f0b94e
                                    nsReflowStatus& aStatus) {
Packit f0b94e
  MarkInReflow();
Packit f0b94e
  MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
Packit f0b94e
  // Constraints we try to satisfy:
Packit f0b94e
Packit f0b94e
  // 1) Default inline size of button is the vertical scrollbar size
Packit f0b94e
  // 2) If the inline size of button is bigger than our inline size, set
Packit f0b94e
  //    inline size of button to 0.
Packit f0b94e
  // 3) Default block size of button is block size of display area
Packit f0b94e
  // 4) Inline size of display area is whatever is left over from our
Packit f0b94e
  //    inline size after allocating inline size for the button.
Packit f0b94e
  // 5) Block Size of display area is GetBSizeOfARow() on the
Packit f0b94e
  //    mListControlFrame.
Packit f0b94e
Packit f0b94e
  if (!mDisplayFrame || !mButtonFrame || !mDropdownFrame) {
Packit f0b94e
    NS_ERROR("Why did the frame constructor allow this to happen?  Fix it!!");
Packit f0b94e
    return;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // Make sure the displayed text is the same as the selected option, bug
Packit f0b94e
  // 297389.
Packit f0b94e
  if (!mDroppedDown) {
Packit f0b94e
    mDisplayedIndex = mListControlFrame->GetSelectedIndex();
Packit f0b94e
  }
Packit f0b94e
  // In dropped down mode the "selected index" is the hovered menu item,
Packit f0b94e
  // we want the last selected item which is |mDisplayedIndex| in this case.
Packit f0b94e
  RedisplayText();
Packit f0b94e
Packit f0b94e
  // First reflow our dropdown so that we know how tall we should be.
Packit f0b94e
  ReflowDropdown(aPresContext, aReflowInput);
Packit f0b94e
  RefPtr<nsResizeDropdownAtFinalPosition> resize =
Packit f0b94e
      new nsResizeDropdownAtFinalPosition(this);
Packit f0b94e
  if (NS_SUCCEEDED(aPresContext->PresShell()->PostReflowCallback(resize))) {
Packit f0b94e
    // The reflow callback queue doesn't AddRef so we keep it alive until
Packit f0b94e
    // it's released in its ReflowFinished / ReflowCallbackCanceled.
Packit f0b94e
    Unused << resize.forget();
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // Get the width of the vertical scrollbar.  That will be the inline
Packit f0b94e
  // size of the dropdown button.
Packit f0b94e
  WritingMode wm = aReflowInput.GetWritingMode();
Packit f0b94e
  nscoord buttonISize;
Packit f0b94e
  if (!HasDropDownButton()) {
Packit f0b94e
    buttonISize = 0;
Packit f0b94e
  } else {
Packit f0b94e
    nsIScrollableFrame* scrollable = do_QueryFrame(mListControlFrame);
Packit f0b94e
    NS_ASSERTION(scrollable, "List must be a scrollable frame");
Packit f0b94e
    buttonISize = scrollable->GetNondisappearingScrollbarWidth(
Packit f0b94e
        PresContext(), aReflowInput.mRenderingContext, wm);
Packit f0b94e
    if (buttonISize > aReflowInput.ComputedISize()) {
Packit f0b94e
      buttonISize = 0;
Packit f0b94e
    }
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  mDisplayISize = aReflowInput.ComputedISize() - buttonISize;
Packit f0b94e
Packit f0b94e
  nsBlockFrame::Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
Packit f0b94e
Packit f0b94e
  // The button should occupy the same space as a scrollbar
Packit f0b94e
  nsSize containerSize = aDesiredSize.PhysicalSize();
Packit f0b94e
  LogicalRect buttonRect = mButtonFrame->GetLogicalRect(containerSize);
Packit f0b94e
Packit f0b94e
  buttonRect.IStart(wm) =
Packit f0b94e
      aReflowInput.ComputedLogicalBorderPadding().IStartEnd(wm) +
Packit f0b94e
      mDisplayISize -
Packit f0b94e
      (aReflowInput.ComputedLogicalBorderPadding().IEnd(wm) -
Packit f0b94e
       aReflowInput.ComputedLogicalPadding().IEnd(wm));
Packit f0b94e
  buttonRect.ISize(wm) = buttonISize;
Packit f0b94e
Packit f0b94e
  buttonRect.BStart(wm) = this->GetLogicalUsedBorder(wm).BStart(wm);
Packit f0b94e
  buttonRect.BSize(wm) =
Packit f0b94e
      mDisplayFrame->BSize(wm) + this->GetLogicalUsedPadding(wm).BStartEnd(wm);
Packit f0b94e
Packit f0b94e
  mButtonFrame->SetRect(buttonRect, containerSize);
Packit f0b94e
Packit f0b94e
  if (!aStatus.IsInlineBreakBefore() && !aStatus.IsFullyComplete()) {
Packit f0b94e
    // This frame didn't fit inside a fragmentation container.  Splitting
Packit f0b94e
    // a nsComboboxControlFrame makes no sense, so we override the status here.
Packit f0b94e
    aStatus.Reset();
Packit f0b94e
  }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
  //--------------------------------------------------------------
Packit f0b94e
Packit f0b94e
#ifdef DEBUG_FRAME_DUMP
Packit f0b94e
nsresult nsComboboxControlFrame::GetFrameName(nsAString& aResult) const {
Packit f0b94e
  return MakeFrameName(NS_LITERAL_STRING("ComboboxControl"), aResult);
Packit f0b94e
}
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
//----------------------------------------------------------------------
Packit f0b94e
// nsIComboboxControlFrame
Packit f0b94e
//----------------------------------------------------------------------
Packit f0b94e
void nsComboboxControlFrame::ShowDropDown(bool aDoDropDown) {
Packit f0b94e
  if (!nsLayoutUtils::IsContentSelectEnabled()) {
Packit f0b94e
    // TODO(kuoe0) remove this assertion after content-select is enabled
Packit f0b94e
    MOZ_ASSERT(!XRE_IsContentProcess());
Packit f0b94e
  }
Packit f0b94e
  mDelayedShowDropDown = false;
Packit f0b94e
  EventStates eventStates = mContent->AsElement()->State();
Packit f0b94e
  if (aDoDropDown && eventStates.HasState(NS_EVENT_STATE_DISABLED)) {
Packit f0b94e
    return;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  if (!mDroppedDown && aDoDropDown) {
Packit f0b94e
    nsFocusManager* fm = nsFocusManager::GetFocusManager();
Packit f0b94e
    if (!fm || fm->GetFocusedContent() == GetContent()) {
Packit f0b94e
      DropDownPositionState state = AbsolutelyPositionDropDown();
Packit f0b94e
      if (state == eDropDownPositionFinal) {
Packit f0b94e
        ShowList(aDoDropDown);  // might destroy us
Packit f0b94e
      } else if (state == eDropDownPositionPendingResize) {
Packit f0b94e
        // Delay until after the resize reflow, see nsAsyncResize.
Packit f0b94e
        mDelayedShowDropDown = true;
Packit f0b94e
      }
Packit f0b94e
    } else {
Packit f0b94e
      // Delay until we get focus, see SetFocus().
Packit f0b94e
      mDelayedShowDropDown = true;
Packit f0b94e
    }
Packit f0b94e
  } else if (mDroppedDown && !aDoDropDown) {
Packit f0b94e
    ShowList(aDoDropDown);  // might destroy us
Packit f0b94e
  }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::SetDropDown(nsIFrame* aDropDownFrame) {
Packit f0b94e
  mDropdownFrame = aDropDownFrame;
Packit f0b94e
  mListControlFrame = do_QueryFrame(mDropdownFrame);
Packit f0b94e
  if (!sFocused && nsContentUtils::IsFocusedContent(GetContent())) {
Packit f0b94e
    sFocused = this;
Packit f0b94e
    nsListControlFrame::ComboboxFocusSet();
Packit f0b94e
  }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
nsIFrame* nsComboboxControlFrame::GetDropDown() { return mDropdownFrame; }
Packit f0b94e
Packit f0b94e
///////////////////////////////////////////////////////////////
Packit f0b94e
Packit f0b94e
NS_IMETHODIMP
Packit f0b94e
nsComboboxControlFrame::RedisplaySelectedText() {
Packit f0b94e
  nsAutoScriptBlocker scriptBlocker;
Packit f0b94e
  mDisplayedIndex = mListControlFrame->GetSelectedIndex();
Packit f0b94e
  return RedisplayText();
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
nsresult nsComboboxControlFrame::RedisplayText() {
Packit f0b94e
  nsString previewValue;
Packit f0b94e
  nsString previousText(mDisplayedOptionTextOrPreview);
Packit f0b94e
Packit f0b94e
  auto* selectElement = static_cast<dom::HTMLSelectElement*>(GetContent());
Packit f0b94e
  selectElement->GetPreviewValue(previewValue);
Packit f0b94e
  // Get the text to display
Packit f0b94e
  if (!previewValue.IsEmpty()) {
Packit f0b94e
    mDisplayedOptionTextOrPreview = previewValue;
Packit f0b94e
  } else if (mDisplayedIndex != -1) {
Packit f0b94e
    mListControlFrame->GetOptionText(mDisplayedIndex,
Packit f0b94e
                                     mDisplayedOptionTextOrPreview);
Packit f0b94e
  } else {
Packit f0b94e
    mDisplayedOptionTextOrPreview.Truncate();
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  REFLOW_DEBUG_MSG2(
Packit f0b94e
      "RedisplayText \"%s\"\n",
Packit f0b94e
      NS_LossyConvertUTF16toASCII(mDisplayedOptionTextOrPreview).get());
Packit f0b94e
Packit f0b94e
  // Send reflow command because the new text maybe larger
Packit f0b94e
  nsresult rv = NS_OK;
Packit f0b94e
  if (mDisplayContent && !previousText.Equals(mDisplayedOptionTextOrPreview)) {
Packit f0b94e
    // Don't call ActuallyDisplayText(true) directly here since that
Packit f0b94e
    // could cause recursive frame construction. See bug 283117 and the comment
Packit f0b94e
    // in HandleRedisplayTextEvent() below.
Packit f0b94e
Packit f0b94e
    // Revoke outstanding events to avoid out-of-order events which could mean
Packit f0b94e
    // displaying the wrong text.
Packit f0b94e
    mRedisplayTextEvent.Revoke();
Packit f0b94e
Packit f0b94e
    NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(),
Packit f0b94e
                 "If we happen to run our redisplay event now, we might kill "
Packit f0b94e
                 "ourselves!");
Packit f0b94e
Packit f0b94e
    mRedisplayTextEvent = new RedisplayTextEvent(this);
Packit f0b94e
    nsContentUtils::AddScriptRunner(mRedisplayTextEvent.get());
Packit f0b94e
  }
Packit f0b94e
  return rv;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::HandleRedisplayTextEvent() {
Packit f0b94e
  // First, make sure that the content model is up to date and we've
Packit f0b94e
  // constructed the frames for all our content in the right places.
Packit f0b94e
  // Otherwise they'll end up under the wrong insertion frame when we
Packit f0b94e
  // ActuallyDisplayText, since that flushes out the content sink by
Packit f0b94e
  // calling SetText on a DOM node with aNotify set to true.  See bug
Packit f0b94e
  // 289730.
Packit f0b94e
  AutoWeakFrame weakThis(this);
Packit f0b94e
  PresContext()->Document()->FlushPendingNotifications(
Packit f0b94e
      FlushType::ContentAndNotify);
Packit f0b94e
  if (!weakThis.IsAlive()) return;
Packit f0b94e
Packit f0b94e
  // Redirect frame insertions during this method (see
Packit f0b94e
  // GetContentInsertionFrame()) so that any reframing that the frame
Packit f0b94e
  // constructor forces upon us is inserted into the correct parent
Packit f0b94e
  // (mDisplayFrame). See bug 282607.
Packit f0b94e
  NS_PRECONDITION(!mInRedisplayText, "Nested RedisplayText");
Packit f0b94e
  mInRedisplayText = true;
Packit f0b94e
  mRedisplayTextEvent.Forget();
Packit f0b94e
Packit f0b94e
  ActuallyDisplayText(true);
Packit f0b94e
  if (!weakThis.IsAlive()) {
Packit f0b94e
    return;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // XXXbz This should perhaps be eResize.  Check.
Packit f0b94e
  PresShell()->FrameNeedsReflow(mDisplayFrame, nsIPresShell::eStyleChange,
Packit f0b94e
                                NS_FRAME_IS_DIRTY);
Packit f0b94e
Packit f0b94e
  mInRedisplayText = false;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::ActuallyDisplayText(bool aNotify) {
Packit f0b94e
  nsCOMPtr<nsIContent> displayContent = mDisplayContent;
Packit f0b94e
  if (mDisplayedOptionTextOrPreview.IsEmpty()) {
Packit f0b94e
    // Have to use a non-breaking space for line-block-size calculations
Packit f0b94e
    // to be right
Packit f0b94e
    static const char16_t space = 0xA0;
Packit f0b94e
    displayContent->SetText(&space, 1, aNotify);
Packit f0b94e
  } else {
Packit f0b94e
    displayContent->SetText(mDisplayedOptionTextOrPreview, aNotify);
Packit f0b94e
  }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
int32_t nsComboboxControlFrame::GetIndexOfDisplayArea() {
Packit f0b94e
  return mDisplayedIndex;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
//----------------------------------------------------------------------
Packit f0b94e
// nsISelectControlFrame
Packit f0b94e
//----------------------------------------------------------------------
Packit f0b94e
NS_IMETHODIMP
Packit f0b94e
nsComboboxControlFrame::DoneAddingChildren(bool aIsDone) {
Packit f0b94e
  nsISelectControlFrame* listFrame = do_QueryFrame(mDropdownFrame);
Packit f0b94e
  if (!listFrame) return NS_ERROR_FAILURE;
Packit f0b94e
Packit f0b94e
  return listFrame->DoneAddingChildren(aIsDone);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
NS_IMETHODIMP
Packit f0b94e
nsComboboxControlFrame::AddOption(int32_t aIndex) {
Packit f0b94e
  if (aIndex <= mDisplayedIndex) {
Packit f0b94e
    ++mDisplayedIndex;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  nsListControlFrame* lcf = static_cast<nsListControlFrame*>(mDropdownFrame);
Packit f0b94e
  return lcf->AddOption(aIndex);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
NS_IMETHODIMP
Packit f0b94e
nsComboboxControlFrame::RemoveOption(int32_t aIndex) {
Packit f0b94e
  AutoWeakFrame weakThis(this);
Packit f0b94e
  if (mListControlFrame->GetNumberOfOptions() > 0) {
Packit f0b94e
    if (aIndex < mDisplayedIndex) {
Packit f0b94e
      --mDisplayedIndex;
Packit f0b94e
    } else if (aIndex == mDisplayedIndex) {
Packit f0b94e
      mDisplayedIndex = 0;  // IE6 compat
Packit f0b94e
      RedisplayText();
Packit f0b94e
    }
Packit f0b94e
  } else {
Packit f0b94e
    // If we removed the last option, we need to blank things out
Packit f0b94e
    mDisplayedIndex = -1;
Packit f0b94e
    RedisplayText();
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  if (!weakThis.IsAlive()) return NS_OK;
Packit f0b94e
Packit f0b94e
  nsListControlFrame* lcf = static_cast<nsListControlFrame*>(mDropdownFrame);
Packit f0b94e
  return lcf->RemoveOption(aIndex);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
NS_IMETHODIMP
Packit f0b94e
nsComboboxControlFrame::OnSetSelectedIndex(int32_t aOldIndex,
Packit f0b94e
                                           int32_t aNewIndex) {
Packit f0b94e
  nsAutoScriptBlocker scriptBlocker;
Packit f0b94e
  mDisplayedIndex = aNewIndex;
Packit f0b94e
  RedisplayText();
Packit f0b94e
  NS_ASSERTION(mDropdownFrame, "No dropdown frame!");
Packit f0b94e
Packit f0b94e
  nsISelectControlFrame* listFrame = do_QueryFrame(mDropdownFrame);
Packit f0b94e
  NS_ASSERTION(listFrame, "No list frame!");
Packit f0b94e
Packit f0b94e
  return listFrame->OnSetSelectedIndex(aOldIndex, aNewIndex);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
// End nsISelectControlFrame
Packit f0b94e
//----------------------------------------------------------------------
Packit f0b94e
Packit f0b94e
nsresult nsComboboxControlFrame::HandleEvent(nsPresContext* aPresContext,
Packit f0b94e
                                             WidgetGUIEvent* aEvent,
Packit f0b94e
                                             nsEventStatus* aEventStatus) {
Packit f0b94e
  NS_ENSURE_ARG_POINTER(aEventStatus);
Packit f0b94e
Packit f0b94e
  if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
Packit f0b94e
    return NS_OK;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  EventStates eventStates = mContent->AsElement()->State();
Packit f0b94e
  if (eventStates.HasState(NS_EVENT_STATE_DISABLED)) {
Packit f0b94e
    return NS_OK;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
#if COMBOBOX_ROLLUP_CONSUME_EVENT == 0
Packit f0b94e
  if (aEvent->mMessage == eMouseDown) {
Packit f0b94e
    if (GetContent() == mozilla::widget::nsAutoRollup::GetLastRollup()) {
Packit f0b94e
      // This event did a Rollup on this control - prevent it from opening
Packit f0b94e
      // the dropdown again!
Packit f0b94e
      *aEventStatus = nsEventStatus_eConsumeNoDefault;
Packit f0b94e
      return NS_OK;
Packit f0b94e
    }
Packit f0b94e
  }
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
  // If we have style that affects how we are selected, feed event down to
Packit f0b94e
  // nsFrame::HandleEvent so that selection takes place when appropriate.
Packit f0b94e
  if (IsContentDisabled()) {
Packit f0b94e
    return nsBlockFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
Packit f0b94e
  }
Packit f0b94e
  return NS_OK;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
nsresult nsComboboxControlFrame::SetFormProperty(nsAtom* aName,
Packit f0b94e
                                                 const nsAString& aValue) {
Packit f0b94e
  nsIFormControlFrame* fcFrame = do_QueryFrame(mDropdownFrame);
Packit f0b94e
  if (!fcFrame) {
Packit f0b94e
    return NS_NOINTERFACE;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  return fcFrame->SetFormProperty(aName, aValue);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
nsContainerFrame* nsComboboxControlFrame::GetContentInsertionFrame() {
Packit f0b94e
  return mInRedisplayText ? mDisplayFrame
Packit f0b94e
                          : mDropdownFrame->GetContentInsertionFrame();
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::AppendDirectlyOwnedAnonBoxes(
Packit f0b94e
    nsTArray<OwnedAnonBox>& aResult) {
Packit f0b94e
  aResult.AppendElement(OwnedAnonBox(mDropdownFrame));
Packit f0b94e
  aResult.AppendElement(OwnedAnonBox(mDisplayFrame));
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
nsresult nsComboboxControlFrame::CreateAnonymousContent(
Packit f0b94e
    nsTArray<ContentInfo>& aElements) {
Packit f0b94e
  // The frames used to display the combo box and the button used to popup the
Packit f0b94e
  // dropdown list are created through anonymous content. The dropdown list is
Packit f0b94e
  // not created through anonymous content because its frame is initialized
Packit f0b94e
  // specifically for the drop-down case and it is placed a special list
Packit f0b94e
  // referenced through NS_COMBO_FRAME_POPUP_LIST_INDEX to keep separate from
Packit f0b94e
  // the layout of the display and button.
Packit f0b94e
  //
Packit f0b94e
  // Note: The value attribute of the display content is set when an item is
Packit f0b94e
  // selected in the dropdown list. If the content specified below does not
Packit f0b94e
  // honor the value attribute than nothing will be displayed.
Packit f0b94e
Packit f0b94e
  // For now the content that is created corresponds to two input buttons. It
Packit f0b94e
  // would be better to create the tag as something other than input, but then
Packit f0b94e
  // there isn't any way to create a button frame since it isn't possible to set
Packit f0b94e
  // the display type in CSS2 to create a button frame.
Packit f0b94e
Packit f0b94e
  // create content used for display
Packit f0b94e
  // nsAtom* tag = NS_Atomize("mozcombodisplay");
Packit f0b94e
Packit f0b94e
  // Add a child text content node for the label
Packit f0b94e
Packit f0b94e
  nsNodeInfoManager* nimgr = mContent->NodeInfo()->NodeInfoManager();
Packit f0b94e
Packit f0b94e
  mDisplayContent = new nsTextNode(nimgr);
Packit f0b94e
Packit f0b94e
  // set the value of the text node
Packit f0b94e
  mDisplayedIndex = mListControlFrame->GetSelectedIndex();
Packit f0b94e
  if (mDisplayedIndex != -1) {
Packit f0b94e
    mListControlFrame->GetOptionText(mDisplayedIndex,
Packit f0b94e
                                     mDisplayedOptionTextOrPreview);
Packit f0b94e
  }
Packit f0b94e
  ActuallyDisplayText(false);
Packit f0b94e
Packit f0b94e
  if (!aElements.AppendElement(mDisplayContent)) return NS_ERROR_OUT_OF_MEMORY;
Packit f0b94e
Packit f0b94e
  mButtonContent = mContent->OwnerDoc()->CreateHTMLElement(nsGkAtoms::button);
Packit f0b94e
  if (!mButtonContent) return NS_ERROR_OUT_OF_MEMORY;
Packit f0b94e
Packit f0b94e
  // make someone to listen to the button. If its pressed by someone like
Packit f0b94e
  // Accessibility then open or close the combo box.
Packit f0b94e
  mButtonListener = new nsComboButtonListener(this);
Packit f0b94e
  mButtonContent->AddEventListener(NS_LITERAL_STRING("click"), mButtonListener,
Packit f0b94e
                                   false, false);
Packit f0b94e
Packit f0b94e
  mButtonContent->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
Packit f0b94e
                          NS_LITERAL_STRING("button"), false);
Packit f0b94e
  // Set tabindex="-1" so that the button is not tabbable
Packit f0b94e
  mButtonContent->SetAttr(kNameSpaceID_None, nsGkAtoms::tabindex,
Packit f0b94e
                          NS_LITERAL_STRING("-1"), false);
Packit f0b94e
Packit f0b94e
  WritingMode wm = GetWritingMode();
Packit f0b94e
  if (wm.IsVertical()) {
Packit f0b94e
    mButtonContent->SetAttr(kNameSpaceID_None, nsGkAtoms::orientation,
Packit f0b94e
                            wm.IsVerticalRL() ? NS_LITERAL_STRING("left")
Packit f0b94e
                                              : NS_LITERAL_STRING("right"),
Packit f0b94e
                            false);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  if (!aElements.AppendElement(mButtonContent)) return NS_ERROR_OUT_OF_MEMORY;
Packit f0b94e
Packit f0b94e
  return NS_OK;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::AppendAnonymousContentTo(
Packit f0b94e
    nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
Packit f0b94e
  if (mDisplayContent) {
Packit f0b94e
    aElements.AppendElement(mDisplayContent);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  if (mButtonContent) {
Packit f0b94e
    aElements.AppendElement(mButtonContent);
Packit f0b94e
  }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
// XXXbz this is a for-now hack.  Now that display:inline-block works,
Packit f0b94e
// need to revisit this.
Packit f0b94e
class nsComboboxDisplayFrame : public nsBlockFrame {
Packit f0b94e
 public:
Packit f0b94e
  NS_DECL_FRAMEARENA_HELPERS(nsComboboxDisplayFrame)
Packit f0b94e
Packit f0b94e
  nsComboboxDisplayFrame(nsStyleContext* aContext,
Packit f0b94e
                         nsComboboxControlFrame* aComboBox)
Packit f0b94e
      : nsBlockFrame(aContext, kClassID), mComboBox(aComboBox) {}
Packit f0b94e
Packit f0b94e
#ifdef DEBUG_FRAME_DUMP
Packit f0b94e
  nsresult GetFrameName(nsAString& aResult) const override {
Packit f0b94e
    return MakeFrameName(NS_LITERAL_STRING("ComboboxDisplay"), aResult);
Packit f0b94e
  }
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
  virtual bool IsFrameOfType(uint32_t aFlags) const override {
Packit f0b94e
    return nsBlockFrame::IsFrameOfType(aFlags &
Packit f0b94e
                                       ~(nsIFrame::eReplacedContainsBlock));
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  virtual void Reflow(nsPresContext* aPresContext, ReflowOutput& aDesiredSize,
Packit f0b94e
                      const ReflowInput& aReflowInput,
Packit f0b94e
                      nsReflowStatus& aStatus) override;
Packit f0b94e
Packit f0b94e
  virtual void BuildDisplayList(nsDisplayListBuilder* aBuilder,
Packit f0b94e
                                const nsDisplayListSet& aLists) override;
Packit f0b94e
Packit f0b94e
 protected:
Packit f0b94e
  nsComboboxControlFrame* mComboBox;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
NS_IMPL_FRAMEARENA_HELPERS(nsComboboxDisplayFrame)
Packit f0b94e
Packit f0b94e
void nsComboboxDisplayFrame::Reflow(nsPresContext* aPresContext,
Packit f0b94e
                                    ReflowOutput& aDesiredSize,
Packit f0b94e
                                    const ReflowInput& aReflowInput,
Packit f0b94e
                                    nsReflowStatus& aStatus) {
Packit f0b94e
  MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
Packit f0b94e
Packit f0b94e
  ReflowInput state(aReflowInput);
Packit f0b94e
  if (state.ComputedBSize() == NS_INTRINSICSIZE) {
Packit f0b94e
    // Note that the only way we can have a computed block size here is
Packit f0b94e
    // if the combobox had a specified block size.  If it didn't, size
Packit f0b94e
    // based on what our rows look like, for lack of anything better.
Packit f0b94e
    state.SetComputedBSize(mComboBox->mListControlFrame->GetBSizeOfARow());
Packit f0b94e
  }
Packit f0b94e
  WritingMode wm = aReflowInput.GetWritingMode();
Packit f0b94e
  nscoord computedISize = mComboBox->mDisplayISize -
Packit f0b94e
                          state.ComputedLogicalBorderPadding().IStartEnd(wm);
Packit f0b94e
  if (computedISize < 0) {
Packit f0b94e
    computedISize = 0;
Packit f0b94e
  }
Packit f0b94e
  state.SetComputedISize(computedISize);
Packit f0b94e
  nsBlockFrame::Reflow(aPresContext, aDesiredSize, state, aStatus);
Packit f0b94e
  aStatus.Reset();  // this type of frame can't be split
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxDisplayFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
Packit f0b94e
                                              const nsDisplayListSet& aLists) {
Packit f0b94e
  nsDisplayListCollection set(aBuilder);
Packit f0b94e
  nsBlockFrame::BuildDisplayList(aBuilder, set);
Packit f0b94e
Packit f0b94e
  // remove background items if parent frame is themed
Packit f0b94e
  if (mComboBox->IsThemed()) {
Packit f0b94e
    set.BorderBackground()->DeleteAll(aBuilder);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  set.MoveTo(aLists);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
nsIFrame* nsComboboxControlFrame::CreateFrameForDisplayNode() {
Packit f0b94e
  MOZ_ASSERT(mDisplayContent);
Packit f0b94e
Packit f0b94e
  // Get PresShell
Packit f0b94e
  nsIPresShell* shell = PresShell();
Packit f0b94e
  StyleSetHandle styleSet = shell->StyleSet();
Packit f0b94e
Packit f0b94e
  // create the style contexts for the anonymous block frame and text frame
Packit f0b94e
  RefPtr<nsStyleContext> styleContext;
Packit f0b94e
  styleContext = styleSet->ResolveInheritingAnonymousBoxStyle(
Packit f0b94e
      nsCSSAnonBoxes::mozDisplayComboboxControlFrame, mStyleContext);
Packit f0b94e
Packit f0b94e
  RefPtr<nsStyleContext> textStyleContext;
Packit f0b94e
  textStyleContext =
Packit f0b94e
      styleSet->ResolveStyleForText(mDisplayContent, mStyleContext);
Packit f0b94e
Packit f0b94e
  // Start by creating our anonymous block frame
Packit f0b94e
  mDisplayFrame = new (shell) nsComboboxDisplayFrame(styleContext, this);
Packit f0b94e
  mDisplayFrame->Init(mContent, this, nullptr);
Packit f0b94e
Packit f0b94e
  // Create a text frame and put it inside the block frame
Packit f0b94e
  nsIFrame* textFrame = NS_NewTextFrame(shell, textStyleContext);
Packit f0b94e
Packit f0b94e
  // initialize the text frame
Packit f0b94e
  textFrame->Init(mDisplayContent, mDisplayFrame, nullptr);
Packit f0b94e
  mDisplayContent->SetPrimaryFrame(textFrame);
Packit f0b94e
Packit f0b94e
  nsFrameList textList(textFrame, textFrame);
Packit f0b94e
  mDisplayFrame->SetInitialChildList(kPrincipalList, textList);
Packit f0b94e
  return mDisplayFrame;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::DestroyFrom(nsIFrame* aDestructRoot,
Packit f0b94e
                                         PostDestroyData& aPostDestroyData) {
Packit f0b94e
  if (sFocused == this) {
Packit f0b94e
    sFocused = nullptr;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // Revoke any pending RedisplayTextEvent
Packit f0b94e
  mRedisplayTextEvent.Revoke();
Packit f0b94e
Packit f0b94e
  nsCheckboxRadioFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), false);
Packit f0b94e
Packit f0b94e
  if (!nsLayoutUtils::IsContentSelectEnabled() && mDroppedDown) {
Packit f0b94e
    MOZ_ASSERT(mDropdownFrame, "mDroppedDown without frame");
Packit f0b94e
    nsView* view = mDropdownFrame->GetView();
Packit f0b94e
    MOZ_ASSERT(view);
Packit f0b94e
    nsIWidget* widget = view->GetWidget();
Packit f0b94e
    if (widget) {
Packit f0b94e
      widget->CaptureRollupEvents(this, false);
Packit f0b94e
    }
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // Cleanup frames in popup child list
Packit f0b94e
  mPopupFrames.DestroyFramesFrom(aDestructRoot, aPostDestroyData);
Packit f0b94e
  aPostDestroyData.AddAnonymousContent(mDisplayContent.forget());
Packit f0b94e
  aPostDestroyData.AddAnonymousContent(mButtonContent.forget());
Packit f0b94e
  nsBlockFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
const nsFrameList& nsComboboxControlFrame::GetChildList(
Packit f0b94e
    ChildListID aListID) const {
Packit f0b94e
  if (kSelectPopupList == aListID) {
Packit f0b94e
    return mPopupFrames;
Packit f0b94e
  }
Packit f0b94e
  return nsBlockFrame::GetChildList(aListID);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::GetChildLists(nsTArray<ChildList>* aLists) const {
Packit f0b94e
  nsBlockFrame::GetChildLists(aLists);
Packit f0b94e
  mPopupFrames.AppendIfNonempty(aLists, kSelectPopupList);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::SetInitialChildList(ChildListID aListID,
Packit f0b94e
                                                 nsFrameList& aChildList) {
Packit f0b94e
  if (kSelectPopupList == aListID) {
Packit f0b94e
    mPopupFrames.SetFrames(aChildList);
Packit f0b94e
  } else {
Packit f0b94e
    for (nsFrameList::Enumerator e(aChildList); !e.AtEnd(); e.Next()) {
Packit f0b94e
      nsCOMPtr<nsIFormControl> formControl =
Packit f0b94e
          do_QueryInterface(e.get()->GetContent());
Packit f0b94e
      if (formControl && formControl->ControlType() == NS_FORM_BUTTON_BUTTON) {
Packit f0b94e
        mButtonFrame = e.get();
Packit f0b94e
        break;
Packit f0b94e
      }
Packit f0b94e
    }
Packit f0b94e
    NS_ASSERTION(mButtonFrame, "missing button frame in initial child list");
Packit f0b94e
    nsBlockFrame::SetInitialChildList(aListID, aChildList);
Packit f0b94e
  }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
//----------------------------------------------------------------------
Packit f0b94e
// nsIRollupListener
Packit f0b94e
//----------------------------------------------------------------------
Packit f0b94e
bool nsComboboxControlFrame::Rollup(uint32_t aCount, bool aFlush,
Packit f0b94e
                                    const nsIntPoint* pos,
Packit f0b94e
                                    nsIContent** aLastRolledUp) {
Packit f0b94e
  if (aLastRolledUp) {
Packit f0b94e
    *aLastRolledUp = nullptr;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  if (!mDroppedDown) {
Packit f0b94e
    return false;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  bool consume = !!COMBOBOX_ROLLUP_CONSUME_EVENT;
Packit f0b94e
  AutoWeakFrame weakFrame(this);
Packit f0b94e
  mListControlFrame->AboutToRollup();  // might destroy us
Packit f0b94e
  if (!weakFrame.IsAlive()) {
Packit f0b94e
    return consume;
Packit f0b94e
  }
Packit f0b94e
  ShowDropDown(false);  // might destroy us
Packit f0b94e
  if (weakFrame.IsAlive()) {
Packit f0b94e
    mListControlFrame->CaptureMouseEvents(false);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  if (!nsLayoutUtils::IsContentSelectEnabled() && aFlush &&
Packit f0b94e
      weakFrame.IsAlive()) {
Packit f0b94e
    // The popup's visibility doesn't update until the minimize animation has
Packit f0b94e
    // finished, so call UpdateWidgetGeometry to update it right away.
Packit f0b94e
    nsViewManager* viewManager = mDropdownFrame->GetView()->GetViewManager();
Packit f0b94e
    viewManager->UpdateWidgetGeometry();  // might destroy us
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  if (!weakFrame.IsAlive()) {
Packit f0b94e
    return consume;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  if (aLastRolledUp) {
Packit f0b94e
    *aLastRolledUp = GetContent();
Packit f0b94e
  }
Packit f0b94e
  return consume;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
nsIWidget* nsComboboxControlFrame::GetRollupWidget() {
Packit f0b94e
  if (nsLayoutUtils::IsContentSelectEnabled()) {
Packit f0b94e
    return nullptr;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  nsView* view = mDropdownFrame->GetView();
Packit f0b94e
  MOZ_ASSERT(view);
Packit f0b94e
  return view->GetWidget();
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::RollupFromList() {
Packit f0b94e
  if (ShowList(false)) mListControlFrame->CaptureMouseEvents(false);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
int32_t nsComboboxControlFrame::UpdateRecentIndex(int32_t aIndex) {
Packit f0b94e
  int32_t index = mRecentSelectedIndex;
Packit f0b94e
  if (mRecentSelectedIndex == NS_SKIP_NOTIFY_INDEX ||
Packit f0b94e
      aIndex == NS_SKIP_NOTIFY_INDEX)
Packit f0b94e
    mRecentSelectedIndex = aIndex;
Packit f0b94e
  return index;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
class nsDisplayComboboxFocus : public nsDisplayItem {
Packit f0b94e
 public:
Packit f0b94e
  nsDisplayComboboxFocus(nsDisplayListBuilder* aBuilder,
Packit f0b94e
                         nsComboboxControlFrame* aFrame)
Packit f0b94e
      : nsDisplayItem(aBuilder, aFrame) {
Packit f0b94e
    MOZ_COUNT_CTOR(nsDisplayComboboxFocus);
Packit f0b94e
  }
Packit f0b94e
#ifdef NS_BUILD_REFCNT_LOGGING
Packit f0b94e
  virtual ~nsDisplayComboboxFocus() { MOZ_COUNT_DTOR(nsDisplayComboboxFocus); }
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
  virtual void Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) override;
Packit f0b94e
  NS_DISPLAY_DECL_NAME("ComboboxFocus", TYPE_COMBOBOX_FOCUS)
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
void nsDisplayComboboxFocus::Paint(nsDisplayListBuilder* aBuilder,
Packit f0b94e
                                   gfxContext* aCtx) {
Packit f0b94e
  static_cast<nsComboboxControlFrame*>(mFrame)->PaintFocus(
Packit f0b94e
      *aCtx->GetDrawTarget(), ToReferenceFrame());
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
Packit f0b94e
                                              const nsDisplayListSet& aLists) {
Packit f0b94e
  if (aBuilder->IsForEventDelivery()) {
Packit f0b94e
    // Don't allow children to receive events.
Packit f0b94e
    // REVIEW: following old GetFrameForPoint
Packit f0b94e
    DisplayBorderBackgroundOutline(aBuilder, aLists);
Packit f0b94e
  } else {
Packit f0b94e
    // REVIEW: Our in-flow child frames are inline-level so they will paint in
Packit f0b94e
    // our content list, so we don't need to mess with layers.
Packit f0b94e
    nsBlockFrame::BuildDisplayList(aBuilder, aLists);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // draw a focus indicator only when focus rings should be drawn
Packit f0b94e
  nsIDocument* doc = mContent->GetComposedDoc();
Packit f0b94e
  if (doc) {
Packit f0b94e
    nsPIDOMWindowOuter* window = doc->GetWindow();
Packit f0b94e
    if (window && window->ShouldShowFocusRing()) {
Packit f0b94e
      nsPresContext* presContext = PresContext();
Packit f0b94e
      const nsStyleDisplay* disp = StyleDisplay();
Packit f0b94e
      if ((!IsThemed(disp) ||
Packit f0b94e
           !presContext->GetTheme()->ThemeDrawsFocusForWidget(
Packit f0b94e
               disp->mAppearance)) &&
Packit f0b94e
          mDisplayFrame && IsVisibleForPainting(aBuilder)) {
Packit f0b94e
        aLists.Content()->AppendToTop(
Packit f0b94e
            MakeDisplayItem<nsDisplayComboboxFocus>(aBuilder, this));
Packit f0b94e
      }
Packit f0b94e
    }
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  DisplaySelectionOverlay(aBuilder, aLists.Content());
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::PaintFocus(DrawTarget& aDrawTarget, nsPoint aPt) {
Packit f0b94e
  /* Do we need to do anything? */
Packit f0b94e
  EventStates eventStates = mContent->AsElement()->State();
Packit f0b94e
  if (eventStates.HasState(NS_EVENT_STATE_DISABLED) || sFocused != this) return;
Packit f0b94e
Packit f0b94e
  int32_t appUnitsPerDevPixel = PresContext()->AppUnitsPerDevPixel();
Packit f0b94e
Packit f0b94e
  nsRect clipRect = mDisplayFrame->GetRect() + aPt;
Packit f0b94e
  aDrawTarget.PushClipRect(
Packit f0b94e
      NSRectToSnappedRect(clipRect, appUnitsPerDevPixel, aDrawTarget));
Packit f0b94e
Packit f0b94e
  // REVIEW: Why does the old code paint mDisplayFrame again? We've
Packit f0b94e
  // already painted it in the children above. So clipping it here won't do
Packit f0b94e
  // us much good.
Packit f0b94e
Packit f0b94e
  /////////////////////
Packit f0b94e
  // draw focus
Packit f0b94e
Packit f0b94e
  StrokeOptions strokeOptions;
Packit f0b94e
  nsLayoutUtils::InitDashPattern(strokeOptions, NS_STYLE_BORDER_STYLE_DOTTED);
Packit f0b94e
  ColorPattern color(ToDeviceColor(StyleColor()->mColor));
Packit f0b94e
  nscoord onePixel = nsPresContext::CSSPixelsToAppUnits(1);
Packit f0b94e
  clipRect.width -= onePixel;
Packit f0b94e
  clipRect.height -= onePixel;
Packit f0b94e
  Rect r = ToRect(nsLayoutUtils::RectToGfxRect(clipRect, appUnitsPerDevPixel));
Packit f0b94e
  StrokeSnappedEdgesOfRect(r, aDrawTarget, color, strokeOptions);
Packit f0b94e
Packit f0b94e
  aDrawTarget.PopClip();
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
//---------------------------------------------------------
Packit f0b94e
// gets the content (an option) by index and then set it as
Packit f0b94e
// being selected or not selected
Packit f0b94e
//---------------------------------------------------------
Packit f0b94e
NS_IMETHODIMP
Packit f0b94e
nsComboboxControlFrame::OnOptionSelected(int32_t aIndex, bool aSelected) {
Packit f0b94e
  if (mDroppedDown) {
Packit f0b94e
    nsISelectControlFrame* selectFrame = do_QueryFrame(mListControlFrame);
Packit f0b94e
    if (selectFrame) {
Packit f0b94e
      selectFrame->OnOptionSelected(aIndex, aSelected);
Packit f0b94e
    }
Packit f0b94e
  } else {
Packit f0b94e
    if (aSelected) {
Packit f0b94e
      nsAutoScriptBlocker blocker;
Packit f0b94e
      mDisplayedIndex = aIndex;
Packit f0b94e
      RedisplayText();
Packit f0b94e
    } else {
Packit f0b94e
      AutoWeakFrame weakFrame(this);
Packit f0b94e
      RedisplaySelectedText();
Packit f0b94e
      if (weakFrame.IsAlive()) {
Packit f0b94e
        FireValueChangeEvent();  // Fire after old option is unselected
Packit f0b94e
      }
Packit f0b94e
    }
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  return NS_OK;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::FireValueChangeEvent() {
Packit f0b94e
  // Fire ValueChange event to indicate data value of combo box has changed
Packit f0b94e
  nsContentUtils::AddScriptRunner(new AsyncEventDispatcher(
Packit f0b94e
      mContent, NS_LITERAL_STRING("ValueChange"), true, false));
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void nsComboboxControlFrame::OnContentReset() {
Packit f0b94e
  if (mListControlFrame) {
Packit f0b94e
    mListControlFrame->OnContentReset();
Packit f0b94e
  }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
//--------------------------------------------------------
Packit f0b94e
// nsIStatefulFrame
Packit f0b94e
//--------------------------------------------------------
Packit f0b94e
NS_IMETHODIMP
Packit f0b94e
nsComboboxControlFrame::SaveState(nsPresState** aState) {
Packit f0b94e
  MOZ_ASSERT(!(*aState));
Packit f0b94e
  (*aState) = new nsPresState();
Packit f0b94e
  (*aState)->SetDroppedDown(mDroppedDown);
Packit f0b94e
  return NS_OK;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
NS_IMETHODIMP
Packit f0b94e
nsComboboxControlFrame::RestoreState(nsPresState* aState) {
Packit f0b94e
  if (!aState) {
Packit f0b94e
    return NS_ERROR_FAILURE;
Packit f0b94e
  }
Packit f0b94e
  ShowList(aState->GetDroppedDown());  // might destroy us
Packit f0b94e
  return NS_OK;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
// Append a suffix so that the state key for the combobox is different
Packit f0b94e
// from the state key the list control uses to sometimes save the scroll
Packit f0b94e
// position for the same Element
Packit f0b94e
NS_IMETHODIMP
Packit f0b94e
nsComboboxControlFrame::GenerateStateKey(nsIContent* aContent,
Packit f0b94e
                                         nsIDocument* aDocument,
Packit f0b94e
                                         nsACString& aKey) {
Packit f0b94e
  nsresult rv = nsContentUtils::GenerateStateKey(aContent, aDocument, aKey);
Packit f0b94e
  if (NS_FAILED(rv) || aKey.IsEmpty()) {
Packit f0b94e
    return rv;
Packit f0b94e
  }
Packit f0b94e
  aKey.AppendLiteral("CCF");
Packit f0b94e
  return NS_OK;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
// Fennec uses a custom combobox built-in widget.
Packit f0b94e
//
Packit f0b94e
Packit f0b94e
/* static */
Packit f0b94e
bool nsComboboxControlFrame::ToolkitHasNativePopup() {
Packit f0b94e
#ifdef MOZ_USE_NATIVE_POPUP_WINDOWS
Packit f0b94e
  return true;
Packit f0b94e
#else
Packit f0b94e
  return false;
Packit f0b94e
#endif /* MOZ_USE_NATIVE_POPUP_WINDOWS */
Packit f0b94e
}