Home | History | Annotate | Download | only in editing
      1 /*
      2  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef Caret_h
     27 #define Caret_h
     28 
     29 #include "core/editing/VisiblePosition.h"
     30 #include "platform/geometry/IntRect.h"
     31 #include "platform/geometry/LayoutRect.h"
     32 #include "wtf/Noncopyable.h"
     33 
     34 namespace blink {
     35 
     36 class LocalFrame;
     37 class GraphicsContext;
     38 class PositionWithAffinity;
     39 class RenderBlock;
     40 class RenderView;
     41 
     42 class CaretBase {
     43     WTF_MAKE_NONCOPYABLE(CaretBase);
     44     WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
     45 protected:
     46     enum CaretVisibility { Visible, Hidden };
     47     explicit CaretBase(CaretVisibility = Hidden);
     48 
     49     void invalidateCaretRect(Node*, bool caretRectChanged = false);
     50     void clearCaretRect();
     51     // Creating VisiblePosition causes synchronous layout so we should use the
     52     // PositionWithAffinity version if possible.
     53     // A position in HTMLTextFromControlElement is a typical example.
     54     bool updateCaretRect(Document*, const PositionWithAffinity& caretPosition);
     55     bool updateCaretRect(Document*, const VisiblePosition& caretPosition);
     56     IntRect absoluteBoundsForLocalRect(Node*, const LayoutRect&) const;
     57     bool shouldRepaintCaret(const RenderView*, bool isContentEditable) const;
     58     void paintCaret(Node*, GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect) const;
     59 
     60     const LayoutRect& localCaretRectWithoutUpdate() const { return m_caretLocalRect; }
     61 
     62     bool shouldUpdateCaretRect() const { return m_caretRectNeedsUpdate; }
     63     void setCaretRectNeedsUpdate() { m_caretRectNeedsUpdate = true; }
     64 
     65     void setCaretVisibility(CaretVisibility visibility) { m_caretVisibility = visibility; }
     66     bool caretIsVisible() const { return m_caretVisibility == Visible; }
     67     CaretVisibility caretVisibility() const { return m_caretVisibility; }
     68 
     69 protected:
     70     static RenderBlock* caretRenderer(Node*);
     71     static void invalidateLocalCaretRect(Node*, const LayoutRect&);
     72 
     73 private:
     74     LayoutRect m_caretLocalRect; // caret rect in coords local to the renderer responsible for painting the caret
     75     bool m_caretRectNeedsUpdate; // true if m_caretRect (and m_absCaretBounds in FrameSelection) need to be calculated
     76     CaretVisibility m_caretVisibility;
     77 };
     78 
     79 class DragCaretController FINAL : public NoBaseWillBeGarbageCollected<DragCaretController>, private CaretBase {
     80     WTF_MAKE_NONCOPYABLE(DragCaretController);
     81     WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
     82 public:
     83     static PassOwnPtrWillBeRawPtr<DragCaretController> create();
     84 
     85     RenderBlock* caretRenderer() const;
     86     void paintDragCaret(LocalFrame*, GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect) const;
     87 
     88     bool isContentEditable() const { return m_position.rootEditableElement(); }
     89     bool isContentRichlyEditable() const;
     90 
     91     bool hasCaret() const { return m_position.isNotNull(); }
     92     const VisiblePosition& caretPosition() { return m_position; }
     93     void setCaretPosition(const VisiblePosition&);
     94     void clear() { setCaretPosition(VisiblePosition()); }
     95 
     96     void nodeWillBeRemoved(Node&);
     97 
     98     void trace(Visitor*);
     99 
    100 private:
    101     DragCaretController();
    102 
    103     VisiblePosition m_position;
    104 };
    105 
    106 } // namespace blink
    107 
    108 
    109 #endif // Caret_h
    110