summaryrefslogtreecommitdiff
path: root/sw/inc/ndhints.hxx
blob: 86e7fd452ff86d974088dac739849700a7083638 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#ifndef INCLUDED_SW_INC_NDHINTS_HXX
#define INCLUDED_SW_INC_NDHINTS_HXX

#include <tools/mempool.hxx>
#include <o3tl/sorted_vector.hxx>

#include "swtypes.hxx"

class SwTxtNode;
class SwRegHistory;                 // Is in RolBck.hxx.
class SwTxtAttr;
class SwTxtAttrNesting;

class SfxPoolItem;
class SfxItemSet;
class SwDoc;

typedef enum {
    COPY = int(true),
    NEW  = int(false),
} CopyOrNew_t;

/// if COPY then pTxtNode must be given!
SwTxtAttr * MakeTxtAttr(
    SwDoc & rDoc,
    SfxPoolItem & rNew,
    sal_Int32 const nStt,
    sal_Int32 const nEnd,
    CopyOrNew_t const bIsCopy = NEW,
    SwTxtNode *const pTxtNode = 0 );

SwTxtAttr * MakeTxtAttr(
    SwDoc & rDoc,
    const SfxItemSet & rSet,
    sal_Int32 nStt,
    sal_Int32 nEnd );

/// create redline dummy text hint that must not be inserted into hints array
SwTxtAttr* MakeRedlineTxtAttr(
    SwDoc & rDoc,
    SfxPoolItem& rAttr );

/** Class SwpHints is derived indirectly via SwpHts, because only the
   class SwTxtNode should be allowed to insert and remove attributes.
   Other classes like the Frames are given only reading access via
   the index-operator.
   Size when created is 1 because an array is created only if
   also a hint is inserted. */

 /// Class SwpHtStart/End

struct CompareSwpHtStart
{
    bool operator()(SwTxtAttr* const lhs, SwTxtAttr* const rhs) const;
};
class SwpHtStart : public o3tl::sorted_vector<SwTxtAttr*, CompareSwpHtStart,
    o3tl::find_partialorder_ptrequals> {};

struct CompareSwpHtEnd
{
    bool operator()(SwTxtAttr* const lhs, SwTxtAttr* const rhs) const;
};
class SwpHtEnd : public o3tl::sorted_vector<SwTxtAttr*, CompareSwpHtEnd,
    o3tl::find_partialorder_ptrequals> {};

/// Class SwpHintsArr

/// the Hints array
class SwpHintsArray
{

protected:
    SwpHtStart m_HintStarts;
    SwpHtEnd   m_HintEnds;

    // SAL_MAX_SIZE is used by GetStartOf to return
    // failure, so just allow SAL_MAX_SIZE-1 hints
    static const size_t MAX_HINTS = SAL_MAX_SIZE-1;

    //FIXME: why are the non-const methods public?
public:
    void Insert( const SwTxtAttr *pHt );
    void DeleteAtPos( const size_t nPosInStart );
    void Resort();
    SwTxtAttr * Cut( const size_t nPosInStart )
    {
        SwTxtAttr *pHt = GetTextHint(nPosInStart);
        DeleteAtPos( nPosInStart );
        return pHt;
    }
    const SwTxtAttr * GetStart( const size_t nPos ) const
    {
        assert(nPos < m_HintStarts.size());
        return m_HintStarts[nPos];
    }
    const SwTxtAttr * GetEnd( const size_t nPos ) const
    {
        assert(nPos < m_HintEnds.size());
        return m_HintEnds[nPos];
    }
    SwTxtAttr * GetStart( const size_t nPos )
    {
        assert(nPos < m_HintStarts.size());
        return m_HintStarts[nPos];
    }
    SwTxtAttr * GetEnd( const size_t nPos )
    {
        assert(nPos < m_HintEnds.size());
        return m_HintEnds[nPos];
    }

    size_t GetEndCount()   const { return m_HintEnds.size(); }
    size_t GetStartCount() const { return m_HintStarts.size(); }

    size_t GetStartOf( const SwTxtAttr *pHt ) const
    {
        SwpHtStart::const_iterator const it =
            m_HintStarts.find(const_cast<SwTxtAttr*>(pHt));
        if ( it == m_HintStarts.end() )
        {
            return SAL_MAX_SIZE;
        }
        return it - m_HintStarts.begin();
    }

    bool Contains( const SwTxtAttr *pHt ) const;

    const SwTxtAttr * GetTextHint( const size_t nIdx ) const
        { return GetStart(nIdx); }
    SwTxtAttr * GetTextHint( const size_t nIdx )
        { return GetStart(nIdx); }
    const SwTxtAttr * operator[]( const size_t nIdx ) const
        { return GetStart(nIdx); }
    size_t Count() const { return GetStartCount(); }

#ifdef DBG_UTIL
    bool Check(bool) const;
#endif
};

// Class SwpHints

/// public interface
class SwpHints : public SwpHintsArray
{
private:
    SwRegHistory* m_pHistory;          ///< for Undo

    bool m_bFontChange          : 1;   ///< font change
    /// true: the Node is in Split and Frames are moved
    bool m_bInSplitNode         : 1;
    /// m_bHasHiddenParaField is invalid, call CalcHiddenParaField()
    bool m_bCalcHiddenParaField : 1;
    bool m_bHasHiddenParaField  : 1;   ///< HiddenParaFld
    bool m_bFootnote            : 1;   ///< footnotes
    bool m_bDDEFields           : 1;   ///< the TextNode has DDE fields

    /// records a new attibute in m_pHistory.
    void NoteInHistory( SwTxtAttr *pAttr, const bool bNew = false );

    void CalcFlags( );

    /** Delete methods may only be called by the TextNode!
       Because the TextNode also guarantees removal of the Character for
       attributes without an end. */
    friend class SwTxtNode;
    void DeleteAtPos( const size_t nPos );
    /// Delete the given Hint. The Hint must actually be in the array!
    void Delete( SwTxtAttr* pTxtHt );

    void SetInSplitNode(bool bInSplit) { m_bInSplitNode = bInSplit; }
    void SetCalcHiddenParaField() { m_bCalcHiddenParaField = true; }
    void SetHiddenParaField( const bool bNew ) { m_bHasHiddenParaField = bNew; }
    bool HasHiddenParaField() const
    {
        if ( m_bCalcHiddenParaField )
        {
            (const_cast<SwpHints*>(this))->CalcHiddenParaField();
        }
        return m_bHasHiddenParaField;
    }

    void InsertNesting(SwTxtAttrNesting & rNewHint);
    bool TryInsertNesting(SwTxtNode & rNode, SwTxtAttrNesting & rNewHint);
    void BuildPortions( SwTxtNode& rNode, SwTxtAttr& rNewHint,
            const SetAttrMode nMode );
    bool MergePortions( SwTxtNode& rNode );

public:
    SwpHints();

    bool CanBeDeleted() const    { return !Count(); }

    /// register a History, which receives all attribute changes (for Undo)
    void Register( SwRegHistory* pHist ) { m_pHistory = pHist; }
    /// deregister the currently registered History
    void DeRegister() { Register(0); }
    SwRegHistory* GetHistory() const    { return m_pHistory; }

    /// try to insert the hint
    /// @return true iff hint successfully inserted
    bool TryInsertHint( SwTxtAttr * const pHint, SwTxtNode & rNode,
            const SetAttrMode nMode = SetAttrMode::DEFAULT );

    bool HasFtn() const          { return m_bFootnote; }
    bool IsInSplitNode() const   { return m_bInSplitNode; }

    /// calc current value of m_bHasHiddenParaField, returns true iff changed
    bool CalcHiddenParaField();

    DECL_FIXEDMEMPOOL_NEWDEL(SwpHints)
};

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */