File indexing completed on 2024-04-28 11:38:36

0001 /*
0002  * This file is part of the HTML rendering engine for KDE.
0003  *
0004  * Copyright (C) 2005 Allan Sandfeld Jensen (kde@carewolf.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 _Counter_Tree_h_
0023 #define _Counter_Tree_h_
0024 
0025 #include "misc/shared.h"
0026 #include "rendering/render_object.h"
0027 
0028 namespace khtml
0029 {
0030 
0031 class CounterReset;
0032 
0033 // This file implements a counter-tree that is used for finding all parents in counters() lookup,
0034 // and for propagating count-changes when nodes are added or removed.
0035 // Please note that only counter-reset and root can be parents here, and that render-tree parents
0036 // are just counter-tree siblings
0037 
0038 // Implementation of counter-increment and counter-content
0039 class CounterNode
0040 {
0041 public:
0042     CounterNode(RenderObject *o);
0043     virtual ~CounterNode();
0044 
0045     CounterReset *parent() const
0046     {
0047         return m_parent;
0048     }
0049     CounterNode *previousSibling() const
0050     {
0051         return m_previous;
0052     }
0053     CounterNode *nextSibling() const
0054     {
0055         return m_next;
0056     }
0057     virtual CounterNode *firstChild() const
0058     {
0059         return nullptr;
0060     }
0061     virtual CounterNode *lastChild() const
0062     {
0063         return nullptr;
0064     }
0065     virtual void insertAfter(CounterNode *newChild, CounterNode *refChild);
0066     virtual void removeChild(CounterNode *oldChild);
0067     // Convenient self-referring version of the above
0068     void remove();
0069 
0070     int value() const
0071     {
0072         return m_value;
0073     }
0074     void setValue(short v)
0075     {
0076         m_value = v;
0077     }
0078     int count() const
0079     {
0080         return m_count;
0081     }
0082 
0083     virtual bool isReset()
0084     {
0085         return false;
0086     }
0087     virtual void recount(bool first = false);
0088     virtual void setSelfDirty();
0089     virtual void setParentDirty();
0090 
0091     bool hasCounters() const
0092     {
0093         return m_hasCounters;
0094     }
0095     bool isVisual() const
0096     {
0097         return m_isVisual;
0098     }
0099     void setHasCounters();
0100     void setIsVisual()
0101     {
0102         m_isVisual = true;
0103     }
0104     bool isRoot()
0105     {
0106         return m_renderer && m_renderer->isRoot();
0107     }
0108 
0109     void setRenderer(RenderObject *o)
0110     {
0111         m_renderer = o;
0112     }
0113     RenderObject *renderer() const
0114     {
0115         return m_renderer;
0116     }
0117 
0118     friend class CounterReset;
0119 protected:
0120     bool m_hasCounters : 1;
0121     bool m_isVisual : 1;
0122     short m_value;
0123     short m_count;
0124     CounterReset *m_parent;
0125     CounterNode *m_previous;
0126     CounterNode *m_next;
0127     RenderObject *m_renderer;
0128 };
0129 
0130 // Implementation of counter-reset and root
0131 class CounterReset : public CounterNode
0132 {
0133 public:
0134     CounterReset(RenderObject *o);
0135     virtual ~CounterReset();
0136 
0137     CounterNode *firstChild() const override
0138     {
0139         return m_first;
0140     }
0141     CounterNode *lastChild() const override
0142     {
0143         return m_last;
0144     }
0145     void insertAfter(CounterNode *newChild, CounterNode *refChild) override;
0146     void removeChild(CounterNode *oldChild) override;
0147 
0148     bool isReset() override
0149     {
0150         return true;
0151     }
0152     void recount(bool first = false) override;
0153     void setSelfDirty() override;
0154     void setParentDirty() override;
0155 
0156     void updateTotal(int value);
0157     // The highest value among children
0158     int total() const
0159     {
0160         return m_total;
0161     }
0162 
0163 protected:
0164     int m_total;
0165     CounterNode *m_first;
0166     CounterNode *m_last;
0167 };
0168 
0169 } // namespace
0170 
0171 #endif
0172