summaryrefslogtreecommitdiffstats
path: root/extensions/source/propctrlr/propertyeditor.cxx
blob: 2fcf556493e3f7e8bc011d7484634f37f078f374 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* -*- 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 "handlerhelper.hxx"
#include "propertyeditor.hxx"
#include "browserpage.hxx"
#include "linedescriptor.hxx"

#include <tools/debug.hxx>
#include <osl/diagnose.h>

namespace pcr
{
    using ::com::sun::star::uno::Any;
    using ::com::sun::star::inspection::XPropertyControl;
    using ::com::sun::star::uno::Reference;

    OPropertyEditor::OPropertyEditor(const css::uno::Reference<css::uno::XComponentContext>& rContext, weld::Builder& rBuilder)
        : m_xContainer(rBuilder.weld_container("box"))
        , m_xTabControl(rBuilder.weld_notebook("tabcontrol"))
        , m_xControlHoldingParent(rBuilder.weld_container("controlparent")) // controls initially have this parent before they are moved
        , m_xContext(rContext)
        , m_pListener(nullptr)
        , m_pObserver(nullptr)
        , m_nNextId(1)
        , m_bHasHelpSection(false)
    {
        PropertyHandlerHelper::setBuilderParent(rContext, m_xControlHoldingParent.get());

        m_xTabControl->connect_leave_page(LINK(this, OPropertyEditor, OnPageDeactivate));
        m_xTabControl->connect_enter_page(LINK(this, OPropertyEditor, OnPageActivate));
    }

    OPropertyEditor::~OPropertyEditor()
    {
        PropertyHandlerHelper::clearBuilderParent(m_xContext);
        ClearAll();
    }

    void OPropertyEditor::ClearAll()
    {
        m_nNextId=1;

        m_aPropertyPageIds.clear();
        m_aShownPages.clear();
        m_aHiddenPages.clear();

        int nCount = m_xTabControl->get_n_pages();
        for (int i = nCount - 1; i >= 0; --i)
        {
            OString sID = m_xTabControl->get_page_ident(i);
            m_xTabControl->remove_page(sID);
        }

        assert(m_xTabControl->get_n_pages() == 0);
    }

    Size OPropertyEditor::get_preferred_size() const
    {
        return m_xTabControl->get_preferred_size();
    }

    void OPropertyEditor::CommitModified()
    {
        // commit all of my pages, if necessary
        for (const auto& page : m_aShownPages)
        {
            OBrowserPage* pPage = page.second.xPage.get();
            if (pPage && pPage->getListBox().IsModified() )
                pPage->getListBox().CommitModified();
        }
    }

    OBrowserPage* OPropertyEditor::getPage(const OUString& rPropertyName)
    {
        OBrowserPage* pPage = nullptr;
        MapStringToPageId::const_iterator aPropertyPageIdPos = m_aPropertyPageIds.find(rPropertyName);
        if (aPropertyPageIdPos != m_aPropertyPageIds.end())
            pPage = getPage(aPropertyPageIdPos->second);
        return pPage;
    }

    const OBrowserPage* OPropertyEditor::getPage( const OUString& _rPropertyName ) const
    {
        return const_cast< OPropertyEditor* >( this )->getPage( _rPropertyName );
    }

    OBrowserPage* OPropertyEditor::getPage(sal_uInt16 rPageId)
    {
        OBrowserPage* pPage = nullptr;
        auto aPagePos = m_aShownPages.find(rPageId);
        if (aPagePos != m_aShownPages.end())
            pPage = aPagePos->second.xPage.get();
        return pPage;
    }

    const OBrowserPage* OPropertyEditor::getPage(sal_uInt16 rPageId) const
    {
        return const_cast<OPropertyEditor*>(this)->getPage(rPageId);
    }

    sal_uInt16 OPropertyEditor::AppendPage(const OUString& rText, const OString& rHelpId)
    {
        // obtain a new id
        sal_uInt16 nId = m_nNextId++;
        // insert the id
        OString sIdent = OString::number(nId);
        m_xTabControl->append_page(sIdent, rText);

        // create a new page
        auto xPage = std::make_unique<OBrowserPage>(m_xTabControl->get_page(sIdent), m_xControlHoldingParent.get());
        // some knittings
        xPage->getListBox().SetListener(m_pListener);
        xPage->getListBox().SetObserver(m_pObserver);
        xPage->getListBox().EnableHelpSection(m_bHasHelpSection);
        xPage->SetHelpId(rHelpId);

        m_aShownPages[nId] = PropertyPage(m_xTabControl->get_n_pages() - 1, rText, std::move(xPage));

        // immediately activate the page
        m_xTabControl->set_current_page(sIdent);

        return nId;
    }

    void OPropertyEditor::SetHelpId( const OString& rHelpId )
    {
        m_xTabControl->set_help_id(rHelpId);
    }

    void OPropertyEditor::RemovePage(sal_uInt16 nID)
    {
        auto aPagePos = m_aShownPages.find(nID);
        if (aPagePos == m_aShownPages.end())
            return;

        m_aShownPages.erase(aPagePos);
        OString sIdent(OString::number(nID));
        m_xTabControl->remove_page(sIdent);
    }

    void OPropertyEditor::SetPage(sal_uInt16 nId)
    {
        m_xTabControl->set_current_page(OString::number(nId));
    }

    sal_uInt16 OPropertyEditor::GetCurPage() const
    {
        return m_xTabControl->get_current_page_ident().toUInt32();
    }

    void OPropertyEditor::forEachPage( PageOperation _pOperation )
    {
        int nCount = m_xTabControl->get_n_pages();
        for (int i = 0; i < nCount; ++i)
        {
            sal_uInt16 nID = m_xTabControl->get_page_ident(i).toUInt32();
            OBrowserPage* pPage = getPage(nID);
            if (!pPage)
                continue;
            (this->*_pOperation)( *pPage, nullptr );
        }
    }

    void OPropertyEditor::setPageLineListener( OBrowserPage& rPage, const void* )
    {
        rPage.getListBox().SetListener( m_pListener );
    }

    void OPropertyEditor::SetLineListener(IPropertyLineListener* pListener)
    {
        m_pListener = pListener;
        forEachPage( &OPropertyEditor::setPageLineListener );
    }

    void OPropertyEditor::setPageControlObserver( OBrowserPage& rPage, const void* )
    {
        rPage.getListBox().SetObserver( m_pObserver );
    }

    void OPropertyEditor::SetControlObserver( IPropertyControlObserver* _pObserver )
    {
        m_pObserver = _pObserver;
        forEachPage( &OPropertyEditor::setPageControlObserver );
    }

    void OPropertyEditor::EnableHelpSection( bool bEnable )
    {
        m_bHasHelpSection = bEnable;
        forEachPage( &OPropertyEditor::enableHelpSection );
    }

    void OPropertyEditor::SetHelpText( const OUString& rHelpText )
    {
        int nCount = m_xTabControl->get_n_pages();
        for (int i = 0; i < nCount; ++i)
        {
            sal_uInt16 nID = m_xTabControl->get_page_ident(i).toUInt32();
            OBrowserPage* pPage = getPage(nID);
            if (!pPage)
                continue;
            setHelpSectionText( *pPage, &rHelpText );
        }
    }

    void OPropertyEditor::enableHelpSection( OBrowserPage& rPage, const void* )
    {
        rPage.getListBox().EnableHelpSection( m_bHasHelpSection );
    }

    void OPropertyEditor::setHelpSectionText( OBrowserPage& rPage, const void* pPointerToOUString )
    {
        OSL_ENSURE( pPointerToOUString, "OPropertyEditor::setHelpSectionText: invalid argument!" );
        if ( !pPointerToOUString )
            return;

        const OUString& rText( *static_cast<const OUString*>(pPointerToOUString) );
        rPage.getListBox().SetHelpText( rText );
    }

    void OPropertyEditor::InsertEntry( const OLineDescriptor& rData, sal_uInt16 nPageId, sal_uInt16 nPos )
    {
        // let the current page handle this
        OBrowserPage* pPage = getPage(nPageId);
        DBG_ASSERT( pPage, "OPropertyEditor::InsertEntry: don't have such a page!" );
        if ( !pPage )
            return;

        pPage->getListBox().InsertEntry( rData, nPos );

        OSL_ENSURE( m_aPropertyPageIds.find( rData.sName ) == m_aPropertyPageIds.end(),
            "OPropertyEditor::InsertEntry: property already present in the map!" );
        m_aPropertyPageIds.emplace( rData.sName, nPageId );
    }

    void OPropertyEditor::RemoveEntry( const OUString& rName )
    {
        OBrowserPage* pPage = getPage( rName );
        if ( pPage )
        {
            OSL_VERIFY( pPage->getListBox().RemoveEntry( rName ) );

            OSL_ENSURE( m_aPropertyPageIds.find( rName ) != m_aPropertyPageIds.end(),
                "OPropertyEditor::RemoveEntry: property not present in the map!" );
            m_aPropertyPageIds.erase( rName );
        }
    }

    void OPropertyEditor::ChangeEntry( const OLineDescriptor& rData )
    {
        OBrowserPage* pPage = getPage( rData.sName );
        if ( pPage )
            pPage->getListBox().ChangeEntry( rData, EDITOR_LIST_REPLACE_EXISTING );
    }

    void OPropertyEditor::SetPropertyValue( const OUString& rEntryName, const Any& _rValue, bool _bUnknownValue )
    {
        OBrowserPage* pPage = getPage( rEntryName );
        if ( pPage )
            pPage->getListBox().SetPropertyValue( rEntryName, _rValue, _bUnknownValue );
    }

    sal_uInt16 OPropertyEditor::GetPropertyPos( const OUString& rEntryName ) const
    {
        sal_uInt16 nVal=EDITOR_LIST_ENTRY_NOTFOUND;
        const OBrowserPage* pPage = getPage( rEntryName );
        if ( pPage )
            nVal = pPage->getListBox().GetPropertyPos( rEntryName );
        return nVal;
    }

    void OPropertyEditor::ShowPropertyPage(sal_uInt16 nPageId, bool bShow)
    {
        assert((m_aHiddenPages.find(nPageId) != m_aHiddenPages.end() ||
                m_aShownPages.find(nPageId) != m_aShownPages.end()) && "page doesn't exist");
        OString sIdent(OString::number(nPageId));
        if (!bShow)
        {
            auto aPagePos = m_aShownPages.find(nPageId);
            if (aPagePos != m_aShownPages.end())
            {
                aPagePos->second.xPage->detach();
                m_xTabControl->remove_page(sIdent);

                m_aHiddenPages[nPageId] = std::move(aPagePos->second);
                m_aShownPages.erase(aPagePos);
            }
        }
        else
        {
            auto aPagePos = m_aHiddenPages.find(nPageId);
            if (aPagePos != m_aHiddenPages.end())
            {
                m_xTabControl->insert_page(sIdent, aPagePos->second.sLabel, aPagePos->second.nPos);
                aPagePos->second.xPage->reattach(m_xTabControl->get_page(sIdent));

                m_aShownPages[nPageId] = std::move(aPagePos->second);
                m_aHiddenPages.erase(aPagePos);
            }
        }
    }

    void OPropertyEditor::EnablePropertyControls( const OUString& rEntryName, sal_Int16 nControls, bool bEnable )
    {
        for (const auto& rPage : m_aShownPages)
        {
            OBrowserPage* pPage = rPage.second.xPage.get();
            if (pPage)
                pPage->getListBox().EnablePropertyControls( rEntryName, nControls, bEnable );
        }
    }

    void OPropertyEditor::EnablePropertyLine( const OUString& rEntryName, bool bEnable )
    {
        for (const auto& rPage : m_aShownPages)
        {
            OBrowserPage* pPage = rPage.second.xPage.get();
            if (pPage)
                pPage->getListBox().EnablePropertyLine( rEntryName, bEnable );
        }
    }

    Reference< XPropertyControl > OPropertyEditor::GetPropertyControl(const OUString& rEntryName)
    {
        Reference< XPropertyControl > xControl;
        // let the current page handle this
        OBrowserPage* pPage = getPage(m_xTabControl->get_current_page_ident().toUInt32());
        if (pPage)
            xControl = pPage->getListBox().GetPropertyControl(rEntryName);
        return xControl;
    }

    IMPL_LINK(OPropertyEditor, OnPageActivate, const OString&, rNewPage, void)
    {
        m_aPageActivationHandler.Call(rNewPage);
    }

    IMPL_LINK(OPropertyEditor, OnPageDeactivate, const OString&, rIdent, bool)
    {
        // commit the data on the current (to-be-deactivated) tab page
        // (79404)
        OBrowserPage* pCurrentPage = getPage(rIdent.toUInt32());
        if (!pCurrentPage)
            return true;

        if (pCurrentPage->getListBox().IsModified())
            pCurrentPage->getListBox().CommitModified();

        return true;
    }

    OPropertyEditor::PropertyPage::PropertyPage()
        : nPos(0)
    {
    }

    OPropertyEditor::PropertyPage::PropertyPage(sal_uInt16 nPagePos, const OUString& rLabel, std::unique_ptr<OBrowserPage> pPage)
        : nPos(nPagePos), sLabel(rLabel), xPage(std::move(pPage))
    {
    }

} // namespace pcr

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