1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=2 sw=2 et tw=78: */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 1998
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : /* the caret is the text cursor used, e.g., when editing */
40 :
41 : #ifndef nsCaret_h__
42 : #define nsCaret_h__
43 :
44 : #include "nsCoord.h"
45 : #include "nsISelectionListener.h"
46 : #include "nsITimer.h"
47 : #include "nsWeakPtr.h"
48 : #include "nsFrameSelection.h"
49 :
50 : class nsRenderingContext;
51 : class nsDisplayListBuilder;
52 :
53 : //-----------------------------------------------------------------------------
54 : class nsCaret : public nsISelectionListener
55 : {
56 : public:
57 :
58 : nsCaret();
59 : virtual ~nsCaret();
60 :
61 : enum EViewCoordinates {
62 : eTopLevelWindowCoordinates,
63 : eRenderingViewCoordinates,
64 : eClosestViewCoordinates
65 : };
66 :
67 : public:
68 :
69 : NS_DECL_ISUPPORTS
70 :
71 : nsresult Init(nsIPresShell *inPresShell);
72 : void Terminate();
73 :
74 : nsISelection* GetCaretDOMSelection();
75 : nsresult SetCaretDOMSelection(nsISelection *inDOMSel);
76 :
77 : /** GetCaretVisible will get the visibility of the caret
78 : * This function is virtual so that it can be used by nsCaretAccessible
79 : * without linking
80 : * @param inMakeVisible true it is shown, false it is hidden
81 : * @return false if and only if inMakeVisible is null, otherwise true
82 : */
83 : virtual nsresult GetCaretVisible(bool *outMakeVisible);
84 :
85 : /** SetCaretVisible will set the visibility of the caret
86 : * @param inMakeVisible true to show the caret, false to hide it
87 : */
88 : void SetCaretVisible(bool intMakeVisible);
89 :
90 : /** SetCaretReadOnly set the appearance of the caret
91 : * @param inMakeReadonly true to show the caret in a 'read only' state,
92 : * false to show the caret in normal, editing state
93 : */
94 : void SetCaretReadOnly(bool inMakeReadonly);
95 :
96 : /** GetCaretReadOnly get the appearance of the caret
97 : * @return true if the caret is in 'read only' state, otherwise,
98 : * returns false
99 : */
100 : bool GetCaretReadOnly()
101 : {
102 : return mReadOnly;
103 : }
104 :
105 : /**
106 : * Gets the position and size of the caret that would be drawn for
107 : * the focus node/offset of aSelection (assuming it would be drawn,
108 : * i.e., disregarding blink status). The geometry is stored in aRect,
109 : * and we return the frame aRect is relative to.
110 : * @param aRect must be non-null
111 : * @param aBidiIndicatorSize if non-null, set to the bidi indicator size.
112 : */
113 : virtual nsIFrame* GetGeometry(nsISelection* aSelection,
114 : nsRect* aRect,
115 : nscoord* aBidiIndicatorSize = nsnull);
116 :
117 : /** EraseCaret
118 : * this will erase the caret if its drawn and reset drawn status
119 : */
120 : void EraseCaret();
121 :
122 : void SetVisibilityDuringSelection(bool aVisibility);
123 :
124 : /** DrawAtPosition
125 : *
126 : * Draw the caret explicitly, at the specified node and offset.
127 : * To avoid drawing glitches, you should call EraseCaret()
128 : * after each call to DrawAtPosition().
129 : *
130 : * Note: This call breaks the caret's ability to blink at all.
131 : **/
132 : nsresult DrawAtPosition(nsIDOMNode* aNode, PRInt32 aOffset);
133 :
134 : /** GetCaretFrame
135 : * Get the current frame that the caret should be drawn in. If the caret is
136 : * not currently visible (i.e., it is between blinks), then this will
137 : * return null.
138 : *
139 : * @param aOffset is result of the caret offset in the content.
140 : */
141 : nsIFrame* GetCaretFrame(PRInt32 *aOffset = nsnull);
142 :
143 : /** GetCaretRect
144 : * Get the current caret rect. Only call this when GetCaretFrame returns
145 : * non-null.
146 : */
147 0 : nsRect GetCaretRect()
148 : {
149 0 : nsRect r;
150 0 : r.UnionRect(mCaretRect, GetHookRect());
151 : return r;
152 : }
153 :
154 : /** InvalidateOutsideCaret
155 : * Invalidate the area that the caret currently occupies if the caret is
156 : * outside of its frame's overflow area. This is used when the content that
157 : * the caret is currently drawn is is being deleted or reflowed.
158 : */
159 : void InvalidateOutsideCaret();
160 :
161 : /** UpdateCaretPosition
162 : * Update the caret's current frame and rect, but don't draw yet. This is
163 : * useful for flickerless moving of the caret (e.g., when the frame the
164 : * caret is in reflows and is moved).
165 : */
166 : void UpdateCaretPosition();
167 :
168 : /** PaintCaret
169 : * Actually paint the caret onto the given rendering context.
170 : */
171 : void PaintCaret(nsDisplayListBuilder *aBuilder,
172 : nsRenderingContext *aCtx,
173 : nsIFrame *aForFrame,
174 : const nsPoint &aOffset);
175 : /**
176 : * Sets whether the caret should only be visible in nodes that are not
177 : * user-modify: read-only, or whether it should be visible in all nodes.
178 : *
179 : * @param aIgnoreUserModify true to have the cursor visible in all nodes,
180 : * false to have it visible in all nodes except
181 : * those with user-modify: read-only
182 : */
183 :
184 : void SetIgnoreUserModify(bool aIgnoreUserModify);
185 :
186 : //nsISelectionListener interface
187 : NS_DECL_NSISELECTIONLISTENER
188 :
189 : static void CaretBlinkCallback(nsITimer *aTimer, void *aClosure);
190 :
191 : nsresult GetCaretFrameForNodeOffset(nsIContent* aContentNode,
192 : PRInt32 aOffset,
193 : nsFrameSelection::HINT aFrameHint,
194 : PRUint8 aBidiLevel,
195 : nsIFrame** aReturnFrame,
196 : PRInt32* aReturnOffset);
197 :
198 : NS_IMETHOD CheckCaretDrawingState();
199 :
200 : protected:
201 :
202 : void KillTimer();
203 : nsresult PrimeTimer();
204 :
205 : void StartBlinking();
206 : void StopBlinking();
207 :
208 : // If the nearest block has a potential 'text-overflow' marker then
209 : // invalidate it.
210 : void InvalidateTextOverflowBlock();
211 :
212 : bool DrawAtPositionWithHint(nsIDOMNode* aNode,
213 : PRInt32 aOffset,
214 : nsFrameSelection::HINT aFrameHint,
215 : PRUint8 aBidiLevel,
216 : bool aInvalidate);
217 :
218 : struct Metrics {
219 : nscoord mBidiIndicatorSize; // width and height of bidi indicator
220 : nscoord mCaretWidth; // full caret width including bidi indicator
221 : };
222 : Metrics ComputeMetrics(nsIFrame* aFrame, PRInt32 aOffset, nscoord aCaretHeight);
223 : nsresult GetGeometryForFrame(nsIFrame* aFrame,
224 : PRInt32 aFrameOffset,
225 : nsRect* aRect,
226 : nscoord* aBidiIndicatorSize);
227 :
228 : // Returns true if the caret should be drawn. When |mDrawn| is true,
229 : // this returns true, so that we erase the drawn caret. If |aIgnoreDrawnState|
230 : // is true, we don't take into account whether the caret is currently
231 : // drawn or not. This can be used to determine if the caret is drawn when
232 : // it shouldn't be.
233 : bool MustDrawCaret(bool aIgnoreDrawnState);
234 :
235 : void DrawCaret(bool aInvalidate);
236 : void DrawCaretAfterBriefDelay();
237 : bool UpdateCaretRects(nsIFrame* aFrame, PRInt32 aFrameOffset);
238 : static void InvalidateRects(const nsRect &aRect, const nsRect &aHook,
239 : nsIFrame *aFrame);
240 0 : nsRect GetHookRect()
241 : {
242 : #ifdef IBMBIDI
243 0 : return mHookRect;
244 : #else
245 : return nsRect();
246 : #endif
247 : }
248 0 : void ToggleDrawnStatus() { mDrawn = !mDrawn; }
249 :
250 : already_AddRefed<nsFrameSelection> GetFrameSelection();
251 :
252 : // Returns true if we should not draw the caret because of XUL menu popups.
253 : // The caret should be hidden if:
254 : // 1. An open popup contains the caret, but a menu popup exists before the
255 : // caret-owning popup in the popup list (i.e. a menu is in front of the
256 : // popup with the caret). If the menu itself contains the caret we don't
257 : // hide it.
258 : // 2. A menu popup is open, but there is no caret present in any popup.
259 : // 3. The caret selection is empty.
260 : bool IsMenuPopupHidingCaret();
261 :
262 : protected:
263 :
264 : nsWeakPtr mPresShell;
265 : nsWeakPtr mDomSelectionWeak;
266 :
267 : nsCOMPtr<nsITimer> mBlinkTimer;
268 :
269 : // XXX these fields should go away and the values be acquired as needed,
270 : // probably by ComputeMetrics.
271 : PRUint32 mBlinkRate; // time for one cyle (on then off), in milliseconds
272 : nscoord mCaretWidthCSSPx; // caret width in CSS pixels
273 : float mCaretAspectRatio; // caret width/height aspect ratio
274 :
275 : bool mVisible; // is the caret blinking
276 :
277 : bool mDrawn; // Denotes when the caret is physically drawn on the screen.
278 : bool mPendingDraw; // True when the last on-state draw was suppressed.
279 :
280 : bool mReadOnly; // it the caret in readonly state (draws differently)
281 : bool mShowDuringSelection; // show when text is selected
282 :
283 : bool mIgnoreUserModify;
284 :
285 : #ifdef IBMBIDI
286 : bool mKeyboardRTL; // is the keyboard language right-to-left
287 : bool mBidiUI; // is bidi UI turned on
288 : nsRect mHookRect; // directional hook on the caret
289 : PRUint8 mLastBidiLevel; // saved bidi level of the last draw request, to use when we erase
290 : #endif
291 : nsRect mCaretRect; // the last caret rect, in the coodinates of the last frame.
292 :
293 : nsCOMPtr<nsIContent> mLastContent; // store the content the caret was last requested to be drawn
294 : // in (by DrawAtPosition()/DrawCaret()),
295 : // note that this can be different than where it was
296 : // actually drawn (anon <BR> in text control)
297 : PRInt32 mLastContentOffset; // the offset for the last request
298 :
299 : nsFrameSelection::HINT mLastHint; // the hint associated with the last request, see also
300 : // mLastBidiLevel below
301 :
302 : };
303 :
304 : // handy stack-based class for temporarily disabling the caret
305 :
306 : class StCaretHider
307 : {
308 : public:
309 0 : StCaretHider(nsCaret* aSelCon)
310 0 : : mWasVisible(false), mCaret(aSelCon)
311 : {
312 0 : if (mCaret)
313 : {
314 0 : mCaret->GetCaretVisible(&mWasVisible);
315 0 : if (mWasVisible)
316 0 : mCaret->SetCaretVisible(false);
317 : }
318 0 : }
319 :
320 0 : ~StCaretHider()
321 0 : {
322 0 : if (mCaret && mWasVisible)
323 0 : mCaret->SetCaretVisible(true);
324 : // nsCOMPtr releases mPresShell
325 0 : }
326 :
327 : protected:
328 :
329 : bool mWasVisible;
330 : nsCOMPtr<nsCaret> mCaret;
331 : };
332 :
333 : #endif //nsCaret_h__
|