Bump version to 5.0-14
[LibreOffice.git] / svtools / source / toolpanel / toolpanelcollection.cxx
blob9537b11e432342a46275576e5fedc9c9a2f8d677
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "toolpanelcollection.hxx"
22 #include "paneldecklisteners.hxx"
24 #include <tools/diagnose_ex.h>
26 #include <vector>
29 namespace svt
34 //= ToolPanelCollection_Data
36 struct ToolPanelCollection_Data
38 ::std::vector< PToolPanel > aPanels;
39 ::boost::optional< size_t > aActivePanel;
40 PanelDeckListeners aListeners;
44 //= ToolPanelCollection
47 ToolPanelCollection::ToolPanelCollection()
48 :m_pData( new ToolPanelCollection_Data )
53 ToolPanelCollection::~ToolPanelCollection()
55 m_pData->aListeners.Dying();
59 size_t ToolPanelCollection::GetPanelCount() const
61 return m_pData->aPanels.size();
65 ::boost::optional< size_t > ToolPanelCollection::GetActivePanel() const
67 return m_pData->aActivePanel;
71 void ToolPanelCollection::ActivatePanel( const ::boost::optional< size_t >& i_rPanel )
73 if ( !!i_rPanel )
75 OSL_ENSURE( *i_rPanel < GetPanelCount(), "ToolPanelCollection::ActivatePanel: illegal panel no.!" );
76 if ( *i_rPanel >= GetPanelCount() )
77 return;
80 if ( m_pData->aActivePanel == i_rPanel )
81 return;
83 const ::boost::optional< size_t > aOldPanel( m_pData->aActivePanel );
84 m_pData->aActivePanel = i_rPanel;
86 // notify listeners
87 m_pData->aListeners.ActivePanelChanged( aOldPanel, m_pData->aActivePanel );
91 PToolPanel ToolPanelCollection::GetPanel( const size_t i_nPos ) const
93 OSL_ENSURE( i_nPos < m_pData->aPanels.size(), "ToolPanelCollection::GetPanel: illegal position!" );
94 if ( i_nPos >= m_pData->aPanels.size() )
95 return PToolPanel();
96 return m_pData->aPanels[ i_nPos ];
100 size_t ToolPanelCollection::InsertPanel( const PToolPanel& i_pPanel, const size_t i_nPosition )
102 OSL_ENSURE( i_pPanel.get(), "ToolPanelCollection::InsertPanel: illegal panel!" );
103 if ( !i_pPanel.get() )
104 return 0;
106 // insert
107 const size_t position = i_nPosition < m_pData->aPanels.size() ? i_nPosition : m_pData->aPanels.size();
108 m_pData->aPanels.insert( m_pData->aPanels.begin() + position, i_pPanel );
110 // update active panel
111 if ( !!m_pData->aActivePanel )
113 if ( i_nPosition <= *m_pData->aActivePanel )
114 ++*m_pData->aActivePanel;
117 // notifications
118 m_pData->aListeners.PanelInserted( i_pPanel, i_nPosition );
120 return position;
124 PToolPanel ToolPanelCollection::RemovePanel( const size_t i_nPosition )
126 OSL_ENSURE( i_nPosition < m_pData->aPanels.size(), "ToolPanelCollection::RemovePanel: illegal position!" );
127 if ( i_nPosition >= m_pData->aPanels.size() )
128 return NULL;
130 // if the active panel is going to be removed, activate another one (before the actual removal)
131 if ( m_pData->aActivePanel == i_nPosition )
133 const ::boost::optional< size_t > aOldActive( m_pData->aActivePanel );
135 if ( i_nPosition + 1 < GetPanelCount() )
137 ++*m_pData->aActivePanel;
139 else if ( i_nPosition > 0 )
141 --*m_pData->aActivePanel;
143 else
145 m_pData->aActivePanel.reset();
148 m_pData->aListeners.ActivePanelChanged( aOldActive, m_pData->aActivePanel );
151 // remember the removed panel for the aller
152 PToolPanel pRemovedPanel( m_pData->aPanels[ i_nPosition ] );
154 // actually remove
155 m_pData->aPanels.erase( m_pData->aPanels.begin() + i_nPosition );
157 if ( !!m_pData->aActivePanel )
159 if ( i_nPosition < *m_pData->aActivePanel )
161 --*m_pData->aActivePanel;
165 // notify removed panel
166 m_pData->aListeners.PanelRemoved( i_nPosition );
168 return pRemovedPanel;
172 void ToolPanelCollection::AddListener( IToolPanelDeckListener& i_rListener )
174 m_pData->aListeners.AddListener( i_rListener );
178 void ToolPanelCollection::RemoveListener( IToolPanelDeckListener& i_rListener )
180 m_pData->aListeners.RemoveListener( i_rListener );
184 } // namespace svt
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */