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