Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / extensions / source / propctrlr / propertyeditor.cxx
blob714e640cc0ecb18869ff282d1ea708b99980a6f3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "propertyeditor.hxx"
21 #include "browserpage.hxx"
22 #include "linedescriptor.hxx"
24 #include <tools/debug.hxx>
27 namespace pcr
31 #define LAYOUT_BORDER_LEFT 3
32 #define LAYOUT_BORDER_TOP 3
33 #define LAYOUT_BORDER_RIGHT 3
34 #define LAYOUT_BORDER_BOTTOM 3
36 using ::com::sun::star::uno::Any;
37 using ::com::sun::star::inspection::XPropertyControl;
38 using ::com::sun::star::uno::Reference;
41 // class OPropertyEditor
44 OPropertyEditor::OPropertyEditor( vcl::Window* pParent)
45 :Control(pParent, WB_DIALOGCONTROL)
46 ,m_aTabControl( VclPtr<TabControl>::Create(this) )
47 ,m_pListener(nullptr)
48 ,m_pObserver(nullptr)
49 ,m_nNextId(1)
50 ,m_bHasHelpSection( false )
51 ,m_nMinHelpLines( 0 )
52 ,m_nMaxHelpLines( 0 )
55 m_aTabControl->Show();
56 m_aTabControl->SetDeactivatePageHdl(LINK(this, OPropertyEditor, OnPageDeactivate));
57 m_aTabControl->SetActivatePageHdl(LINK(this, OPropertyEditor, OnPageActivate));
58 m_aTabControl->SetBackground(GetBackground());
59 m_aTabControl->SetPaintTransparent(true);
63 OPropertyEditor::~OPropertyEditor()
65 disposeOnce();
68 void OPropertyEditor::dispose()
70 Hide();
71 ClearAll();
72 m_aTabControl.disposeAndClear();
73 Control::dispose();
77 void OPropertyEditor::ClearAll()
79 m_nNextId=1;
80 sal_uInt16 nCount = m_aTabControl->GetPageCount();
81 for(long i = nCount-1; i >= 0; --i)
83 sal_uInt16 nID = m_aTabControl->GetPageId((sal_uInt16)i);
84 VclPtr<OBrowserPage> pPage = static_cast<OBrowserPage*>(m_aTabControl->GetTabPage(nID));
85 if (pPage)
87 pPage->EnableInput(false);
88 m_aTabControl->RemovePage(nID);
89 pPage.disposeAndClear();
92 m_aTabControl->Clear();
95 MapStringToPageId aEmpty;
96 m_aPropertyPageIds.swap( aEmpty );
99 while ( !m_aHiddenPages.empty() )
101 m_aHiddenPages.begin()->second.pPage.disposeAndClear();
102 m_aHiddenPages.erase( m_aHiddenPages.begin() );
104 m_aHiddenPages.clear();
108 sal_Int32 OPropertyEditor::getMinimumHeight()
110 sal_Int32 nMinHeight( LAYOUT_BORDER_TOP + LAYOUT_BORDER_BOTTOM );
112 if ( m_aTabControl->GetPageCount() > 0 )
114 sal_uInt16 nFirstID = m_aTabControl->GetPageId( 0 );
116 // reserve space for the tabs themself
117 tools::Rectangle aTabArea( m_aTabControl->GetTabBounds( nFirstID ) );
118 nMinHeight += aTabArea.GetHeight();
120 // ask the page how much it requires
121 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( nFirstID ) );
122 if ( pPage )
123 nMinHeight += pPage->getMinimumHeight();
125 else
126 nMinHeight += 250; // arbitrary ...
128 return nMinHeight;
132 sal_Int32 OPropertyEditor::getMinimumWidth()
134 sal_uInt16 nCount = m_aTabControl->GetPageCount();
135 sal_Int32 nPageMinWidth = 0;
136 for(long i = nCount-1; i >= 0; --i)
138 sal_uInt16 nID = m_aTabControl->GetPageId((sal_uInt16)i);
139 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl->GetTabPage(nID));
140 if (pPage)
142 sal_Int32 nCurPageMinWidth = pPage->getMinimumWidth();
143 if( nCurPageMinWidth > nPageMinWidth )
144 nPageMinWidth = nCurPageMinWidth;
147 return nPageMinWidth+6;
151 void OPropertyEditor::CommitModified()
153 // commit all of my pages, if necessary
155 sal_uInt16 nCount = m_aTabControl->GetPageCount();
156 for ( sal_uInt16 i=0; i<nCount; ++i )
158 sal_uInt16 nID = m_aTabControl->GetPageId( i );
159 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( nID ) );
161 if ( pPage && pPage->getListBox().IsModified() )
162 pPage->getListBox().CommitModified();
167 void OPropertyEditor::GetFocus()
169 if ( m_aTabControl )
170 m_aTabControl->GrabFocus();
174 OBrowserPage* OPropertyEditor::getPage( const OUString& _rPropertyName )
176 OBrowserPage* pPage = nullptr;
177 MapStringToPageId::const_iterator aPropertyPageIdPos = m_aPropertyPageIds.find( _rPropertyName );
178 if ( aPropertyPageIdPos != m_aPropertyPageIds.end() )
179 pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( aPropertyPageIdPos->second ) );
180 return pPage;
184 const OBrowserPage* OPropertyEditor::getPage( const OUString& _rPropertyName ) const
186 return const_cast< OPropertyEditor* >( this )->getPage( _rPropertyName );
190 OBrowserPage* OPropertyEditor::getPage( sal_uInt16& _rPageId )
192 return static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( _rPageId ) );
196 const OBrowserPage* OPropertyEditor::getPage( sal_uInt16& _rPageId ) const
198 return const_cast< OPropertyEditor* >( this )->getPage( _rPageId );
202 void OPropertyEditor::Resize()
204 tools::Rectangle aPlayground(
205 Point( LAYOUT_BORDER_LEFT, LAYOUT_BORDER_TOP ),
206 Size(
207 GetOutputSizePixel().Width() - LAYOUT_BORDER_LEFT - LAYOUT_BORDER_RIGHT,
208 GetOutputSizePixel().Height() - LAYOUT_BORDER_TOP - LAYOUT_BORDER_BOTTOM
212 tools::Rectangle aTabArea( aPlayground );
213 m_aTabControl->SetPosSizePixel( aTabArea.TopLeft(), aTabArea.GetSize() );
217 sal_uInt16 OPropertyEditor::AppendPage( const OUString & _rText, const OString& _rHelpId )
219 // obtain a new id
220 sal_uInt16 nId = m_nNextId++;
221 // insert the id
222 m_aTabControl->InsertPage(nId, _rText);
224 // create a new page
225 VclPtrInstance<OBrowserPage> pPage(m_aTabControl.get());
226 pPage->SetText( _rText );
227 // some knittings
228 pPage->SetSizePixel(m_aTabControl->GetTabPageSizePixel());
229 pPage->getListBox().SetListener(m_pListener);
230 pPage->getListBox().SetObserver(m_pObserver);
231 pPage->getListBox().EnableHelpSection( m_bHasHelpSection );
232 pPage->getListBox().SetHelpLineLimites( m_nMinHelpLines, m_nMaxHelpLines );
233 pPage->SetHelpId( _rHelpId );
235 // immediately activate the page
236 m_aTabControl->SetTabPage(nId, pPage);
237 m_aTabControl->SetCurPageId(nId);
239 return nId;
243 void OPropertyEditor::SetHelpId( const OString& rHelpId )
245 Control::SetHelpId("");
246 m_aTabControl->SetHelpId(rHelpId);
250 void OPropertyEditor::RemovePage(sal_uInt16 nID)
252 VclPtr<OBrowserPage> pPage = static_cast<OBrowserPage*>(m_aTabControl->GetTabPage(nID));
254 if (pPage)
255 pPage->EnableInput(false);
256 m_aTabControl->RemovePage(nID);
257 pPage.disposeAndClear();
261 void OPropertyEditor::SetPage(sal_uInt16 nId)
263 m_aTabControl->SetCurPageId(nId);
267 sal_uInt16 OPropertyEditor::GetCurPage()
269 if(m_aTabControl->GetPageCount()>0)
270 return m_aTabControl->GetCurPageId();
271 else
272 return 0;
276 void OPropertyEditor::Update(const std::mem_fun_t<void,OBrowserListBox>& _aUpdateFunction)
278 // forward this to all our pages
279 sal_uInt16 nCount = m_aTabControl->GetPageCount();
280 for (sal_uInt16 i=0;i<nCount;++i)
282 sal_uInt16 nID = m_aTabControl->GetPageId(i);
283 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl->GetTabPage(nID));
284 if (pPage)
285 _aUpdateFunction(&pPage->getListBox());
289 void OPropertyEditor::EnableUpdate()
291 Update(std::mem_fun(&OBrowserListBox::EnableUpdate));
294 void OPropertyEditor::DisableUpdate()
296 Update(std::mem_fun(&OBrowserListBox::DisableUpdate));
300 void OPropertyEditor::forEachPage( PageOperation _pOperation )
302 sal_uInt16 nCount = m_aTabControl->GetPageCount();
303 for ( sal_uInt16 i=0; i<nCount; ++i )
305 sal_uInt16 nID = m_aTabControl->GetPageId(i);
306 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( nID ) );
307 if ( !pPage )
308 continue;
309 (this->*_pOperation)( *pPage, nullptr );
314 void OPropertyEditor::setPageLineListener( OBrowserPage& _rPage, const void* )
316 _rPage.getListBox().SetListener( m_pListener );
320 void OPropertyEditor::SetLineListener(IPropertyLineListener* _pListener)
322 m_pListener = _pListener;
323 forEachPage( &OPropertyEditor::setPageLineListener );
327 void OPropertyEditor::setPageControlObserver( OBrowserPage& _rPage, const void* )
329 _rPage.getListBox().SetObserver( m_pObserver );
333 void OPropertyEditor::SetControlObserver( IPropertyControlObserver* _pObserver )
335 m_pObserver = _pObserver;
336 forEachPage( &OPropertyEditor::setPageControlObserver );
340 void OPropertyEditor::EnableHelpSection( bool _bEnable )
342 m_bHasHelpSection = _bEnable;
343 forEachPage( &OPropertyEditor::enableHelpSection );
347 void OPropertyEditor::SetHelpText( const OUString& _rHelpText )
349 sal_uInt16 nCount = m_aTabControl->GetPageCount();
350 for ( sal_uInt16 i=0; i<nCount; ++i )
352 sal_uInt16 nID = m_aTabControl->GetPageId(i);
353 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( nID ) );
354 if ( !pPage )
355 continue;
356 setHelpSectionText( *pPage, &_rHelpText );
361 void OPropertyEditor::SetHelpLineLimites( sal_Int32 _nMinLines, sal_Int32 _nMaxLines )
363 m_nMinHelpLines = _nMinLines;
364 m_nMaxHelpLines = _nMaxLines;
365 forEachPage( &OPropertyEditor::setHelpLineLimits );
369 void OPropertyEditor::enableHelpSection( OBrowserPage& _rPage, const void* )
371 _rPage.getListBox().EnableHelpSection( m_bHasHelpSection );
375 void OPropertyEditor::setHelpSectionText( OBrowserPage& _rPage, const void* _pPointerToOUString )
377 OSL_ENSURE( _pPointerToOUString, "OPropertyEditor::setHelpSectionText: invalid argument!" );
378 if ( !_pPointerToOUString )
379 return;
381 const OUString& rText( *static_cast<const OUString*>(_pPointerToOUString) );
382 _rPage.getListBox().SetHelpText( rText );
386 void OPropertyEditor::setHelpLineLimits( OBrowserPage& _rPage, const void* )
388 _rPage.getListBox().SetHelpLineLimites( m_nMinHelpLines, m_nMaxHelpLines );
392 void OPropertyEditor::InsertEntry( const OLineDescriptor& rData, sal_uInt16 _nPageId, sal_uInt16 nPos )
394 // let the current page handle this
395 OBrowserPage* pPage = getPage( _nPageId );
396 DBG_ASSERT( pPage, "OPropertyEditor::InsertEntry: don't have such a page!" );
397 if ( !pPage )
398 return;
400 pPage->getListBox().InsertEntry( rData, nPos );
402 OSL_ENSURE( m_aPropertyPageIds.find( rData.sName ) == m_aPropertyPageIds.end(),
403 "OPropertyEditor::InsertEntry: property already present in the map!" );
404 m_aPropertyPageIds.insert( MapStringToPageId::value_type( rData.sName, _nPageId ) );
408 void OPropertyEditor::RemoveEntry( const OUString& _rName )
410 OBrowserPage* pPage = getPage( _rName );
411 if ( pPage )
413 OSL_VERIFY( pPage->getListBox().RemoveEntry( _rName ) );
415 OSL_ENSURE( m_aPropertyPageIds.find( _rName ) != m_aPropertyPageIds.end(),
416 "OPropertyEditor::RemoveEntry: property not present in the map!" );
417 m_aPropertyPageIds.erase( _rName );
422 void OPropertyEditor::ChangeEntry( const OLineDescriptor& rData )
424 OBrowserPage* pPage = getPage( rData.sName );
425 if ( pPage )
426 pPage->getListBox().ChangeEntry( rData, EDITOR_LIST_REPLACE_EXISTING );
430 void OPropertyEditor::SetPropertyValue( const OUString& rEntryName, const Any& _rValue, bool _bUnknownValue )
432 OBrowserPage* pPage = getPage( rEntryName );
433 if ( pPage )
434 pPage->getListBox().SetPropertyValue( rEntryName, _rValue, _bUnknownValue );
438 sal_uInt16 OPropertyEditor::GetPropertyPos( const OUString& rEntryName ) const
440 sal_uInt16 nVal=EDITOR_LIST_ENTRY_NOTFOUND;
441 const OBrowserPage* pPage = getPage( rEntryName );
442 if ( pPage )
443 nVal = pPage->getListBox().GetPropertyPos( rEntryName );
444 return nVal;
448 void OPropertyEditor::ShowPropertyPage( sal_uInt16 _nPageId, bool _bShow )
450 if ( !_bShow )
452 sal_uInt16 nPagePos = m_aTabControl->GetPagePos( _nPageId );
453 if ( TAB_PAGE_NOTFOUND == nPagePos )
454 return;
455 DBG_ASSERT( m_aHiddenPages.find( _nPageId ) == m_aHiddenPages.end(), "OPropertyEditor::ShowPropertyPage: page already hidden!" );
457 m_aHiddenPages[ _nPageId ] = HiddenPage( nPagePos, m_aTabControl->GetTabPage( _nPageId ) );
458 m_aTabControl->RemovePage( _nPageId );
460 else
462 std::map< sal_uInt16, HiddenPage >::iterator aPagePos = m_aHiddenPages.find( _nPageId );
463 if ( aPagePos == m_aHiddenPages.end() )
464 return;
466 aPagePos->second.pPage->SetSizePixel( m_aTabControl->GetTabPageSizePixel() );
467 m_aTabControl->InsertPage( aPagePos->first, aPagePos->second.pPage->GetText(), aPagePos->second.nPos );
468 m_aTabControl->SetTabPage( aPagePos->first, aPagePos->second.pPage );
470 m_aHiddenPages.erase( aPagePos );
475 void OPropertyEditor::EnablePropertyControls( const OUString& _rEntryName, sal_Int16 _nControls, bool _bEnable )
477 for ( sal_uInt16 i = 0; i < m_aTabControl->GetPageCount(); ++i )
479 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( m_aTabControl->GetPageId( i ) ) );
480 if ( pPage )
481 pPage->getListBox().EnablePropertyControls( _rEntryName, _nControls, _bEnable );
486 void OPropertyEditor::EnablePropertyLine( const OUString& _rEntryName, bool _bEnable )
488 for ( sal_uInt16 i = 0; i < m_aTabControl->GetPageCount(); ++i )
490 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl->GetTabPage( m_aTabControl->GetPageId( i ) ) );
491 if ( pPage )
492 pPage->getListBox().EnablePropertyLine( _rEntryName, _bEnable );
497 Reference< XPropertyControl > OPropertyEditor::GetPropertyControl(const OUString& rEntryName)
499 Reference< XPropertyControl > xControl;
500 // let the current page handle this
501 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl->GetTabPage(m_aTabControl->GetCurPageId()));
502 if (pPage)
503 xControl = pPage->getListBox().GetPropertyControl(rEntryName);
504 return xControl;
508 IMPL_LINK_NOARG(OPropertyEditor, OnPageActivate, TabControl*, void)
510 m_aPageActivationHandler.Call(nullptr);
514 IMPL_LINK_NOARG(OPropertyEditor, OnPageDeactivate, TabControl *, bool)
516 // commit the data on the current (to-be-deactivated) tab page
517 // (79404)
518 sal_Int32 nCurrentId = m_aTabControl->GetCurPageId();
519 OBrowserPage* pCurrentPage = static_cast<OBrowserPage*>(m_aTabControl->GetTabPage((sal_uInt16)nCurrentId));
520 if ( !pCurrentPage )
521 return true;
523 if ( pCurrentPage->getListBox().IsModified() )
524 pCurrentPage->getListBox().CommitModified();
526 return true;
530 } // namespace pcr
533 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */