xref: /AOO41X/main/forms/source/component/ImageControl.cxx (revision d3e0dd8eb215533c15e891ee35bd141abe9397ee)
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_forms.hxx"
26 #include "ImageControl.hxx"
27 
28 #include "property.hrc"
29 #include "frm_resource.hrc"
30 #include "frm_resource.hxx"
31 #include "services.hxx"
32 #include "componenttools.hxx"
33 
34 #include <svtools/imageresourceaccess.hxx>
35 #include <unotools/ucblockbytes.hxx>
36 #include <sfx2/filedlghelper.hxx>
37 #include <com/sun/star/awt/XPopupMenu.hpp>
38 #include <com/sun/star/awt/PopupMenuDirection.hpp>
39 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
40 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
41 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
42 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
43 #include <com/sun/star/sdbc/DataType.hpp>
44 #include <com/sun/star/awt/MouseButton.hpp>
45 #include <com/sun/star/awt/XWindow.hpp>
46 #include <com/sun/star/awt/XDialog.hpp>
47 #include <com/sun/star/io/XActiveDataSink.hpp>
48 #include <com/sun/star/io/NotConnectedException.hpp>
49 #include <com/sun/star/beans/PropertyValue.hpp>
50 #include <com/sun/star/graphic/XGraphic.hpp>
51 #include <com/sun/star/graphic/GraphicObject.hpp>
52 #include <tools/urlobj.hxx>
53 #include <tools/stream.hxx>
54 #include <tools/debug.hxx>
55 #include <tools/diagnose_ex.h>
56 #include <vcl/svapp.hxx>
57 #include <unotools/streamhelper.hxx>
58 #include <comphelper/extract.hxx>
59 #include <comphelper/guarding.hxx>
60 #include <unotools/ucbstreamhelper.hxx>
61 #include <svl/urihelper.hxx>
62 
63 #include <memory>
64 
65 #define ID_OPEN_GRAPHICS            1
66 #define ID_CLEAR_GRAPHICS           2
67 
68 //.........................................................................
69 namespace frm
70 {
71 //.........................................................................
72 using namespace ::com::sun::star;
73 using namespace ::com::sun::star::uno;
74 using namespace ::com::sun::star::sdb;
75 using namespace ::com::sun::star::sdbc;
76 using namespace ::com::sun::star::sdbcx;
77 using namespace ::com::sun::star::beans;
78 using namespace ::com::sun::star::container;
79 using namespace ::com::sun::star::form;
80 using namespace ::com::sun::star::awt;
81 using namespace ::com::sun::star::io;
82 using namespace ::com::sun::star::ui::dialogs;
83 using namespace ::com::sun::star::lang;
84 using namespace ::com::sun::star::util;
85 using namespace ::com::sun::star::graphic;
86 using namespace ::com::sun::star::frame;
87 
88 //==============================================================================
89 //= OImageControlModel
90 //==============================================================================
91 namespace
92 {
93     enum ImageStoreType
94     {
95         ImageStoreBinary,
96         ImageStoreLink,
97 
98         ImageStoreInvalid
99     };
100 
lcl_getImageStoreType(const sal_Int32 _nFieldType)101     ImageStoreType lcl_getImageStoreType( const sal_Int32 _nFieldType )
102     {
103         // binary/longvarchar types could be used to store images in binary representation
104         if  (   ( _nFieldType == DataType::BINARY )
105             ||  ( _nFieldType == DataType::VARBINARY )
106             ||  ( _nFieldType == DataType::LONGVARBINARY )
107             ||  ( _nFieldType == DataType::OTHER )
108             ||  ( _nFieldType == DataType::OBJECT )
109             ||  ( _nFieldType == DataType::BLOB )
110             ||  ( _nFieldType == DataType::LONGVARCHAR )
111             ||  ( _nFieldType == DataType::CLOB )
112             )
113             return ImageStoreBinary;
114 
115         // char types could be used to store links to images
116         if  (   ( _nFieldType == DataType::CHAR )
117             ||  ( _nFieldType == DataType::VARCHAR )
118             )
119             return ImageStoreLink;
120 
121         return ImageStoreInvalid;
122     }
123 }
124 
125 //==============================================================================
126 // OImageControlModel
127 //==============================================================================
128 
129 //------------------------------------------------------------------------------
OImageControlModel_CreateInstance(const Reference<XMultiServiceFactory> & _rxFactory)130 InterfaceRef SAL_CALL OImageControlModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
131 {
132     return *(new OImageControlModel(_rxFactory));
133 }
134 
135 //------------------------------------------------------------------------------
_getTypes()136 Sequence<Type> OImageControlModel::_getTypes()
137 {
138     return concatSequences(
139         OBoundControlModel::_getTypes(),
140         OImageControlModel_Base::getTypes()
141     );
142 }
143 
DBG_NAME(OImageControlModel)144 DBG_NAME(OImageControlModel)
145 //------------------------------------------------------------------
146 OImageControlModel::OImageControlModel(const Reference<XMultiServiceFactory>& _rxFactory)
147     :OBoundControlModel( _rxFactory, VCL_CONTROLMODEL_IMAGECONTROL, FRM_SUN_CONTROL_IMAGECONTROL, sal_False, sal_False, sal_False )
148                     // use the old control name for compytibility reasons
149     ,m_pImageProducer( NULL )
150     ,m_bExternalGraphic( true )
151     ,m_bReadOnly( sal_False )
152     ,m_sImageURL()
153     ,m_xGraphicObject()
154 {
155     DBG_CTOR( OImageControlModel, NULL );
156     m_nClassId = FormComponentType::IMAGECONTROL;
157     initOwnValueProperty( PROPERTY_IMAGE_URL );
158 
159     implConstruct();
160 }
161 
162 //------------------------------------------------------------------
OImageControlModel(const OImageControlModel * _pOriginal,const Reference<XMultiServiceFactory> & _rxFactory)163 OImageControlModel::OImageControlModel( const OImageControlModel* _pOriginal, const Reference< XMultiServiceFactory >& _rxFactory )
164     :OBoundControlModel( _pOriginal, _rxFactory )
165                 // use the old control name for compytibility reasons
166     ,m_pImageProducer( NULL )
167     ,m_bExternalGraphic( true )
168     ,m_bReadOnly( _pOriginal->m_bReadOnly )
169     ,m_sImageURL( _pOriginal->m_sImageURL )
170     ,m_xGraphicObject( _pOriginal->m_xGraphicObject )
171 {
172     DBG_CTOR( OImageControlModel, NULL );
173     implConstruct();
174 
175     osl_incrementInterlockedCount( &m_refCount );
176     {
177         // simulate a propertyChanged event for the ImageURL
178         // 2003-05-15 - #109591# - fs@openoffice.org
179         ::osl::MutexGuard aGuard( m_aMutex );
180         impl_handleNewImageURL_lck( eOther );
181     }
182     osl_decrementInterlockedCount( &m_refCount );
183 }
184 
185 //------------------------------------------------------------------
implConstruct()186 void OImageControlModel::implConstruct()
187 {
188     m_pImageProducer = new ImageProducer;
189     m_xImageProducer = m_pImageProducer;
190     m_pImageProducer->SetDoneHdl( LINK( this, OImageControlModel, OnImageImportDone ) );
191 }
192 
193 //------------------------------------------------------------------
~OImageControlModel()194 OImageControlModel::~OImageControlModel()
195 {
196     if (!OComponentHelper::rBHelper.bDisposed)
197     {
198         acquire();
199         dispose();
200     }
201 
202     DBG_DTOR(OImageControlModel,NULL);
203 }
204 
205 // XCloneable
206 //------------------------------------------------------------------------------
IMPLEMENT_DEFAULT_CLONING(OImageControlModel)207 IMPLEMENT_DEFAULT_CLONING( OImageControlModel )
208 
209 // XServiceInfo
210 //------------------------------------------------------------------------------
211 StringSequence  OImageControlModel::getSupportedServiceNames() throw()
212 {
213     StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
214     aSupported.realloc(aSupported.getLength() + 1);
215 
216     ::rtl::OUString*pArray = aSupported.getArray();
217     pArray[aSupported.getLength()-1] = FRM_SUN_COMPONENT_IMAGECONTROL;
218     return aSupported;
219 }
220 
221 //------------------------------------------------------------------------------
queryAggregation(const Type & _rType)222 Any SAL_CALL OImageControlModel::queryAggregation(const Type& _rType) throw (RuntimeException)
223 {
224     // oder matters: we want to "override" the XImageProducer interface of the aggreate with out
225     // own XImageProducer interface, thus we need to query OImageControlModel_Base first
226     Any aReturn = OImageControlModel_Base::queryInterface( _rType );
227 
228     // BUT: _don't_ let it feel responsible for the XTypeProvider interface
229     // (as this is implemented by our base class in the proper way)
230     if  (   _rType.equals( ::getCppuType( static_cast< Reference< XTypeProvider >* >( NULL ) ) )
231         ||  !aReturn.hasValue()
232         )
233         aReturn = OBoundControlModel::queryAggregation( _rType );
234 
235     return aReturn;
236 }
237 
238 //------------------------------------------------------------------------------
approveDbColumnType(sal_Int32 _nColumnType)239 sal_Bool OImageControlModel::approveDbColumnType( sal_Int32 _nColumnType )
240 {
241     return ImageStoreInvalid != lcl_getImageStoreType( _nColumnType );
242 }
243 
244 //------------------------------------------------------------------------------
getFastPropertyValue(Any & rValue,sal_Int32 nHandle) const245 void OImageControlModel::getFastPropertyValue(Any& rValue, sal_Int32 nHandle) const
246 {
247     switch (nHandle)
248     {
249         case PROPERTY_ID_READONLY:
250             rValue <<= (sal_Bool)m_bReadOnly;
251             break;
252         case PROPERTY_ID_IMAGE_URL:
253             rValue <<= m_sImageURL;
254             break;
255         case PROPERTY_ID_GRAPHIC:
256             rValue <<= m_xGraphicObject.is() ? m_xGraphicObject->getGraphic() : Reference< XGraphic >();
257             break;
258         default:
259             OBoundControlModel::getFastPropertyValue(rValue, nHandle);
260     }
261 }
262 
263 //------------------------------------------------------------------------------
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any & rValue)264 void OImageControlModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const Any& rValue) throw ( ::com::sun::star::uno::Exception)
265 {
266     switch (nHandle)
267     {
268         case PROPERTY_ID_READONLY :
269             DBG_ASSERT(rValue.getValueType().getTypeClass() == TypeClass_BOOLEAN, "OImageControlModel::setFastPropertyValue_NoBroadcast : invalid type !" );
270             m_bReadOnly = getBOOL(rValue);
271             break;
272 
273         case PROPERTY_ID_IMAGE_URL:
274             OSL_VERIFY( rValue >>= m_sImageURL );
275             impl_handleNewImageURL_lck( eOther );
276             {
277                 ControlModelLock aLock( *this );
278                     // that's a fake ... onValuePropertyChange expects to receive the only lock to our instance,
279                     // but we're already called with our mutex locked ...
280                 onValuePropertyChange( aLock );
281             }
282             break;
283 
284         case PROPERTY_ID_GRAPHIC:
285         {
286             Reference< XGraphic > xGraphic;
287             OSL_VERIFY( rValue >>= xGraphic );
288             if ( !xGraphic.is() )
289                 m_xGraphicObject.clear();
290             else
291             {
292                 m_xGraphicObject = GraphicObject::create( m_aContext.getUNOContext() );
293                 m_xGraphicObject->setGraphic( xGraphic );
294             }
295 
296             if ( m_bExternalGraphic )
297             {
298                 // if that's an external graphic, i.e. one which has not been loaded by ourselves in response to a
299                 // new image URL, then also adjust our ImageURL.
300                 ::rtl::OUString sNewImageURL;
301                 if ( m_xGraphicObject.is() )
302                 {
303                     sNewImageURL = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.GraphicObject:" ) );
304                     sNewImageURL = sNewImageURL + m_xGraphicObject->getUniqueID();
305                 }
306                 m_sImageURL = sNewImageURL;
307                 // TODO: speaking strictly, this would need to be notified, since ImageURL is a bound property. However,
308                 // this method here is called with a locked mutex, so we cannot simply call listeners ...
309                 // I think the missing notification (and thus clients which potentially cannot observe the change)
310                 // is less severe than the potential deadlock ...
311             }
312         }
313         break;
314 
315         default:
316             OBoundControlModel::setFastPropertyValue_NoBroadcast(nHandle, rValue);
317             break;
318     }
319 }
320 
321 //------------------------------------------------------------------------------
convertFastPropertyValue(Any & rConvertedValue,Any & rOldValue,sal_Int32 nHandle,const Any & rValue)322 sal_Bool OImageControlModel::convertFastPropertyValue(Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle, const Any& rValue)
323                                 throw( IllegalArgumentException )
324 {
325     switch (nHandle)
326     {
327         case PROPERTY_ID_READONLY :
328             return tryPropertyValue(rConvertedValue, rOldValue, rValue, m_bReadOnly);
329 
330         case PROPERTY_ID_IMAGE_URL:
331             return tryPropertyValue( rConvertedValue, rOldValue, rValue, m_sImageURL );
332 
333         case PROPERTY_ID_GRAPHIC:
334         {
335             const Reference< XGraphic > xGraphic( getFastPropertyValue( PROPERTY_ID_GRAPHIC ), UNO_QUERY );
336             return tryPropertyValue( rConvertedValue, rOldValue, rValue, xGraphic );
337         }
338 
339         default:
340             return OBoundControlModel::convertFastPropertyValue(rConvertedValue, rOldValue, nHandle, rValue);
341     }
342 }
343 
344 //------------------------------------------------------------------------------
describeFixedProperties(Sequence<Property> & _rProps) const345 void OImageControlModel::describeFixedProperties( Sequence< Property >& _rProps ) const
346 {
347     BEGIN_DESCRIBE_PROPERTIES( 4, OBoundControlModel )
348         DECL_IFACE_PROP2( GRAPHIC,   XGraphic,        BOUND, TRANSIENT );
349         DECL_PROP1      ( IMAGE_URL, ::rtl::OUString, BOUND );
350         DECL_BOOL_PROP1 ( READONLY,                   BOUND );
351         DECL_PROP1      ( TABINDEX,  sal_Int16,       BOUND );
352     END_DESCRIBE_PROPERTIES();
353 }
354 
355 //------------------------------------------------------------------------------
describeAggregateProperties(Sequence<Property> & o_rAggregateProperties) const356 void OImageControlModel::describeAggregateProperties( Sequence< Property >& /* [out] */ o_rAggregateProperties ) const
357 {
358     OBoundControlModel::describeAggregateProperties( o_rAggregateProperties );
359     // remove ImageULR and Graphic properties, we "overload" them. This is because our aggregate synchronizes those
360     // two, but we have an own sychronization mechanism.
361     RemoveProperty( o_rAggregateProperties, PROPERTY_IMAGE_URL );
362     RemoveProperty( o_rAggregateProperties, PROPERTY_GRAPHIC );
363 }
364 
365 //------------------------------------------------------------------------------
getServiceName()366 ::rtl::OUString OImageControlModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException)
367 {
368     return FRM_COMPONENT_IMAGECONTROL;  // old (non-sun) name for compatibility !
369 }
370 
371 //------------------------------------------------------------------------------
write(const Reference<XObjectOutputStream> & _rxOutStream)372 void OImageControlModel::write(const Reference<XObjectOutputStream>& _rxOutStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
373 {
374     // Basisklasse
375     OBoundControlModel::write(_rxOutStream);
376     // Version
377     _rxOutStream->writeShort(0x0003);
378     // Name
379     _rxOutStream->writeBoolean(m_bReadOnly);
380     writeHelpTextCompatibly(_rxOutStream);
381     // from version 0x0003 : common properties
382     writeCommonProperties(_rxOutStream);
383 }
384 
385 //------------------------------------------------------------------------------
read(const Reference<XObjectInputStream> & _rxInStream)386 void OImageControlModel::read(const Reference<XObjectInputStream>& _rxInStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
387 {
388     OBoundControlModel::read(_rxInStream);
389 
390     // Version
391     sal_uInt16 nVersion = _rxInStream->readShort();
392     switch (nVersion)
393     {
394         case 0x0001:
395             m_bReadOnly = _rxInStream->readBoolean();
396             break;
397         case 0x0002:
398             m_bReadOnly = _rxInStream->readBoolean();
399             readHelpTextCompatibly(_rxInStream);
400             break;
401         case 0x0003:
402             m_bReadOnly = _rxInStream->readBoolean();
403             readHelpTextCompatibly(_rxInStream);
404             readCommonProperties(_rxInStream);
405             break;
406         default :
407             DBG_ERROR("OImageControlModel::read : unknown version !");
408             m_bReadOnly = sal_False;
409             defaultCommonProperties();
410             break;
411     }
412     // Nach dem Lesen die Defaultwerte anzeigen
413     if ( getControlSource().getLength() )
414     {   // (not if we don't have a control source - the "State" property acts like it is persistent, then
415         ::osl::MutexGuard aGuard(m_aMutex); // resetNoBroadcast expects this mutex guarding
416         resetNoBroadcast();
417     }
418 }
419 
420 //------------------------------------------------------------------------------
impl_updateStreamForURL_lck(const::rtl::OUString & _rURL,ValueChangeInstigator _eInstigator)421 sal_Bool OImageControlModel::impl_updateStreamForURL_lck( const ::rtl::OUString& _rURL, ValueChangeInstigator _eInstigator )
422 {
423     // create a stream for the image specified by the URL
424     ::std::auto_ptr< SvStream > pImageStream;
425     Reference< XInputStream > xImageStream;
426 
427     if ( ::svt::GraphicAccess::isSupportedURL( _rURL ) )
428     {
429         xImageStream = ::svt::GraphicAccess::getImageXStream( getContext().getLegacyServiceFactory(), _rURL );
430     }
431     else
432     {
433         pImageStream.reset( ::utl::UcbStreamHelper::CreateStream( _rURL, STREAM_READ ) );
434         sal_Bool bSetNull = ( pImageStream.get() == NULL ) || ( ERRCODE_NONE != pImageStream->GetErrorCode() );
435 
436         if ( !bSetNull )
437         {
438             // get the size of the stream
439             pImageStream->Seek(STREAM_SEEK_TO_END);
440             sal_Int32 nSize = (sal_Int32)pImageStream->Tell();
441             if (pImageStream->GetBufferSize() < 8192)
442                 pImageStream->SetBufferSize(8192);
443             pImageStream->Seek(STREAM_SEEK_TO_BEGIN);
444 
445             xImageStream = new ::utl::OInputStreamHelper( new SvLockBytes( pImageStream.get(), sal_False ), nSize );
446         }
447     }
448 
449     if ( xImageStream.is() )
450     {
451         if ( m_xColumnUpdate.is() )
452             m_xColumnUpdate->updateBinaryStream( xImageStream, xImageStream->available() );
453         else
454             setControlValue( makeAny( xImageStream ), _eInstigator );
455         xImageStream->closeInput();
456         return sal_True;
457     }
458 
459     return sal_False;
460 }
461 
462 //------------------------------------------------------------------------------
impl_handleNewImageURL_lck(ValueChangeInstigator _eInstigator)463 sal_Bool OImageControlModel::impl_handleNewImageURL_lck( ValueChangeInstigator _eInstigator )
464 {
465     switch ( lcl_getImageStoreType( getFieldType() ) )
466     {
467     case ImageStoreBinary:
468         if ( impl_updateStreamForURL_lck( m_sImageURL, _eInstigator ) )
469             return sal_True;
470         break;
471 
472     case ImageStoreLink:
473     {
474         ::rtl::OUString sCommitURL( m_sImageURL );
475         if ( m_sDocumentURL.getLength() )
476             sCommitURL = URIHelper::simpleNormalizedMakeRelative( m_sDocumentURL, sCommitURL );
477         OSL_ENSURE( m_xColumnUpdate.is(), "OImageControlModel::impl_handleNewImageURL_lck: no bound field, but ImageStoreLink?!" );
478         if ( m_xColumnUpdate.is() )
479         {
480             m_xColumnUpdate->updateString( sCommitURL );
481             return sal_True;
482         }
483     }
484     break;
485 
486     case ImageStoreInvalid:
487         OSL_ENSURE( false, "OImageControlModel::impl_handleNewImageURL_lck: image storage type type!" );
488         break;
489     }
490 
491     // if we're here, then the above code was unable to update our field/control from the given URL
492     // => fall back to NULL/VOID
493     if ( m_xColumnUpdate.is() )
494         m_xColumnUpdate->updateNull();
495     else
496         setControlValue( Any(), _eInstigator );
497 
498     return sal_True;
499 }
500 
501 //------------------------------------------------------------------------------
commitControlValueToDbColumn(bool _bPostReset)502 sal_Bool OImageControlModel::commitControlValueToDbColumn( bool _bPostReset )
503 {
504     if ( _bPostReset )
505     {
506         // since this is a "commit after reset", we can simply update the column
507         // with null - this is our "default" which we were just reset to
508         if ( m_xColumnUpdate.is() )
509             m_xColumnUpdate->updateNull();
510     }
511     else
512     {
513         ::osl::MutexGuard aGuard(m_aMutex);
514         return impl_handleNewImageURL_lck( eDbColumnBinding );
515     }
516 
517     return sal_True;
518 }
519 
520 //------------------------------------------------------------------------------
521 namespace
522 {
lcl_isValidDocumentURL(const::rtl::OUString & _rDocURL)523     bool lcl_isValidDocumentURL( const ::rtl::OUString& _rDocURL )
524     {
525         return ( _rDocURL.getLength() && !_rDocURL.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:object" ) ) );
526     }
527 }
528 
529 //------------------------------------------------------------------------------
onConnectedDbColumn(const Reference<XInterface> & _rxForm)530 void OImageControlModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
531 {
532     OBoundControlModel::onConnectedDbColumn( _rxForm );
533 
534     try
535     {
536         Reference< XModel > xDocument( getXModel( *this ) );
537         if ( xDocument.is() )
538         {
539             m_sDocumentURL = xDocument->getURL();
540             if ( !lcl_isValidDocumentURL( m_sDocumentURL ) )
541             {
542                 Reference< XChild > xAsChild( xDocument, UNO_QUERY );
543                 while ( xAsChild.is() && !lcl_isValidDocumentURL( m_sDocumentURL ) )
544                 {
545                     xDocument.set( xAsChild->getParent(), UNO_QUERY );
546                     if ( xDocument.is() )
547                         m_sDocumentURL = xDocument->getURL();
548                     xAsChild.set( xDocument, UNO_QUERY );
549                 }
550             }
551         }
552     }
553     catch( const Exception& )
554     {
555         DBG_UNHANDLED_EXCEPTION();
556     }
557 }
558 
559 //------------------------------------------------------------------------------
onDisconnectedDbColumn()560 void OImageControlModel::onDisconnectedDbColumn()
561 {
562     OBoundControlModel::onDisconnectedDbColumn();
563 
564     m_sDocumentURL = ::rtl::OUString();
565 }
566 
567 //------------------------------------------------------------------------------
translateDbColumnToControlValue()568 Any OImageControlModel::translateDbColumnToControlValue()
569 {
570     switch ( lcl_getImageStoreType( getFieldType() ) )
571     {
572     case ImageStoreBinary:
573     {
574         Reference< XInputStream > xImageStream( m_xColumn->getBinaryStream() );
575         if ( m_xColumn->wasNull() )
576             xImageStream.clear();
577         return makeAny( xImageStream );
578     }
579     case ImageStoreLink:
580     {
581         ::rtl::OUString sImageLink( m_xColumn->getString() );
582         if ( m_sDocumentURL.getLength() )
583             sImageLink = INetURLObject::GetAbsURL( m_sDocumentURL, sImageLink );
584         return makeAny( sImageLink );
585     }
586     case ImageStoreInvalid:
587         OSL_ENSURE( false, "OImageControlModel::translateDbColumnToControlValue: invalid field type!" );
588         break;
589     }
590     return Any();
591 }
592 
593 //------------------------------------------------------------------------------
getControlValue() const594 Any OImageControlModel::getControlValue( ) const
595 {
596     return makeAny( m_sImageURL );
597 }
598 
599 //------------------------------------------------------------------------------
doSetControlValue(const Any & _rValue)600 void OImageControlModel::doSetControlValue( const Any& _rValue )
601 {
602     DBG_ASSERT( GetImageProducer() && m_xImageProducer.is(), "OImageControlModel::doSetControlValue: no image producer!" );
603     if ( !GetImageProducer() || !m_xImageProducer.is() )
604         return;
605 
606     bool bStartProduction = false;
607     switch ( lcl_getImageStoreType( getFieldType() ) )
608     {
609     case ImageStoreBinary:
610     {
611         // give the image producer the stream
612         Reference< XInputStream > xInStream;
613         _rValue >>= xInStream;
614         GetImageProducer()->setImage( xInStream );
615         bStartProduction = true;
616     }
617     break;
618 
619     case ImageStoreLink:
620     {
621         ::rtl::OUString sImageURL;
622         _rValue >>= sImageURL;
623         GetImageProducer()->SetImage( sImageURL );
624         bStartProduction = true;
625     }
626     break;
627 
628     case ImageStoreInvalid:
629         OSL_ENSURE( false, "OImageControlModel::doSetControlValue: invalid field type!" );
630         break;
631 
632     }   // switch ( lcl_getImageStoreType( getFieldType() ) )
633 
634     if ( bStartProduction )
635     {
636         // start production
637         Reference< XImageProducer > xProducer = m_xImageProducer;
638         {
639             // release our mutex once (it's acquired in the calling method!), as starting the image production may
640             // result in the locking of the solar mutex (unfortunally the default implementation of our aggregate,
641             // VCLXImageControl, does this locking)
642             // FS - 74438 - 30.03.00
643             MutexRelease aRelease(m_aMutex);
644             xProducer->startProduction();
645         }
646     }
647 }
648 
649 // OComponentHelper
650 //------------------------------------------------------------------
disposing()651 void SAL_CALL OImageControlModel::disposing()
652 {
653     OBoundControlModel::disposing();
654 }
655 
656 //------------------------------------------------------------------------------
resetNoBroadcast()657 void OImageControlModel::resetNoBroadcast()
658 {
659     if ( hasField() )          // only reset when we are connected to a column
660         OBoundControlModel::resetNoBroadcast( );
661 }
662 
663 //--------------------------------------------------------------------
getImageProducer()664 Reference< XImageProducer > SAL_CALL OImageControlModel::getImageProducer() throw ( RuntimeException)
665 {
666     return this;
667 }
668 
669 //--------------------------------------------------------------------
addConsumer(const Reference<XImageConsumer> & _rxConsumer)670 void SAL_CALL OImageControlModel::addConsumer( const Reference< XImageConsumer >& _rxConsumer ) throw (RuntimeException)
671 {
672     GetImageProducer()->addConsumer( _rxConsumer );
673 }
674 
675 //--------------------------------------------------------------------
removeConsumer(const Reference<XImageConsumer> & _rxConsumer)676 void SAL_CALL OImageControlModel::removeConsumer( const Reference< XImageConsumer >& _rxConsumer ) throw (RuntimeException)
677 {
678     GetImageProducer()->removeConsumer( _rxConsumer );
679 }
680 
681 //--------------------------------------------------------------------
startProduction()682 void SAL_CALL OImageControlModel::startProduction(  ) throw (RuntimeException)
683 {
684     GetImageProducer()->startProduction();
685 }
686 
687 //------------------------------------------------------------------------------
IMPL_LINK(OImageControlModel,OnImageImportDone,::Graphic *,i_pGraphic)688 IMPL_LINK( OImageControlModel, OnImageImportDone, ::Graphic*, i_pGraphic )
689 {
690     const Reference< XGraphic > xGraphic( i_pGraphic != NULL ? Image( i_pGraphic->GetBitmapEx() ).GetXGraphic() : NULL );
691     m_bExternalGraphic = false;
692     try
693     {
694         setPropertyValue( PROPERTY_GRAPHIC, makeAny( xGraphic ) );
695     }
696     catch ( const Exception& )
697     {
698         DBG_UNHANDLED_EXCEPTION();
699     }
700     m_bExternalGraphic = true;
701     return 1L;
702 }
703 
704 //==================================================================
705 // OImageControlControl
706 //==================================================================
707 
708 //------------------------------------------------------------------
OImageControlControl_CreateInstance(const Reference<XMultiServiceFactory> & _rxFactory)709 InterfaceRef SAL_CALL OImageControlControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
710 {
711     return *(new OImageControlControl(_rxFactory));
712 }
713 
714 //------------------------------------------------------------------------------
_getTypes()715 Sequence<Type> OImageControlControl::_getTypes()
716 {
717     return concatSequences(
718         OBoundControl::_getTypes(),
719         OImageControlControl_Base::getTypes()
720     );
721 }
722 
723 //------------------------------------------------------------------------------
OImageControlControl(const Reference<XMultiServiceFactory> & _rxFactory)724 OImageControlControl::OImageControlControl(const Reference<XMultiServiceFactory>& _rxFactory)
725     :OBoundControl(_rxFactory, VCL_CONTROL_IMAGECONTROL)
726     ,m_aModifyListeners( m_aMutex )
727 {
728     increment(m_refCount);
729     {
730         // als Focus- und MouseListener anmelden
731         Reference< XWindow > xComp;
732         query_aggregation( m_xAggregate, xComp );
733         if ( xComp.is() )
734             xComp->addMouseListener( this );
735     }
736     decrement(m_refCount);
737 }
738 
739 //------------------------------------------------------------------------------
queryAggregation(const Type & _rType)740 Any SAL_CALL OImageControlControl::queryAggregation(const Type& _rType) throw (RuntimeException)
741 {
742     Any aReturn = OBoundControl::queryAggregation( _rType );
743     if ( !aReturn.hasValue() )
744         aReturn = ::cppu::queryInterface(
745             _rType,
746             static_cast< XMouseListener* >( this ),
747             static_cast< XModifyBroadcaster* >( this )
748         );
749 
750     return aReturn;
751 }
752 
753 //------------------------------------------------------------------------------
getSupportedServiceNames()754 StringSequence  OImageControlControl::getSupportedServiceNames() throw()
755 {
756     StringSequence aSupported = OBoundControl::getSupportedServiceNames();
757     aSupported.realloc(aSupported.getLength() + 1);
758 
759     ::rtl::OUString*pArray = aSupported.getArray();
760     pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_IMAGECONTROL;
761     return aSupported;
762 }
763 
764 //------------------------------------------------------------------------------
addModifyListener(const Reference<XModifyListener> & _Listener)765 void SAL_CALL OImageControlControl::addModifyListener( const Reference< XModifyListener >& _Listener ) throw (RuntimeException)
766 {
767     m_aModifyListeners.addInterface( _Listener );
768 }
769 
770 //------------------------------------------------------------------------------
removeModifyListener(const Reference<XModifyListener> & _Listener)771 void SAL_CALL OImageControlControl::removeModifyListener( const Reference< XModifyListener >& _Listener ) throw (RuntimeException)
772 {
773     m_aModifyListeners.removeInterface( _Listener );
774 }
775 
776 //------------------------------------------------------------------------------
disposing()777 void SAL_CALL OImageControlControl::disposing()
778 {
779     EventObject aEvent( *this );
780     m_aModifyListeners.disposeAndClear( aEvent );
781 
782     OBoundControl::disposing();
783 }
784 
785 //------------------------------------------------------------------------------
disposing(const EventObject & _Event)786 void SAL_CALL OImageControlControl::disposing( const EventObject& _Event ) throw(RuntimeException)
787 {
788     OBoundControl::disposing( _Event );
789 }
790 
791 //------------------------------------------------------------------------------
implClearGraphics(sal_Bool _bForce)792 void OImageControlControl::implClearGraphics( sal_Bool _bForce )
793 {
794     Reference< XPropertySet > xSet( getModel(), UNO_QUERY );
795     if ( xSet.is() )
796     {
797         if ( _bForce )
798         {
799             ::rtl::OUString sOldImageURL;
800             xSet->getPropertyValue( PROPERTY_IMAGE_URL ) >>= sOldImageURL;
801 
802             if ( !sOldImageURL.getLength() )
803                 // the ImageURL is already empty, so simply setting a new empty one would not suffice
804                 // (since it would be ignored)
805                 xSet->setPropertyValue( PROPERTY_IMAGE_URL, makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "private:emptyImage" ) ) ) );
806                     // (the concrete URL we're passing here doens't matter. It's important that
807                     // the model cannot resolve it to a a valid resource describing an image stream
808         }
809 
810         xSet->setPropertyValue( PROPERTY_IMAGE_URL, makeAny( ::rtl::OUString() ) );
811     }
812 }
813 
814 //------------------------------------------------------------------------------
implInsertGraphics()815 bool OImageControlControl::implInsertGraphics()
816 {
817     Reference< XPropertySet > xSet( getModel(), UNO_QUERY );
818     if ( !xSet.is() )
819         return false;
820 
821     ::rtl::OUString sTitle = FRM_RES_STRING(RID_STR_IMPORT_GRAPHIC);
822     // build some arguments for the upcoming dialog
823     try
824     {
825         ::sfx2::FileDialogHelper aDialog( TemplateDescription::FILEOPEN_LINK_PREVIEW, SFXWB_GRAPHIC );
826         aDialog.SetTitle( sTitle );
827 
828         Reference< XFilePickerControlAccess > xController( aDialog.GetFilePicker(), UNO_QUERY_THROW );
829         xController->setValue(ExtendedFilePickerElementIds::CHECKBOX_PREVIEW, 0, ::cppu::bool2any(sal_True));
830 
831         Reference<XPropertySet> xBoundField;
832         if ( hasProperty( PROPERTY_BOUNDFIELD, xSet ) )
833             xSet->getPropertyValue( PROPERTY_BOUNDFIELD ) >>= xBoundField;
834         sal_Bool bHasField = xBoundField.is();
835 
836         // if the control is bound to a DB field, then it's not possible to decide whether or not to link
837         xController->enableControl(ExtendedFilePickerElementIds::CHECKBOX_LINK, !bHasField );
838 
839         // if the control is bound to a DB field, then linking of the image depends on the type of the field
840         sal_Bool bImageIsLinked = sal_True;
841         if ( bHasField )
842         {
843             sal_Int32 nFieldType = DataType::OTHER;
844             OSL_VERIFY( xBoundField->getPropertyValue( PROPERTY_FIELDTYPE ) >>= nFieldType );
845             bImageIsLinked = ( lcl_getImageStoreType( nFieldType ) == ImageStoreLink );
846         }
847         xController->setValue(ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, makeAny( bImageIsLinked ) );
848 
849         if ( ERRCODE_NONE == aDialog.Execute() )
850         {
851             // reset the url property in case it already has the value we're about to set - in this case
852             // our propertyChanged would not get called without this.
853             implClearGraphics( sal_False );
854             sal_Bool bIsLink = sal_False;
855             xController->getValue(ExtendedFilePickerElementIds::CHECKBOX_LINK, 0) >>= bIsLink;
856             // Force bIsLink to be sal_True if we're bound to a field. Though we initialized the file picker with IsLink=TRUE
857             // in this case, and disabled the respective control, there might be picker implementations which do not
858             // respect this, and return IsLink=FALSE here. In this case, "normalize" the flag.
859             // #i112659# / 2010-08-26 / frank.schoenheit@oracle.com
860             bIsLink |= bHasField;
861             if ( !bIsLink )
862             {
863                 Graphic aGraphic;
864                 aDialog.GetGraphic( aGraphic );
865                 xSet->setPropertyValue( PROPERTY_GRAPHIC, makeAny( aGraphic.GetXGraphic() ) );
866             }
867             else
868                 xSet->setPropertyValue( PROPERTY_IMAGE_URL, makeAny( ::rtl::OUString( aDialog.GetPath() ) ) );
869 
870             return true;
871         }
872     }
873     catch(Exception&)
874     {
875         DBG_ERROR("OImageControlControl::implInsertGraphics: caught an exception while attempting to execute the FilePicker!");
876     }
877     return false;
878 }
879 
880 //------------------------------------------------------------------------------
impl_isEmptyGraphics_nothrow() const881 bool OImageControlControl::impl_isEmptyGraphics_nothrow() const
882 {
883     bool bIsEmpty = true;
884 
885     try
886     {
887         Reference< XPropertySet > xModelProps( const_cast< OImageControlControl* >( this )->getModel(), UNO_QUERY_THROW );
888         Reference< XGraphic > xGraphic;
889         OSL_VERIFY( xModelProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Graphic" ) ) ) >>= xGraphic );
890         bIsEmpty = !xGraphic.is();
891     }
892     catch( const Exception& )
893     {
894         DBG_UNHANDLED_EXCEPTION();
895     }
896 
897     return bIsEmpty;
898 }
899 
900 // MouseListener
901 //------------------------------------------------------------------------------
mousePressed(const::com::sun::star::awt::MouseEvent & e)902 void OImageControlControl::mousePressed(const ::com::sun::star::awt::MouseEvent& e) throw ( ::com::sun::star::uno::RuntimeException)
903 {
904     ::vos::OGuard aGuard( Application::GetSolarMutex() );
905 
906     if (e.Buttons != MouseButton::LEFT)
907         return;
908 
909     bool bModified = false;
910     // is this a request for a context menu?
911     if ( e.PopupTrigger )
912     {
913         Reference< XPopupMenu > xMenu( m_aContext.createComponent( "com.sun.star.awt.PopupMenu" ), UNO_QUERY );
914         DBG_ASSERT( xMenu.is(), "OImageControlControl::mousePressed: could not create a popup menu!" );
915 
916         Reference< XWindowPeer > xWindowPeer = getPeer();
917         DBG_ASSERT( xWindowPeer.is(), "OImageControlControl::mousePressed: no window!" );
918 
919         if ( xMenu.is() && xWindowPeer.is() )
920         {
921             xMenu->insertItem( ID_OPEN_GRAPHICS, FRM_RES_STRING( RID_STR_OPEN_GRAPHICS ), 0, 0 );
922             xMenu->insertItem( ID_CLEAR_GRAPHICS, FRM_RES_STRING( RID_STR_CLEAR_GRAPHICS ), 0, 1 );
923 
924             // check if the ImageURL is empty
925             if ( impl_isEmptyGraphics_nothrow() )
926                 xMenu->enableItem( ID_CLEAR_GRAPHICS, sal_False );
927 
928             awt::Rectangle aRect( e.X, e.Y, 0, 0 );
929             if ( ( e.X < 0 ) || ( e.Y < 0 ) )
930             {   // context menu triggered by keyboard
931                 // position it in the center of the control
932                 // 102205 - 16.08.2002 - fs@openoffice.org
933                 Reference< XWindow > xWindow( static_cast< ::cppu::OWeakObject* >( this ), UNO_QUERY );
934                 OSL_ENSURE( xWindow.is(), "OImageControlControl::mousePressed: me not a window? How this?" );
935                 if ( xWindow.is() )
936                 {
937                     awt::Rectangle aPosSize = xWindow->getPosSize();
938                     aRect.X = aPosSize.Width / 2;
939                     aRect.Y = aPosSize.Height / 2;
940                 }
941             }
942 
943             const sal_Int16 nResult = xMenu->execute( xWindowPeer, aRect, PopupMenuDirection::EXECUTE_DEFAULT );
944 
945             switch ( nResult )
946             {
947             case ID_OPEN_GRAPHICS:
948                 implInsertGraphics();
949                 bModified = true;
950                 break;
951 
952             case ID_CLEAR_GRAPHICS:
953                 implClearGraphics( sal_True );
954                 bModified = true;
955                 break;
956             }
957         }
958     }
959     else
960     {
961         //////////////////////////////////////////////////////////////////////
962         // Doppelclick
963         if (e.ClickCount == 2)
964         {
965 
966             Reference<XPropertySet>  xSet(getModel(), UNO_QUERY);
967             if (!xSet.is())
968                 return;
969 
970             // wenn Control nicht gebunden ist, kein Dialog (da die zu schickende URL hinterher sowieso
971             // versanden wuerde)
972             // FS - #64946# - 19.04.99
973             Reference<XPropertySet> xBoundField;
974             if (hasProperty(PROPERTY_BOUNDFIELD, xSet))
975                 ::cppu::extractInterface(xBoundField, xSet->getPropertyValue(PROPERTY_BOUNDFIELD));
976             if (!xBoundField.is())
977             {
978                 // but only if our IMAGE_URL property is handled as if it is transient, which is equivalent to
979                 // an empty control source
980                 if (!hasProperty(PROPERTY_CONTROLSOURCE, xSet) || (::comphelper::getString(xSet->getPropertyValue(PROPERTY_CONTROLSOURCE)).getLength() != 0))
981                     return;
982             }
983 
984             sal_Bool bReadOnly = false;
985             xSet->getPropertyValue(PROPERTY_READONLY) >>= bReadOnly;
986             if (bReadOnly)
987                 return;
988 
989             if ( implInsertGraphics() )
990                 bModified = true;
991         }
992     }
993 
994     if ( bModified )
995     {
996         EventObject aEvent( *this );
997         m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
998     }
999 }
1000 
1001 //------------------------------------------------------------------------------
mouseReleased(const awt::MouseEvent &)1002 void SAL_CALL OImageControlControl::mouseReleased(const awt::MouseEvent& /*e*/) throw ( RuntimeException )
1003 {
1004 }
1005 
1006 //------------------------------------------------------------------------------
mouseEntered(const awt::MouseEvent &)1007 void SAL_CALL OImageControlControl::mouseEntered(const awt::MouseEvent& /*e*/) throw ( RuntimeException )
1008 {
1009 }
1010 
1011 //------------------------------------------------------------------------------
mouseExited(const awt::MouseEvent &)1012 void SAL_CALL OImageControlControl::mouseExited(const awt::MouseEvent& /*e*/) throw ( RuntimeException )
1013 {
1014 }
1015 
1016 //.........................................................................
1017 }   // namespace frm
1018 //.........................................................................
1019 
1020