1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_toolkit.hxx"
30 
31 #include <toolkit/controls/geometrycontrolmodel.hxx>
32 #include <toolkit/controls/tabpagecontainer.hxx>
33 #include <toolkit/controls/tabpagemodel.hxx>
34 #include <toolkit/helper/property.hxx>
35 #include <toolkit/helper/unopropertyarrayhelper.hxx>
36 
37 #include <com/sun/star/awt/XControlModel.hpp>
38 #include <com/sun/star/awt/XVclWindowPeer.hpp>
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 
41 #include <comphelper/processfactory.hxx>
42 #include <osl/diagnose.h>
43 #include <tools/diagnose_ex.h>
44 #include <vcl/svapp.hxx>
45 #include <vos/mutex.hxx>
46 
47 using ::rtl::OUString;
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::container;
53 using namespace ::com::sun::star::view;
54 using ::com::sun::star::awt::tab::XTabPageModel;
55 
56 #define WRONG_TYPE_EXCEPTION "Type must be ::com::sun::star::awt::tab::XTabPageModel!"
57 //	----------------------------------------------------
58 //	class UnoControlTabPageContainerModel
59 //	----------------------------------------------------
60 UnoControlTabPageContainerModel::UnoControlTabPageContainerModel( const Reference< XMultiServiceFactory >& i_factory )
61     :UnoControlTabPageContainerModel_Base( i_factory )
62     ,maContainerListeners( *this )
63 {
64     ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
65     ImplRegisterProperty( BASEPROPERTY_BORDER );
66     ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
67     ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
68     ImplRegisterProperty( BASEPROPERTY_ENABLED );
69     ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
70     ImplRegisterProperty( BASEPROPERTY_HELPURL );
71     ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
72     ImplRegisterProperty( BASEPROPERTY_TEXT );
73 }
74 
75 ::rtl::OUString UnoControlTabPageContainerModel::getServiceName() throw(RuntimeException)
76 {
77     return ::rtl::OUString::createFromAscii( szServiceName_UnoControlTabPageContainerModel );
78 }
79 
80 uno::Any UnoControlTabPageContainerModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
81 {
82     switch(nPropId)
83     {
84         case BASEPROPERTY_DEFAULTCONTROL:
85             return uno::makeAny( ::rtl::OUString::createFromAscii( szServiceName_UnoControlTabPageContainer ) );
86         case BASEPROPERTY_BORDER:
87             return uno::makeAny((sal_Int16) 0);              // No Border
88         default:
89             return UnoControlModel::ImplGetDefaultValue( nPropId );
90     }
91 }
92 
93 ::cppu::IPropertyArrayHelper& UnoControlTabPageContainerModel::getInfoHelper()
94 {
95     static UnoPropertyArrayHelper* pHelper = NULL;
96     if ( !pHelper )
97     {
98         com::sun::star::uno::Sequence<sal_Int32>	aIDs = ImplGetPropertyIds();
99         pHelper = new UnoPropertyArrayHelper( aIDs );
100     }
101     return *pHelper;
102 }
103 Reference< ::com::sun::star::beans::XPropertySetInfo > UnoControlTabPageContainerModel::getPropertySetInfo(  ) throw(RuntimeException)
104 {
105     static Reference< ::com::sun::star::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
106     return xInfo;
107 }
108 
109 namespace
110 {
111     Reference< XTabPageModel > lcl_createTabPageModel( ::comphelper::ComponentContext const & i_context,
112         Sequence< Any > const & i_initArguments, Reference< XPropertySet > const & i_parentModel )
113     {
114         try
115         {
116             Reference< XPropertySet > const xParentDelegator( i_parentModel, UNO_QUERY_THROW );
117             Reference< XPropertySetInfo > const xPSI( xParentDelegator->getPropertySetInfo() );
118             bool const isGeometryControlModel = xPSI.is() && xPSI->hasPropertyByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PositionX" ) ) );
119 
120             Reference< XInterface > xInstance;
121             if ( isGeometryControlModel )
122                 xInstance = *( new OGeometryControlModel< UnoControlTabPageModel >( i_context.getLegacyServiceFactory() ) );
123             else
124                 xInstance = *( new UnoControlTabPageModel( i_context.getLegacyServiceFactory() ) );
125 
126             Reference< XTabPageModel > const xTabPageModel( xInstance, UNO_QUERY_THROW );
127             Reference< XInitialization > const xInit( xTabPageModel, UNO_QUERY_THROW );
128             xInit->initialize( i_initArguments );
129 
130             return xTabPageModel;
131         }
132         catch( const RuntimeException& )
133         {
134             throw;
135         }
136         catch( const Exception& )
137         {
138     	    DBG_UNHANDLED_EXCEPTION();
139         }
140         return NULL;
141     }
142 }
143 
144 Reference< XTabPageModel > SAL_CALL UnoControlTabPageContainerModel::createTabPage( ::sal_Int16 i_tabPageID ) throw (RuntimeException)
145 {
146     Sequence< Any > aInitArgs(1);
147     aInitArgs[0] <<= i_tabPageID;
148     return lcl_createTabPageModel( maContext, aInitArgs, this );
149 }
150 
151 Reference< XTabPageModel > SAL_CALL UnoControlTabPageContainerModel::loadTabPage( ::sal_Int16 i_tabPageID, const ::rtl::OUString& i_resourceURL ) throw (RuntimeException)
152 {
153     Sequence< Any > aInitArgs(2);
154     aInitArgs[0] <<= i_tabPageID;
155     aInitArgs[1] <<= i_resourceURL;
156     return lcl_createTabPageModel( maContext, aInitArgs, this );
157 }
158 
159 void SAL_CALL UnoControlTabPageContainerModel::insertByIndex( ::sal_Int32 nIndex, const com::sun::star::uno::Any& aElement) throw (IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, uno::RuntimeException)
160 {
161     vos::OGuard aSolarGuard( Application::GetSolarMutex() );
162     uno::Reference < XTabPageModel > xTabPageModel;
163     if(aElement >>= xTabPageModel)
164     {
165         if ( sal_Int32( m_aTabPageVector.size()) ==nIndex )
166             m_aTabPageVector.push_back( xTabPageModel );
167         else if ( sal_Int32( m_aTabPageVector.size()) > nIndex )
168         {
169             std::vector< uno::Reference< XTabPageModel > >::iterator aIter = m_aTabPageVector.begin();
170             aIter += nIndex;
171             m_aTabPageVector.insert( aIter, xTabPageModel );
172         }
173         else
174             throw IndexOutOfBoundsException( ::rtl::OUString(), (OWeakObject *)this );
175         ContainerEvent aEvent;
176         aEvent.Source = *this;
177         aEvent.Element <<= aElement;
178         aEvent.Accessor <<= ::rtl::OUString::valueOf(nIndex);
179         maContainerListeners.elementInserted( aEvent );
180     }
181     else
182         throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( WRONG_TYPE_EXCEPTION )),
183             (OWeakObject *)this, 2 );
184 }
185 // -----------------------------------------------------------------------------
186 void SAL_CALL UnoControlTabPageContainerModel::removeByIndex( ::sal_Int32 /*Index*/ ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
187 {
188 }
189 // XIndexReplace
190 void SAL_CALL UnoControlTabPageContainerModel::replaceByIndex( ::sal_Int32 /*Index*/, const uno::Any& /*Element*/ ) throw (lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
191 {
192 }
193 // -----------------------------------------------------------------------------
194 // XIndexAccess
195 ::sal_Int32 SAL_CALL UnoControlTabPageContainerModel::getCount(  ) throw (uno::RuntimeException)
196 {
197     ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
198     return sal_Int32( m_aTabPageVector.size());
199 }
200 // -----------------------------------------------------------------------------
201 uno::Any SAL_CALL UnoControlTabPageContainerModel::getByIndex( ::sal_Int32 nIndex ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
202 {
203     ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
204     if ( nIndex < 0 || nIndex > sal_Int32(m_aTabPageVector.size()) )
205         throw lang::IndexOutOfBoundsException();
206     return uno::makeAny(m_aTabPageVector[nIndex]);
207 }
208 // -----------------------------------------------------------------------------
209 // XElementAccess
210 uno::Type SAL_CALL UnoControlTabPageContainerModel::getElementType(  ) throw (uno::RuntimeException)
211 {
212     return ::getCppuType(static_cast<  Reference< com::sun::star::awt::XControlModel>* >(NULL));
213 }
214 // -----------------------------------------------------------------------------
215 ::sal_Bool SAL_CALL UnoControlTabPageContainerModel::hasElements(  ) throw (uno::RuntimeException)
216 {
217     ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
218     return !m_aTabPageVector.empty();
219 }
220 // XContainer
221 void UnoControlTabPageContainerModel::addContainerListener( const Reference< XContainerListener >& l ) throw(RuntimeException)
222 {
223     maContainerListeners.addInterface( l );
224 }
225 
226 void UnoControlTabPageContainerModel::removeContainerListener( const Reference< XContainerListener >& l ) throw(RuntimeException)
227 {
228     maContainerListeners.removeInterface( l );
229 }
230 
231 //	----------------------------------------------------
232 //	class UnoControlTabPageContainer
233 //	----------------------------------------------------
234 UnoControlTabPageContainer::UnoControlTabPageContainer( const Reference< XMultiServiceFactory >& i_factory )
235     :UnoControlTabPageContainer_Base( i_factory )
236     ,m_aTabPageListeners( *this )
237 {
238 }
239 
240 OUString UnoControlTabPageContainer::GetComponentServiceName()
241 {
242     return OUString::createFromAscii( "TabPageContainer" );
243 }
244 
245 void SAL_CALL UnoControlTabPageContainer::dispose(  ) throw(RuntimeException)
246 {
247     lang::EventObject aEvt;
248     aEvt.Source = (::cppu::OWeakObject*)this;
249     m_aTabPageListeners.disposeAndClear( aEvt );
250     UnoControl::dispose();
251 }
252 
253 void UnoControlTabPageContainer::createPeer( const uno::Reference< awt::XToolkit > & rxToolkit, const uno::Reference< awt::XWindowPeer >  & rParentPeer ) throw(uno::RuntimeException)
254 {
255     UnoControlBase::createPeer( rxToolkit, rParentPeer );
256 
257     Reference< XTabPageContainer >  xTPContainer( getPeer(), UNO_QUERY_THROW );
258     if ( m_aTabPageListeners.getLength() )
259         xTPContainer->addTabPageContainerListener(&m_aTabPageListeners);
260 }
261 
262 // -------------------------------------------------------------------
263 // XTabPageContainer
264 
265 ::sal_Int16 SAL_CALL UnoControlTabPageContainer::getActiveTabPageID() throw (RuntimeException)
266 {
267     vos::OGuard aSolarGuard( Application::GetSolarMutex() );
268     Reference< XTabPageContainer >  xTPContainer( getPeer(), UNO_QUERY_THROW );
269     return xTPContainer->getActiveTabPageID();
270 }
271 void SAL_CALL UnoControlTabPageContainer::setActiveTabPageID( ::sal_Int16 _activetabpageid ) throw (RuntimeException)
272 {
273     vos::OGuard aSolarGuard( Application::GetSolarMutex() );
274     Reference< XTabPageContainer >  xTPContainer( getPeer(), UNO_QUERY_THROW );
275     xTPContainer->setActiveTabPageID(_activetabpageid);
276 }
277 ::sal_Int16 SAL_CALL UnoControlTabPageContainer::getTabPageCount(  ) throw (RuntimeException)
278 {
279     vos::OGuard aSolarGuard( Application::GetSolarMutex() );
280     Reference< XTabPageContainer >  xTPContainer( getPeer(), UNO_QUERY_THROW );
281     return xTPContainer->getTabPageCount();
282 }
283 ::sal_Bool SAL_CALL UnoControlTabPageContainer::isTabPageActive( ::sal_Int16 tabPageIndex ) throw (RuntimeException)
284 {
285     vos::OGuard aSolarGuard( Application::GetSolarMutex() );
286     Reference< XTabPageContainer >  xTPContainer( getPeer(), UNO_QUERY_THROW );
287     return xTPContainer->isTabPageActive(tabPageIndex);
288 }
289 Reference< ::com::sun::star::awt::tab::XTabPage > SAL_CALL UnoControlTabPageContainer::getTabPage( ::sal_Int16 tabPageIndex ) throw (RuntimeException)
290 {
291     vos::OGuard aSolarGuard( Application::GetSolarMutex() );
292     Reference< XTabPageContainer >  xTPContainer( getPeer(), UNO_QUERY_THROW );
293     return xTPContainer->getTabPage(tabPageIndex);
294 }
295 Reference< ::com::sun::star::awt::tab::XTabPage > SAL_CALL UnoControlTabPageContainer::getTabPageByID( ::sal_Int16 tabPageID ) throw (RuntimeException)
296 {
297     vos::OGuard aSolarGuard( Application::GetSolarMutex() );
298     Reference< XTabPageContainer >  xTPContainer( getPeer(), UNO_QUERY_THROW );
299     return xTPContainer->getTabPageByID(tabPageID);
300 }
301 void SAL_CALL UnoControlTabPageContainer::addTabPageContainerListener( const Reference< ::com::sun::star::awt::tab::XTabPageContainerListener >& listener ) throw (RuntimeException)
302 {
303     m_aTabPageListeners.addInterface( listener );
304     if( getPeer().is() && m_aTabPageListeners.getLength() == 1 )
305     {
306         uno::Reference < awt::tab::XTabPageContainer >  xTabPageContainer( getPeer(), uno::UNO_QUERY );
307         xTabPageContainer->addTabPageContainerListener( &m_aTabPageListeners );
308     }
309 }
310 void SAL_CALL UnoControlTabPageContainer::removeTabPageContainerListener( const Reference< ::com::sun::star::awt::tab::XTabPageContainerListener >& listener ) throw (RuntimeException)
311 {
312     if( getPeer().is() && m_aTabPageListeners.getLength() == 1 )
313     {
314         uno::Reference < awt::tab::XTabPageContainer >  xTabPageContainer( getPeer(), uno::UNO_QUERY );
315         xTabPageContainer->addTabPageContainerListener( &m_aTabPageListeners );
316     }
317     m_aTabPageListeners.removeInterface( listener );
318 }
319 
320 void UnoControlTabPageContainer::updateFromModel()
321 {
322     UnoControlTabPageContainer_Base::updateFromModel();
323     Reference< XContainerListener > xContainerListener( getPeer(), UNO_QUERY );
324     ENSURE_OR_RETURN_VOID( xContainerListener.is(), "UnoListBoxControl::updateFromModel: a peer which is no ItemListListener?!" );
325 
326     ContainerEvent aEvent;
327     aEvent.Source = getModel();
328     Sequence< Reference< XControl > > aControls = getControls();
329     const Reference< XControl >* pCtrls = aControls.getConstArray();
330     const Reference< XControl >* pCtrlsEnd = pCtrls + aControls.getLength();
331 
332     for ( ; pCtrls < pCtrlsEnd; ++pCtrls )
333     {
334         aEvent.Element <<= *pCtrls;
335         xContainerListener->elementInserted( aEvent );
336     }
337 }
338 void SAL_CALL UnoControlTabPageContainer::addControl( const ::rtl::OUString& Name, const Reference< ::com::sun::star::awt::XControl >& Control ) throw (RuntimeException)
339 {
340     vos::OGuard aSolarGuard( Application::GetSolarMutex() );
341     ControlContainerBase::addControl(Name,Control);
342     Reference< XContainerListener > xContainerListener( getPeer(), UNO_QUERY );
343     ContainerEvent aEvent;
344     aEvent.Source = getModel();
345     aEvent.Element <<= Control;
346     xContainerListener->elementInserted( aEvent );
347 }
348 
349