michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_ContentEventHandler_h_ michael@0: #define mozilla_ContentEventHandler_h_ michael@0: michael@0: #include "mozilla/EventForwards.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsISelection.h" michael@0: #include "nsRange.h" michael@0: michael@0: class nsPresContext; michael@0: michael@0: struct nsRect; michael@0: michael@0: namespace mozilla { michael@0: michael@0: enum LineBreakType michael@0: { michael@0: LINE_BREAK_TYPE_NATIVE, michael@0: LINE_BREAK_TYPE_XP michael@0: }; michael@0: michael@0: /* michael@0: * Query Content Event Handler michael@0: * ContentEventHandler is a helper class for EventStateManager. michael@0: * The platforms request some content informations, e.g., the selected text, michael@0: * the offset of the selected text and the text for specified range. michael@0: * This class answers to NS_QUERY_* events from actual contents. michael@0: */ michael@0: michael@0: class MOZ_STACK_CLASS ContentEventHandler michael@0: { michael@0: public: michael@0: ContentEventHandler(nsPresContext* aPresContext); michael@0: michael@0: // NS_QUERY_SELECTED_TEXT event handler michael@0: nsresult OnQuerySelectedText(WidgetQueryContentEvent* aEvent); michael@0: // NS_QUERY_TEXT_CONTENT event handler michael@0: nsresult OnQueryTextContent(WidgetQueryContentEvent* aEvent); michael@0: // NS_QUERY_CARET_RECT event handler michael@0: nsresult OnQueryCaretRect(WidgetQueryContentEvent* aEvent); michael@0: // NS_QUERY_TEXT_RECT event handler michael@0: nsresult OnQueryTextRect(WidgetQueryContentEvent* aEvent); michael@0: // NS_QUERY_EDITOR_RECT event handler michael@0: nsresult OnQueryEditorRect(WidgetQueryContentEvent* aEvent); michael@0: // NS_QUERY_CONTENT_STATE event handler michael@0: nsresult OnQueryContentState(WidgetQueryContentEvent* aEvent); michael@0: // NS_QUERY_SELECTION_AS_TRANSFERABLE event handler michael@0: nsresult OnQuerySelectionAsTransferable(WidgetQueryContentEvent* aEvent); michael@0: // NS_QUERY_CHARACTER_AT_POINT event handler michael@0: nsresult OnQueryCharacterAtPoint(WidgetQueryContentEvent* aEvent); michael@0: // NS_QUERY_DOM_WIDGET_HITTEST event handler michael@0: nsresult OnQueryDOMWidgetHittest(WidgetQueryContentEvent* aEvent); michael@0: michael@0: // NS_SELECTION_* event michael@0: nsresult OnSelectionEvent(WidgetSelectionEvent* aEvent); michael@0: michael@0: protected: michael@0: nsPresContext* mPresContext; michael@0: nsCOMPtr mPresShell; michael@0: nsCOMPtr mSelection; michael@0: nsRefPtr mFirstSelectedRange; michael@0: nsCOMPtr mRootContent; michael@0: michael@0: nsresult Init(WidgetQueryContentEvent* aEvent); michael@0: nsresult Init(WidgetSelectionEvent* aEvent); michael@0: michael@0: nsresult InitBasic(); michael@0: nsresult InitCommon(); michael@0: michael@0: public: michael@0: // FlatText means the text that is generated from DOM tree. The BR elements michael@0: // are replaced to native linefeeds. Other elements are ignored. michael@0: michael@0: // Get the offset in FlatText of the range. (also used by IMEContentObserver) michael@0: static nsresult GetFlatTextOffsetOfRange(nsIContent* aRootContent, michael@0: nsINode* aNode, michael@0: int32_t aNodeOffset, michael@0: uint32_t* aOffset, michael@0: LineBreakType aLineBreakType); michael@0: static nsresult GetFlatTextOffsetOfRange(nsIContent* aRootContent, michael@0: nsRange* aRange, michael@0: uint32_t* aOffset, michael@0: LineBreakType aLineBreakType); michael@0: // Get the native text length of a content node excluding any children michael@0: static uint32_t GetNativeTextLength(nsIContent* aContent, michael@0: uint32_t aMaxLength = UINT32_MAX); michael@0: protected: michael@0: static uint32_t GetTextLength(nsIContent* aContent, michael@0: LineBreakType aLineBreakType, michael@0: uint32_t aMaxLength = UINT32_MAX); michael@0: static LineBreakType GetLineBreakType(WidgetQueryContentEvent* aEvent); michael@0: static LineBreakType GetLineBreakType(WidgetSelectionEvent* aEvent); michael@0: static LineBreakType GetLineBreakType(bool aUseNativeLineBreak); michael@0: // Returns focused content (including its descendant documents). michael@0: nsIContent* GetFocusedContent(); michael@0: // Returns true if the content is a plugin host. michael@0: bool IsPlugin(nsIContent* aContent); michael@0: // QueryContentRect() sets the rect of aContent's frame(s) to aEvent. michael@0: nsresult QueryContentRect(nsIContent* aContent, michael@0: WidgetQueryContentEvent* aEvent); michael@0: // Make the DOM range from the offset of FlatText and the text length. michael@0: // If aExpandToClusterBoundaries is true, the start offset and the end one are michael@0: // expanded to nearest cluster boundaries. michael@0: nsresult SetRangeFromFlatTextOffset(nsRange* aRange, michael@0: uint32_t aOffset, michael@0: uint32_t aLength, michael@0: LineBreakType aLineBreakType, michael@0: bool aExpandToClusterBoundaries, michael@0: uint32_t* aNewOffset = nullptr); michael@0: // Find the first textframe for the range, and get the start offset in michael@0: // the frame. michael@0: nsresult GetStartFrameAndOffset(nsRange* aRange, michael@0: nsIFrame** aFrame, michael@0: int32_t* aOffsetInFrame); michael@0: // Convert the frame relative offset to the root view relative offset. michael@0: nsresult ConvertToRootViewRelativeOffset(nsIFrame* aFrame, michael@0: nsRect& aRect); michael@0: // Expand aXPOffset to the nearest offset in cluster boundary. aForward is michael@0: // true, it is expanded to forward. michael@0: nsresult ExpandToClusterBoundary(nsIContent* aContent, bool aForward, michael@0: uint32_t* aXPOffset); michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozilla_ContentEventHandler_h_