Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/vcl/toolkit/treelist.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#pragma once
21
22
#if !defined(VCL_DLLIMPLEMENTATION) && !defined(TOOLKIT_DLLIMPLEMENTATION) && !defined(VCL_INTERNALS)
23
#error "don't use this in new code"
24
#endif
25
26
#include <config_options.h>
27
#include <vcl/dllapi.h>
28
#include <vcl/toolkit/treelistentries.hxx>
29
30
#include <tools/link.hxx>
31
#include <tools/contnr.hxx>
32
33
#include <memory>
34
35
enum class SvListAction
36
{
37
    INSERTED         = 1,
38
    REMOVING         = 2,
39
    REMOVED          = 3,
40
    MOVING           = 4,
41
    MOVED            = 5,
42
    CLEARING         = 6,
43
    INSERTED_TREE    = 7,
44
    INVALIDATE_ENTRY = 8,
45
    RESORTING        = 9,
46
    RESORTED         = 10,
47
    CLEARED          = 11
48
};
49
50
class SvTreeListEntry;
51
class SvListView;
52
class SvViewDataEntry;
53
54
enum class SvSortMode { Ascending, Descending, None };
55
56
// For the return values of Sortlink:
57
// See International::Compare( pLeft, pRight )
58
// ( Compare(a,b) ==> b.Compare(a) ==> strcmp(a,b) )
59
struct SvSortData
60
{
61
    const SvTreeListEntry* pLeft;
62
    const SvTreeListEntry* pRight;
63
};
64
65
class UNLESS_MERGELIBS_MORE(VCL_DLLPUBLIC) SvTreeList final
66
{
67
    friend class        SvListView;
68
69
    SvListView&         mrOwnerListView;
70
    sal_uInt32          nEntryCount;
71
72
    Link<SvTreeListEntry*, SvTreeListEntry*>  aCloneLink;
73
    Link<const SvSortData&, sal_Int32>        aCompareLink;
74
    SvSortMode          eSortMode;
75
76
    bool                bAbsPositionsValid;
77
78
    bool mbEnableInvalidate;
79
80
0
    SvTreeListEntry*        FirstVisible() const { return First(); }
81
    SvTreeListEntry*        NextVisible( const SvListView*,SvTreeListEntry* pEntry, sal_uInt16* pDepth=nullptr ) const;
82
    SvTreeListEntry*        PrevVisible( const SvListView*,SvTreeListEntry* pEntry ) const;
83
    SvTreeListEntry*        LastVisible( const SvListView* ) const;
84
    SvTreeListEntry*        NextVisible( const SvListView*,SvTreeListEntry* pEntry, sal_uInt16& rDelta ) const;
85
    SvTreeListEntry*        PrevVisible( const SvListView*,SvTreeListEntry* pEntry, sal_uInt16& rDelta ) const;
86
87
    bool               IsEntryVisible( const SvListView*,SvTreeListEntry* pEntry ) const;
88
    SvTreeListEntry*    GetEntryAtVisPos( const SvListView*, sal_uInt32 nVisPos ) const;
89
    sal_uInt32           GetVisiblePos( const SvListView*,SvTreeListEntry const * pEntry ) const;
90
    sal_uInt32           GetVisibleCount( SvListView* ) const;
91
    sal_uInt32           GetVisibleChildCount( const SvListView*,SvTreeListEntry* pParent ) const;
92
93
    SvTreeListEntry*        FirstSelected( const SvListView*) const;
94
    SvTreeListEntry*        NextSelected( const SvListView*,SvTreeListEntry* pEntry ) const;
95
96
    sal_uInt32           GetChildSelectionCount( const SvListView*,SvTreeListEntry* pParent ) const;
97
98
    SAL_DLLPRIVATE void SetAbsolutePositions();
99
100
    SAL_DLLPRIVATE void CloneChildren(
101
        SvTreeListEntries& rDst, sal_uInt32& rCloneCount, SvTreeListEntries& rSrc, SvTreeListEntry& rNewParent) const;
102
103
    /**
104
     * Invalidate the cached position data to have them re-generated before
105
     * the next access.
106
     */
107
    SAL_DLLPRIVATE static void SetListPositions( SvTreeListEntries& rEntries );
108
109
    // rPos is not changed for SortModeNone
110
    SAL_DLLPRIVATE void GetInsertionPos(
111
                            SvTreeListEntry const * pEntry,
112
                            SvTreeListEntry* pParent,
113
                            sal_uInt32& rPos
114
                        );
115
116
    SAL_DLLPRIVATE void ResortChildren( SvTreeListEntry* pParent );
117
118
    SvTreeList(const SvTreeList&) = delete;
119
    SvTreeList& operator= (const SvTreeList&) = delete;
120
121
    std::unique_ptr<SvTreeListEntry>  pRootItem;
122
123
public:
124
125
                        SvTreeList() = delete;
126
                        SvTreeList(SvListView&);
127
                        ~SvTreeList();
128
129
    void                Broadcast(
130
                            SvListAction nActionId,
131
                            SvTreeListEntry* pEntry1=nullptr,
132
                            SvTreeListEntry* pEntry2=nullptr,
133
                            sal_uInt32 nPos=0
134
                        );
135
136
    void                EnableInvalidate( bool bEnable );
137
0
    bool                IsEnableInvalidate() const { return mbEnableInvalidate; }
138
139
    // Notify all Listeners
140
    void                InvalidateEntry( SvTreeListEntry* );
141
142
0
    sal_uInt32          GetEntryCount() const { return nEntryCount; }
143
    SvTreeListEntry*    First() const;
144
    SvTreeListEntry*    Next( SvTreeListEntry* pEntry, sal_uInt16* pDepth=nullptr ) const;
145
    SvTreeListEntry*    Prev( SvTreeListEntry* pEntry ) const;
146
    SvTreeListEntry*    Last() const;
147
148
    SvTreeListEntry*    FirstChild( SvTreeListEntry* pParent ) const;
149
150
    sal_uInt32          Insert( SvTreeListEntry* pEntry,SvTreeListEntry* pPar,sal_uInt32 nPos = TREELIST_APPEND);
151
    sal_uInt32          Insert( SvTreeListEntry* pEntry,sal_uInt32 nRootPos = TREELIST_APPEND )
152
0
    { return Insert(pEntry, pRootItem.get(), nRootPos ); }
153
154
    void                InsertTree( SvTreeListEntry* pTree, SvTreeListEntry* pTargetParent, sal_uInt32 nListPos );
155
156
    // Entries need to be in the same Model!
157
    void                Move( SvTreeListEntry* pSource, SvTreeListEntry* pTarget );
158
159
    // Creates ChildList if needed
160
    sal_uInt32          Move( SvTreeListEntry* pSource, SvTreeListEntry* pTargetParent, sal_uInt32 nListPos);
161
    sal_uInt32          Copy( SvTreeListEntry* pSource, SvTreeListEntry* pTargetParent, sal_uInt32 nListPos);
162
163
    bool Remove( const SvTreeListEntry* pEntry );
164
    void                Clear();
165
166
    bool                IsChild(const SvTreeListEntry* pParent, const SvTreeListEntry* pChild) const;
167
    SvTreeListEntry*        GetEntry( SvTreeListEntry* pParent, sal_uInt32 nPos ) const;
168
    SvTreeListEntry*        GetEntry( sal_uInt32 nRootPos ) const;
169
    SvTreeListEntry*        GetEntryAtAbsPos( sal_uInt32 nAbsPos ) const;
170
171
    const SvTreeListEntry* GetParent( const SvTreeListEntry* pEntry ) const;
172
    SvTreeListEntry* GetParent( SvTreeListEntry* pEntry );
173
174
    const SvTreeListEntries& GetChildList( SvTreeListEntry* pParent ) const;
175
    SvTreeListEntries& GetChildList( SvTreeListEntry* pParent );
176
177
    sal_uInt32 GetAbsPos( const SvTreeListEntry* pEntry ) const;
178
    static sal_uInt32 GetRelPos( const SvTreeListEntry* pChild );
179
180
    sal_uInt32 GetChildCount( const SvTreeListEntry* pParent ) const;
181
    sal_uInt16 GetDepth( const SvTreeListEntry* pEntry ) const;
182
    bool IsAtRootDepth( const SvTreeListEntry* pEntry ) const;
183
184
    // The Model calls the Clone Link to clone Entries.
185
    // Thus we do not need to derive from the Model if we derive from SvTreeListEntry.
186
    // The Handler needs to return a SvTreeListEntry*
187
    SvTreeListEntry*    Clone( SvTreeListEntry* pEntry, sal_uInt32& nCloneCount ) const;
188
    void                SetCloneLink( const Link<SvTreeListEntry*,SvTreeListEntry*>& rLink )
189
0
    { aCloneLink=rLink; }
190
191
    const Link<SvTreeListEntry*,SvTreeListEntry*>&       GetCloneLink() const
192
0
    { return aCloneLink; }
193
194
    SvTreeListEntry*    CloneEntry( SvTreeListEntry* pSource ) const; // Calls the Clone Link
195
196
0
    void                SetSortMode( SvSortMode eMode ) { eSortMode = eMode; }
197
0
    SvSortMode          GetSortMode() const { return eSortMode; }
198
    sal_Int32           Compare(const SvTreeListEntry* pLeft, const SvTreeListEntry* pRight) const;
199
0
    void                SetCompareHdl( const Link<const SvSortData&, sal_Int32>& rLink ) { aCompareLink = rLink; }
200
    void                Resort();
201
};
202
203
class UNLESS_MERGELIBS_MORE(VCL_DLLPUBLIC) SvListView
204
{
205
    friend class SvTreeList;
206
207
    struct SAL_DLLPRIVATE Impl;
208
    std::unique_ptr<Impl> m_pImpl;
209
210
protected:
211
    std::unique_ptr<SvTreeList> pModel;
212
213
    void                ExpandListEntry( SvTreeListEntry* pParent );
214
    void                CollapseListEntry( SvTreeListEntry* pParent );
215
    bool                SelectListEntry( SvTreeListEntry* pEntry, bool bSelect );
216
217
public:
218
                        SvListView();   // Sets the Model to 0
219
    void                dispose();
220
    virtual             ~SvListView();
221
    void                Clear();
222
    virtual void        ModelNotification(
223
                            SvListAction nActionId,
224
                            SvTreeListEntry* pEntry1,
225
                            SvTreeListEntry* pEntry2,
226
                            sal_uInt32 nPos
227
                        );
228
229
    sal_uInt32          GetVisibleCount() const
230
0
    { return pModel->GetVisibleCount( const_cast<SvListView*>(this) ); }
231
232
    SvTreeListEntry*        FirstVisible() const
233
0
    { return pModel->FirstVisible(); }
234
235
    SvTreeListEntry*        NextVisible( SvTreeListEntry* pEntry ) const
236
0
    { return pModel->NextVisible(this,pEntry); }
237
238
    SvTreeListEntry*        PrevVisible( SvTreeListEntry* pEntry ) const
239
0
    { return pModel->PrevVisible(this,pEntry); }
240
241
    SvTreeListEntry*        LastVisible() const
242
0
    { return pModel->LastVisible(this); }
243
244
    SvTreeListEntry*        NextVisible( SvTreeListEntry* pEntry, sal_uInt16& rDelta ) const
245
0
    { return pModel->NextVisible(this,pEntry,rDelta); }
246
247
    SvTreeListEntry*        PrevVisible( SvTreeListEntry* pEntry, sal_uInt16& rDelta ) const
248
0
    { return pModel->PrevVisible(this,pEntry,rDelta); }
249
250
    sal_uInt32              GetSelectionCount() const;
251
252
    SvTreeListEntry* FirstSelected() const
253
0
    { return pModel->FirstSelected(this); }
254
255
    SvTreeListEntry*        NextSelected( SvTreeListEntry* pEntry ) const
256
0
    { return pModel->NextSelected(this,pEntry); }
257
258
    SvTreeListEntry*        GetEntryAtAbsPos( sal_uInt32 nAbsPos ) const
259
0
    { return pModel->GetEntryAtAbsPos(nAbsPos); }
260
261
    SvTreeListEntry*        GetEntryAtVisPos( sal_uInt32 nVisPos ) const
262
0
    { return pModel->GetEntryAtVisPos(this,nVisPos); }
263
264
    sal_uInt32              GetAbsPos( SvTreeListEntry const * pEntry ) const
265
0
    { return pModel->GetAbsPos(pEntry); }
266
267
    sal_uInt32           GetVisiblePos( SvTreeListEntry const * pEntry ) const
268
0
    { return pModel->GetVisiblePos(this,pEntry); }
269
270
    sal_uInt32           GetVisibleChildCount(SvTreeListEntry* pParent ) const
271
0
    { return pModel->GetVisibleChildCount(this,pParent); }
272
273
    bool               IsEntryVisible( SvTreeListEntry* pEntry ) const
274
0
    { return pModel->IsEntryVisible(this,pEntry); }
275
276
    bool                IsExpanded( SvTreeListEntry* pEntry ) const;
277
    bool                IsAllExpanded( SvTreeListEntry* pEntry) const;
278
    bool                IsSelected(const SvTreeListEntry* pEntry) const;
279
    void                SetEntryFocus( SvTreeListEntry* pEntry, bool bFocus );
280
    const SvViewDataEntry*         GetViewData( const SvTreeListEntry* pEntry ) const;
281
    SvViewDataEntry*         GetViewData( SvTreeListEntry* pEntry );
282
    bool                HasViewData() const;
283
284
    virtual void        InitViewData( SvViewDataEntry*, SvTreeListEntry* pEntry );
285
286
    virtual void        ModelHasCleared();
287
    virtual void        ModelHasInserted( SvTreeListEntry* pEntry );
288
    virtual void        ModelHasInsertedTree( SvTreeListEntry* pEntry );
289
    virtual void        ModelIsMoving( SvTreeListEntry* pSource );
290
    virtual void        ModelHasMoved( SvTreeListEntry* pSource );
291
    virtual void        ModelIsRemoving( SvTreeListEntry* pEntry );
292
    virtual void        ModelHasRemoved( SvTreeListEntry* pEntry );
293
    virtual void        ModelHasEntryInvalidated( SvTreeListEntry* pEntry );
294
};
295
296
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */