summaryrefslogtreecommitdiff
path: root/extensions/source/propctrlr/propertyeditor.cxx
blob: 3585ba3945973a81903d7597997c1f7ec1e1141f (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* -*- 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 "propertyeditor.hxx"
#include "browserpage.hxx"
#include "linedescriptor.hxx"

#include <tools/debug.hxx>


namespace pcr
{


    #define LAYOUT_BORDER_LEFT      3
    #define LAYOUT_BORDER_TOP       3
    #define LAYOUT_BORDER_RIGHT     3
    #define LAYOUT_BORDER_BOTTOM    3

    using ::com::sun::star::uno::Any;
    using ::com::sun::star::inspection::XPropertyControl;
    using ::com::sun::star::uno::Reference;


    // class OPropertyEditor


    OPropertyEditor::OPropertyEditor( vcl::Window* pParent, WinBits nWinStyle)
            :Control(pParent, nWinStyle)
            ,m_aTabControl( new TabControl(this) )
            ,m_pListener(NULL)
            ,m_pObserver(NULL)
            ,m_nNextId(1)
            ,m_bHasHelpSection( false )
            ,m_nMinHelpLines( 0 )
            ,m_nMaxHelpLines( 0 )
    {

        m_aTabControl->Show();
        m_aTabControl->SetDeactivatePageHdl(LINK(this, OPropertyEditor, OnPageDeactivate));
        m_aTabControl->SetActivatePageHdl(LINK(this, OPropertyEditor, OnPageActivate));
        m_aTabControl->SetBackground(GetBackground());
        m_aTabControl->SetPaintTransparent(true);
    }


    OPropertyEditor::~OPropertyEditor()
    {
        disposeOnce();
    }

    void OPropertyEditor::dispose()
    {
        Hide();
        ClearAll();
        m_aTabControl.disposeAndClear();
        Control::dispose();
    }


    void OPropertyEditor::ClearAll()
    {
        m_nNextId=1;
        sal_uInt16 nCount = m_aTabControl->GetPageCount();
        for(long i = nCount-1; i >= 0; --i)
        {
            sal_uInt16 nID = m_aTabControl->GetPageId((sal_uInt16)i);
            VclPtr<OBrowserPage> pPage = static_cast<OBrowserPage*>(m_aTabControl->GetTabPage(nID));
            if (pPage)
            {
                pPage->EnableInput(false);
                m_aTabControl->RemovePage(nID);
                pPage.disposeAndClear();
            }
        }
        m_aTabControl->Clear();

        {
            MapStringToPageId aEmpty;
            m_aPropertyPageIds.swap( aEmpty );
        }

        m_aHiddenPages.clear();
    }


    sal_Int32 OPropertyEditor::getMinimumHeight()
    {
        sal_Int32 nMinHeight( LAYOUT_BORDER_TOP + LAYOUT_BORDER_BOTTOM );

        if ( m_aTabControl->GetPageCount() > 0 )
        {
            sal_uInt16 nFirstID = m_aTabControl->GetPageId( 0 );

            // reserve space for the tabs themself
            Rectangle aTabArea( m_aTabControl->GetTabBounds( nFirstID ) );
            nMinHeight += aTabArea.GetHeight();

            // ask the page how much it requires
            OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( nFirstID ) );
            if ( pPage )
                nMinHeight += pPage->getMinimumHeight();
        }
        else
            nMinHeight += 250;  // arbitrary ...

        return nMinHeight;
    }


    sal_Int32 OPropertyEditor::getMinimumWidth()
    {
        sal_uInt16 nCount = m_aTabControl->GetPageCount();
        sal_Int32 nPageMinWidth = 0;
        for(long i = nCount-1; i >= 0; --i)
        {
            sal_uInt16 nID = m_aTabControl->GetPageId((sal_uInt16)i);
            OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl->GetTabPage(nID));
            if (pPage)
            {
                sal_Int32 nCurPageMinWidth = pPage->getMinimumWidth();
                if( nCurPageMinWidth > nPageMinWidth )
                    nPageMinWidth = nCurPageMinWidth;
            }
        }
        return nPageMinWidth+6;
    }


    void OPropertyEditor::CommitModified()
    {
        // commit all of my pages, if necessary

        sal_uInt16 nCount = m_aTabControl->GetPageCount();
        for ( sal_uInt16 i=0; i<nCount; ++i )
        {
            sal_uInt16 nID = m_aTabControl->GetPageId( i );
            OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( nID ) );

            if ( pPage && pPage->getListBox().IsModified() )
                pPage->getListBox().CommitModified();
        }
    }


    void OPropertyEditor::GetFocus()
    {
        m_aTabControl->GrabFocus();
    }


    OBrowserPage* OPropertyEditor::getPage( const OUString& _rPropertyName )
    {
        OBrowserPage* pPage = NULL;
        MapStringToPageId::const_iterator aPropertyPageIdPos = m_aPropertyPageIds.find( _rPropertyName );
        if ( aPropertyPageIdPos != m_aPropertyPageIds.end() )
            pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( 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 )
    {
        return static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( _rPageId ) );
    }


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


    void OPropertyEditor::Resize()
    {
        Rectangle aPlayground(
            Point( LAYOUT_BORDER_LEFT, LAYOUT_BORDER_TOP ),
            Size(
                GetOutputSizePixel().Width() - LAYOUT_BORDER_LEFT - LAYOUT_BORDER_RIGHT,
                GetOutputSizePixel().Height() - LAYOUT_BORDER_TOP - LAYOUT_BORDER_BOTTOM
            )
        );

        Rectangle aTabArea( aPlayground );
        m_aTabControl->SetPosSizePixel( aTabArea.TopLeft(), aTabArea.GetSize() );
    }


    sal_uInt16 OPropertyEditor::AppendPage( const OUString & _rText, const OString& _rHelpId )
    {
        // obtain a new id
        sal_uInt16 nId = m_nNextId++;
        // insert the id
        m_aTabControl->InsertPage(nId, _rText);

        // create a new page
        OBrowserPage* pPage = new OBrowserPage(m_aTabControl.get());
        pPage->SetText( _rText );
        // some knittings
        pPage->SetSizePixel(m_aTabControl->GetTabPageSizePixel());
        pPage->getListBox().SetListener(m_pListener);
        pPage->getListBox().SetObserver(m_pObserver);
        pPage->getListBox().EnableHelpSection( m_bHasHelpSection );
        pPage->getListBox().SetHelpLineLimites( m_nMinHelpLines, m_nMaxHelpLines );
        pPage->SetHelpId( _rHelpId );

        // immediately activate the page
        m_aTabControl->SetTabPage(nId, pPage);
        m_aTabControl->SetCurPageId(nId);

        return nId;
    }


    void OPropertyEditor::SetHelpId( const OString& rHelpId )
    {
        Control::SetHelpId("");
        m_aTabControl->SetHelpId(rHelpId);
    }


    void OPropertyEditor::RemovePage(sal_uInt16 nID)
    {
        VclPtr<OBrowserPage> pPage = static_cast<OBrowserPage*>(m_aTabControl->GetTabPage(nID));

        if (pPage)
            pPage->EnableInput(false);
        m_aTabControl->RemovePage(nID);
        pPage.disposeAndClear();
    }


    void OPropertyEditor::SetPage(sal_uInt16 nId)
    {
        m_aTabControl->SetCurPageId(nId);
    }


    sal_uInt16 OPropertyEditor::GetCurPage()
    {
        if(m_aTabControl->GetPageCount()>0)
            return m_aTabControl->GetCurPageId();
        else
            return 0;
    }


    void OPropertyEditor::Update(const ::std::mem_fun_t<void,OBrowserListBox>& _aUpdateFunction)
    {
        // forward this to all our pages
        sal_uInt16 nCount = m_aTabControl->GetPageCount();
        for (sal_uInt16 i=0;i<nCount;++i)
        {
            sal_uInt16 nID = m_aTabControl->GetPageId(i);
            OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl->GetTabPage(nID));
            if (pPage)
                _aUpdateFunction(&pPage->getListBox());
        }
    }

    void OPropertyEditor::EnableUpdate()
    {
        Update(::std::mem_fun(&OBrowserListBox::EnableUpdate));
    }

    void OPropertyEditor::DisableUpdate()
    {
        Update(::std::mem_fun(&OBrowserListBox::DisableUpdate));
    }


    void OPropertyEditor::forEachPage( PageOperation _pOperation, const void* _pArgument )
    {
        sal_uInt16 nCount = m_aTabControl->GetPageCount();
        for ( sal_uInt16 i=0; i<nCount; ++i )
        {
            sal_uInt16 nID = m_aTabControl->GetPageId(i);
            OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( nID ) );
            if ( !pPage )
                continue;
            (this->*_pOperation)( *pPage, _pArgument );
        }
    }


    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 )
    {
        forEachPage( &OPropertyEditor::setHelpSectionText, &_rHelpText );
    }


    void OPropertyEditor::SetHelpLineLimites( sal_Int32 _nMinLines, sal_Int32 _nMaxLines )
    {
        m_nMinHelpLines = _nMinLines;
        m_nMaxHelpLines = _nMaxLines;
        forEachPage( &OPropertyEditor::setHelpLineLimits );
    }


    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::setHelpLineLimits( OBrowserPage& _rPage, const void* )
    {
        _rPage.getListBox().SetHelpLineLimites( m_nMinHelpLines, m_nMaxHelpLines );
    }


    sal_uInt16 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 EDITOR_LIST_ENTRY_NOTFOUND;

        sal_uInt16 nEntry = 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.insert( MapStringToPageId::value_type( rData.sName, _nPageId ) );

        return nEntry;
    }


    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 )
    {
        if ( !_bShow )
        {
            sal_uInt16 nPagePos = m_aTabControl->GetPagePos( _nPageId );
            if ( TAB_PAGE_NOTFOUND == nPagePos )
                return;
            DBG_ASSERT( m_aHiddenPages.find( _nPageId ) == m_aHiddenPages.end(), "OPropertyEditor::ShowPropertyPage: page already hidden!" );

            m_aHiddenPages[ _nPageId ] = HiddenPage( nPagePos, m_aTabControl->GetTabPage( _nPageId ) );
            m_aTabControl->RemovePage( _nPageId );
        }
        else
        {
            ::std::map< sal_uInt16, HiddenPage >::iterator aPagePos = m_aHiddenPages.find( _nPageId );
            if ( aPagePos == m_aHiddenPages.end() )
                return;

            aPagePos->second.pPage->SetSizePixel( m_aTabControl->GetTabPageSizePixel() );
            m_aTabControl->InsertPage( aPagePos->first, aPagePos->second.pPage->GetText(), aPagePos->second.nPos );
            m_aTabControl->SetTabPage( aPagePos->first, aPagePos->second.pPage );

            m_aHiddenPages.erase( aPagePos );
        }
    }


    void OPropertyEditor::EnablePropertyControls( const OUString& _rEntryName, sal_Int16 _nControls, bool _bEnable )
    {
        for ( sal_uInt16 i = 0; i < m_aTabControl->GetPageCount(); ++i )
        {
            OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( m_aTabControl->GetPageId( i ) ) );
            if ( pPage )
                pPage->getListBox().EnablePropertyControls( _rEntryName, _nControls, _bEnable );
        }
    }


    void OPropertyEditor::EnablePropertyLine( const OUString& _rEntryName, bool _bEnable )
    {
        for ( sal_uInt16 i = 0; i < m_aTabControl->GetPageCount(); ++i )
        {
            OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( m_aTabControl->GetPageId( i ) ) );
            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 = static_cast<OBrowserPage*>(m_aTabControl->GetTabPage(m_aTabControl->GetCurPageId()));
        if (pPage)
            xControl = pPage->getListBox().GetPropertyControl(rEntryName);
        return xControl;
    }


    IMPL_LINK_NOARG(OPropertyEditor, OnPageActivate)
    {
        if (m_aPageActivationHandler.IsSet())
            m_aPageActivationHandler.Call(NULL);
        return 0L;
    }


    IMPL_LINK_NOARG(OPropertyEditor, OnPageDeactivate)
    {
        // commit the data on the current (to-be-decativated) tab page
        // (79404)
        sal_Int32 nCurrentId = m_aTabControl->GetCurPageId();
        OBrowserPage* pCurrentPage = static_cast<OBrowserPage*>(m_aTabControl->GetTabPage((sal_uInt16)nCurrentId));
        if ( !pCurrentPage )
            return 1L;

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

        return 1L;
    }


} // namespace pcr



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