update credits
[LibreOffice.git] / include / svtools / treelist.hxx
blob220b2f4c7f8827a7999f71e6ccfb14a11db822d5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef _SVTREELIST_HXX
21 #define _SVTREELIST_HXX
23 #include "svtools/svtdllapi.h"
24 #include "svtools/treelistentries.hxx"
25 #include "svtools/viewdataentry.hxx"
27 #include <tools/solar.h>
28 #include <tools/link.hxx>
29 #include <tools/string.hxx>
30 #include <tools/debug.hxx>
32 #include <limits.h>
33 #include <vector>
34 #include <boost/ptr_container/ptr_map.hpp>
36 #define LISTACTION_INSERTED 1
37 #define LISTACTION_REMOVING 2
38 #define LISTACTION_REMOVED 3
39 #define LISTACTION_MOVING 4
40 #define LISTACTION_MOVED 5
41 #define LISTACTION_CLEARING 6
42 #define LISTACTION_INSERTED_TREE 7
43 #define LISTACTION_INVALIDATE_ENTRY 8
44 #define LISTACTION_RESORTING 9
45 #define LISTACTION_RESORTED 10
46 #define LISTACTION_CLEARED 11
48 class SvTreeListEntry;
49 class SvListView;
51 enum SvSortMode { SortAscending, SortDescending, SortNone };
53 // For the return values of Sortlink:
54 // See International::Compare( pLeft, pRight )
55 // ( Compare(a,b) ==> b.Compare(a) ==> strcmp(a,b) )
56 struct SvSortData
58 const SvTreeListEntry* pLeft;
59 const SvTreeListEntry* pRight;
62 class SVT_DLLPUBLIC SvTreeList
64 typedef std::vector<SvListView*> ListViewsType;
66 friend class SvListView;
68 ListViewsType aViewList;
69 sal_uLong nEntryCount;
71 Link aCloneLink;
72 Link aCompareLink;
73 SvSortMode eSortMode;
75 sal_uInt16 nRefCount;
77 sal_Bool bAbsPositionsValid;
79 SvTreeListEntry* FirstVisible() const { return First(); }
80 SvTreeListEntry* NextVisible( const SvListView*,SvTreeListEntry* pEntry, sal_uInt16* pDepth=0 ) const;
81 SvTreeListEntry* PrevVisible( const SvListView*,SvTreeListEntry* pEntry, sal_uInt16* pDepth=0 ) const;
82 SvTreeListEntry* LastVisible( const SvListView*,sal_uInt16* pDepth=0 ) const;
83 SvTreeListEntry* NextVisible( const SvListView*,SvTreeListEntry* pEntry, sal_uInt16& rDelta ) const;
84 SvTreeListEntry* PrevVisible( const SvListView*,SvTreeListEntry* pEntry, sal_uInt16& rDelta ) const;
86 sal_Bool IsEntryVisible( const SvListView*,SvTreeListEntry* pEntry ) const;
87 SvTreeListEntry* GetEntryAtVisPos( const SvListView*,sal_uLong nVisPos ) const;
88 sal_uLong GetVisiblePos( const SvListView*,SvTreeListEntry* pEntry ) const;
89 sal_uLong GetVisibleCount( SvListView* ) const;
90 sal_uLong GetVisibleChildCount( const SvListView*,SvTreeListEntry* pParent ) const;
92 SvTreeListEntry* FirstSelected( const SvListView*) const;
93 SvTreeListEntry* NextSelected( const SvListView*,SvTreeListEntry* pEntry ) const;
94 SvTreeListEntry* PrevSelected( const SvListView*,SvTreeListEntry* pEntry ) const;
95 SvTreeListEntry* LastSelected( const SvListView*) const;
97 sal_Bool Select( SvListView*,SvTreeListEntry* pEntry, sal_Bool bSelect=sal_True );
98 void SelectAll( SvListView*,sal_Bool bSelect ); // Does not call Select Handler
99 sal_uLong GetChildSelectionCount( const SvListView*,SvTreeListEntry* pParent ) const;
101 void Expand( SvListView*,SvTreeListEntry* pParent );
102 void Collapse( SvListView*,SvTreeListEntry* pParent );
104 SVT_DLLPRIVATE void SetAbsolutePositions();
106 SVT_DLLPRIVATE void CloneChildren(
107 SvTreeListEntries& rDst, sal_uLong& rCloneCount, SvTreeListEntries& rSrc, SvTreeListEntry* pNewParent) const;
110 * Invalidate the cached position data to have them re-generated before
111 * the next access.
113 SVT_DLLPRIVATE void SetListPositions( SvTreeListEntries& rEntries );
115 // rPos is not changed for SortModeNone
116 SVT_DLLPRIVATE void GetInsertionPos(
117 SvTreeListEntry* pEntry,
118 SvTreeListEntry* pParent,
119 sal_uLong& rPos
122 SVT_DLLPRIVATE void ResortChildren( SvTreeListEntry* pParent );
124 SvTreeList(const SvTreeList&); // disabled
125 SvTreeList& operator= (const SvTreeList&); // disabled
127 protected:
128 SvTreeListEntry* pRootItem;
130 public:
132 SvTreeList();
133 virtual ~SvTreeList();
135 void InsertView( SvListView* );
136 void RemoveView( SvListView* );
137 sal_uLong GetViewCount() const
138 { return aViewList.size(); }
140 SvListView* GetView( sal_uLong nPos ) const
141 { return ( nPos < aViewList.size() ) ? aViewList[ nPos ] : NULL; }
143 void Broadcast(
144 sal_uInt16 nActionId,
145 SvTreeListEntry* pEntry1=0,
146 SvTreeListEntry* pEntry2=0,
147 sal_uLong nPos=0
150 // Notify all Listeners
151 void InvalidateEntry( SvTreeListEntry* );
153 sal_uLong GetEntryCount() const { return nEntryCount; }
154 SvTreeListEntry* First() const;
155 SvTreeListEntry* Next( SvTreeListEntry* pEntry, sal_uInt16* pDepth=0 ) const;
156 SvTreeListEntry* Prev( SvTreeListEntry* pEntry, sal_uInt16* pDepth=0 ) const;
157 SvTreeListEntry* Last() const;
159 SvTreeListEntry* FirstChild( SvTreeListEntry* pParent ) const;
160 SvTreeListEntry* NextSibling( SvTreeListEntry* pEntry ) const;
161 SvTreeListEntry* PrevSibling( SvTreeListEntry* pEntry ) const;
162 SvTreeListEntry* LastSibling( SvTreeListEntry* pEntry ) const;
164 sal_uLong Insert( SvTreeListEntry* pEntry,SvTreeListEntry* pPar,sal_uLong nPos=ULONG_MAX);
165 sal_uLong Insert( SvTreeListEntry* pEntry,sal_uLong nRootPos = ULONG_MAX )
166 { return Insert(pEntry, pRootItem, nRootPos ); }
168 void InsertTree( SvTreeListEntry* pTree, SvTreeListEntry* pTargetParent, sal_uLong nListPos );
170 // Entries need to be in the same Model!
171 void Move( SvTreeListEntry* pSource, SvTreeListEntry* pTarget );
173 // Creates ChildList if needed
174 sal_uLong Move( SvTreeListEntry* pSource, SvTreeListEntry* pTargetParent, sal_uLong nListPos);
175 void Copy( SvTreeListEntry* pSource, SvTreeListEntry* pTarget );
176 sal_uLong Copy( SvTreeListEntry* pSource, SvTreeListEntry* pTargetParent, sal_uLong nListPos);
178 bool Remove( const SvTreeListEntry* pEntry );
179 void Clear();
181 bool HasChildren( const SvTreeListEntry* pEntry ) const;
182 bool HasParent( const SvTreeListEntry* pEntry ) const;
184 bool IsChild(const SvTreeListEntry* pParent, const SvTreeListEntry* pChild) const;
185 SvTreeListEntry* GetEntry( SvTreeListEntry* pParent, sal_uLong nPos ) const;
186 SvTreeListEntry* GetEntry( sal_uLong nRootPos ) const;
187 SvTreeListEntry* GetEntryAtAbsPos( sal_uLong nAbsPos ) const;
189 const SvTreeListEntry* GetParent( const SvTreeListEntry* pEntry ) const;
190 SvTreeListEntry* GetParent( SvTreeListEntry* pEntry );
192 SvTreeListEntry* GetRootLevelParent( SvTreeListEntry* pEntry ) const;
193 const SvTreeListEntries& GetChildList( SvTreeListEntry* pParent ) const;
194 SvTreeListEntries& GetChildList( SvTreeListEntry* pParent );
196 std::pair<SvTreeListEntries::iterator, SvTreeListEntries::iterator>
197 GetChildIterators(SvTreeListEntry* pParent);
199 sal_uLong GetAbsPos( const SvTreeListEntry* pEntry ) const;
200 sal_uLong GetRelPos( const SvTreeListEntry* pChild ) const;
202 sal_uLong GetChildCount( const SvTreeListEntry* pParent ) const;
203 sal_uInt16 GetDepth( const SvTreeListEntry* pEntry ) const;
204 bool IsAtRootDepth( const SvTreeListEntry* pEntry ) const;
206 // The Model calls the Clone Link to clone Entries.
207 // Thus we do not need to derive from the Model if we derive from SvTreeListEntry.
208 // Declaration of the Clone Handler:
209 // DECL_LINK(CloneHdl,SvTreeListEntry*);
210 // The Handler needs to return a SvTreeListEntry*
211 SvTreeListEntry* Clone( SvTreeListEntry* pEntry, sal_uLong& nCloneCount ) const;
212 void SetCloneLink( const Link& rLink )
213 { aCloneLink=rLink; }
215 const Link& GetCloneLink() const
216 { return aCloneLink; }
218 virtual SvTreeListEntry* CloneEntry( SvTreeListEntry* pSource ) const; // Calls the Clone Link
219 virtual SvTreeListEntry* CreateEntry() const; // To create Entries
221 sal_uInt16 GetRefCount() const { return nRefCount; }
222 void SetRefCount( sal_uInt16 nRef ) { nRefCount = nRef; }
224 void SetSortMode( SvSortMode eMode ) { eSortMode = eMode; }
225 SvSortMode GetSortMode() const { return eSortMode; }
226 StringCompare Compare(const SvTreeListEntry* pLeft, const SvTreeListEntry* pRight) const;
227 void SetCompareHdl( const Link& rLink ) { aCompareLink = rLink; }
228 const Link& GetCompareHdl() const { return aCompareLink; }
229 void Resort();
232 class SVT_DLLPUBLIC SvListView
234 friend class SvTreeList;
236 typedef boost::ptr_map<SvTreeListEntry*, SvViewDataEntry> SvDataTable;
238 sal_uLong nVisibleCount;
239 sal_uLong nSelectionCount;
240 sal_Bool bVisPositionsValid;
242 SVT_DLLPRIVATE void InitTable();
243 SVT_DLLPRIVATE void RemoveViewData( SvTreeListEntry* pParent );
245 SvDataTable maDataTable; // Mapping SvTreeListEntry -> ViewData
247 void ActionMoving( SvTreeListEntry* pEntry,SvTreeListEntry* pTargetPrnt,sal_uLong nChildPos);
248 void ActionMoved( SvTreeListEntry* pEntry,SvTreeListEntry* pTargetPrnt,sal_uLong nChildPos);
249 void ActionInserted( SvTreeListEntry* pEntry );
250 void ActionInsertedTree( SvTreeListEntry* pEntry );
251 void ActionRemoving( SvTreeListEntry* pEntry );
252 void ActionRemoved( SvTreeListEntry* pEntry );
253 void ActionClear();
255 protected:
256 SvTreeList* pModel;
258 public:
259 SvListView(); // Sets the Model to 0
260 virtual ~SvListView();
261 void Clear();
262 virtual void SetModel( SvTreeList* );
263 virtual void ModelNotification(
264 sal_uInt16 nActionId,
265 SvTreeListEntry* pEntry1,
266 SvTreeListEntry* pEntry2,
267 sal_uLong nPos
270 sal_uLong GetVisibleCount() const
271 { return pModel->GetVisibleCount( (SvListView*)this ); }
273 SvTreeListEntry* FirstVisible() const
274 { return pModel->FirstVisible(); }
276 SvTreeListEntry* NextVisible( SvTreeListEntry* pEntry, sal_uInt16* pDepth=0 ) const
277 { return pModel->NextVisible(this,pEntry,pDepth); }
279 SvTreeListEntry* PrevVisible( SvTreeListEntry* pEntry, sal_uInt16* pDepth=0 ) const
280 { return pModel->PrevVisible(this,pEntry,pDepth); }
282 SvTreeListEntry* LastVisible( sal_uInt16* pDepth=0 ) const
283 { return pModel->LastVisible(this,pDepth); }
285 SvTreeListEntry* NextVisible( SvTreeListEntry* pEntry, sal_uInt16& rDelta ) const
286 { return pModel->NextVisible(this,pEntry,rDelta); }
288 SvTreeListEntry* PrevVisible( SvTreeListEntry* pEntry, sal_uInt16& rDelta ) const
289 { return pModel->PrevVisible(this,pEntry,rDelta); }
291 sal_uLong GetSelectionCount() const
292 { return nSelectionCount; }
294 SvTreeListEntry* FirstSelected() const
295 { return pModel->FirstSelected(this); }
297 SvTreeListEntry* NextSelected( SvTreeListEntry* pEntry ) const
298 { return pModel->NextSelected(this,pEntry); }
300 SvTreeListEntry* PrevSelected( SvTreeListEntry* pEntry ) const
301 { return pModel->PrevSelected(this,pEntry); }
303 SvTreeListEntry* LastSelected() const
304 { return pModel->LastSelected(this); }
305 SvTreeListEntry* GetEntryAtAbsPos( sal_uLong nAbsPos ) const
306 { return pModel->GetEntryAtAbsPos(nAbsPos); }
308 SvTreeListEntry* GetEntryAtVisPos( sal_uLong nVisPos ) const
309 { return pModel->GetEntryAtVisPos((SvListView*)this,nVisPos); }
311 sal_uLong GetAbsPos( SvTreeListEntry* pEntry ) const
312 { return pModel->GetAbsPos(pEntry); }
314 sal_uLong GetVisiblePos( SvTreeListEntry* pEntry ) const
315 { return pModel->GetVisiblePos((SvListView*)this,pEntry); }
317 sal_uLong GetVisibleChildCount(SvTreeListEntry* pParent ) const
318 { return pModel->GetVisibleChildCount((SvListView*)this,pParent); }
320 sal_uLong GetChildSelectionCount( SvTreeListEntry* pParent ) const
321 { return pModel->GetChildSelectionCount((SvListView*)this,pParent); }
323 void Expand( SvTreeListEntry* pParent )
324 { pModel->Expand((SvListView*)this,pParent); }
326 void Collapse( SvTreeListEntry* pParent )
327 { pModel->Collapse((SvListView*)this,pParent); }
329 sal_Bool Select( SvTreeListEntry* pEntry, sal_Bool bSelect=sal_True )
330 { return pModel->Select((SvListView*)this,pEntry,bSelect); }
332 // Does not call the Select Handler
333 virtual void SelectAll( sal_Bool bSelect, sal_Bool )
334 { pModel->SelectAll((SvListView*)this, bSelect); }
336 sal_Bool IsEntryVisible( SvTreeListEntry* pEntry ) const
337 { return pModel->IsEntryVisible((SvListView*)this,pEntry); }
339 sal_Bool IsExpanded( SvTreeListEntry* pEntry ) const;
340 sal_Bool IsSelected( SvTreeListEntry* pEntry ) const;
341 void SetEntryFocus( SvTreeListEntry* pEntry, sal_Bool bFocus );
342 const SvViewDataEntry* GetViewData( const SvTreeListEntry* pEntry ) const;
343 SvViewDataEntry* GetViewData( SvTreeListEntry* pEntry );
344 sal_Bool HasViewData() const
345 { return maDataTable.size() > 1; } // There's always a ROOT
347 virtual SvViewDataEntry* CreateViewData( SvTreeListEntry* pEntry );
348 virtual void InitViewData( SvViewDataEntry*, SvTreeListEntry* pEntry );
350 virtual void ModelHasCleared();
351 virtual void ModelHasInserted( SvTreeListEntry* pEntry );
352 virtual void ModelHasInsertedTree( SvTreeListEntry* pEntry );
353 virtual void ModelIsMoving(
354 SvTreeListEntry* pSource,
355 SvTreeListEntry* pTargetParent,
356 sal_uLong nPos
358 virtual void ModelHasMoved( SvTreeListEntry* pSource );
359 virtual void ModelIsRemoving( SvTreeListEntry* pEntry );
360 virtual void ModelHasRemoved( SvTreeListEntry* pEntry );
361 virtual void ModelHasEntryInvalidated( SvTreeListEntry* pEntry );
364 #endif
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */