xref: /AOO41X/main/sw/source/ui/docvw/frmsidebarwincontainer.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1  /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2008 by Sun Microsystems, Inc.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * $RCSfile: $
10  * $Revision: $
11  *
12  * This file is part of OpenOffice.org.
13  *
14  * OpenOffice.org is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU Lesser General Public License version 3
16  * only, as published by the Free Software Foundation.
17  *
18  * OpenOffice.org is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU Lesser General Public License version 3 for more details
22  * (a copy is included in the LICENSE file that accompanied this code).
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * version 3 along with OpenOffice.org.  If not, see
26  * <http://www.openoffice.org/license.html>
27  * for a copy of the LGPLv3 License.
28  *
29  ************************************************************************/
30 
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
33 
34 #include <frmsidebarwincontainer.hxx>
35 
36 #include <map>
37 #include <fmtfld.hxx>
38 #include <txtfld.hxx>
39 #include <SidebarWin.hxx>
40 
41 namespace {
42     struct SidebarWinKey
43     {
44         const xub_StrLen mnIndex;
45 
46         explicit SidebarWinKey( const xub_StrLen nIndex )
47             : mnIndex( nIndex )
48         {}
49 
50         bool operator < ( const SidebarWinKey& rSidebarWinKey ) const
51         {
52             return mnIndex < rSidebarWinKey.mnIndex;
53         }
54     };
55 
56     struct SidebarWinOrder
57     {
58         sal_Bool operator()( const SidebarWinKey& rSidebarWinKeyA,
59                              const SidebarWinKey& rSidebarWinKeyB ) const
60         {
61             return rSidebarWinKeyA < rSidebarWinKeyB;
62         }
63     };
64 
65     typedef ::std::map < SidebarWinKey, sw::sidebarwindows::SwSidebarWin*, SidebarWinOrder > SidebarWinContainer;
66 
67     struct FrmKey
68     {
69         const SwFrm* mpFrm;
70 
71         explicit FrmKey( const SwFrm* pFrm )
72             : mpFrm( pFrm )
73         {}
74 
75         bool operator < ( const FrmKey& rFrmKey ) const
76         {
77             return mpFrm < rFrmKey.mpFrm;
78         }
79     };
80 
81     struct FrmOrder
82     {
83         sal_Bool operator()( const FrmKey& rFrmKeyA,
84                              const FrmKey& rFrmKeyB ) const
85         {
86             return rFrmKeyA < rFrmKeyB;
87         }
88     };
89 
90     typedef ::std::map < FrmKey, SidebarWinContainer, FrmOrder > _FrmSidebarWinContainer;
91 }
92 
93 namespace sw { namespace sidebarwindows {
94 
95 class FrmSidebarWinContainer : public _FrmSidebarWinContainer
96 {
97 };
98 
99 SwFrmSidebarWinContainer::SwFrmSidebarWinContainer()
100     : mpFrmSidebarWinContainer( new FrmSidebarWinContainer() )
101 {}
102 
103 SwFrmSidebarWinContainer::~SwFrmSidebarWinContainer()
104 {
105     mpFrmSidebarWinContainer->clear();
106     delete mpFrmSidebarWinContainer;
107 }
108 
109 bool SwFrmSidebarWinContainer::insert( const SwFrm& rFrm,
110                                        const SwFmtFld& rFmtFld,
111                                        SwSidebarWin& rSidebarWin )
112 {
113     bool bInserted( false );
114 
115     FrmKey aFrmKey( &rFrm );
116     SidebarWinContainer& rSidebarWinContainer = (*mpFrmSidebarWinContainer)[ aFrmKey ];
117 
118     SidebarWinKey aSidebarWinKey( *(rFmtFld.GetTxtFld()->GetStart()) );
119     if ( rSidebarWinContainer.empty() ||
120          rSidebarWinContainer.find( aSidebarWinKey) == rSidebarWinContainer.end() )
121     {
122         rSidebarWinContainer[ aSidebarWinKey ] = &rSidebarWin;
123         bInserted = true;
124     }
125 
126     return bInserted;
127 }
128 
129 bool SwFrmSidebarWinContainer::remove( const SwFrm& rFrm,
130                                        const SwSidebarWin& rSidebarWin )
131 {
132     bool bRemoved( false );
133 
134     FrmKey aFrmKey( &rFrm );
135     FrmSidebarWinContainer::iterator aFrmIter = mpFrmSidebarWinContainer->find( aFrmKey );
136     if ( aFrmIter != mpFrmSidebarWinContainer->end() )
137     {
138         SidebarWinContainer& rSidebarWinContainer = (*aFrmIter).second;
139         for ( SidebarWinContainer::iterator aIter = rSidebarWinContainer.begin();
140               aIter != rSidebarWinContainer.end();
141               ++aIter )
142         {
143             if ( (*aIter).second == &rSidebarWin )
144             {
145                 rSidebarWinContainer.erase( aIter );
146                 bRemoved = true;
147                 break;
148             }
149         }
150     }
151 
152     return bRemoved;
153 }
154 
155 bool SwFrmSidebarWinContainer::empty( const SwFrm& rFrm )
156 {
157     bool bEmpty( true );
158 
159     FrmKey aFrmKey( &rFrm );
160     FrmSidebarWinContainer::iterator aFrmIter = mpFrmSidebarWinContainer->find( aFrmKey );
161     if ( aFrmIter != mpFrmSidebarWinContainer->end() )
162     {
163         bEmpty = (*aFrmIter).second.empty();
164     }
165 
166     return bEmpty;
167 }
168 
169 SwSidebarWin* SwFrmSidebarWinContainer::get( const SwFrm& rFrm,
170                                              const sal_Int32 nIndex )
171 {
172     SwSidebarWin* pRet( 0 );
173 
174     FrmKey aFrmKey( &rFrm );
175     FrmSidebarWinContainer::iterator aFrmIter = mpFrmSidebarWinContainer->find( aFrmKey );
176     if ( aFrmIter != mpFrmSidebarWinContainer->end() )
177     {
178         SidebarWinContainer& rSidebarWinContainer = (*aFrmIter).second;
179         sal_Int32 nCounter( nIndex );
180         for ( SidebarWinContainer::iterator aIter = rSidebarWinContainer.begin();
181               nCounter >= 0 && aIter != rSidebarWinContainer.end();
182               ++aIter )
183         {
184             if ( nCounter == 0 )
185             {
186                 pRet = (*aIter).second;
187                 break;
188             }
189 
190             --nCounter;
191         }
192     }
193 
194     return pRet;
195 }
196 
197 void SwFrmSidebarWinContainer::getAll( const SwFrm& rFrm,
198                                        std::vector< Window* >* pSidebarWins )
199 {
200     pSidebarWins->clear();
201 
202     FrmKey aFrmKey( &rFrm );
203     FrmSidebarWinContainer::iterator aFrmIter = mpFrmSidebarWinContainer->find( aFrmKey );
204     if ( aFrmIter != mpFrmSidebarWinContainer->end() )
205     {
206         SidebarWinContainer& rSidebarWinContainer = (*aFrmIter).second;
207         for ( SidebarWinContainer::iterator aIter = rSidebarWinContainer.begin();
208               aIter != rSidebarWinContainer.end();
209               ++aIter )
210         {
211             pSidebarWins->push_back( (*aIter).second );
212         }
213     }
214 }
215 
216 } } // eof of namespace sw::sidebarwindows::
217