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