layout/forms/nsTextControlFrame.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/forms/nsTextControlFrame.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,312 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsTextControlFrame_h___
    1.10 +#define nsTextControlFrame_h___
    1.11 +
    1.12 +#include "mozilla/Attributes.h"
    1.13 +#include "nsContainerFrame.h"
    1.14 +#include "nsIAnonymousContentCreator.h"
    1.15 +#include "nsITextControlFrame.h"
    1.16 +#include "nsITextControlElement.h"
    1.17 +#include "nsIStatefulFrame.h"
    1.18 +
    1.19 +class nsISelectionController;
    1.20 +class EditorInitializerEntryTracker;
    1.21 +class nsTextEditorState;
    1.22 +class nsIEditor;
    1.23 +namespace mozilla {
    1.24 +namespace dom {
    1.25 +class Element;
    1.26 +}
    1.27 +}
    1.28 +
    1.29 +class nsTextControlFrame : public nsContainerFrame,
    1.30 +                           public nsIAnonymousContentCreator,
    1.31 +                           public nsITextControlFrame,
    1.32 +                           public nsIStatefulFrame
    1.33 +{
    1.34 +public:
    1.35 +  NS_DECL_FRAMEARENA_HELPERS
    1.36 +
    1.37 +  NS_DECLARE_FRAME_PROPERTY(ContentScrollPos, DestroyPoint)
    1.38 +
    1.39 +  nsTextControlFrame(nsIPresShell* aShell, nsStyleContext* aContext);
    1.40 +  virtual ~nsTextControlFrame();
    1.41 +
    1.42 +  virtual void DestroyFrom(nsIFrame* aDestructRoot) MOZ_OVERRIDE;
    1.43 +
    1.44 +  virtual nsIScrollableFrame* GetScrollTargetFrame() MOZ_OVERRIDE {
    1.45 +    return do_QueryFrame(GetFirstPrincipalChild());
    1.46 +  }
    1.47 +
    1.48 +  virtual nscoord GetMinWidth(nsRenderingContext* aRenderingContext) MOZ_OVERRIDE;
    1.49 +  virtual nscoord GetPrefWidth(nsRenderingContext* aRenderingContext) MOZ_OVERRIDE;
    1.50 +
    1.51 +  virtual nsSize ComputeAutoSize(nsRenderingContext *aRenderingContext,
    1.52 +                                 nsSize aCBSize, nscoord aAvailableWidth,
    1.53 +                                 nsSize aMargin, nsSize aBorder,
    1.54 +                                 nsSize aPadding, bool aShrinkWrap) MOZ_OVERRIDE;
    1.55 +
    1.56 +  virtual nsresult Reflow(nsPresContext*           aPresContext,
    1.57 +                          nsHTMLReflowMetrics&     aDesiredSize,
    1.58 +                          const nsHTMLReflowState& aReflowState,
    1.59 +                          nsReflowStatus&          aStatus) MOZ_OVERRIDE;
    1.60 +
    1.61 +  virtual nsSize GetMinSize(nsBoxLayoutState& aBoxLayoutState) MOZ_OVERRIDE;
    1.62 +  virtual bool IsCollapsed() MOZ_OVERRIDE;
    1.63 +
    1.64 +  virtual bool IsLeaf() const MOZ_OVERRIDE;
    1.65 +  
    1.66 +#ifdef ACCESSIBILITY
    1.67 +  virtual mozilla::a11y::AccType AccessibleType() MOZ_OVERRIDE;
    1.68 +#endif
    1.69 +
    1.70 +#ifdef DEBUG_FRAME_DUMP
    1.71 +  virtual nsresult GetFrameName(nsAString& aResult) const MOZ_OVERRIDE
    1.72 +  {
    1.73 +    aResult.AssignLiteral("nsTextControlFrame");
    1.74 +    return NS_OK;
    1.75 +  }
    1.76 +#endif
    1.77 +
    1.78 +  virtual bool IsFrameOfType(uint32_t aFlags) const MOZ_OVERRIDE
    1.79 +  {
    1.80 +    // nsStackFrame is already both of these, but that's somewhat bogus,
    1.81 +    // and we really mean it.
    1.82 +    return nsContainerFrame::IsFrameOfType(aFlags &
    1.83 +      ~(nsIFrame::eReplaced | nsIFrame::eReplacedContainsBlock));
    1.84 +  }
    1.85 +
    1.86 +  // nsIAnonymousContentCreator
    1.87 +  virtual nsresult CreateAnonymousContent(nsTArray<ContentInfo>& aElements) MOZ_OVERRIDE;
    1.88 +  virtual void AppendAnonymousContentTo(nsBaseContentList& aElements,
    1.89 +                                        uint32_t aFilter) MOZ_OVERRIDE;
    1.90 +
    1.91 +  // Utility methods to set current widget state
    1.92 +
    1.93 +  virtual nsresult SetInitialChildList(ChildListID     aListID,
    1.94 +                                       nsFrameList&    aChildList) MOZ_OVERRIDE;
    1.95 +
    1.96 +  virtual void BuildDisplayList(nsDisplayListBuilder*   aBuilder,
    1.97 +                                const nsRect&           aDirtyRect,
    1.98 +                                const nsDisplayListSet& aLists) MOZ_OVERRIDE;
    1.99 +
   1.100 +  virtual mozilla::dom::Element* GetPseudoElement(nsCSSPseudoElements::Type aType) MOZ_OVERRIDE;
   1.101 +
   1.102 +//==== BEGIN NSIFORMCONTROLFRAME
   1.103 +  virtual void SetFocus(bool aOn , bool aRepaint) MOZ_OVERRIDE; 
   1.104 +  virtual nsresult SetFormProperty(nsIAtom* aName, const nsAString& aValue) MOZ_OVERRIDE;
   1.105 +
   1.106 +//==== END NSIFORMCONTROLFRAME
   1.107 +
   1.108 +//==== NSITEXTCONTROLFRAME
   1.109 +
   1.110 +  NS_IMETHOD    GetEditor(nsIEditor **aEditor) MOZ_OVERRIDE;
   1.111 +  NS_IMETHOD    SetSelectionStart(int32_t aSelectionStart) MOZ_OVERRIDE;
   1.112 +  NS_IMETHOD    SetSelectionEnd(int32_t aSelectionEnd) MOZ_OVERRIDE;
   1.113 +  NS_IMETHOD    SetSelectionRange(int32_t aSelectionStart,
   1.114 +                                  int32_t aSelectionEnd,
   1.115 +                                  SelectionDirection aDirection = eNone) MOZ_OVERRIDE;
   1.116 +  NS_IMETHOD    GetSelectionRange(int32_t* aSelectionStart,
   1.117 +                                  int32_t* aSelectionEnd,
   1.118 +                                  SelectionDirection* aDirection = nullptr) MOZ_OVERRIDE;
   1.119 +  NS_IMETHOD    GetOwnedSelectionController(nsISelectionController** aSelCon) MOZ_OVERRIDE;
   1.120 +  virtual nsFrameSelection* GetOwnedFrameSelection() MOZ_OVERRIDE;
   1.121 +
   1.122 +  nsresult GetPhonetic(nsAString& aPhonetic) MOZ_OVERRIDE;
   1.123 +
   1.124 +  /**
   1.125 +   * Ensure mEditor is initialized with the proper flags and the default value.
   1.126 +   * @throws NS_ERROR_NOT_INITIALIZED if mEditor has not been created
   1.127 +   * @throws various and sundry other things
   1.128 +   */
   1.129 +  virtual nsresult EnsureEditorInitialized() MOZ_OVERRIDE;
   1.130 +
   1.131 +//==== END NSITEXTCONTROLFRAME
   1.132 +
   1.133 +//==== NSISTATEFULFRAME
   1.134 +
   1.135 +  NS_IMETHOD SaveState(nsPresState** aState) MOZ_OVERRIDE;
   1.136 +  NS_IMETHOD RestoreState(nsPresState* aState) MOZ_OVERRIDE;
   1.137 +
   1.138 +//=== END NSISTATEFULFRAME
   1.139 +
   1.140 +//==== OVERLOAD of nsIFrame
   1.141 +  virtual nsIAtom* GetType() const MOZ_OVERRIDE;
   1.142 +
   1.143 +  /** handler for attribute changes to mContent */
   1.144 +  virtual nsresult AttributeChanged(int32_t         aNameSpaceID,
   1.145 +                                    nsIAtom*        aAttribute,
   1.146 +                                    int32_t         aModType) MOZ_OVERRIDE;
   1.147 +
   1.148 +  nsresult GetText(nsString& aText);
   1.149 +
   1.150 +  virtual nsresult PeekOffset(nsPeekOffsetStruct *aPos) MOZ_OVERRIDE;
   1.151 +
   1.152 +  NS_DECL_QUERYFRAME
   1.153 +
   1.154 +  // Temp reference to scriptrunner
   1.155 +  // We could make these auto-Revoking via the "delete" entry for safety
   1.156 +  NS_DECLARE_FRAME_PROPERTY(TextControlInitializer, nullptr)
   1.157 +
   1.158 +protected:
   1.159 +  /**
   1.160 +   * Launch the reflow on the child frames - see nsTextControlFrame::Reflow()
   1.161 +   */
   1.162 +  void ReflowTextControlChild(nsIFrame*                aFrame,
   1.163 +                              nsPresContext*           aPresContext,
   1.164 +                              const nsHTMLReflowState& aReflowState,
   1.165 +                              nsReflowStatus&          aStatus,
   1.166 +                              nsHTMLReflowMetrics& aParentDesiredSize);
   1.167 +
   1.168 +public: //for methods who access nsTextControlFrame directly
   1.169 +  void SetValueChanged(bool aValueChanged);
   1.170 +  
   1.171 +  // called by the focus listener
   1.172 +  nsresult MaybeBeginSecureKeyboardInput();
   1.173 +  void MaybeEndSecureKeyboardInput();
   1.174 +
   1.175 +#define DEFINE_TEXTCTRL_FORWARDER(type, name)                                  \
   1.176 +  type name() {                                                                \
   1.177 +    nsCOMPtr<nsITextControlElement> txtCtrl = do_QueryInterface(GetContent()); \
   1.178 +    NS_ASSERTION(txtCtrl, "Content not a text control element");               \
   1.179 +    return txtCtrl->name();                                                    \
   1.180 +  }
   1.181 +#define DEFINE_TEXTCTRL_CONST_FORWARDER(type, name)                            \
   1.182 +  type name() const {                                                          \
   1.183 +    nsCOMPtr<nsITextControlElement> txtCtrl = do_QueryInterface(GetContent()); \
   1.184 +    NS_ASSERTION(txtCtrl, "Content not a text control element");               \
   1.185 +    return txtCtrl->name();                                                    \
   1.186 +  }
   1.187 +
   1.188 +  DEFINE_TEXTCTRL_CONST_FORWARDER(bool, IsSingleLineTextControl)
   1.189 +  DEFINE_TEXTCTRL_CONST_FORWARDER(bool, IsTextArea)
   1.190 +  DEFINE_TEXTCTRL_CONST_FORWARDER(bool, IsPlainTextControl)
   1.191 +  DEFINE_TEXTCTRL_CONST_FORWARDER(bool, IsPasswordTextControl)
   1.192 +  DEFINE_TEXTCTRL_FORWARDER(int32_t, GetCols)
   1.193 +  DEFINE_TEXTCTRL_FORWARDER(int32_t, GetWrapCols)
   1.194 +  DEFINE_TEXTCTRL_FORWARDER(int32_t, GetRows)
   1.195 +
   1.196 +#undef DEFINE_TEXTCTRL_CONST_FORWARDER
   1.197 +#undef DEFINE_TEXTCTRL_FORWARDER
   1.198 +
   1.199 +protected:
   1.200 +  class EditorInitializer;
   1.201 +  friend class EditorInitializer;
   1.202 +  friend class nsTextEditorState; // needs access to UpdateValueDisplay
   1.203 +
   1.204 +  class EditorInitializer : public nsRunnable {
   1.205 +  public:
   1.206 +    EditorInitializer(nsTextControlFrame* aFrame) :
   1.207 +      mFrame(aFrame) {}
   1.208 +
   1.209 +    NS_IMETHOD Run() MOZ_OVERRIDE;
   1.210 +
   1.211 +    // avoids use of nsWeakFrame
   1.212 +    void Revoke() {
   1.213 +      mFrame = nullptr;
   1.214 +    }
   1.215 +
   1.216 +  private:
   1.217 +    nsTextControlFrame* mFrame;
   1.218 +  };
   1.219 +
   1.220 +  class ScrollOnFocusEvent;
   1.221 +  friend class ScrollOnFocusEvent;
   1.222 +
   1.223 +  class ScrollOnFocusEvent : public nsRunnable {
   1.224 +  public:
   1.225 +    ScrollOnFocusEvent(nsTextControlFrame* aFrame) :
   1.226 +      mFrame(aFrame) {}
   1.227 +
   1.228 +    NS_DECL_NSIRUNNABLE
   1.229 +
   1.230 +    void Revoke() {
   1.231 +      mFrame = nullptr;
   1.232 +    }
   1.233 +
   1.234 +  private:
   1.235 +    nsTextControlFrame* mFrame;
   1.236 +  };
   1.237 +
   1.238 +  nsresult OffsetToDOMPoint(int32_t aOffset, nsIDOMNode** aResult, int32_t* aPosition);
   1.239 +
   1.240 +  /**
   1.241 +   * Update the textnode under our anonymous div to show the new
   1.242 +   * value. This should only be called when we have no editor yet.
   1.243 +   * @throws NS_ERROR_UNEXPECTED if the div has no text content
   1.244 +   */
   1.245 +  nsresult UpdateValueDisplay(bool aNotify,
   1.246 +                              bool aBeforeEditorInit = false,
   1.247 +                              const nsAString *aValue = nullptr);
   1.248 +
   1.249 +  /**
   1.250 +   * Get the maxlength attribute
   1.251 +   * @param aMaxLength the value of the max length attr
   1.252 +   * @returns false if attr not defined
   1.253 +   */
   1.254 +  bool GetMaxLength(int32_t* aMaxLength);
   1.255 +
   1.256 +  /**
   1.257 +   * Find out whether an attribute exists on the content or not.
   1.258 +   * @param aAtt the attribute to determine the existence of
   1.259 +   * @returns false if it does not exist
   1.260 +   */
   1.261 +  bool AttributeExists(nsIAtom *aAtt) const
   1.262 +  { return mContent && mContent->HasAttr(kNameSpaceID_None, aAtt); }
   1.263 +
   1.264 +  /**
   1.265 +   * We call this when we are being destroyed or removed from the PFM.
   1.266 +   * @param aPresContext the current pres context
   1.267 +   */
   1.268 +  void PreDestroy();
   1.269 +
   1.270 +  // Compute our intrinsic size.  This does not include any borders, paddings,
   1.271 +  // etc.  Just the size of our actual area for the text (and the scrollbars,
   1.272 +  // for <textarea>).
   1.273 +  nsresult CalcIntrinsicSize(nsRenderingContext* aRenderingContext,
   1.274 +                             nsSize&             aIntrinsicSize,
   1.275 +                             float               aFontSizeInflation);
   1.276 +
   1.277 +  nsresult ScrollSelectionIntoView() MOZ_OVERRIDE;
   1.278 +
   1.279 +private:
   1.280 +  //helper methods
   1.281 +  nsresult SetSelectionInternal(nsIDOMNode *aStartNode, int32_t aStartOffset,
   1.282 +                                nsIDOMNode *aEndNode, int32_t aEndOffset,
   1.283 +                                SelectionDirection aDirection = eNone);
   1.284 +  nsresult SelectAllOrCollapseToEndOfText(bool aSelect);
   1.285 +  nsresult SetSelectionEndPoints(int32_t aSelStart, int32_t aSelEnd,
   1.286 +                                 SelectionDirection aDirection = eNone);
   1.287 +
   1.288 +  /**
   1.289 +   * Return the root DOM element, and implicitly initialize the editor if needed.
   1.290 +   */
   1.291 +  mozilla::dom::Element* GetRootNodeAndInitializeEditor();
   1.292 +  nsresult GetRootNodeAndInitializeEditor(nsIDOMElement **aRootElement);
   1.293 +
   1.294 +  void FinishedInitializer() {
   1.295 +    Properties().Delete(TextControlInitializer());
   1.296 +  }
   1.297 +
   1.298 +private:
   1.299 +  // these packed bools could instead use the high order bits on mState, saving 4 bytes 
   1.300 +  bool mEditorHasBeenInitialized;
   1.301 +  bool mIsProcessing;
   1.302 +  // Keep track if we have asked a placeholder node creation.
   1.303 +  bool mUsePlaceholder;
   1.304 +
   1.305 +#ifdef DEBUG
   1.306 +  bool mInEditorInitialization;
   1.307 +  friend class EditorInitializerEntryTracker;
   1.308 +#endif
   1.309 +
   1.310 +  nsRevocableEventPtr<ScrollOnFocusEvent> mScrollEvent;
   1.311 +};
   1.312 +
   1.313 +#endif
   1.314 +
   1.315 +

mercurial