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