File indexing completed on 2024-05-05 16:11:32

0001 /* This file is part of the KDE project
0002  *
0003  * Copyright (C) 2008 Bernhard Beschow <bbeschow AT cs DOT tu-berlin DOT de>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 #ifndef __khtml_find_p_h__
0021 #define __khtml_find_p_h__
0022 
0023 #include <QObject>
0024 
0025 #include "xml/dom_nodeimpl.h"
0026 
0027 #include <kfind.h>
0028 
0029 #include <QStringList>
0030 #include <QPointer>
0031 #include "khtmlfindbar.h"
0032 
0033 class KHTMLPart;
0034 class QString;
0035 class QWidget;
0036 class KFindDialog;
0037 class KFind;
0038 
0039 /**
0040  * This class implements the find activity for the @p KHTMLPart.
0041  *
0042  *
0043  *
0044  * @author Bernhard Beschow <bbeschow cs tu berlin de>
0045  */
0046 class KHTMLFind : public QObject
0047 {
0048     Q_OBJECT
0049 public:
0050     KHTMLFind(KHTMLPart *part, KHTMLFind *parent = nullptr);
0051     ~KHTMLFind();
0052     void findTextBegin();
0053     bool initFindNode(bool selection, bool reverse, bool fromCursor);
0054     void createNewKFind(const QString &str, long options, QWidget *parent, KFindDialog *findDialog);
0055     bool findTextNext(bool reverse = false);
0056     KHTMLFindBar *findBar() const
0057     {
0058         return m_parent ? m_parent->findBar() : m_findDialog.data();
0059     }
0060 
0061 public Q_SLOTS:
0062     void activate();
0063     void deactivate();
0064 
0065 private Q_SLOTS:
0066     void slotFindDestroyed();
0067     void slotSelectionChanged();
0068     void slotHighlight(const QString &, int index, int length);
0069     void slotSearchChanged();
0070     void slotFindNext();
0071     void slotFindPrevious();
0072 
0073 Q_SIGNALS:
0074     void foundMatch(const DOM::Selection &selection, int length);
0075 
0076 protected:
0077     KFind *find() const
0078     {
0079         return m_find;
0080     }
0081 
0082 private:
0083     KHTMLPart *m_part;
0084 
0085     struct StringPortion {
0086         // Just basic ref/deref on our node to make sure it doesn't get deleted
0087         StringPortion(int i, DOM::NodeImpl *n) : index(i), node(n)
0088         {
0089             if (node) {
0090                 node->ref();
0091             }
0092         }
0093         StringPortion() : index(0), node(nullptr) {} // for QValueList
0094         StringPortion(const StringPortion &other) : node(nullptr)
0095         {
0096             operator=(other);
0097         }
0098         StringPortion &operator=(const StringPortion &other)
0099         {
0100             index = other.index;
0101             if (other.node) {
0102                 other.node->ref();
0103             }
0104             if (node) {
0105                 node->deref();
0106             }
0107             node = other.node;
0108             return *this;
0109         }
0110         ~StringPortion()
0111         {
0112             if (node) {
0113                 node->deref();
0114             }
0115         }
0116 
0117         int index;
0118         DOM::NodeImpl *node;
0119     };
0120     QList<StringPortion> m_stringPortions;
0121 
0122     KFind *m_find;
0123     KHTMLFind *m_parent;
0124     QPointer<KHTMLFindBar> m_findDialog;
0125 
0126     struct findState {
0127         findState() : options(0), last_dir(-1) {}
0128         QStringList history;
0129         QString text;
0130         int options;
0131         int last_dir; // -1=unknown,0=forward,1=backward
0132     };
0133 
0134     findState m_lastFindState;
0135 
0136     DOM::NodeImpl *m_findNode; // current node
0137     DOM::NodeImpl *m_findNodeEnd; // end node
0138     DOM::NodeImpl *m_findNodeStart; // start node
0139     DOM::NodeImpl *m_findNodePrevious; // previous node used for find
0140     int m_findPos; // current pos in current node
0141     int m_findPosEnd; // pos in end node
0142     int m_findPosStart; // pos in start node
0143 };
0144 
0145 #endif