xref: /trunk/main/toolkit/source/controls/tabpagemodel.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 //  ----------------------------------------------------
UnoControlTabPageModel(Reference<XMultiServiceFactory> const & i_factory)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 }
84 
getServiceName()85 ::rtl::OUString UnoControlTabPageModel::getServiceName( )
86 {
87     return ::rtl::OUString::createFromAscii( szServiceName_UnoControlTabPageModel );
88 }
89 
ImplGetDefaultValue(sal_uInt16 nPropId) const90 Any UnoControlTabPageModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
91 {
92     Any aAny;
93 
94     switch ( nPropId )
95     {
96         case BASEPROPERTY_DEFAULTCONTROL:
97             aAny <<= ::rtl::OUString::createFromAscii( szServiceName_UnoControlTabPage );
98             break;
99         default:
100             aAny = UnoControlModel::ImplGetDefaultValue( nPropId );
101     }
102 
103     return aAny;
104 }
105 
getInfoHelper()106 ::cppu::IPropertyArrayHelper& UnoControlTabPageModel::getInfoHelper()
107 {
108     static UnoPropertyArrayHelper* pHelper = NULL;
109     if ( !pHelper )
110     {
111         Sequence<sal_Int32> aIDs = ImplGetPropertyIds();
112         pHelper = new UnoPropertyArrayHelper( aIDs );
113     }
114     return *pHelper;
115 }
116 // beans::XMultiPropertySet
getPropertySetInfo()117 uno::Reference< beans::XPropertySetInfo > UnoControlTabPageModel::getPropertySetInfo(  )
118 {
119     static uno::Reference< beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
120     return xInfo;
121 }
122 ////----- XInitialization -------------------------------------------------------------------
initialize(const Sequence<Any> & rArguments)123 void SAL_CALL UnoControlTabPageModel::initialize (const Sequence<Any>& rArguments)
124 {
125     sal_Int16 nPageId = -1;
126     if ( rArguments.getLength() == 1 )
127     {
128          if ( !( rArguments[ 0 ] >>= nPageId ))
129              throw lang::IllegalArgumentException();
130         m_nTabPageId = nPageId;
131     }
132     else if ( rArguments.getLength() == 2 )
133     {
134         if ( !( rArguments[ 0 ] >>= nPageId ))
135              throw lang::IllegalArgumentException();
136         m_nTabPageId = nPageId;
137         ::rtl::OUString sURL;
138         if ( !( rArguments[ 1 ] >>= sURL ))
139             throw lang::IllegalArgumentException();
140         Reference<container::XNameContainer > xDialogModel = awt::UnoControlDialogModelProvider::create( maContext.getUNOContext(),sURL);
141         if ( xDialogModel.is() )
142         {
143             Sequence< ::rtl::OUString> aNames = xDialogModel->getElementNames();
144             const ::rtl::OUString* pIter = aNames.getConstArray();
145             const ::rtl::OUString* pEnd = pIter + aNames.getLength();
146             for(;pIter != pEnd;++pIter)
147             {
148                 try
149                 {
150                     Any aElement(xDialogModel->getByName(*pIter));
151                     xDialogModel->removeByName(*pIter);
152                     insertByName(*pIter,aElement);
153                 }
154                 catch(const Exception& ex)
155                 {
156                     (void)ex;
157                 }
158             }
159             Reference<XPropertySet> xDialogProp(xDialogModel,UNO_QUERY);
160             if ( xDialogProp.is() )
161             {
162                 static const ::rtl::OUString s_sResourceResolver(RTL_CONSTASCII_USTRINGPARAM("ResourceResolver"));
163                 Reference<XPropertySet> xThis(*this,UNO_QUERY);
164                 xThis->setPropertyValue(s_sResourceResolver,xDialogProp->getPropertyValue(s_sResourceResolver));
165                 xThis->setPropertyValue(GetPropertyName(BASEPROPERTY_TITLE),xDialogProp->getPropertyValue(GetPropertyName(BASEPROPERTY_TITLE)));
166                 xThis->setPropertyValue(GetPropertyName(BASEPROPERTY_HELPTEXT),xDialogProp->getPropertyValue(GetPropertyName(BASEPROPERTY_HELPTEXT)));
167                 xThis->setPropertyValue(GetPropertyName(BASEPROPERTY_HELPURL),xDialogProp->getPropertyValue(GetPropertyName(BASEPROPERTY_HELPURL)));
168             }
169         }
170     }
171     else
172         m_nTabPageId = -1;
173 }
174 //===== Service ===============================================================
UnoControlTabPageModel_getImplementationName(void)175 ::rtl::OUString UnoControlTabPageModel_getImplementationName (void)
176 {
177     return rtl::OUString::createFromAscii("com.sun.star.awt.tab.UnoControlTabPageModel");
178 }
179 
UnoControlTabPageModel_getSupportedServiceNames(void)180 Sequence<rtl::OUString> SAL_CALL UnoControlTabPageModel_getSupportedServiceNames (void)
181 {
182     static const ::rtl::OUString sServiceName(
183          ::rtl::OUString::createFromAscii("com.sun.star.awt.tab.UnoControlTabPageModel"));
184     return Sequence<rtl::OUString>(&sServiceName, 1);
185 }
186 //=============================================================================
187 // = class UnoControlTabPage
188 // ============================================================================
189 
UnoControlTabPage(const Reference<XMultiServiceFactory> & i_factory)190 UnoControlTabPage::UnoControlTabPage( const Reference< XMultiServiceFactory >& i_factory )
191     :UnoControlTabPage_Base( i_factory )
192     ,m_bWindowListener(false)
193 {
194     maComponentInfos.nWidth = 280;
195     maComponentInfos.nHeight = 400;
196 }
~UnoControlTabPage()197 UnoControlTabPage::~UnoControlTabPage()
198 {
199 }
200 
GetComponentServiceName()201 ::rtl::OUString UnoControlTabPage::GetComponentServiceName()
202 {
203     return ::rtl::OUString::createFromAscii( "TabPageModel" );
204 }
205 
dispose()206 void UnoControlTabPage::dispose()
207 {
208     vos::OGuard aSolarGuard( Application::GetSolarMutex() );
209 
210     EventObject aEvt;
211     aEvt.Source = static_cast< ::cppu::OWeakObject* >( this );
212     ControlContainerBase::dispose();
213 }
214 
disposing(const EventObject & Source)215 void SAL_CALL UnoControlTabPage::disposing(    const EventObject& Source )
216 {
217     ControlContainerBase::disposing( Source );
218 }
219 
createPeer(const Reference<XToolkit> & rxToolkit,const Reference<XWindowPeer> & rParentPeer)220 void UnoControlTabPage::createPeer( const Reference< XToolkit > & rxToolkit, const Reference< XWindowPeer >  & rParentPeer )
221 {
222     vos::OGuard aSolarGuard( Application::GetSolarMutex() );
223     ImplUpdateResourceResolver();
224 
225     UnoControlContainer::createPeer( rxToolkit, rParentPeer );
226 
227     Reference < tab::XTabPage > xTabPage( getPeer(), UNO_QUERY );
228     if ( xTabPage.is() )
229     {
230         if ( !m_bWindowListener )
231         {
232             Reference< XWindowListener > xWL( static_cast< cppu::OWeakObject*>( this ), UNO_QUERY );
233             addWindowListener( xWL );
234             m_bWindowListener = true;
235         }
236     }
237 }
238 
ImplMapPixelToAppFont(OutputDevice * pOutDev,const::Size & aSize)239 static ::Size ImplMapPixelToAppFont( OutputDevice* pOutDev, const ::Size& aSize )
240 {
241     ::Size aTmp = pOutDev->PixelToLogic( aSize, MAP_APPFONT );
242     return aTmp;
243 }
244 // ::com::sun::star::awt::XWindowListener
windowResized(const::com::sun::star::awt::WindowEvent & e)245 void SAL_CALL UnoControlTabPage::windowResized( const ::com::sun::star::awt::WindowEvent& e )
246 {
247     OutputDevice*pOutDev = Application::GetDefaultDevice();
248     DBG_ASSERT( pOutDev, "Missing Default Device!" );
249     if ( pOutDev && !mbSizeModified )
250     {
251         // Currentley we are simply using MAP_APPFONT
252         ::Size aAppFontSize( e.Width, e.Height );
253 
254         Reference< XControl > xDialogControl( *this, UNO_QUERY_THROW );
255         Reference< XDevice > xDialogDevice( xDialogControl->getPeer(), UNO_QUERY );
256         OSL_ENSURE( xDialogDevice.is(), "UnoDialogControl::windowResized: no peer, but a windowResized event?" );
257         if ( xDialogDevice.is() )
258         {
259             DeviceInfo aDeviceInfo( xDialogDevice->getInfo() );
260             aAppFontSize.Width() -= aDeviceInfo.LeftInset + aDeviceInfo.RightInset;
261             aAppFontSize.Height() -= aDeviceInfo.TopInset + aDeviceInfo.BottomInset;
262         }
263 
264         aAppFontSize = ImplMapPixelToAppFont( pOutDev, aAppFontSize );
265 
266         // Remember that changes have been done by listener. No need to
267         // update the position because of property change event.
268         mbSizeModified = true;
269         Sequence< rtl::OUString > aProps( 2 );
270         Sequence< Any > aValues( 2 );
271         // Properties in a sequence must be sorted!
272         aProps[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Height" ));
273         aProps[1] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Width"  ));
274         aValues[0] <<= aAppFontSize.Height();
275         aValues[1] <<= aAppFontSize.Width();
276 
277         ImplSetPropertyValues( aProps, aValues, true );
278         mbSizeModified = false;
279     }
280 }
281 
windowMoved(const::com::sun::star::awt::WindowEvent & e)282 void SAL_CALL UnoControlTabPage::windowMoved( const ::com::sun::star::awt::WindowEvent& e )
283 {
284     OutputDevice*pOutDev = Application::GetDefaultDevice();
285     DBG_ASSERT( pOutDev, "Missing Default Device!" );
286     if ( pOutDev && !mbPosModified )
287     {
288         // Currentley we are simply using MAP_APPFONT
289         Any    aAny;
290         ::Size aTmp( e.X, e.Y );
291         aTmp = ImplMapPixelToAppFont( pOutDev, aTmp );
292 
293         // Remember that changes have been done by listener. No need to
294         // update the position because of property change event.
295         mbPosModified = true;
296         Sequence< rtl::OUString > aProps( 2 );
297         Sequence< Any > aValues( 2 );
298         aProps[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PositionX"  ));
299         aProps[1] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PositionY" ));
300         aValues[0] <<= aTmp.Width();
301         aValues[1] <<= aTmp.Height();
302 
303         ImplSetPropertyValues( aProps, aValues, true );
304         mbPosModified = false;
305     }
306 }
307 
windowShown(const::com::sun::star::lang::EventObject & e)308 void SAL_CALL UnoControlTabPage::windowShown( const ::com::sun::star::lang::EventObject& e )
309 {
310     (void)e;
311 }
312 
windowHidden(const::com::sun::star::lang::EventObject & e)313 void SAL_CALL UnoControlTabPage::windowHidden( const ::com::sun::star::lang::EventObject& e )
314 {
315     (void)e;
316 }
317