summaryrefslogtreecommitdiff
path: root/svtools/source/toolpanel/toolpanelcollection.cxx
blob: c6491dc621ff4be6b6ff9823c1038a981493d75c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include "toolpanelcollection.hxx"
#include "paneldecklisteners.hxx"

#include <tools/diagnose_ex.h>

#include <vector>


namespace svt
{



    //= ToolPanelCollection_Data

    struct ToolPanelCollection_Data
    {
        ::std::vector< PToolPanel >                 aPanels;
        ::boost::optional< size_t >                 aActivePanel;
        PanelDeckListeners                          aListeners;
    };


    //= ToolPanelCollection


    ToolPanelCollection::ToolPanelCollection()
        :m_pData( new ToolPanelCollection_Data )
    {
    }


    ToolPanelCollection::~ToolPanelCollection()
    {
        m_pData->aListeners.Dying();
    }


    size_t ToolPanelCollection::GetPanelCount() const
    {
        return m_pData->aPanels.size();
    }


    ::boost::optional< size_t > ToolPanelCollection::GetActivePanel() const
    {
        return m_pData->aActivePanel;
    }


    void ToolPanelCollection::ActivatePanel( const ::boost::optional< size_t >& i_rPanel )
    {
        if ( !!i_rPanel )
        {
            OSL_ENSURE( *i_rPanel < GetPanelCount(), "ToolPanelCollection::ActivatePanel: illegal panel no.!" );
            if ( *i_rPanel >= GetPanelCount() )
                return;
        }

        if ( m_pData->aActivePanel == i_rPanel )
            return;

        const ::boost::optional< size_t > aOldPanel( m_pData->aActivePanel );
        m_pData->aActivePanel = i_rPanel;

        // notify listeners
        m_pData->aListeners.ActivePanelChanged( aOldPanel, m_pData->aActivePanel );
    }


    PToolPanel ToolPanelCollection::GetPanel( const size_t i_nPos ) const
    {
        OSL_ENSURE( i_nPos < m_pData->aPanels.size(), "ToolPanelCollection::GetPanel: illegal position!" );
        if ( i_nPos >= m_pData->aPanels.size() )
            return PToolPanel();
        return m_pData->aPanels[ i_nPos ];
    }


    size_t ToolPanelCollection::InsertPanel( const PToolPanel& i_pPanel, const size_t i_nPosition )
    {
        OSL_ENSURE( i_pPanel.get(), "ToolPanelCollection::InsertPanel: illegal panel!" );
        if ( !i_pPanel.get() )
            return 0;

        // insert
        const size_t position = i_nPosition < m_pData->aPanels.size() ? i_nPosition : m_pData->aPanels.size();
        m_pData->aPanels.insert( m_pData->aPanels.begin() + position, i_pPanel );

        // update active panel
        if ( !!m_pData->aActivePanel )
        {
            if ( i_nPosition <= *m_pData->aActivePanel )
                ++*m_pData->aActivePanel;
        }

        // notifications
        m_pData->aListeners.PanelInserted( i_pPanel, i_nPosition );

        return position;
    }


    PToolPanel ToolPanelCollection::RemovePanel( const size_t i_nPosition )
    {
        OSL_ENSURE( i_nPosition < m_pData->aPanels.size(), "ToolPanelCollection::RemovePanel: illegal position!" );
        if ( i_nPosition >= m_pData->aPanels.size() )
            return nullptr;

        // if the active panel is going to be removed, activate another one (before the actual removal)
        if ( m_pData->aActivePanel == i_nPosition )
        {
            const ::boost::optional< size_t > aOldActive( m_pData->aActivePanel );

            if ( i_nPosition + 1 < GetPanelCount() )
            {
                ++*m_pData->aActivePanel;
            }
            else if ( i_nPosition > 0 )
            {
                --*m_pData->aActivePanel;
            }
            else
            {
                m_pData->aActivePanel.reset();
            }

            m_pData->aListeners.ActivePanelChanged( aOldActive, m_pData->aActivePanel );
        }

        // remember the removed panel for the aller
        PToolPanel pRemovedPanel( m_pData->aPanels[ i_nPosition ] );

        // actually remove
        m_pData->aPanels.erase( m_pData->aPanels.begin() + i_nPosition );

        if ( !!m_pData->aActivePanel )
        {
            if ( i_nPosition < *m_pData->aActivePanel )
            {
                --*m_pData->aActivePanel;
            }
        }

        // notify removed panel
        m_pData->aListeners.PanelRemoved( i_nPosition );

        return pRemovedPanel;
    }


    void ToolPanelCollection::AddListener( IToolPanelDeckListener& i_rListener )
    {
        m_pData->aListeners.AddListener( i_rListener );
    }


    void ToolPanelCollection::RemoveListener( IToolPanelDeckListener& i_rListener )
    {
        m_pData->aListeners.RemoveListener( i_rListener );
    }


} // namespace svt


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */