|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef mozilla_ContentEventHandler_h_ |
|
7 #define mozilla_ContentEventHandler_h_ |
|
8 |
|
9 #include "mozilla/EventForwards.h" |
|
10 #include "nsCOMPtr.h" |
|
11 #include "nsISelection.h" |
|
12 #include "nsRange.h" |
|
13 |
|
14 class nsPresContext; |
|
15 |
|
16 struct nsRect; |
|
17 |
|
18 namespace mozilla { |
|
19 |
|
20 enum LineBreakType |
|
21 { |
|
22 LINE_BREAK_TYPE_NATIVE, |
|
23 LINE_BREAK_TYPE_XP |
|
24 }; |
|
25 |
|
26 /* |
|
27 * Query Content Event Handler |
|
28 * ContentEventHandler is a helper class for EventStateManager. |
|
29 * The platforms request some content informations, e.g., the selected text, |
|
30 * the offset of the selected text and the text for specified range. |
|
31 * This class answers to NS_QUERY_* events from actual contents. |
|
32 */ |
|
33 |
|
34 class MOZ_STACK_CLASS ContentEventHandler |
|
35 { |
|
36 public: |
|
37 ContentEventHandler(nsPresContext* aPresContext); |
|
38 |
|
39 // NS_QUERY_SELECTED_TEXT event handler |
|
40 nsresult OnQuerySelectedText(WidgetQueryContentEvent* aEvent); |
|
41 // NS_QUERY_TEXT_CONTENT event handler |
|
42 nsresult OnQueryTextContent(WidgetQueryContentEvent* aEvent); |
|
43 // NS_QUERY_CARET_RECT event handler |
|
44 nsresult OnQueryCaretRect(WidgetQueryContentEvent* aEvent); |
|
45 // NS_QUERY_TEXT_RECT event handler |
|
46 nsresult OnQueryTextRect(WidgetQueryContentEvent* aEvent); |
|
47 // NS_QUERY_EDITOR_RECT event handler |
|
48 nsresult OnQueryEditorRect(WidgetQueryContentEvent* aEvent); |
|
49 // NS_QUERY_CONTENT_STATE event handler |
|
50 nsresult OnQueryContentState(WidgetQueryContentEvent* aEvent); |
|
51 // NS_QUERY_SELECTION_AS_TRANSFERABLE event handler |
|
52 nsresult OnQuerySelectionAsTransferable(WidgetQueryContentEvent* aEvent); |
|
53 // NS_QUERY_CHARACTER_AT_POINT event handler |
|
54 nsresult OnQueryCharacterAtPoint(WidgetQueryContentEvent* aEvent); |
|
55 // NS_QUERY_DOM_WIDGET_HITTEST event handler |
|
56 nsresult OnQueryDOMWidgetHittest(WidgetQueryContentEvent* aEvent); |
|
57 |
|
58 // NS_SELECTION_* event |
|
59 nsresult OnSelectionEvent(WidgetSelectionEvent* aEvent); |
|
60 |
|
61 protected: |
|
62 nsPresContext* mPresContext; |
|
63 nsCOMPtr<nsIPresShell> mPresShell; |
|
64 nsCOMPtr<nsISelection> mSelection; |
|
65 nsRefPtr<nsRange> mFirstSelectedRange; |
|
66 nsCOMPtr<nsIContent> mRootContent; |
|
67 |
|
68 nsresult Init(WidgetQueryContentEvent* aEvent); |
|
69 nsresult Init(WidgetSelectionEvent* aEvent); |
|
70 |
|
71 nsresult InitBasic(); |
|
72 nsresult InitCommon(); |
|
73 |
|
74 public: |
|
75 // FlatText means the text that is generated from DOM tree. The BR elements |
|
76 // are replaced to native linefeeds. Other elements are ignored. |
|
77 |
|
78 // Get the offset in FlatText of the range. (also used by IMEContentObserver) |
|
79 static nsresult GetFlatTextOffsetOfRange(nsIContent* aRootContent, |
|
80 nsINode* aNode, |
|
81 int32_t aNodeOffset, |
|
82 uint32_t* aOffset, |
|
83 LineBreakType aLineBreakType); |
|
84 static nsresult GetFlatTextOffsetOfRange(nsIContent* aRootContent, |
|
85 nsRange* aRange, |
|
86 uint32_t* aOffset, |
|
87 LineBreakType aLineBreakType); |
|
88 // Get the native text length of a content node excluding any children |
|
89 static uint32_t GetNativeTextLength(nsIContent* aContent, |
|
90 uint32_t aMaxLength = UINT32_MAX); |
|
91 protected: |
|
92 static uint32_t GetTextLength(nsIContent* aContent, |
|
93 LineBreakType aLineBreakType, |
|
94 uint32_t aMaxLength = UINT32_MAX); |
|
95 static LineBreakType GetLineBreakType(WidgetQueryContentEvent* aEvent); |
|
96 static LineBreakType GetLineBreakType(WidgetSelectionEvent* aEvent); |
|
97 static LineBreakType GetLineBreakType(bool aUseNativeLineBreak); |
|
98 // Returns focused content (including its descendant documents). |
|
99 nsIContent* GetFocusedContent(); |
|
100 // Returns true if the content is a plugin host. |
|
101 bool IsPlugin(nsIContent* aContent); |
|
102 // QueryContentRect() sets the rect of aContent's frame(s) to aEvent. |
|
103 nsresult QueryContentRect(nsIContent* aContent, |
|
104 WidgetQueryContentEvent* aEvent); |
|
105 // Make the DOM range from the offset of FlatText and the text length. |
|
106 // If aExpandToClusterBoundaries is true, the start offset and the end one are |
|
107 // expanded to nearest cluster boundaries. |
|
108 nsresult SetRangeFromFlatTextOffset(nsRange* aRange, |
|
109 uint32_t aOffset, |
|
110 uint32_t aLength, |
|
111 LineBreakType aLineBreakType, |
|
112 bool aExpandToClusterBoundaries, |
|
113 uint32_t* aNewOffset = nullptr); |
|
114 // Find the first textframe for the range, and get the start offset in |
|
115 // the frame. |
|
116 nsresult GetStartFrameAndOffset(nsRange* aRange, |
|
117 nsIFrame** aFrame, |
|
118 int32_t* aOffsetInFrame); |
|
119 // Convert the frame relative offset to the root view relative offset. |
|
120 nsresult ConvertToRootViewRelativeOffset(nsIFrame* aFrame, |
|
121 nsRect& aRect); |
|
122 // Expand aXPOffset to the nearest offset in cluster boundary. aForward is |
|
123 // true, it is expanded to forward. |
|
124 nsresult ExpandToClusterBoundary(nsIContent* aContent, bool aForward, |
|
125 uint32_t* aXPOffset); |
|
126 }; |
|
127 |
|
128 } // namespace mozilla |
|
129 |
|
130 #endif // mozilla_ContentEventHandler_h_ |