File indexing completed on 2024-11-10 09:39:04

0001 /*
0002  * This file is part of the DOM implementation for KDE.
0003  *
0004  * Copyright (C) 2009 Vyacheslav Tokarev (tsjoker@gmail.com)
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  */
0021 
0022 #ifndef Render_Position_H
0023 #define Render_Position_H
0024 
0025 #include "xml/dom_position.h"
0026 #include "rendering/render_object.h"
0027 
0028 using DOM::Position;
0029 
0030 namespace DOM
0031 {
0032 class NodeImpl;
0033 }
0034 
0035 using DOM::NodeImpl;
0036 
0037 namespace khtml
0038 {
0039 class InlineBox;
0040 
0041 class RenderPosition
0042 {
0043 public:
0044     // constructs empty position
0045     RenderPosition() {}
0046     RenderPosition(const Position &position) : m_position(position) {}
0047     RenderPosition(NodeImpl *node, int offset);
0048 
0049     static RenderPosition fromDOMPosition(const Position &position);
0050 
0051     Position position() const
0052     {
0053         return m_position;
0054     }
0055     const RenderObject *renderer() const
0056     {
0057         return m_position.isEmpty() ? nullptr : m_position.node()->renderer();
0058     }
0059     NodeImpl *node() const
0060     {
0061         return m_position.node();
0062     }
0063     int domOffset() const
0064     {
0065         return m_position.offset();
0066     }
0067 
0068     inline bool isEmpty() const
0069     {
0070         return m_position.isEmpty();
0071     }
0072     inline bool notEmpty() const
0073     {
0074         return m_position.notEmpty();
0075     }
0076 
0077     // QRect caretRegion() const;
0078 
0079     // int renderedOffset() const; // convert to rendered offset, though we need to eliminate other code using it
0080     int renderedOffset()
0081     {
0082         int result;
0083         getInlineBoxAndOffset(result);
0084         return result;
0085     }
0086 
0087     bool inRenderedContent()
0088     {
0089         return renderer();
0090     }
0091     static bool inRenderedContent(const Position &position)
0092     {
0093         return fromDOMPosition(position).inRenderedContent();
0094     }
0095 
0096     // implementation: same RootInlineBox
0097     static bool rendersOnSameLine(const RenderPosition &self, const RenderPosition &other);
0098     // implementation: either renderers are different, or inline boxes or offsets inside
0099     static bool rendersInDifferentPosition(const RenderPosition &self, const RenderPosition &other);
0100 
0101     // static bool rendersOnDifferentLine(); // compare inline boxes and its roots
0102 
0103     // bool isFirstOnRenderedLine() const; // either iterate backwards over DOM tree or check if we're at the beginning of the RootInlineBox?
0104     // bool isLastOnRenderedLine() const; // -||-
0105 
0106     RenderPosition previousLinePosition(int);
0107     RenderPosition nextLinePosition(int);
0108 
0109 protected:
0110     // returns rendered offset
0111     InlineBox *getInlineBoxAndOffset(int &offset) const;
0112 
0113 private:
0114     Position m_position;
0115     // affinity thing, so we'll have where to put caret at the end of inline box or at the start of next one
0116 };
0117 
0118 inline bool operator==(const RenderPosition &a, const RenderPosition &b)
0119 {
0120     return a.position() == b.position();
0121 }
0122 
0123 inline bool operator!=(const RenderPosition &a, const RenderPosition &b)
0124 {
0125     return !(a == b);
0126 }
0127 
0128 QDebug operator<<(QDebug stream, const RenderPosition &renderPosition);
0129 
0130 } // namespace
0131 
0132 #endif