xref: /AOO41X/main/svtools/source/toolpanel/toolpanelcollection.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
11*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
12*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
13*cdf0e10cSrcweir  *
14*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
15*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
18*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
19*cdf0e10cSrcweir  *
20*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
21*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
22*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
23*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
24*cdf0e10cSrcweir  *
25*cdf0e10cSrcweir ************************************************************************/
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir #include "precompiled_svtools.hxx"
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include "toolpanelcollection.hxx"
30*cdf0e10cSrcweir #include "paneldecklisteners.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <vector>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir //........................................................................
37*cdf0e10cSrcweir namespace svt
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir //........................................................................
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 	//====================================================================
42*cdf0e10cSrcweir 	//= ToolPanelCollection_Data
43*cdf0e10cSrcweir 	//====================================================================
44*cdf0e10cSrcweir     struct ToolPanelCollection_Data
45*cdf0e10cSrcweir     {
46*cdf0e10cSrcweir         ::std::vector< PToolPanel >                 aPanels;
47*cdf0e10cSrcweir         ::boost::optional< size_t >                 aActivePanel;
48*cdf0e10cSrcweir         PanelDeckListeners                          aListeners;
49*cdf0e10cSrcweir     };
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 	//====================================================================
52*cdf0e10cSrcweir 	//= ToolPanelCollection
53*cdf0e10cSrcweir 	//====================================================================
54*cdf0e10cSrcweir 	//--------------------------------------------------------------------
55*cdf0e10cSrcweir     ToolPanelCollection::ToolPanelCollection()
56*cdf0e10cSrcweir         :m_pData( new ToolPanelCollection_Data )
57*cdf0e10cSrcweir     {
58*cdf0e10cSrcweir     }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 	//--------------------------------------------------------------------
61*cdf0e10cSrcweir     ToolPanelCollection::~ToolPanelCollection()
62*cdf0e10cSrcweir     {
63*cdf0e10cSrcweir         m_pData->aListeners.Dying();
64*cdf0e10cSrcweir     }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 	//--------------------------------------------------------------------
67*cdf0e10cSrcweir     size_t ToolPanelCollection::GetPanelCount() const
68*cdf0e10cSrcweir     {
69*cdf0e10cSrcweir         return m_pData->aPanels.size();
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 	//--------------------------------------------------------------------
73*cdf0e10cSrcweir     ::boost::optional< size_t > ToolPanelCollection::GetActivePanel() const
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         return m_pData->aActivePanel;
76*cdf0e10cSrcweir     }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     //--------------------------------------------------------------------
79*cdf0e10cSrcweir     void ToolPanelCollection::ActivatePanel( const ::boost::optional< size_t >& i_rPanel )
80*cdf0e10cSrcweir     {
81*cdf0e10cSrcweir         if ( !!i_rPanel )
82*cdf0e10cSrcweir         {
83*cdf0e10cSrcweir             OSL_ENSURE( *i_rPanel < GetPanelCount(), "ToolPanelCollection::ActivatePanel: illegal panel no.!" );
84*cdf0e10cSrcweir             if ( *i_rPanel >= GetPanelCount() )
85*cdf0e10cSrcweir                 return;
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir         if ( m_pData->aActivePanel == i_rPanel )
89*cdf0e10cSrcweir             return;
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         const ::boost::optional< size_t > aOldPanel( m_pData->aActivePanel );
92*cdf0e10cSrcweir         m_pData->aActivePanel = i_rPanel;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         // notify listeners
95*cdf0e10cSrcweir         m_pData->aListeners.ActivePanelChanged( aOldPanel, m_pData->aActivePanel );
96*cdf0e10cSrcweir     }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	//--------------------------------------------------------------------
99*cdf0e10cSrcweir     PToolPanel ToolPanelCollection::GetPanel( const size_t i_nPos ) const
100*cdf0e10cSrcweir     {
101*cdf0e10cSrcweir         OSL_ENSURE( i_nPos < m_pData->aPanels.size(), "ToolPanelCollection::GetPanel: illegal position!" );
102*cdf0e10cSrcweir         if ( i_nPos >= m_pData->aPanels.size() )
103*cdf0e10cSrcweir             return PToolPanel();
104*cdf0e10cSrcweir         return m_pData->aPanels[ i_nPos ];
105*cdf0e10cSrcweir     }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 	//--------------------------------------------------------------------
108*cdf0e10cSrcweir     size_t ToolPanelCollection::InsertPanel( const PToolPanel& i_pPanel, const size_t i_nPosition )
109*cdf0e10cSrcweir     {
110*cdf0e10cSrcweir         OSL_ENSURE( i_pPanel.get(), "ToolPanelCollection::InsertPanel: illegal panel!" );
111*cdf0e10cSrcweir         if ( !i_pPanel.get() )
112*cdf0e10cSrcweir             return 0;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         // insert
115*cdf0e10cSrcweir         const size_t position = i_nPosition < m_pData->aPanels.size() ? i_nPosition : m_pData->aPanels.size();
116*cdf0e10cSrcweir         m_pData->aPanels.insert( m_pData->aPanels.begin() + position, i_pPanel );
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         // update active panel
119*cdf0e10cSrcweir         if ( !!m_pData->aActivePanel )
120*cdf0e10cSrcweir         {
121*cdf0e10cSrcweir             if ( i_nPosition <= *m_pData->aActivePanel )
122*cdf0e10cSrcweir                 ++*m_pData->aActivePanel;
123*cdf0e10cSrcweir         }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir         // notifications
126*cdf0e10cSrcweir         m_pData->aListeners.PanelInserted( i_pPanel, i_nPosition );
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir         return position;
129*cdf0e10cSrcweir     }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 	//--------------------------------------------------------------------
132*cdf0e10cSrcweir     PToolPanel ToolPanelCollection::RemovePanel( const size_t i_nPosition )
133*cdf0e10cSrcweir     {
134*cdf0e10cSrcweir         OSL_ENSURE( i_nPosition < m_pData->aPanels.size(), "ToolPanelCollection::RemovePanel: illegal position!" );
135*cdf0e10cSrcweir         if ( i_nPosition >= m_pData->aPanels.size() )
136*cdf0e10cSrcweir             return NULL;
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         // if the active panel is going to be removed, activate another one (before the actual removal)
139*cdf0e10cSrcweir         if ( m_pData->aActivePanel == i_nPosition )
140*cdf0e10cSrcweir         {
141*cdf0e10cSrcweir             const ::boost::optional< size_t > aOldActive( m_pData->aActivePanel );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir             if ( i_nPosition + 1 < GetPanelCount() )
144*cdf0e10cSrcweir             {
145*cdf0e10cSrcweir                 ++*m_pData->aActivePanel;
146*cdf0e10cSrcweir             }
147*cdf0e10cSrcweir             else if ( i_nPosition > 0 )
148*cdf0e10cSrcweir             {
149*cdf0e10cSrcweir                 --*m_pData->aActivePanel;
150*cdf0e10cSrcweir             }
151*cdf0e10cSrcweir             else
152*cdf0e10cSrcweir             {
153*cdf0e10cSrcweir                 m_pData->aActivePanel.reset();
154*cdf0e10cSrcweir             }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir             m_pData->aListeners.ActivePanelChanged( aOldActive, m_pData->aActivePanel );
157*cdf0e10cSrcweir         }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         // remember the removed panel for the aller
160*cdf0e10cSrcweir         PToolPanel pRemovedPanel( m_pData->aPanels[ i_nPosition ] );
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir         // actually remove
163*cdf0e10cSrcweir         m_pData->aPanels.erase( m_pData->aPanels.begin() + i_nPosition );
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         if ( !!m_pData->aActivePanel )
166*cdf0e10cSrcweir         {
167*cdf0e10cSrcweir             if ( i_nPosition < *m_pData->aActivePanel )
168*cdf0e10cSrcweir             {
169*cdf0e10cSrcweir                 --*m_pData->aActivePanel;
170*cdf0e10cSrcweir             }
171*cdf0e10cSrcweir         }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir         // notify removed panel
174*cdf0e10cSrcweir         m_pData->aListeners.PanelRemoved( i_nPosition );
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir         return pRemovedPanel;
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	//--------------------------------------------------------------------
180*cdf0e10cSrcweir     void ToolPanelCollection::AddListener( IToolPanelDeckListener& i_rListener )
181*cdf0e10cSrcweir     {
182*cdf0e10cSrcweir         m_pData->aListeners.AddListener( i_rListener );
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	//--------------------------------------------------------------------
186*cdf0e10cSrcweir     void ToolPanelCollection::RemoveListener( IToolPanelDeckListener& i_rListener )
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         m_pData->aListeners.RemoveListener( i_rListener );
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir //........................................................................
192*cdf0e10cSrcweir } // namespace svt
193*cdf0e10cSrcweir //........................................................................
194