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 <vcl/svapp.hxx>
32 #include <vcl/window.hxx>
33 #include <vcl/wall.hxx>
34 #include <vos/mutex.hxx>
35 #include <toolkit/controls/tabpagemodel.hxx>
36 #include <toolkit/helper/property.hxx>
37 #include <toolkit/helper/unopropertyarrayhelper.hxx>
38 #include <toolkit/controls/stdtabcontroller.hxx>
39 #include <com/sun/star/awt/PosSize.hpp>
40 #include <com/sun/star/awt/WindowAttribute.hpp>
41 #include <com/sun/star/awt/UnoControlDialogModelProvider.hpp>
42 #include <com/sun/star/resource/XStringResourceResolver.hpp>
43 #include <com/sun/star/graphic/XGraphicProvider.hpp>
44 #include <tools/list.hxx>
45 #include <cppuhelper/typeprovider.hxx>
46 #include <tools/debug.hxx>
47 #include <tools/diagnose_ex.h>
48 #include <comphelper/sequence.hxx>
49 #include <vcl/svapp.hxx>
50 #include <vcl/outdev.hxx>
51 
52 #include <toolkit/helper/vclunohelper.hxx>
53 #include <unotools/ucbstreamhelper.hxx>
54 #include <vcl/graph.hxx>
55 #include <vcl/image.hxx>
56 #include <toolkit/controls/geometrycontrolmodel.hxx>
57 
58 #include <map>
59 #include <algorithm>
60 #include <functional>
61 #include "tools/urlobj.hxx"
62 #include "osl/file.hxx"
63 
64 #include <com/sun/star/beans/XPropertySet.hpp>
65 
66 using namespace ::com::sun::star;
67 using namespace ::com::sun::star::uno;
68 using namespace ::com::sun::star::awt;
69 using namespace ::com::sun::star::lang;
70 using namespace ::com::sun::star::container;
71 using namespace ::com::sun::star::beans;
72 using namespace ::com::sun::star::util;
73 
74 ////HELPER
75 ::rtl::OUString getPhysicalLocation( const ::com::sun::star::uno::Any& rbase, const ::com::sun::star::uno::Any& rUrl );
76 
77 //	----------------------------------------------------
78 //	class UnoControlTabPageModel
79 //	----------------------------------------------------
80 UnoControlTabPageModel::UnoControlTabPageModel( Reference< XMultiServiceFactory > const & i_factory )
81     :ControlModelContainerBase( i_factory )
82 {
83 	ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
84     ImplRegisterProperty( BASEPROPERTY_TITLE );
85     ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
86 	ImplRegisterProperty( BASEPROPERTY_HELPURL );
87     ImplRegisterProperty( BASEPROPERTY_IMAGEURL );
88     ImplRegisterProperty( BASEPROPERTY_ENABLED );
89 }
90 
91 ::rtl::OUString UnoControlTabPageModel::getServiceName( ) throw(RuntimeException)
92 {
93 	return ::rtl::OUString::createFromAscii( szServiceName_UnoControlTabPageModel );
94 }
95 
96 Any UnoControlTabPageModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
97 {
98     Any aAny;
99 
100     switch ( nPropId )
101     {
102         case BASEPROPERTY_DEFAULTCONTROL:
103 		    aAny <<= ::rtl::OUString::createFromAscii( szServiceName_UnoControlTabPage );
104             break;
105         default:
106             aAny = UnoControlModel::ImplGetDefaultValue( nPropId );
107     }
108 
109     return aAny;
110 }
111 
112 ::cppu::IPropertyArrayHelper& UnoControlTabPageModel::getInfoHelper()
113 {
114 	static UnoPropertyArrayHelper* pHelper = NULL;
115 	if ( !pHelper )
116 	{
117 		Sequence<sal_Int32> aIDs = ImplGetPropertyIds();
118 		pHelper = new UnoPropertyArrayHelper( aIDs );
119 	}
120 	return *pHelper;
121 }
122 // beans::XMultiPropertySet
123 uno::Reference< beans::XPropertySetInfo > UnoControlTabPageModel::getPropertySetInfo(  ) throw(uno::RuntimeException)
124 {
125 	static uno::Reference< beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
126 	return xInfo;
127 }
128 ////----- XInitialization -------------------------------------------------------------------
129 void SAL_CALL UnoControlTabPageModel::initialize (const Sequence<Any>& rArguments)
130 			throw (com::sun::star::uno::Exception, com::sun::star::uno::RuntimeException)
131 {
132 	sal_Int16 nPageId = -1;
133 	if ( rArguments.getLength() == 1 )
134     {
135          if ( !( rArguments[ 0 ] >>= nPageId ))
136              throw lang::IllegalArgumentException();
137         m_nTabPageId = nPageId;
138     }
139     else if ( rArguments.getLength() == 2 )
140     {
141         if ( !( rArguments[ 0 ] >>= nPageId ))
142              throw lang::IllegalArgumentException();
143         m_nTabPageId = nPageId;
144         ::rtl::OUString sURL;
145         if ( !( rArguments[ 1 ] >>= sURL ))
146             throw lang::IllegalArgumentException();
147         Reference<container::XNameContainer > xDialogModel = awt::UnoControlDialogModelProvider::create( maContext.getUNOContext(),sURL);
148         if ( xDialogModel.is() )
149         {
150             Sequence< ::rtl::OUString> aNames = xDialogModel->getElementNames();
151             const ::rtl::OUString* pIter = aNames.getConstArray();
152             const ::rtl::OUString* pEnd = pIter + aNames.getLength();
153             for(;pIter != pEnd;++pIter)
154             {
155                 try
156                 {
157                     Any aElement(xDialogModel->getByName(*pIter));
158                     xDialogModel->removeByName(*pIter);
159                     insertByName(*pIter,aElement);
160                 }
161                 catch(const Exception& ex)
162                 {
163                     (void)ex;
164                 }
165             }
166             Reference<XPropertySet> xDialogProp(xDialogModel,UNO_QUERY);
167             if ( xDialogProp.is() )
168             {
169                 static const ::rtl::OUString s_sResourceResolver(RTL_CONSTASCII_USTRINGPARAM("ResourceResolver"));
170                 Reference<XPropertySet> xThis(*this,UNO_QUERY);
171                 xThis->setPropertyValue(s_sResourceResolver,xDialogProp->getPropertyValue(s_sResourceResolver));
172                 xThis->setPropertyValue(GetPropertyName(BASEPROPERTY_TITLE),xDialogProp->getPropertyValue(GetPropertyName(BASEPROPERTY_TITLE)));
173                 xThis->setPropertyValue(GetPropertyName(BASEPROPERTY_IMAGEURL),xDialogProp->getPropertyValue(GetPropertyName(BASEPROPERTY_IMAGEURL)));
174                 xThis->setPropertyValue(GetPropertyName(BASEPROPERTY_HELPTEXT),xDialogProp->getPropertyValue(GetPropertyName(BASEPROPERTY_HELPTEXT)));
175                 xThis->setPropertyValue(GetPropertyName(BASEPROPERTY_ENABLED),xDialogProp->getPropertyValue(GetPropertyName(BASEPROPERTY_ENABLED)));
176                 xThis->setPropertyValue(GetPropertyName(BASEPROPERTY_HELPURL),xDialogProp->getPropertyValue(GetPropertyName(BASEPROPERTY_HELPURL)));
177             }
178         }
179     }
180 	else
181 		m_nTabPageId = -1;
182 }
183 //===== Service ===============================================================
184 ::rtl::OUString UnoControlTabPageModel_getImplementationName (void) throw(RuntimeException)
185 {
186 	return rtl::OUString::createFromAscii("com.sun.star.awt.tab.UnoControlTabPageModel");
187 }
188 
189 Sequence<rtl::OUString> SAL_CALL UnoControlTabPageModel_getSupportedServiceNames (void)
190      throw (RuntimeException)
191 {
192  	static const ::rtl::OUString sServiceName(
193          ::rtl::OUString::createFromAscii("com.sun.star.awt.tab.UnoControlTabPageModel"));
194  	return Sequence<rtl::OUString>(&sServiceName, 1);
195 }
196 //=============================================================================
197 // = class UnoControlTabPage
198 // ============================================================================
199 
200 UnoControlTabPage::UnoControlTabPage( const Reference< XMultiServiceFactory >& i_factory )
201     :UnoControlTabPage_Base( i_factory )
202     ,m_bWindowListener(false)
203 {
204 	maComponentInfos.nWidth = 280;
205 	maComponentInfos.nHeight = 400;
206 }
207 UnoControlTabPage::~UnoControlTabPage()
208 {
209 }
210 
211 ::rtl::OUString UnoControlTabPage::GetComponentServiceName()
212 {
213     return ::rtl::OUString::createFromAscii( "TabPageModel" );
214 }
215 
216 void UnoControlTabPage::dispose() throw(RuntimeException)
217 {
218 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
219 
220 	EventObject aEvt;
221 	aEvt.Source = static_cast< ::cppu::OWeakObject* >( this );
222     ControlContainerBase::dispose();
223 }
224 
225 void SAL_CALL UnoControlTabPage::disposing(    const EventObject& Source )throw(RuntimeException)
226 {
227  	ControlContainerBase::disposing( Source );
228 }
229 
230 void UnoControlTabPage::createPeer( const Reference< XToolkit > & rxToolkit, const Reference< XWindowPeer >  & rParentPeer ) throw(RuntimeException)
231 {
232 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
233     ImplUpdateResourceResolver();
234 
235 	UnoControlContainer::createPeer( rxToolkit, rParentPeer );
236 
237 	Reference < tab::XTabPage > xTabPage( getPeer(), UNO_QUERY );
238     if ( xTabPage.is() )
239     {
240         if ( !m_bWindowListener )
241         {
242             Reference< XWindowListener > xWL( static_cast< cppu::OWeakObject*>( this ), UNO_QUERY );
243             addWindowListener( xWL );
244             m_bWindowListener = true;
245         }
246     }
247 }
248 
249 static ::Size ImplMapPixelToAppFont( OutputDevice* pOutDev, const ::Size& aSize )
250 {
251     ::Size aTmp = pOutDev->PixelToLogic( aSize, MAP_APPFONT );
252     return aTmp;
253 }
254 // ::com::sun::star::awt::XWindowListener
255 void SAL_CALL UnoControlTabPage::windowResized( const ::com::sun::star::awt::WindowEvent& e )
256 throw (::com::sun::star::uno::RuntimeException)
257 {
258     OutputDevice*pOutDev = Application::GetDefaultDevice();
259 	DBG_ASSERT( pOutDev, "Missing Default Device!" );
260 	if ( pOutDev && !mbSizeModified )
261 	{
262         // Currentley we are simply using MAP_APPFONT
263 		::Size aAppFontSize( e.Width, e.Height );
264 
265         Reference< XControl > xDialogControl( *this, UNO_QUERY_THROW );
266         Reference< XDevice > xDialogDevice( xDialogControl->getPeer(), UNO_QUERY );
267         OSL_ENSURE( xDialogDevice.is(), "UnoDialogControl::windowResized: no peer, but a windowResized event?" );
268         if ( xDialogDevice.is() )
269         {
270             DeviceInfo aDeviceInfo( xDialogDevice->getInfo() );
271             aAppFontSize.Width() -= aDeviceInfo.LeftInset + aDeviceInfo.RightInset;
272             aAppFontSize.Height() -= aDeviceInfo.TopInset + aDeviceInfo.BottomInset;
273         }
274 
275         aAppFontSize = ImplMapPixelToAppFont( pOutDev, aAppFontSize );
276 
277         // Remember that changes have been done by listener. No need to
278         // update the position because of property change event.
279         mbSizeModified = true;
280         Sequence< rtl::OUString > aProps( 2 );
281         Sequence< Any > aValues( 2 );
282         // Properties in a sequence must be sorted!
283         aProps[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Height" ));
284         aProps[1] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Width"  ));
285         aValues[0] <<= aAppFontSize.Height();
286         aValues[1] <<= aAppFontSize.Width();
287 
288 	    ImplSetPropertyValues( aProps, aValues, true );
289         mbSizeModified = false;
290 	}
291 }
292 
293 void SAL_CALL UnoControlTabPage::windowMoved( const ::com::sun::star::awt::WindowEvent& e )
294 throw (::com::sun::star::uno::RuntimeException)
295 {
296 	OutputDevice*pOutDev = Application::GetDefaultDevice();
297 	DBG_ASSERT( pOutDev, "Missing Default Device!" );
298 	if ( pOutDev && !mbPosModified )
299 	{
300         // Currentley we are simply using MAP_APPFONT
301         Any    aAny;
302 		::Size aTmp( e.X, e.Y );
303         aTmp = ImplMapPixelToAppFont( pOutDev, aTmp );
304 
305         // Remember that changes have been done by listener. No need to
306         // update the position because of property change event.
307         mbPosModified = true;
308         Sequence< rtl::OUString > aProps( 2 );
309         Sequence< Any > aValues( 2 );
310         aProps[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PositionX"  ));
311         aProps[1] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PositionY" ));
312         aValues[0] <<= aTmp.Width();
313         aValues[1] <<= aTmp.Height();
314 
315 	    ImplSetPropertyValues( aProps, aValues, true );
316         mbPosModified = false;
317 	}
318 }
319 
320 void SAL_CALL UnoControlTabPage::windowShown( const ::com::sun::star::lang::EventObject& e )
321 throw (::com::sun::star::uno::RuntimeException)
322 {
323     (void)e;
324 }
325 
326 void SAL_CALL UnoControlTabPage::windowHidden( const ::com::sun::star::lang::EventObject& e )
327 throw (::com::sun::star::uno::RuntimeException)
328 {
329     (void)e;
330 }
331