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 #include "precompiled_svtools.hxx"
25 
26 #include "toolpanelcollection.hxx"
27 #include "paneldecklisteners.hxx"
28 
29 #include <tools/diagnose_ex.h>
30 
31 #include <vector>
32 
33 //........................................................................
34 namespace svt
35 {
36 //........................................................................
37 
38 	//====================================================================
39 	//= ToolPanelCollection_Data
40 	//====================================================================
41     struct ToolPanelCollection_Data
42     {
43         ::std::vector< PToolPanel >                 aPanels;
44         ::boost::optional< size_t >                 aActivePanel;
45         PanelDeckListeners                          aListeners;
46     };
47 
48 	//====================================================================
49 	//= ToolPanelCollection
50 	//====================================================================
51 	//--------------------------------------------------------------------
ToolPanelCollection()52     ToolPanelCollection::ToolPanelCollection()
53         :m_pData( new ToolPanelCollection_Data )
54     {
55     }
56 
57 	//--------------------------------------------------------------------
~ToolPanelCollection()58     ToolPanelCollection::~ToolPanelCollection()
59     {
60         m_pData->aListeners.Dying();
61     }
62 
63 	//--------------------------------------------------------------------
GetPanelCount() const64     size_t ToolPanelCollection::GetPanelCount() const
65     {
66         return m_pData->aPanels.size();
67     }
68 
69 	//--------------------------------------------------------------------
GetActivePanel() const70     ::boost::optional< size_t > ToolPanelCollection::GetActivePanel() const
71     {
72         return m_pData->aActivePanel;
73     }
74 
75     //--------------------------------------------------------------------
ActivatePanel(const::boost::optional<size_t> & i_rPanel)76     void ToolPanelCollection::ActivatePanel( const ::boost::optional< size_t >& i_rPanel )
77     {
78         if ( !!i_rPanel )
79         {
80             OSL_ENSURE( *i_rPanel < GetPanelCount(), "ToolPanelCollection::ActivatePanel: illegal panel no.!" );
81             if ( *i_rPanel >= GetPanelCount() )
82                 return;
83         }
84 
85         if ( m_pData->aActivePanel == i_rPanel )
86             return;
87 
88         const ::boost::optional< size_t > aOldPanel( m_pData->aActivePanel );
89         m_pData->aActivePanel = i_rPanel;
90 
91         // notify listeners
92         m_pData->aListeners.ActivePanelChanged( aOldPanel, m_pData->aActivePanel );
93     }
94 
95 	//--------------------------------------------------------------------
GetPanel(const size_t i_nPos) const96     PToolPanel ToolPanelCollection::GetPanel( const size_t i_nPos ) const
97     {
98         OSL_ENSURE( i_nPos < m_pData->aPanels.size(), "ToolPanelCollection::GetPanel: illegal position!" );
99         if ( i_nPos >= m_pData->aPanels.size() )
100             return PToolPanel();
101         return m_pData->aPanels[ i_nPos ];
102     }
103 
104 	//--------------------------------------------------------------------
InsertPanel(const PToolPanel & i_pPanel,const size_t i_nPosition)105     size_t ToolPanelCollection::InsertPanel( const PToolPanel& i_pPanel, const size_t i_nPosition )
106     {
107         OSL_ENSURE( i_pPanel.get(), "ToolPanelCollection::InsertPanel: illegal panel!" );
108         if ( !i_pPanel.get() )
109             return 0;
110 
111         // insert
112         const size_t position = i_nPosition < m_pData->aPanels.size() ? i_nPosition : m_pData->aPanels.size();
113         m_pData->aPanels.insert( m_pData->aPanels.begin() + position, i_pPanel );
114 
115         // update active panel
116         if ( !!m_pData->aActivePanel )
117         {
118             if ( i_nPosition <= *m_pData->aActivePanel )
119                 ++*m_pData->aActivePanel;
120         }
121 
122         // notifications
123         m_pData->aListeners.PanelInserted( i_pPanel, i_nPosition );
124 
125         return position;
126     }
127 
128 	//--------------------------------------------------------------------
RemovePanel(const size_t i_nPosition)129     PToolPanel ToolPanelCollection::RemovePanel( const size_t i_nPosition )
130     {
131         OSL_ENSURE( i_nPosition < m_pData->aPanels.size(), "ToolPanelCollection::RemovePanel: illegal position!" );
132         if ( i_nPosition >= m_pData->aPanels.size() )
133             return NULL;
134 
135         // if the active panel is going to be removed, activate another one (before the actual removal)
136         if ( m_pData->aActivePanel == i_nPosition )
137         {
138             const ::boost::optional< size_t > aOldActive( m_pData->aActivePanel );
139 
140             if ( i_nPosition + 1 < GetPanelCount() )
141             {
142                 ++*m_pData->aActivePanel;
143             }
144             else if ( i_nPosition > 0 )
145             {
146                 --*m_pData->aActivePanel;
147             }
148             else
149             {
150                 m_pData->aActivePanel.reset();
151             }
152 
153             m_pData->aListeners.ActivePanelChanged( aOldActive, m_pData->aActivePanel );
154         }
155 
156         // remember the removed panel for the aller
157         PToolPanel pRemovedPanel( m_pData->aPanels[ i_nPosition ] );
158 
159         // actually remove
160         m_pData->aPanels.erase( m_pData->aPanels.begin() + i_nPosition );
161 
162         if ( !!m_pData->aActivePanel )
163         {
164             if ( i_nPosition < *m_pData->aActivePanel )
165             {
166                 --*m_pData->aActivePanel;
167             }
168         }
169 
170         // notify removed panel
171         m_pData->aListeners.PanelRemoved( i_nPosition );
172 
173         return pRemovedPanel;
174     }
175 
176 	//--------------------------------------------------------------------
AddListener(IToolPanelDeckListener & i_rListener)177     void ToolPanelCollection::AddListener( IToolPanelDeckListener& i_rListener )
178     {
179         m_pData->aListeners.AddListener( i_rListener );
180     }
181 
182 	//--------------------------------------------------------------------
RemoveListener(IToolPanelDeckListener & i_rListener)183     void ToolPanelCollection::RemoveListener( IToolPanelDeckListener& i_rListener )
184     {
185         m_pData->aListeners.RemoveListener( i_rListener );
186     }
187 
188 //........................................................................
189 } // namespace svt
190 //........................................................................
191