Line data Source code
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 : #ifndef INCLUDED_SVTOOLS_TREELIST_HXX
21 : #define INCLUDED_SVTOOLS_TREELIST_HXX
22 :
23 : #include <svtools/svtdllapi.h>
24 : #include <svtools/treelistentries.hxx>
25 : #include <svtools/viewdataentry.hxx>
26 :
27 : #include <tools/solar.h>
28 : #include <tools/link.hxx>
29 : #include <tools/contnr.hxx>
30 :
31 : #include <limits.h>
32 : #include <vector>
33 : #include <boost/ptr_container/ptr_map.hpp>
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 : REVERSING = 12,
49 : REVERSED = 13
50 : };
51 :
52 : class SvTreeListEntry;
53 : class SvListView;
54 :
55 : enum SvSortMode { SortAscending, SortDescending, SortNone };
56 :
57 : // For the return values of Sortlink:
58 : // See International::Compare( pLeft, pRight )
59 : // ( Compare(a,b) ==> b.Compare(a) ==> strcmp(a,b) )
60 : struct SvSortData
61 : {
62 : const SvTreeListEntry* pLeft;
63 : const SvTreeListEntry* pRight;
64 : };
65 :
66 : class SVT_DLLPUBLIC SvTreeList
67 : {
68 : typedef std::vector<SvListView*> ListViewsType;
69 :
70 : friend class SvListView;
71 :
72 : ListViewsType aViewList;
73 : sal_uLong nEntryCount;
74 :
75 : Link<> aCloneLink;
76 : Link<> aCompareLink;
77 : SvSortMode eSortMode;
78 :
79 : sal_uInt16 nRefCount;
80 :
81 : bool bAbsPositionsValid;
82 :
83 : bool mbEnableInvalidate;
84 :
85 174 : SvTreeListEntry* FirstVisible() const { return First(); }
86 : SvTreeListEntry* NextVisible( const SvListView*,SvTreeListEntry* pEntry, sal_uInt16* pDepth=0 ) const;
87 : SvTreeListEntry* PrevVisible( const SvListView*,SvTreeListEntry* pEntry, sal_uInt16* pDepth=0 ) const;
88 : SvTreeListEntry* LastVisible( const SvListView*,sal_uInt16* pDepth=0 ) const;
89 : SvTreeListEntry* NextVisible( const SvListView*,SvTreeListEntry* pEntry, sal_uInt16& rDelta ) const;
90 : SvTreeListEntry* PrevVisible( const SvListView*,SvTreeListEntry* pEntry, sal_uInt16& rDelta ) const;
91 :
92 : bool IsEntryVisible( const SvListView*,SvTreeListEntry* pEntry ) const;
93 : SvTreeListEntry* GetEntryAtVisPos( const SvListView*,sal_uLong nVisPos ) const;
94 : sal_uLong GetVisiblePos( const SvListView*,SvTreeListEntry* pEntry ) const;
95 : sal_uLong GetVisibleCount( SvListView* ) const;
96 : sal_uLong GetVisibleChildCount( const SvListView*,SvTreeListEntry* pParent ) const;
97 :
98 : SvTreeListEntry* FirstSelected( const SvListView*) const;
99 : SvTreeListEntry* NextSelected( const SvListView*,SvTreeListEntry* pEntry ) const;
100 : SvTreeListEntry* PrevSelected( const SvListView*,SvTreeListEntry* pEntry ) const;
101 : SvTreeListEntry* LastSelected( const SvListView*) const;
102 :
103 : static bool Select( SvListView*,SvTreeListEntry* pEntry, bool bSelect=true );
104 : void SelectAll( SvListView*, bool bSelect ); // Does not call Select Handler
105 : sal_uLong GetChildSelectionCount( const SvListView*,SvTreeListEntry* pParent ) const;
106 :
107 : static void Expand( SvListView*,SvTreeListEntry* pParent );
108 : static void Collapse( SvListView*,SvTreeListEntry* pParent );
109 :
110 : SVT_DLLPRIVATE void SetAbsolutePositions();
111 :
112 : SVT_DLLPRIVATE void CloneChildren(
113 : SvTreeListEntries& rDst, sal_uLong& rCloneCount, SvTreeListEntries& rSrc, SvTreeListEntry* pNewParent) const;
114 :
115 : /**
116 : * Invalidate the cached position data to have them re-generated before
117 : * the next access.
118 : */
119 : SVT_DLLPRIVATE void SetListPositions( SvTreeListEntries& rEntries );
120 :
121 : // rPos is not changed for SortModeNone
122 : SVT_DLLPRIVATE void GetInsertionPos(
123 : SvTreeListEntry* pEntry,
124 : SvTreeListEntry* pParent,
125 : sal_uLong& rPos
126 : );
127 :
128 : SVT_DLLPRIVATE void ResortChildren( SvTreeListEntry* pParent );
129 : SVT_DLLPRIVATE void ReverseChildren( SvTreeListEntry* pParent );
130 :
131 : SvTreeList(const SvTreeList&) SAL_DELETED_FUNCTION;
132 : SvTreeList& operator= (const SvTreeList&) SAL_DELETED_FUNCTION;
133 :
134 : protected:
135 : SvTreeListEntry* pRootItem;
136 :
137 : public:
138 :
139 : SvTreeList();
140 : virtual ~SvTreeList();
141 :
142 : void InsertView( SvListView* );
143 : void RemoveView( SvListView* );
144 : sal_uLong GetViewCount() const
145 : { return aViewList.size(); }
146 :
147 0 : SvListView* GetView( sal_uLong nPos ) const
148 0 : { return ( nPos < aViewList.size() ) ? aViewList[ nPos ] : NULL; }
149 :
150 : void Broadcast(
151 : SvListAction nActionId,
152 : SvTreeListEntry* pEntry1=0,
153 : SvTreeListEntry* pEntry2=0,
154 : sal_uLong nPos=0
155 : );
156 :
157 : void EnableInvalidate( bool bEnable );
158 :
159 : // Notify all Listeners
160 : void InvalidateEntry( SvTreeListEntry* );
161 :
162 296 : sal_uLong GetEntryCount() const { return nEntryCount; }
163 : SvTreeListEntry* First() const;
164 : SvTreeListEntry* Next( SvTreeListEntry* pEntry, sal_uInt16* pDepth=0 ) const;
165 : SvTreeListEntry* Prev( SvTreeListEntry* pEntry, sal_uInt16* pDepth=0 ) const;
166 : SvTreeListEntry* Last() const;
167 :
168 : SvTreeListEntry* FirstChild( SvTreeListEntry* pParent ) const;
169 : static SvTreeListEntry* NextSibling( SvTreeListEntry* pEntry );
170 : static SvTreeListEntry* PrevSibling( SvTreeListEntry* pEntry );
171 : static SvTreeListEntry* LastSibling( SvTreeListEntry* pEntry );
172 :
173 : sal_uLong Insert( SvTreeListEntry* pEntry,SvTreeListEntry* pPar,sal_uLong nPos = TREELIST_APPEND);
174 98 : sal_uLong Insert( SvTreeListEntry* pEntry,sal_uLong nRootPos = TREELIST_APPEND )
175 98 : { return Insert(pEntry, pRootItem, nRootPos ); }
176 :
177 : void InsertTree( SvTreeListEntry* pTree, SvTreeListEntry* pTargetParent, sal_uLong nListPos );
178 :
179 : // Entries need to be in the same Model!
180 : void Move( SvTreeListEntry* pSource, SvTreeListEntry* pTarget );
181 :
182 : // Creates ChildList if needed
183 : sal_uLong Move( SvTreeListEntry* pSource, SvTreeListEntry* pTargetParent, sal_uLong nListPos);
184 : void Copy( SvTreeListEntry* pSource, SvTreeListEntry* pTarget );
185 : sal_uLong Copy( SvTreeListEntry* pSource, SvTreeListEntry* pTargetParent, sal_uLong nListPos);
186 :
187 : bool Remove( const SvTreeListEntry* pEntry );
188 : void Clear();
189 :
190 : bool HasChildren( const SvTreeListEntry* pEntry ) const;
191 : bool HasParent( const SvTreeListEntry* pEntry ) const;
192 :
193 : bool IsChild(const SvTreeListEntry* pParent, const SvTreeListEntry* pChild) const;
194 : SvTreeListEntry* GetEntry( SvTreeListEntry* pParent, sal_uLong nPos ) const;
195 : SvTreeListEntry* GetEntry( sal_uLong nRootPos ) const;
196 : SvTreeListEntry* GetEntryAtAbsPos( sal_uLong nAbsPos ) const;
197 :
198 : const SvTreeListEntry* GetParent( const SvTreeListEntry* pEntry ) const;
199 : SvTreeListEntry* GetParent( SvTreeListEntry* pEntry );
200 :
201 : SvTreeListEntry* GetRootLevelParent( SvTreeListEntry* pEntry ) const;
202 : const SvTreeListEntries& GetChildList( SvTreeListEntry* pParent ) const;
203 : SvTreeListEntries& GetChildList( SvTreeListEntry* pParent );
204 :
205 : std::pair<SvTreeListEntries::iterator, SvTreeListEntries::iterator>
206 : GetChildIterators(SvTreeListEntry* pParent);
207 :
208 : sal_uLong GetAbsPos( const SvTreeListEntry* pEntry ) const;
209 : static sal_uLong GetRelPos( const SvTreeListEntry* pChild );
210 :
211 : sal_uLong GetChildCount( const SvTreeListEntry* pParent ) const;
212 : sal_uInt16 GetDepth( const SvTreeListEntry* pEntry ) const;
213 : bool IsAtRootDepth( const SvTreeListEntry* pEntry ) const;
214 :
215 : // The Model calls the Clone Link to clone Entries.
216 : // Thus we do not need to derive from the Model if we derive from SvTreeListEntry.
217 : // Declaration of the Clone Handler:
218 : // DECL_LINK(CloneHdl,SvTreeListEntry*);
219 : // The Handler needs to return a SvTreeListEntry*
220 : SvTreeListEntry* Clone( SvTreeListEntry* pEntry, sal_uLong& nCloneCount ) const;
221 57 : void SetCloneLink( const Link<>& rLink )
222 57 : { aCloneLink=rLink; }
223 :
224 0 : const Link<>& GetCloneLink() const
225 0 : { return aCloneLink; }
226 :
227 : SvTreeListEntry* CloneEntry( SvTreeListEntry* pSource ) const; // Calls the Clone Link
228 : static SvTreeListEntry* CreateEntry(); // To create Entries
229 :
230 28 : sal_uInt16 GetRefCount() const { return nRefCount; }
231 29 : void SetRefCount( sal_uInt16 nRef ) { nRefCount = nRef; }
232 :
233 57 : void SetSortMode( SvSortMode eMode ) { eSortMode = eMode; }
234 0 : SvSortMode GetSortMode() const { return eSortMode; }
235 : sal_Int32 Compare(const SvTreeListEntry* pLeft, const SvTreeListEntry* pRight) const;
236 54 : void SetCompareHdl( const Link<>& rLink ) { aCompareLink = rLink; }
237 : const Link<>& GetCompareHdl() const { return aCompareLink; }
238 : void Resort();
239 : void Reverse();
240 : };
241 :
242 : class SVT_DLLPUBLIC SvListView
243 : {
244 : friend class SvTreeList;
245 :
246 : typedef boost::ptr_map<SvTreeListEntry*, SvViewDataEntry> SvDataTable;
247 :
248 : sal_uLong nVisibleCount;
249 : sal_uLong nSelectionCount;
250 : bool bVisPositionsValid;
251 :
252 : SVT_DLLPRIVATE void InitTable();
253 : SVT_DLLPRIVATE void RemoveViewData( SvTreeListEntry* pParent );
254 :
255 : SvDataTable maDataTable; // Mapping SvTreeListEntry -> ViewData
256 :
257 : void ActionMoving( SvTreeListEntry* pEntry,SvTreeListEntry* pTargetPrnt,sal_uLong nChildPos);
258 : void ActionMoved( SvTreeListEntry* pEntry,SvTreeListEntry* pTargetPrnt,sal_uLong nChildPos);
259 : void ActionInserted( SvTreeListEntry* pEntry );
260 : void ActionInsertedTree( SvTreeListEntry* pEntry );
261 : void ActionRemoving( SvTreeListEntry* pEntry );
262 : void ActionClear();
263 :
264 : protected:
265 : SvTreeList* pModel;
266 :
267 2 : void ExpandListEntry( SvTreeListEntry* pParent )
268 2 : { SvTreeList::Expand(this,pParent); }
269 :
270 0 : void CollapseListEntry( SvTreeListEntry* pParent )
271 0 : { SvTreeList::Collapse(this,pParent); }
272 :
273 13 : bool SelectListEntry( SvTreeListEntry* pEntry, bool bSelect )
274 13 : { return SvTreeList::Select(this,pEntry,bSelect); }
275 :
276 : public:
277 : SvListView(); // Sets the Model to 0
278 : virtual ~SvListView();
279 : void Clear();
280 : virtual void SetModel( SvTreeList* );
281 : virtual void ModelNotification(
282 : SvListAction nActionId,
283 : SvTreeListEntry* pEntry1,
284 : SvTreeListEntry* pEntry2,
285 : sal_uLong nPos
286 : );
287 :
288 828 : sal_uLong GetVisibleCount() const
289 828 : { return pModel->GetVisibleCount( const_cast<SvListView*>(this) ); }
290 :
291 174 : SvTreeListEntry* FirstVisible() const
292 174 : { return pModel->FirstVisible(); }
293 :
294 283 : SvTreeListEntry* NextVisible( SvTreeListEntry* pEntry, sal_uInt16* pDepth=0 ) const
295 283 : { return pModel->NextVisible(this,pEntry,pDepth); }
296 :
297 15 : SvTreeListEntry* PrevVisible( SvTreeListEntry* pEntry, sal_uInt16* pDepth=0 ) const
298 15 : { return pModel->PrevVisible(this,pEntry,pDepth); }
299 :
300 19 : SvTreeListEntry* LastVisible( sal_uInt16* pDepth=0 ) const
301 19 : { return pModel->LastVisible(this,pDepth); }
302 :
303 0 : SvTreeListEntry* NextVisible( SvTreeListEntry* pEntry, sal_uInt16& rDelta ) const
304 0 : { return pModel->NextVisible(this,pEntry,rDelta); }
305 :
306 0 : SvTreeListEntry* PrevVisible( SvTreeListEntry* pEntry, sal_uInt16& rDelta ) const
307 0 : { return pModel->PrevVisible(this,pEntry,rDelta); }
308 :
309 15 : sal_uLong GetSelectionCount() const
310 15 : { return nSelectionCount; }
311 :
312 198 : SvTreeListEntry* FirstSelected() const
313 198 : { return pModel->FirstSelected(this); }
314 :
315 1 : SvTreeListEntry* NextSelected( SvTreeListEntry* pEntry ) const
316 1 : { return pModel->NextSelected(this,pEntry); }
317 :
318 0 : SvTreeListEntry* PrevSelected( SvTreeListEntry* pEntry ) const
319 0 : { return pModel->PrevSelected(this,pEntry); }
320 :
321 0 : SvTreeListEntry* LastSelected() const
322 0 : { return pModel->LastSelected(this); }
323 0 : SvTreeListEntry* GetEntryAtAbsPos( sal_uLong nAbsPos ) const
324 0 : { return pModel->GetEntryAtAbsPos(nAbsPos); }
325 :
326 165 : SvTreeListEntry* GetEntryAtVisPos( sal_uLong nVisPos ) const
327 165 : { return pModel->GetEntryAtVisPos(this,nVisPos); }
328 :
329 0 : sal_uLong GetAbsPos( SvTreeListEntry* pEntry ) const
330 0 : { return pModel->GetAbsPos(pEntry); }
331 :
332 182 : sal_uLong GetVisiblePos( SvTreeListEntry* pEntry ) const
333 182 : { return pModel->GetVisiblePos(this,pEntry); }
334 :
335 0 : sal_uLong GetVisibleChildCount(SvTreeListEntry* pParent ) const
336 0 : { return pModel->GetVisibleChildCount(this,pParent); }
337 :
338 2 : sal_uLong GetChildSelectionCount( SvTreeListEntry* pParent ) const
339 2 : { return pModel->GetChildSelectionCount(this,pParent); }
340 :
341 : // Does not call the Select Handler
342 0 : virtual void SelectAll( bool bSelect, bool )
343 0 : { pModel->SelectAll(this, bSelect); }
344 :
345 55 : bool IsEntryVisible( SvTreeListEntry* pEntry ) const
346 55 : { return pModel->IsEntryVisible(this,pEntry); }
347 :
348 : bool IsExpanded( SvTreeListEntry* pEntry ) const;
349 : bool IsSelected( SvTreeListEntry* pEntry ) const;
350 : void SetEntryFocus( SvTreeListEntry* pEntry, bool bFocus );
351 : const SvViewDataEntry* GetViewData( const SvTreeListEntry* pEntry ) const;
352 : SvViewDataEntry* GetViewData( SvTreeListEntry* pEntry );
353 972 : bool HasViewData() const
354 972 : { return maDataTable.size() > 1; } // There's always a ROOT
355 :
356 : virtual SvViewDataEntry* CreateViewData( SvTreeListEntry* pEntry );
357 : virtual void InitViewData( SvViewDataEntry*, SvTreeListEntry* pEntry );
358 :
359 : virtual void ModelHasCleared();
360 : virtual void ModelHasInserted( SvTreeListEntry* pEntry );
361 : virtual void ModelHasInsertedTree( SvTreeListEntry* pEntry );
362 : virtual void ModelIsMoving(
363 : SvTreeListEntry* pSource,
364 : SvTreeListEntry* pTargetParent,
365 : sal_uLong nPos
366 : );
367 : virtual void ModelHasMoved( SvTreeListEntry* pSource );
368 : virtual void ModelIsRemoving( SvTreeListEntry* pEntry );
369 : virtual void ModelHasRemoved( SvTreeListEntry* pEntry );
370 : virtual void ModelHasEntryInvalidated( SvTreeListEntry* pEntry );
371 : };
372 :
373 : #endif
374 :
375 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|