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