merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / propctrlr / propertyeditor.cxx
blob2357edcfbd4a9b62db27e4b0c0081fb9d4cff526
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: propertyeditor.cxx,v $
10 * $Revision: 1.22 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "propertyeditor.hxx"
34 #include "browserpage.hxx"
35 #include "linedescriptor.hxx"
37 /** === begin UNO includes === **/
38 /** === end UNO includes === **/
39 #include <tools/debug.hxx>
41 //............................................................................
42 namespace pcr
44 //............................................................................
46 #define LAYOUT_BORDER_LEFT 3
47 #define LAYOUT_BORDER_TOP 3
48 #define LAYOUT_BORDER_RIGHT 3
49 #define LAYOUT_BORDER_BOTTOM 3
51 /** === begin UNO using === **/
52 using ::com::sun::star::uno::Any;
53 using ::com::sun::star::inspection::XPropertyControl;
54 using ::com::sun::star::uno::Reference;
55 /** === end UNO using === **/
57 //==================================================================
58 // class OPropertyEditor
59 //==================================================================
60 DBG_NAME(OPropertyEditor)
61 //------------------------------------------------------------------
62 OPropertyEditor::OPropertyEditor( Window* pParent, WinBits nWinStyle)
63 :Control(pParent, nWinStyle)
64 ,m_aTabControl( this )
65 ,m_nNextId(1)
66 ,m_bHasHelpSection( false )
67 ,m_nMinHelpLines( 0 )
68 ,m_nMaxHelpLines( 0 )
70 DBG_CTOR(OPropertyEditor,NULL);
72 m_aTabControl.Show();
73 m_aTabControl.SetDeactivatePageHdl(LINK(this, OPropertyEditor, OnPageDeactivate));
74 m_aTabControl.SetActivatePageHdl(LINK(this, OPropertyEditor, OnPageActivate));
75 m_aTabControl.SetBackground(GetBackground());
76 m_aTabControl.SetPaintTransparent(sal_True);
79 //------------------------------------------------------------------
80 OPropertyEditor::~OPropertyEditor()
82 Hide();
83 ClearAll();
84 DBG_DTOR(OPropertyEditor,NULL);
87 //------------------------------------------------------------------
88 void OPropertyEditor::ClearAll()
90 m_nNextId=1;
91 sal_uInt16 nCount = m_aTabControl.GetPageCount();
92 for(long i = nCount-1; i >= 0; --i)
94 sal_uInt16 nID = m_aTabControl.GetPageId((sal_uInt16)i);
95 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl.GetTabPage(nID));
96 if (pPage)
98 pPage->EnableInput(sal_False);
99 m_aTabControl.RemovePage(nID);
100 delete pPage;
103 m_aTabControl.Clear();
106 MapStringToPageId aEmpty;
107 m_aPropertyPageIds.swap( aEmpty );
110 while ( !m_aHiddenPages.empty() )
112 delete m_aHiddenPages.begin()->second.pPage;
113 m_aHiddenPages.erase( m_aHiddenPages.begin() );
117 //------------------------------------------------------------------
118 sal_Int32 OPropertyEditor::getMinimumHeight()
120 sal_Int32 nMinHeight( LAYOUT_BORDER_TOP + LAYOUT_BORDER_BOTTOM );
122 if ( m_aTabControl.GetPageCount() > 0 )
124 sal_uInt16 nFirstID = m_aTabControl.GetPageId( 0 );
126 // reserve space for the tabs themself
127 Rectangle aTabArea( m_aTabControl.GetTabBounds( nFirstID ) );
128 nMinHeight += aTabArea.GetHeight();
130 // ask the page how much it requires
131 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( nFirstID ) );
132 if ( pPage )
133 nMinHeight += pPage->getMinimumHeight();
135 else
136 nMinHeight += 250; // arbitrary ...
138 return nMinHeight;
141 //------------------------------------------------------------------
142 sal_Int32 OPropertyEditor::getMinimumWidth()
144 sal_uInt16 nCount = m_aTabControl.GetPageCount();
145 sal_Int32 nPageMinWidth = 0;
146 for(long i = nCount-1; i >= 0; --i)
148 sal_uInt16 nID = m_aTabControl.GetPageId((sal_uInt16)i);
149 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl.GetTabPage(nID));
150 if (pPage)
152 sal_Int32 nCurPageMinWidth = pPage->getMinimumWidth();
153 if( nCurPageMinWidth > nPageMinWidth )
154 nPageMinWidth = nCurPageMinWidth;
157 return nPageMinWidth+6;
160 //------------------------------------------------------------------
161 void OPropertyEditor::CommitModified()
163 // commit all of my pages, if necessary
165 sal_uInt16 nCount = m_aTabControl.GetPageCount();
166 for ( sal_uInt16 i=0; i<nCount; ++i )
168 sal_uInt16 nID = m_aTabControl.GetPageId( i );
169 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( nID ) );
171 if ( pPage && pPage->getListBox().IsModified() )
172 pPage->getListBox().CommitModified();
176 //------------------------------------------------------------------
177 void OPropertyEditor::GetFocus()
179 m_aTabControl.GrabFocus();
182 //------------------------------------------------------------------
183 OBrowserPage* OPropertyEditor::getPage( const ::rtl::OUString& _rPropertyName )
185 OBrowserPage* pPage = NULL;
186 MapStringToPageId::const_iterator aPropertyPageIdPos = m_aPropertyPageIds.find( _rPropertyName );
187 if ( aPropertyPageIdPos != m_aPropertyPageIds.end() )
188 pPage = static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( aPropertyPageIdPos->second ) );
189 return pPage;
192 //------------------------------------------------------------------
193 const OBrowserPage* OPropertyEditor::getPage( const ::rtl::OUString& _rPropertyName ) const
195 return const_cast< OPropertyEditor* >( this )->getPage( _rPropertyName );
198 //------------------------------------------------------------------
199 OBrowserPage* OPropertyEditor::getPage( sal_uInt16& _rPageId )
201 return static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( _rPageId ) );
204 //------------------------------------------------------------------
205 const OBrowserPage* OPropertyEditor::getPage( sal_uInt16& _rPageId ) const
207 return const_cast< OPropertyEditor* >( this )->getPage( _rPageId );
210 //------------------------------------------------------------------
211 void OPropertyEditor::Resize()
213 Rectangle aPlayground(
214 Point( LAYOUT_BORDER_LEFT, LAYOUT_BORDER_TOP ),
215 Size(
216 GetOutputSizePixel().Width() - LAYOUT_BORDER_LEFT - LAYOUT_BORDER_RIGHT,
217 GetOutputSizePixel().Height() - LAYOUT_BORDER_TOP - LAYOUT_BORDER_BOTTOM
221 Rectangle aTabArea( aPlayground );
222 m_aTabControl.SetPosSizePixel( aTabArea.TopLeft(), aTabArea.GetSize() );
225 //------------------------------------------------------------------
226 sal_uInt16 OPropertyEditor::AppendPage( const String & _rText, const SmartId& _rHelpId )
228 // obtain a new id
229 sal_uInt16 nId = m_nNextId++;
230 // insert the id
231 m_aTabControl.InsertPage(nId, _rText);
233 // create a new page
234 OBrowserPage* pPage = new OBrowserPage(&m_aTabControl);
235 pPage->SetText( _rText );
236 // some knittings
237 pPage->SetSizePixel(m_aTabControl.GetTabPageSizePixel());
238 pPage->getListBox().SetListener(m_pListener);
239 pPage->getListBox().SetObserver(m_pObserver);
240 pPage->getListBox().EnableHelpSection( m_bHasHelpSection );
241 pPage->getListBox().SetHelpLineLimites( m_nMinHelpLines, m_nMaxHelpLines );
242 pPage->SetSmartHelpId( _rHelpId );
244 // immediately activate the page
245 m_aTabControl.SetTabPage(nId, pPage);
246 m_aTabControl.SetCurPageId(nId);
248 return nId;
251 //------------------------------------------------------------------
252 void OPropertyEditor::SetHelpId( sal_uInt32 nHelpId )
254 Control::SetHelpId(0);
255 m_aTabControl.SetHelpId(nHelpId);
258 //------------------------------------------------------------------
259 void OPropertyEditor::RemovePage(sal_uInt16 nID)
261 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl.GetTabPage(nID));
263 if (pPage)
264 pPage->EnableInput(sal_False);
265 m_aTabControl.RemovePage(nID);
266 if (pPage)
267 delete pPage;
270 //------------------------------------------------------------------
271 void OPropertyEditor::SetPage(sal_uInt16 nId)
273 m_aTabControl.SetCurPageId(nId);
276 //------------------------------------------------------------------
277 sal_uInt16 OPropertyEditor::GetCurPage()
279 if(m_aTabControl.GetPageCount()>0)
280 return m_aTabControl.GetCurPageId();
281 else
282 return 0;
285 //------------------------------------------------------------------
286 void OPropertyEditor::Update(const ::std::mem_fun_t<void,OBrowserListBox>& _aUpdateFunction)
288 // forward this to all our pages
289 sal_uInt16 nCount = m_aTabControl.GetPageCount();
290 for (sal_uInt16 i=0;i<nCount;++i)
292 sal_uInt16 nID = m_aTabControl.GetPageId(i);
293 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl.GetTabPage(nID));
294 if (pPage)
295 _aUpdateFunction(&pPage->getListBox());
298 //------------------------------------------------------------------
299 void OPropertyEditor::EnableUpdate()
301 Update(::std::mem_fun(&OBrowserListBox::EnableUpdate));
303 //------------------------------------------------------------------
304 void OPropertyEditor::DisableUpdate()
306 Update(::std::mem_fun(&OBrowserListBox::DisableUpdate));
309 //------------------------------------------------------------------
310 void OPropertyEditor::forEachPage( PageOperation _pOperation, const void* _pArgument )
312 sal_uInt16 nCount = m_aTabControl.GetPageCount();
313 for ( sal_uInt16 i=0; i<nCount; ++i )
315 sal_uInt16 nID = m_aTabControl.GetPageId(i);
316 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( nID ) );
317 if ( !pPage )
318 continue;
319 (this->*_pOperation)( *pPage, _pArgument );
323 //------------------------------------------------------------------
324 void OPropertyEditor::setPageLineListener( OBrowserPage& _rPage, const void* )
326 _rPage.getListBox().SetListener( m_pListener );
329 //------------------------------------------------------------------
330 void OPropertyEditor::SetLineListener(IPropertyLineListener* _pListener)
332 m_pListener = _pListener;
333 forEachPage( &OPropertyEditor::setPageLineListener );
336 //------------------------------------------------------------------
337 void OPropertyEditor::setPageControlObserver( OBrowserPage& _rPage, const void* )
339 _rPage.getListBox().SetObserver( m_pObserver );
342 //------------------------------------------------------------------
343 void OPropertyEditor::SetControlObserver( IPropertyControlObserver* _pObserver )
345 m_pObserver = _pObserver;
346 forEachPage( &OPropertyEditor::setPageControlObserver );
349 //------------------------------------------------------------------
350 void OPropertyEditor::EnableHelpSection( bool _bEnable )
352 m_bHasHelpSection = _bEnable;
353 forEachPage( &OPropertyEditor::enableHelpSection );
356 //------------------------------------------------------------------
357 bool OPropertyEditor::HasHelpSection() const
359 return m_bHasHelpSection;
362 //------------------------------------------------------------------
363 void OPropertyEditor::SetHelpText( const ::rtl::OUString& _rHelpText )
365 forEachPage( &OPropertyEditor::setHelpSectionText, &_rHelpText );
368 //------------------------------------------------------------------
369 void OPropertyEditor::SetHelpLineLimites( sal_Int32 _nMinLines, sal_Int32 _nMaxLines )
371 m_nMinHelpLines = _nMinLines;
372 m_nMaxHelpLines = _nMaxLines;
373 forEachPage( &OPropertyEditor::setHelpLineLimits );
376 //------------------------------------------------------------------
377 void OPropertyEditor::enableHelpSection( OBrowserPage& _rPage, const void* )
379 _rPage.getListBox().EnableHelpSection( m_bHasHelpSection );
382 //------------------------------------------------------------------
383 void OPropertyEditor::setHelpSectionText( OBrowserPage& _rPage, const void* _pPointerToOUString )
385 OSL_ENSURE( _pPointerToOUString, "OPropertyEditor::setHelpSectionText: invalid argument!" );
386 if ( !_pPointerToOUString )
387 return;
389 const ::rtl::OUString& rText( *(const ::rtl::OUString*)_pPointerToOUString );
390 _rPage.getListBox().SetHelpText( rText );
393 //------------------------------------------------------------------
394 void OPropertyEditor::setHelpLineLimits( OBrowserPage& _rPage, const void* )
396 _rPage.getListBox().SetHelpLineLimites( m_nMinHelpLines, m_nMaxHelpLines );
399 //------------------------------------------------------------------
400 sal_uInt16 OPropertyEditor::InsertEntry( const OLineDescriptor& rData, sal_uInt16 _nPageId, sal_uInt16 nPos )
402 // let the current page handle this
403 OBrowserPage* pPage = getPage( _nPageId );
404 DBG_ASSERT( pPage, "OPropertyEditor::InsertEntry: don't have such a page!" );
405 if ( !pPage )
406 return LISTBOX_ENTRY_NOTFOUND;
408 sal_uInt16 nEntry = pPage->getListBox().InsertEntry( rData, nPos );
410 OSL_ENSURE( m_aPropertyPageIds.find( rData.sName ) == m_aPropertyPageIds.end(),
411 "OPropertyEditor::InsertEntry: property already present in the map!" );
412 m_aPropertyPageIds.insert( MapStringToPageId::value_type( rData.sName, _nPageId ) );
414 return nEntry;
417 //------------------------------------------------------------------
418 void OPropertyEditor::RemoveEntry( const ::rtl::OUString& _rName )
420 OBrowserPage* pPage = getPage( _rName );
421 if ( pPage )
423 OSL_VERIFY( pPage->getListBox().RemoveEntry( _rName ) );
425 OSL_ENSURE( m_aPropertyPageIds.find( _rName ) != m_aPropertyPageIds.end(),
426 "OPropertyEditor::RemoveEntry: property not present in the map!" );
427 m_aPropertyPageIds.erase( _rName );
431 //------------------------------------------------------------------
432 void OPropertyEditor::ChangeEntry( const OLineDescriptor& rData )
434 OBrowserPage* pPage = getPage( rData.sName );
435 if ( pPage )
436 pPage->getListBox().ChangeEntry( rData, EDITOR_LIST_REPLACE_EXISTING );
439 //------------------------------------------------------------------
440 void OPropertyEditor::SetPropertyValue( const ::rtl::OUString& rEntryName, const Any& _rValue, bool _bUnknownValue )
442 OBrowserPage* pPage = getPage( rEntryName );
443 if ( pPage )
444 pPage->getListBox().SetPropertyValue( rEntryName, _rValue, _bUnknownValue );
447 //------------------------------------------------------------------
448 sal_uInt16 OPropertyEditor::GetPropertyPos( const ::rtl::OUString& rEntryName ) const
450 sal_uInt16 nVal=LISTBOX_ENTRY_NOTFOUND;
451 const OBrowserPage* pPage = getPage( rEntryName );
452 if ( pPage )
453 nVal = pPage->getListBox().GetPropertyPos( rEntryName );
454 return nVal;
457 //------------------------------------------------------------------
458 void OPropertyEditor::ShowPropertyPage( sal_uInt16 _nPageId, bool _bShow )
460 if ( !_bShow )
462 sal_uInt16 nPagePos = m_aTabControl.GetPagePos( _nPageId );
463 if ( TAB_PAGE_NOTFOUND == nPagePos )
464 return;
465 DBG_ASSERT( m_aHiddenPages.find( _nPageId ) == m_aHiddenPages.end(), "OPropertyEditor::ShowPropertyPage: page already hidden!" );
467 m_aHiddenPages[ _nPageId ] = HiddenPage( nPagePos, m_aTabControl.GetTabPage( _nPageId ) );
468 m_aTabControl.RemovePage( _nPageId );
470 else
472 ::std::map< sal_uInt16, HiddenPage >::iterator aPagePos = m_aHiddenPages.find( _nPageId );
473 if ( aPagePos == m_aHiddenPages.end() )
474 return;
476 aPagePos->second.pPage->SetSizePixel( m_aTabControl.GetTabPageSizePixel() );
477 m_aTabControl.InsertPage( aPagePos->first, aPagePos->second.pPage->GetText(), aPagePos->second.nPos );
478 m_aTabControl.SetTabPage( aPagePos->first, aPagePos->second.pPage );
480 m_aHiddenPages.erase( aPagePos );
484 //------------------------------------------------------------------
485 void OPropertyEditor::EnablePropertyControls( const ::rtl::OUString& _rEntryName, sal_Int16 _nControls, bool _bEnable )
487 for ( USHORT i = 0; i < m_aTabControl.GetPageCount(); ++i )
489 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( m_aTabControl.GetPageId( i ) ) );
490 if ( pPage )
491 pPage->getListBox().EnablePropertyControls( _rEntryName, _nControls, _bEnable );
495 //------------------------------------------------------------------
496 void OPropertyEditor::EnablePropertyLine( const ::rtl::OUString& _rEntryName, bool _bEnable )
498 for ( USHORT i = 0; i < m_aTabControl.GetPageCount(); ++i )
500 OBrowserPage* pPage = static_cast< OBrowserPage* >( m_aTabControl.GetTabPage( m_aTabControl.GetPageId( i ) ) );
501 if ( pPage )
502 pPage->getListBox().EnablePropertyLine( _rEntryName, _bEnable );
506 //------------------------------------------------------------------
507 Reference< XPropertyControl > OPropertyEditor::GetPropertyControl(const ::rtl::OUString& rEntryName)
509 Reference< XPropertyControl > xControl;
510 // let the current page handle this
511 OBrowserPage* pPage = static_cast<OBrowserPage*>(m_aTabControl.GetTabPage(m_aTabControl.GetCurPageId()));
512 if (pPage)
513 xControl = pPage->getListBox().GetPropertyControl(rEntryName);
514 return xControl;
517 //------------------------------------------------------------------
518 IMPL_LINK(OPropertyEditor, OnPageActivate, TabControl*, EMPTYARG)
520 if (m_aPageActivationHandler.IsSet())
521 m_aPageActivationHandler.Call(NULL);
522 return 0L;
525 //------------------------------------------------------------------
526 IMPL_LINK(OPropertyEditor, OnPageDeactivate, TabControl*, EMPTYARG)
528 // commit the data on the current (to-be-decativated) tab page
529 // (79404)
530 sal_Int32 nCurrentId = m_aTabControl.GetCurPageId();
531 OBrowserPage* pCurrentPage = static_cast<OBrowserPage*>(m_aTabControl.GetTabPage((sal_uInt16)nCurrentId));
532 if ( !pCurrentPage )
533 return 1L;
535 if ( pCurrentPage->getListBox().IsModified() )
536 pCurrentPage->getListBox().CommitModified();
538 return 1L;
541 //............................................................................
542 } // namespace pcr
543 //............................................................................