1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 #include <frmsidebarwincontainer.hxx>
28 
29 #include <map>
30 #include <fmtfld.hxx>
31 #include <txtfld.hxx>
32 #include <SidebarWin.hxx>
33 
34 namespace {
35     struct SidebarWinKey
36     {
37         const xub_StrLen mnIndex;
38 
SidebarWinKey__anon351a0ba00111::SidebarWinKey39         explicit SidebarWinKey( const xub_StrLen nIndex )
40             : mnIndex( nIndex )
41         {}
42 
operator <__anon351a0ba00111::SidebarWinKey43         bool operator < ( const SidebarWinKey& rSidebarWinKey ) const
44         {
45             return mnIndex < rSidebarWinKey.mnIndex;
46         }
47     };
48 
49     struct SidebarWinOrder
50     {
operator ()__anon351a0ba00111::SidebarWinOrder51         sal_Bool operator()( const SidebarWinKey& rSidebarWinKeyA,
52                              const SidebarWinKey& rSidebarWinKeyB ) const
53         {
54             return rSidebarWinKeyA < rSidebarWinKeyB;
55         }
56     };
57 
58     typedef ::std::map < SidebarWinKey, sw::sidebarwindows::SwSidebarWin*, SidebarWinOrder > SidebarWinContainer;
59 
60     struct FrmKey
61     {
62         const SwFrm* mpFrm;
63 
FrmKey__anon351a0ba00111::FrmKey64         explicit FrmKey( const SwFrm* pFrm )
65             : mpFrm( pFrm )
66         {}
67 
operator <__anon351a0ba00111::FrmKey68         bool operator < ( const FrmKey& rFrmKey ) const
69         {
70             return mpFrm < rFrmKey.mpFrm;
71         }
72     };
73 
74     struct FrmOrder
75     {
operator ()__anon351a0ba00111::FrmOrder76         sal_Bool operator()( const FrmKey& rFrmKeyA,
77                              const FrmKey& rFrmKeyB ) const
78         {
79             return rFrmKeyA < rFrmKeyB;
80         }
81     };
82 
83     typedef ::std::map < FrmKey, SidebarWinContainer, FrmOrder > _FrmSidebarWinContainer;
84 }
85 
86 namespace sw { namespace sidebarwindows {
87 
88 class FrmSidebarWinContainer : public _FrmSidebarWinContainer
89 {
90 };
91 
SwFrmSidebarWinContainer()92 SwFrmSidebarWinContainer::SwFrmSidebarWinContainer()
93     : mpFrmSidebarWinContainer( new FrmSidebarWinContainer() )
94 {}
95 
~SwFrmSidebarWinContainer()96 SwFrmSidebarWinContainer::~SwFrmSidebarWinContainer()
97 {
98     mpFrmSidebarWinContainer->clear();
99     delete mpFrmSidebarWinContainer;
100 }
101 
insert(const SwFrm & rFrm,const SwFmtFld & rFmtFld,SwSidebarWin & rSidebarWin)102 bool SwFrmSidebarWinContainer::insert( const SwFrm& rFrm,
103                                        const SwFmtFld& rFmtFld,
104                                        SwSidebarWin& rSidebarWin )
105 {
106     bool bInserted( false );
107 
108     FrmKey aFrmKey( &rFrm );
109     SidebarWinContainer& rSidebarWinContainer = (*mpFrmSidebarWinContainer)[ aFrmKey ];
110 
111     SidebarWinKey aSidebarWinKey( *(rFmtFld.GetTxtFld()->GetStart()) );
112     if ( rSidebarWinContainer.empty() ||
113          rSidebarWinContainer.find( aSidebarWinKey) == rSidebarWinContainer.end() )
114     {
115         rSidebarWinContainer[ aSidebarWinKey ] = &rSidebarWin;
116         bInserted = true;
117     }
118 
119     return bInserted;
120 }
121 
remove(const SwFrm & rFrm,const SwSidebarWin & rSidebarWin)122 bool SwFrmSidebarWinContainer::remove( const SwFrm& rFrm,
123                                        const SwSidebarWin& rSidebarWin )
124 {
125     bool bRemoved( false );
126 
127     FrmKey aFrmKey( &rFrm );
128     FrmSidebarWinContainer::iterator aFrmIter = mpFrmSidebarWinContainer->find( aFrmKey );
129     if ( aFrmIter != mpFrmSidebarWinContainer->end() )
130     {
131         SidebarWinContainer& rSidebarWinContainer = (*aFrmIter).second;
132         for ( SidebarWinContainer::iterator aIter = rSidebarWinContainer.begin();
133               aIter != rSidebarWinContainer.end();
134               ++aIter )
135         {
136             if ( (*aIter).second == &rSidebarWin )
137             {
138                 rSidebarWinContainer.erase( aIter );
139                 bRemoved = true;
140                 break;
141             }
142         }
143     }
144 
145     return bRemoved;
146 }
147 
empty(const SwFrm & rFrm)148 bool SwFrmSidebarWinContainer::empty( const SwFrm& rFrm )
149 {
150     bool bEmpty( true );
151 
152     FrmKey aFrmKey( &rFrm );
153     FrmSidebarWinContainer::iterator aFrmIter = mpFrmSidebarWinContainer->find( aFrmKey );
154     if ( aFrmIter != mpFrmSidebarWinContainer->end() )
155     {
156         bEmpty = (*aFrmIter).second.empty();
157     }
158 
159     return bEmpty;
160 }
161 
get(const SwFrm & rFrm,const sal_Int32 nIndex)162 SwSidebarWin* SwFrmSidebarWinContainer::get( const SwFrm& rFrm,
163                                              const sal_Int32 nIndex )
164 {
165     SwSidebarWin* pRet( 0 );
166 
167     FrmKey aFrmKey( &rFrm );
168     FrmSidebarWinContainer::iterator aFrmIter = mpFrmSidebarWinContainer->find( aFrmKey );
169     if ( aFrmIter != mpFrmSidebarWinContainer->end() )
170     {
171         SidebarWinContainer& rSidebarWinContainer = (*aFrmIter).second;
172         sal_Int32 nCounter( nIndex );
173         for ( SidebarWinContainer::iterator aIter = rSidebarWinContainer.begin();
174               nCounter >= 0 && aIter != rSidebarWinContainer.end();
175               ++aIter )
176         {
177             if ( nCounter == 0 )
178             {
179                 pRet = (*aIter).second;
180                 break;
181             }
182 
183             --nCounter;
184         }
185     }
186 
187     return pRet;
188 }
189 
getAll(const SwFrm & rFrm,std::vector<Window * > * pSidebarWins)190 void SwFrmSidebarWinContainer::getAll( const SwFrm& rFrm,
191                                        std::vector< Window* >* pSidebarWins )
192 {
193     pSidebarWins->clear();
194 
195     FrmKey aFrmKey( &rFrm );
196     FrmSidebarWinContainer::iterator aFrmIter = mpFrmSidebarWinContainer->find( aFrmKey );
197     if ( aFrmIter != mpFrmSidebarWinContainer->end() )
198     {
199         SidebarWinContainer& rSidebarWinContainer = (*aFrmIter).second;
200         for ( SidebarWinContainer::iterator aIter = rSidebarWinContainer.begin();
201               aIter != rSidebarWinContainer.end();
202               ++aIter )
203         {
204             pSidebarWins->push_back( (*aIter).second );
205         }
206     }
207 }
208 
209 } } // eof of namespace sw::sidebarwindows::
210