bump product version to 4.1.6.2
[LibreOffice.git] / extensions / source / propctrlr / propertyeditor.cxx
blob0440de348a58d6b0b3d2c84761b6748592b476e7
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>
26 //............................................................................
27 namespace pcr
29 //............................................................................
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;
40 //==================================================================
41 // class OPropertyEditor
42 //==================================================================
43 DBG_NAME(OPropertyEditor)
44 //------------------------------------------------------------------
45 OPropertyEditor::OPropertyEditor( Window* pParent, WinBits nWinStyle)
46 :Control(pParent, nWinStyle)
47 ,m_aTabControl( this )
48 ,m_nNextId(1)
49 ,m_bHasHelpSection( false )
50 ,m_nMinHelpLines( 0 )
51 ,m_nMaxHelpLines( 0 )
53 DBG_CTOR(OPropertyEditor,NULL);
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(sal_True);
62 //------------------------------------------------------------------
63 OPropertyEditor::~OPropertyEditor()
65 Hide();
66 ClearAll();
67 DBG_DTOR(OPropertyEditor,NULL);
70 //------------------------------------------------------------------
71 void OPropertyEditor::ClearAll()
73 m_nNextId=1;
74 sal_uInt16 nCount = m_aTabControl.GetPageCount();
75 for(long i = nCount-1; i >= 0; --i)
77 sal_uInt16 nID = m_aTabControl.GetPageId((sal_uInt16)i);
78 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl.GetTabPage(nID));
79 if (pPage)
81 pPage->EnableInput(sal_False);
82 m_aTabControl.RemovePage(nID);
83 delete pPage;
86 m_aTabControl.Clear();
89 MapStringToPageId aEmpty;
90 m_aPropertyPageIds.swap( aEmpty );
93 while ( !m_aHiddenPages.empty() )
95 delete m_aHiddenPages.begin()->second.pPage;
96 m_aHiddenPages.erase( m_aHiddenPages.begin() );
100 //------------------------------------------------------------------
101 sal_Int32 OPropertyEditor::getMinimumHeight()
103 sal_Int32 nMinHeight( LAYOUT_BORDER_TOP + LAYOUT_BORDER_BOTTOM );
105 if ( m_aTabControl.GetPageCount() > 0 )
107 sal_uInt16 nFirstID = m_aTabControl.GetPageId( 0 );
109 // reserve space for the tabs themself
110 Rectangle aTabArea( m_aTabControl.GetTabBounds( nFirstID ) );
111 nMinHeight += aTabArea.GetHeight();
113 // ask the page how much it requires
114 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( nFirstID ) );
115 if ( pPage )
116 nMinHeight += pPage->getMinimumHeight();
118 else
119 nMinHeight += 250; // arbitrary ...
121 return nMinHeight;
124 //------------------------------------------------------------------
125 sal_Int32 OPropertyEditor::getMinimumWidth()
127 sal_uInt16 nCount = m_aTabControl.GetPageCount();
128 sal_Int32 nPageMinWidth = 0;
129 for(long i = nCount-1; i >= 0; --i)
131 sal_uInt16 nID = m_aTabControl.GetPageId((sal_uInt16)i);
132 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl.GetTabPage(nID));
133 if (pPage)
135 sal_Int32 nCurPageMinWidth = pPage->getMinimumWidth();
136 if( nCurPageMinWidth > nPageMinWidth )
137 nPageMinWidth = nCurPageMinWidth;
140 return nPageMinWidth+6;
143 //------------------------------------------------------------------
144 void OPropertyEditor::CommitModified()
146 // commit all of my pages, if necessary
148 sal_uInt16 nCount = m_aTabControl.GetPageCount();
149 for ( sal_uInt16 i=0; i<nCount; ++i )
151 sal_uInt16 nID = m_aTabControl.GetPageId( i );
152 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( nID ) );
154 if ( pPage && pPage->getListBox().IsModified() )
155 pPage->getListBox().CommitModified();
159 //------------------------------------------------------------------
160 void OPropertyEditor::GetFocus()
162 m_aTabControl.GrabFocus();
165 //------------------------------------------------------------------
166 OBrowserPage* OPropertyEditor::getPage( const OUString& _rPropertyName )
168 OBrowserPage* pPage = NULL;
169 MapStringToPageId::const_iterator aPropertyPageIdPos = m_aPropertyPageIds.find( _rPropertyName );
170 if ( aPropertyPageIdPos != m_aPropertyPageIds.end() )
171 pPage = static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( aPropertyPageIdPos->second ) );
172 return pPage;
175 //------------------------------------------------------------------
176 const OBrowserPage* OPropertyEditor::getPage( const OUString& _rPropertyName ) const
178 return const_cast< OPropertyEditor* >( this )->getPage( _rPropertyName );
181 //------------------------------------------------------------------
182 OBrowserPage* OPropertyEditor::getPage( sal_uInt16& _rPageId )
184 return static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( _rPageId ) );
187 //------------------------------------------------------------------
188 const OBrowserPage* OPropertyEditor::getPage( sal_uInt16& _rPageId ) const
190 return const_cast< OPropertyEditor* >( this )->getPage( _rPageId );
193 //------------------------------------------------------------------
194 void OPropertyEditor::Resize()
196 Rectangle aPlayground(
197 Point( LAYOUT_BORDER_LEFT, LAYOUT_BORDER_TOP ),
198 Size(
199 GetOutputSizePixel().Width() - LAYOUT_BORDER_LEFT - LAYOUT_BORDER_RIGHT,
200 GetOutputSizePixel().Height() - LAYOUT_BORDER_TOP - LAYOUT_BORDER_BOTTOM
204 Rectangle aTabArea( aPlayground );
205 m_aTabControl.SetPosSizePixel( aTabArea.TopLeft(), aTabArea.GetSize() );
208 //------------------------------------------------------------------
209 sal_uInt16 OPropertyEditor::AppendPage( const String & _rText, const OString& _rHelpId )
211 // obtain a new id
212 sal_uInt16 nId = m_nNextId++;
213 // insert the id
214 m_aTabControl.InsertPage(nId, _rText);
216 // create a new page
217 OBrowserPage* pPage = new OBrowserPage(&m_aTabControl);
218 pPage->SetText( _rText );
219 // some knittings
220 pPage->SetSizePixel(m_aTabControl.GetTabPageSizePixel());
221 pPage->getListBox().SetListener(m_pListener);
222 pPage->getListBox().SetObserver(m_pObserver);
223 pPage->getListBox().EnableHelpSection( m_bHasHelpSection );
224 pPage->getListBox().SetHelpLineLimites( m_nMinHelpLines, m_nMaxHelpLines );
225 pPage->SetHelpId( _rHelpId );
227 // immediately activate the page
228 m_aTabControl.SetTabPage(nId, pPage);
229 m_aTabControl.SetCurPageId(nId);
231 return nId;
234 //------------------------------------------------------------------
235 void OPropertyEditor::SetHelpId( const OString& rHelpId )
237 Control::SetHelpId("");
238 m_aTabControl.SetHelpId(rHelpId);
241 //------------------------------------------------------------------
242 void OPropertyEditor::RemovePage(sal_uInt16 nID)
244 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl.GetTabPage(nID));
246 if (pPage)
247 pPage->EnableInput(sal_False);
248 m_aTabControl.RemovePage(nID);
249 if (pPage)
250 delete pPage;
253 //------------------------------------------------------------------
254 void OPropertyEditor::SetPage(sal_uInt16 nId)
256 m_aTabControl.SetCurPageId(nId);
259 //------------------------------------------------------------------
260 sal_uInt16 OPropertyEditor::GetCurPage()
262 if(m_aTabControl.GetPageCount()>0)
263 return m_aTabControl.GetCurPageId();
264 else
265 return 0;
268 //------------------------------------------------------------------
269 void OPropertyEditor::Update(const ::std::mem_fun_t<void,OBrowserListBox>& _aUpdateFunction)
271 // forward this to all our pages
272 sal_uInt16 nCount = m_aTabControl.GetPageCount();
273 for (sal_uInt16 i=0;i<nCount;++i)
275 sal_uInt16 nID = m_aTabControl.GetPageId(i);
276 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl.GetTabPage(nID));
277 if (pPage)
278 _aUpdateFunction(&pPage->getListBox());
281 //------------------------------------------------------------------
282 void OPropertyEditor::EnableUpdate()
284 Update(::std::mem_fun(&OBrowserListBox::EnableUpdate));
286 //------------------------------------------------------------------
287 void OPropertyEditor::DisableUpdate()
289 Update(::std::mem_fun(&OBrowserListBox::DisableUpdate));
292 //------------------------------------------------------------------
293 void OPropertyEditor::forEachPage( PageOperation _pOperation, const void* _pArgument )
295 sal_uInt16 nCount = m_aTabControl.GetPageCount();
296 for ( sal_uInt16 i=0; i<nCount; ++i )
298 sal_uInt16 nID = m_aTabControl.GetPageId(i);
299 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( nID ) );
300 if ( !pPage )
301 continue;
302 (this->*_pOperation)( *pPage, _pArgument );
306 //------------------------------------------------------------------
307 void OPropertyEditor::setPageLineListener( OBrowserPage& _rPage, const void* )
309 _rPage.getListBox().SetListener( m_pListener );
312 //------------------------------------------------------------------
313 void OPropertyEditor::SetLineListener(IPropertyLineListener* _pListener)
315 m_pListener = _pListener;
316 forEachPage( &OPropertyEditor::setPageLineListener );
319 //------------------------------------------------------------------
320 void OPropertyEditor::setPageControlObserver( OBrowserPage& _rPage, const void* )
322 _rPage.getListBox().SetObserver( m_pObserver );
325 //------------------------------------------------------------------
326 void OPropertyEditor::SetControlObserver( IPropertyControlObserver* _pObserver )
328 m_pObserver = _pObserver;
329 forEachPage( &OPropertyEditor::setPageControlObserver );
332 //------------------------------------------------------------------
333 void OPropertyEditor::EnableHelpSection( bool _bEnable )
335 m_bHasHelpSection = _bEnable;
336 forEachPage( &OPropertyEditor::enableHelpSection );
339 //------------------------------------------------------------------
340 bool OPropertyEditor::HasHelpSection() const
342 return m_bHasHelpSection;
345 //------------------------------------------------------------------
346 void OPropertyEditor::SetHelpText( const OUString& _rHelpText )
348 forEachPage( &OPropertyEditor::setHelpSectionText, &_rHelpText );
351 //------------------------------------------------------------------
352 void OPropertyEditor::SetHelpLineLimites( sal_Int32 _nMinLines, sal_Int32 _nMaxLines )
354 m_nMinHelpLines = _nMinLines;
355 m_nMaxHelpLines = _nMaxLines;
356 forEachPage( &OPropertyEditor::setHelpLineLimits );
359 //------------------------------------------------------------------
360 void OPropertyEditor::enableHelpSection( OBrowserPage& _rPage, const void* )
362 _rPage.getListBox().EnableHelpSection( m_bHasHelpSection );
365 //------------------------------------------------------------------
366 void OPropertyEditor::setHelpSectionText( OBrowserPage& _rPage, const void* _pPointerToOUString )
368 OSL_ENSURE( _pPointerToOUString, "OPropertyEditor::setHelpSectionText: invalid argument!" );
369 if ( !_pPointerToOUString )
370 return;
372 const OUString& rText( *(const OUString*)_pPointerToOUString );
373 _rPage.getListBox().SetHelpText( rText );
376 //------------------------------------------------------------------
377 void OPropertyEditor::setHelpLineLimits( OBrowserPage& _rPage, const void* )
379 _rPage.getListBox().SetHelpLineLimites( m_nMinHelpLines, m_nMaxHelpLines );
382 //------------------------------------------------------------------
383 sal_uInt16 OPropertyEditor::InsertEntry( const OLineDescriptor& rData, sal_uInt16 _nPageId, sal_uInt16 nPos )
385 // let the current page handle this
386 OBrowserPage* pPage = getPage( _nPageId );
387 DBG_ASSERT( pPage, "OPropertyEditor::InsertEntry: don't have such a page!" );
388 if ( !pPage )
389 return LISTBOX_ENTRY_NOTFOUND;
391 sal_uInt16 nEntry = pPage->getListBox().InsertEntry( rData, nPos );
393 OSL_ENSURE( m_aPropertyPageIds.find( rData.sName ) == m_aPropertyPageIds.end(),
394 "OPropertyEditor::InsertEntry: property already present in the map!" );
395 m_aPropertyPageIds.insert( MapStringToPageId::value_type( rData.sName, _nPageId ) );
397 return nEntry;
400 //------------------------------------------------------------------
401 void OPropertyEditor::RemoveEntry( const OUString& _rName )
403 OBrowserPage* pPage = getPage( _rName );
404 if ( pPage )
406 OSL_VERIFY( pPage->getListBox().RemoveEntry( _rName ) );
408 OSL_ENSURE( m_aPropertyPageIds.find( _rName ) != m_aPropertyPageIds.end(),
409 "OPropertyEditor::RemoveEntry: property not present in the map!" );
410 m_aPropertyPageIds.erase( _rName );
414 //------------------------------------------------------------------
415 void OPropertyEditor::ChangeEntry( const OLineDescriptor& rData )
417 OBrowserPage* pPage = getPage( rData.sName );
418 if ( pPage )
419 pPage->getListBox().ChangeEntry( rData, EDITOR_LIST_REPLACE_EXISTING );
422 //------------------------------------------------------------------
423 void OPropertyEditor::SetPropertyValue( const OUString& rEntryName, const Any& _rValue, bool _bUnknownValue )
425 OBrowserPage* pPage = getPage( rEntryName );
426 if ( pPage )
427 pPage->getListBox().SetPropertyValue( rEntryName, _rValue, _bUnknownValue );
430 //------------------------------------------------------------------
431 sal_uInt16 OPropertyEditor::GetPropertyPos( const OUString& rEntryName ) const
433 sal_uInt16 nVal=LISTBOX_ENTRY_NOTFOUND;
434 const OBrowserPage* pPage = getPage( rEntryName );
435 if ( pPage )
436 nVal = pPage->getListBox().GetPropertyPos( rEntryName );
437 return nVal;
440 //------------------------------------------------------------------
441 void OPropertyEditor::ShowPropertyPage( sal_uInt16 _nPageId, bool _bShow )
443 if ( !_bShow )
445 sal_uInt16 nPagePos = m_aTabControl.GetPagePos( _nPageId );
446 if ( TAB_PAGE_NOTFOUND == nPagePos )
447 return;
448 DBG_ASSERT( m_aHiddenPages.find( _nPageId ) == m_aHiddenPages.end(), "OPropertyEditor::ShowPropertyPage: page already hidden!" );
450 m_aHiddenPages[ _nPageId ] = HiddenPage( nPagePos, m_aTabControl.GetTabPage( _nPageId ) );
451 m_aTabControl.RemovePage( _nPageId );
453 else
455 ::std::map< sal_uInt16, HiddenPage >::iterator aPagePos = m_aHiddenPages.find( _nPageId );
456 if ( aPagePos == m_aHiddenPages.end() )
457 return;
459 aPagePos->second.pPage->SetSizePixel( m_aTabControl.GetTabPageSizePixel() );
460 m_aTabControl.InsertPage( aPagePos->first, aPagePos->second.pPage->GetText(), aPagePos->second.nPos );
461 m_aTabControl.SetTabPage( aPagePos->first, aPagePos->second.pPage );
463 m_aHiddenPages.erase( aPagePos );
467 //------------------------------------------------------------------
468 void OPropertyEditor::EnablePropertyControls( const OUString& _rEntryName, sal_Int16 _nControls, bool _bEnable )
470 for ( sal_uInt16 i = 0; i < m_aTabControl.GetPageCount(); ++i )
472 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( m_aTabControl.GetPageId( i ) ) );
473 if ( pPage )
474 pPage->getListBox().EnablePropertyControls( _rEntryName, _nControls, _bEnable );
478 //------------------------------------------------------------------
479 void OPropertyEditor::EnablePropertyLine( const OUString& _rEntryName, bool _bEnable )
481 for ( sal_uInt16 i = 0; i < m_aTabControl.GetPageCount(); ++i )
483 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( m_aTabControl.GetPageId( i ) ) );
484 if ( pPage )
485 pPage->getListBox().EnablePropertyLine( _rEntryName, _bEnable );
489 //------------------------------------------------------------------
490 Reference< XPropertyControl > OPropertyEditor::GetPropertyControl(const OUString& rEntryName)
492 Reference< XPropertyControl > xControl;
493 // let the current page handle this
494 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl.GetTabPage(m_aTabControl.GetCurPageId()));
495 if (pPage)
496 xControl = pPage->getListBox().GetPropertyControl(rEntryName);
497 return xControl;
500 //------------------------------------------------------------------
501 IMPL_LINK_NOARG(OPropertyEditor, OnPageActivate)
503 if (m_aPageActivationHandler.IsSet())
504 m_aPageActivationHandler.Call(NULL);
505 return 0L;
508 //------------------------------------------------------------------
509 IMPL_LINK_NOARG(OPropertyEditor, OnPageDeactivate)
511 // commit the data on the current (to-be-decativated) tab page
512 // (79404)
513 sal_Int32 nCurrentId = m_aTabControl.GetCurPageId();
514 OBrowserPage* pCurrentPage = static_cast<OBrowserPage*>(m_aTabControl.GetTabPage((sal_uInt16)nCurrentId));
515 if ( !pCurrentPage )
516 return 1L;
518 if ( pCurrentPage->getListBox().IsModified() )
519 pCurrentPage->getListBox().CommitModified();
521 return 1L;
524 //............................................................................
525 } // namespace pcr
526 //............................................................................
529 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */