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_extensions.hxx"
30 #include "editpropertyhandler.hxx"
31 #include "formstrings.hxx"
32 #include "formmetadata.hxx"
33 
34 /** === begin UNO includes === **/
35 #include <com/sun/star/inspection/XObjectInspectorUI.hpp>
36 /** === end UNO includes === **/
37 #include <tools/debug.hxx>
38 
39 #define TEXTTYPE_SINGLELINE     0
40 #define TEXTTYPE_MULTILINE      1
41 #define TEXTTYPE_RICHTEXT       2
42 
43 //------------------------------------------------------------------------
44 extern "C" void SAL_CALL createRegistryInfo_EditPropertyHandler()
45 {
46     ::pcr::EditPropertyHandler::registerImplementation();
47 }
48 
49 //........................................................................
50 namespace pcr
51 {
52 //........................................................................
53 
54     using namespace ::com::sun::star::uno;
55     using namespace ::com::sun::star::lang;
56     using namespace ::com::sun::star::beans;
57     using namespace ::com::sun::star::script;
58     using namespace ::com::sun::star::frame;
59     using namespace ::com::sun::star::inspection;
60 
61     //====================================================================
62 	//= EditPropertyHandler
63 	//====================================================================
64     DBG_NAME( EditPropertyHandler )
65 	//--------------------------------------------------------------------
66     EditPropertyHandler::EditPropertyHandler( const Reference< XComponentContext >& _rxContext )
67         :EditPropertyHandler_Base( _rxContext )
68     {
69         DBG_CTOR( EditPropertyHandler, NULL );
70     }
71 
72 	//--------------------------------------------------------------------
73     EditPropertyHandler::~EditPropertyHandler( )
74     {
75         DBG_DTOR( EditPropertyHandler, NULL );
76     }
77 
78     //--------------------------------------------------------------------
79     ::rtl::OUString SAL_CALL EditPropertyHandler::getImplementationName_static(  ) throw (RuntimeException)
80     {
81         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.EditPropertyHandler" ) );
82     }
83 
84     //--------------------------------------------------------------------
85     Sequence< ::rtl::OUString > SAL_CALL EditPropertyHandler::getSupportedServiceNames_static(  ) throw (RuntimeException)
86     {
87         Sequence< ::rtl::OUString > aSupported( 1 );
88         aSupported[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.form.inspection.EditPropertyHandler" ) );
89         return aSupported;
90     }
91 
92     //--------------------------------------------------------------------
93     Any SAL_CALL EditPropertyHandler::getPropertyValue( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
94     {
95         ::osl::MutexGuard aGuard( m_aMutex );
96         PropertyId nPropId( impl_getPropertyId_throw( _rPropertyName ) );
97 
98         Any aReturn;
99         try
100         {
101             switch ( nPropId )
102             {
103             case PROPERTY_ID_SHOW_SCROLLBARS:
104             {
105                 sal_Bool bHasVScroll = sal_False;
106                 m_xComponent->getPropertyValue( PROPERTY_VSCROLL ) >>= bHasVScroll;
107                 sal_Bool bHasHScroll = sal_False;
108                 m_xComponent->getPropertyValue( PROPERTY_HSCROLL ) >>= bHasHScroll;
109 
110                 aReturn <<= (sal_Int32)( ( bHasVScroll ? 2 : 0 ) + ( bHasHScroll ? 1 : 0 ) );
111             }
112             break;
113 
114             case PROPERTY_ID_TEXTTYPE:
115             {
116                 sal_Int32 nTextType = TEXTTYPE_SINGLELINE;
117                 sal_Bool bRichText = sal_False;
118                 OSL_VERIFY( m_xComponent->getPropertyValue( PROPERTY_RICHTEXT ) >>= bRichText );
119                 if ( bRichText )
120                     nTextType = TEXTTYPE_RICHTEXT;
121                 else
122                 {
123                     sal_Bool bMultiLine = sal_False;
124                     OSL_VERIFY( m_xComponent->getPropertyValue( PROPERTY_MULTILINE ) >>= bMultiLine );
125                     if ( bMultiLine )
126                         nTextType = TEXTTYPE_MULTILINE;
127                     else
128                         nTextType = TEXTTYPE_SINGLELINE;
129                 }
130                 aReturn <<= nTextType;
131             }
132             break;
133 
134 
135             default:
136                 DBG_ERROR( "EditPropertyHandler::getPropertyValue: cannot handle this property!" );
137                 break;
138             }
139         }
140         catch( const Exception& )
141         {
142         	OSL_ENSURE( sal_False, "EditPropertyHandler::getPropertyValue: caught an exception!" );
143         }
144 
145         return aReturn;
146     }
147 
148     //--------------------------------------------------------------------
149     void SAL_CALL EditPropertyHandler::setPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException)
150     {
151         ::osl::MutexGuard aGuard( m_aMutex );
152         PropertyId nPropId( impl_getPropertyId_throw( _rPropertyName ) );
153 
154         try
155         {
156             switch ( nPropId )
157             {
158             case PROPERTY_ID_SHOW_SCROLLBARS:
159             {
160                 sal_Int32 nScrollbars = 0;
161                 _rValue >>= nScrollbars;
162 
163                 sal_Bool bHasVScroll = 0 != ( nScrollbars & 2 );
164                 sal_Bool bHasHScroll = 0 != ( nScrollbars & 1 );
165 
166                 m_xComponent->setPropertyValue( PROPERTY_VSCROLL, makeAny( (sal_Bool)bHasVScroll ) );
167                 m_xComponent->setPropertyValue( PROPERTY_HSCROLL, makeAny( (sal_Bool)bHasHScroll ) );
168             }
169             break;
170 
171             case PROPERTY_ID_TEXTTYPE:
172             {
173                 sal_Bool bMultiLine = sal_False;
174                 sal_Bool bRichText = sal_False;
175                 sal_Int32 nTextType = TEXTTYPE_SINGLELINE;
176                 OSL_VERIFY( _rValue >>= nTextType );
177                 switch ( nTextType )
178                 {
179                 case TEXTTYPE_SINGLELINE: bMultiLine = bRichText = sal_False; break;
180                 case TEXTTYPE_MULTILINE:  bMultiLine = sal_True; bRichText = sal_False; break;
181                 case TEXTTYPE_RICHTEXT:   bMultiLine = sal_True; bRichText = sal_True; break;
182                 default:
183                     OSL_ENSURE( sal_False, "EditPropertyHandler::setPropertyValue: invalid text type!" );
184                 }
185 
186                 m_xComponent->setPropertyValue( PROPERTY_MULTILINE, makeAny( bMultiLine ) );
187                 m_xComponent->setPropertyValue( PROPERTY_RICHTEXT, makeAny( bRichText ) );
188             }
189             break;
190 
191             default:
192                 OSL_ENSURE( sal_False, "EditPropertyHandler::setPropertyValue: cannot handle this id!" );
193             }
194         }
195         catch( const Exception& )
196         {
197         	OSL_ENSURE( sal_False, "EditPropertyHandler::setPropertyValue: caught an exception!" );
198         }
199     }
200 
201     //--------------------------------------------------------------------
202     bool EditPropertyHandler::implHaveBothScrollBarProperties() const
203     {
204         // have a "Scrollbars" property if the object supports both "HScroll" and "VScroll"
205         Reference< XPropertySetInfo > xPSI;
206         if ( m_xComponent.is() )
207             xPSI = m_xComponent->getPropertySetInfo();
208 
209         return xPSI.is()
210             && xPSI->hasPropertyByName( PROPERTY_HSCROLL )
211             && xPSI->hasPropertyByName( PROPERTY_VSCROLL );
212     }
213 
214     //--------------------------------------------------------------------
215     bool EditPropertyHandler::implHaveTextTypeProperty() const
216     {
217         // have a "Scrollbars" property if the object supports both "HScroll" and "VScroll"
218         Reference< XPropertySetInfo > xPSI;
219         if ( m_xComponent.is() )
220             xPSI = m_xComponent->getPropertySetInfo();
221 
222         return xPSI.is()
223             && xPSI->hasPropertyByName( PROPERTY_RICHTEXT )
224             && xPSI->hasPropertyByName( PROPERTY_MULTILINE );
225     }
226 
227     //--------------------------------------------------------------------
228     Sequence< Property > SAL_CALL EditPropertyHandler::doDescribeSupportedProperties() const
229     {
230         ::std::vector< Property > aProperties;
231 
232         if ( implHaveBothScrollBarProperties() )
233             addInt32PropertyDescription( aProperties, PROPERTY_SHOW_SCROLLBARS );
234 
235         if ( implHaveTextTypeProperty() )
236             addInt32PropertyDescription( aProperties, PROPERTY_TEXTTYPE );
237 
238         if ( aProperties.empty() )
239             return Sequence< Property >();
240         return Sequence< Property >( &(*aProperties.begin()), aProperties.size() );
241     }
242 
243     //--------------------------------------------------------------------
244     Sequence< ::rtl::OUString > SAL_CALL EditPropertyHandler::getSupersededProperties( ) throw (RuntimeException)
245     {
246         ::osl::MutexGuard aGuard( m_aMutex );
247         ::std::vector< ::rtl::OUString > aSuperseded;
248         if ( implHaveBothScrollBarProperties() )
249         {
250             aSuperseded.push_back( PROPERTY_HSCROLL );
251             aSuperseded.push_back( PROPERTY_VSCROLL );
252         }
253         if ( implHaveTextTypeProperty() )
254         {
255             aSuperseded.push_back( PROPERTY_RICHTEXT );
256             aSuperseded.push_back( PROPERTY_MULTILINE );
257         }
258         if ( aSuperseded.empty() )
259             return Sequence< ::rtl::OUString >();
260         return Sequence< ::rtl::OUString >( &(*aSuperseded.begin()), aSuperseded.size() );
261     }
262 
263     //--------------------------------------------------------------------
264     Sequence< ::rtl::OUString > SAL_CALL EditPropertyHandler::getActuatingProperties( ) throw (RuntimeException)
265     {
266         ::osl::MutexGuard aGuard( m_aMutex );
267         ::std::vector< ::rtl::OUString > aInterestingActuatingProps;
268         if ( implHaveTextTypeProperty() )
269             aInterestingActuatingProps.push_back( PROPERTY_TEXTTYPE );
270         aInterestingActuatingProps.push_back( PROPERTY_MULTILINE );
271         return Sequence< ::rtl::OUString >( &(*aInterestingActuatingProps.begin()), aInterestingActuatingProps.size() );;
272     }
273 
274     //--------------------------------------------------------------------
275     void SAL_CALL EditPropertyHandler::actuatingPropertyChanged( const ::rtl::OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool ) throw (NullPointerException, RuntimeException)
276     {
277         if ( !_rxInspectorUI.is() )
278             throw NullPointerException();
279 
280         ::osl::MutexGuard aGuard( m_aMutex );
281         PropertyId nActuatingPropId( impl_getPropertyId_throw( _rActuatingPropertyName ) );
282         switch ( nActuatingPropId )
283         {
284         case PROPERTY_ID_TEXTTYPE:
285         {
286             sal_Int32 nTextType = TEXTTYPE_SINGLELINE;
287             getPropertyValue( PROPERTY_TEXTTYPE ) >>= nTextType;
288 
289             if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_WORDBREAK ) )
290                 _rxInspectorUI->enablePropertyUI( PROPERTY_WORDBREAK,   nTextType == TEXTTYPE_RICHTEXT );
291             _rxInspectorUI->enablePropertyUI( PROPERTY_MAXTEXTLEN,      nTextType != TEXTTYPE_RICHTEXT );
292             _rxInspectorUI->enablePropertyUI( PROPERTY_ECHO_CHAR,       nTextType == TEXTTYPE_SINGLELINE );
293             _rxInspectorUI->enablePropertyUI( PROPERTY_FONT,            nTextType != TEXTTYPE_RICHTEXT );
294             _rxInspectorUI->enablePropertyUI( PROPERTY_ALIGN,           nTextType != TEXTTYPE_RICHTEXT );
295             _rxInspectorUI->enablePropertyUI( PROPERTY_DEFAULT_TEXT,    nTextType != TEXTTYPE_RICHTEXT );
296             _rxInspectorUI->enablePropertyUI( PROPERTY_SHOW_SCROLLBARS, nTextType != TEXTTYPE_SINGLELINE );
297             _rxInspectorUI->enablePropertyUI( PROPERTY_LINEEND_FORMAT,  nTextType != TEXTTYPE_SINGLELINE );
298             _rxInspectorUI->enablePropertyUI( PROPERTY_VERTICAL_ALIGN,  nTextType == TEXTTYPE_SINGLELINE );
299 
300             _rxInspectorUI->showCategory( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Data" ) ), nTextType != TEXTTYPE_RICHTEXT );
301         }
302         break;
303 
304         case PROPERTY_ID_MULTILINE:
305         {
306             sal_Bool bIsMultiline = sal_False;
307             _rNewValue >>= bIsMultiline;
308 
309             _rxInspectorUI->enablePropertyUI( PROPERTY_SHOW_SCROLLBARS, bIsMultiline );
310             _rxInspectorUI->enablePropertyUI( PROPERTY_ECHO_CHAR, !bIsMultiline );
311             _rxInspectorUI->enablePropertyUI( PROPERTY_LINEEND_FORMAT, bIsMultiline );
312         }
313         break;
314 
315         default:
316             OSL_ENSURE( sal_False, "EditPropertyHandler::actuatingPropertyChanged: cannot handle this id!" );
317         }
318     }
319 
320 //........................................................................
321 }   // namespace pcr
322 //........................................................................
323 
324