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)
223 {
224 // order matters: we want to "override" the XImageProducer interface of the aggregate 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)
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 {
324 switch (nHandle)
325 {
326 case PROPERTY_ID_READONLY :
327 return tryPropertyValue(rConvertedValue, rOldValue, rValue, m_bReadOnly);
328
329 case PROPERTY_ID_IMAGE_URL:
330 return tryPropertyValue( rConvertedValue, rOldValue, rValue, m_sImageURL );
331
332 case PROPERTY_ID_GRAPHIC:
333 {
334 const Reference< XGraphic > xGraphic( getFastPropertyValue( PROPERTY_ID_GRAPHIC ), UNO_QUERY );
335 return tryPropertyValue( rConvertedValue, rOldValue, rValue, xGraphic );
336 }
337
338 default:
339 return OBoundControlModel::convertFastPropertyValue(rConvertedValue, rOldValue, nHandle, rValue);
340 }
341 }
342
343 //------------------------------------------------------------------------------
describeFixedProperties(Sequence<Property> & _rProps) const344 void OImageControlModel::describeFixedProperties( Sequence< Property >& _rProps ) const
345 {
346 BEGIN_DESCRIBE_PROPERTIES( 4, OBoundControlModel )
347 DECL_IFACE_PROP2( GRAPHIC, XGraphic, BOUND, TRANSIENT );
348 DECL_PROP1 ( IMAGE_URL, ::rtl::OUString, BOUND );
349 DECL_BOOL_PROP1 ( READONLY, BOUND );
350 DECL_PROP1 ( TABINDEX, sal_Int16, BOUND );
351 END_DESCRIBE_PROPERTIES();
352 }
353
354 //------------------------------------------------------------------------------
describeAggregateProperties(Sequence<Property> & o_rAggregateProperties) const355 void OImageControlModel::describeAggregateProperties( Sequence< Property >& /* [out] */ o_rAggregateProperties ) const
356 {
357 OBoundControlModel::describeAggregateProperties( o_rAggregateProperties );
358 // remove ImageULR and Graphic properties, we "overload" them. This is because our aggregate synchronizes those
359 // two, but we have an own sychronization mechanism.
360 RemoveProperty( o_rAggregateProperties, PROPERTY_IMAGE_URL );
361 RemoveProperty( o_rAggregateProperties, PROPERTY_GRAPHIC );
362 }
363
364 //------------------------------------------------------------------------------
getServiceName()365 ::rtl::OUString OImageControlModel::getServiceName()
366 {
367 return FRM_COMPONENT_IMAGECONTROL; // old (non-sun) name for compatibility !
368 }
369
370 //------------------------------------------------------------------------------
write(const Reference<XObjectOutputStream> & _rxOutStream)371 void OImageControlModel::write(const Reference<XObjectOutputStream>& _rxOutStream)
372 {
373 // Basisklasse
374 OBoundControlModel::write(_rxOutStream);
375 // Version
376 _rxOutStream->writeShort(0x0003);
377 // Name
378 _rxOutStream->writeBoolean(m_bReadOnly);
379 writeHelpTextCompatibly(_rxOutStream);
380 // from version 0x0003 : common properties
381 writeCommonProperties(_rxOutStream);
382 }
383
384 //------------------------------------------------------------------------------
read(const Reference<XObjectInputStream> & _rxInStream)385 void OImageControlModel::read(const Reference<XObjectInputStream>& _rxInStream)
386 {
387 OBoundControlModel::read(_rxInStream);
388
389 // Version
390 sal_uInt16 nVersion = _rxInStream->readShort();
391 switch (nVersion)
392 {
393 case 0x0001:
394 m_bReadOnly = _rxInStream->readBoolean();
395 break;
396 case 0x0002:
397 m_bReadOnly = _rxInStream->readBoolean();
398 readHelpTextCompatibly(_rxInStream);
399 break;
400 case 0x0003:
401 m_bReadOnly = _rxInStream->readBoolean();
402 readHelpTextCompatibly(_rxInStream);
403 readCommonProperties(_rxInStream);
404 break;
405 default :
406 DBG_ERROR("OImageControlModel::read : unknown version !");
407 m_bReadOnly = sal_False;
408 defaultCommonProperties();
409 break;
410 }
411 // Nach dem Lesen die Defaultwerte anzeigen
412 if ( getControlSource().getLength() )
413 { // (not if we don't have a control source - the "State" property acts like it is persistent, then
414 ::osl::MutexGuard aGuard(m_aMutex); // resetNoBroadcast expects this mutex guarding
415 resetNoBroadcast();
416 }
417 }
418
419 //------------------------------------------------------------------------------
impl_updateStreamForURL_lck(const::rtl::OUString & _rURL,ValueChangeInstigator _eInstigator)420 sal_Bool OImageControlModel::impl_updateStreamForURL_lck( const ::rtl::OUString& _rURL, ValueChangeInstigator _eInstigator )
421 {
422 // create a stream for the image specified by the URL
423 ::std::auto_ptr< SvStream > pImageStream;
424 Reference< XInputStream > xImageStream;
425
426 if ( ::svt::GraphicAccess::isSupportedURL( _rURL ) )
427 {
428 xImageStream = ::svt::GraphicAccess::getImageXStream( getContext().getLegacyServiceFactory(), _rURL );
429 }
430 else
431 {
432 pImageStream.reset( ::utl::UcbStreamHelper::CreateStream( _rURL, STREAM_READ ) );
433 sal_Bool bSetNull = ( pImageStream.get() == NULL ) || ( ERRCODE_NONE != pImageStream->GetErrorCode() );
434
435 if ( !bSetNull )
436 {
437 // get the size of the stream
438 pImageStream->Seek(STREAM_SEEK_TO_END);
439 sal_Int32 nSize = (sal_Int32)pImageStream->Tell();
440 if (pImageStream->GetBufferSize() < 8192)
441 pImageStream->SetBufferSize(8192);
442 pImageStream->Seek(STREAM_SEEK_TO_BEGIN);
443
444 xImageStream = new ::utl::OInputStreamHelper( new SvLockBytes( pImageStream.get(), sal_False ), nSize );
445 }
446 }
447
448 if ( xImageStream.is() )
449 {
450 if ( m_xColumnUpdate.is() )
451 m_xColumnUpdate->updateBinaryStream( xImageStream, xImageStream->available() );
452 else
453 setControlValue( makeAny( xImageStream ), _eInstigator );
454 xImageStream->closeInput();
455 return sal_True;
456 }
457
458 return sal_False;
459 }
460
461 //------------------------------------------------------------------------------
impl_handleNewImageURL_lck(ValueChangeInstigator _eInstigator)462 sal_Bool OImageControlModel::impl_handleNewImageURL_lck( ValueChangeInstigator _eInstigator )
463 {
464 switch ( lcl_getImageStoreType( getFieldType() ) )
465 {
466 case ImageStoreBinary:
467 if ( impl_updateStreamForURL_lck( m_sImageURL, _eInstigator ) )
468 return sal_True;
469 break;
470
471 case ImageStoreLink:
472 {
473 ::rtl::OUString sCommitURL( m_sImageURL );
474 if ( m_sDocumentURL.getLength() )
475 sCommitURL = URIHelper::simpleNormalizedMakeRelative( m_sDocumentURL, sCommitURL );
476 OSL_ENSURE( m_xColumnUpdate.is(), "OImageControlModel::impl_handleNewImageURL_lck: no bound field, but ImageStoreLink?!" );
477 if ( m_xColumnUpdate.is() )
478 {
479 m_xColumnUpdate->updateString( sCommitURL );
480 return sal_True;
481 }
482 }
483 break;
484
485 case ImageStoreInvalid:
486 OSL_ENSURE( false, "OImageControlModel::impl_handleNewImageURL_lck: image storage type type!" );
487 break;
488 }
489
490 // if we're here, then the above code was unable to update our field/control from the given URL
491 // => fall back to NULL/VOID
492 if ( m_xColumnUpdate.is() )
493 m_xColumnUpdate->updateNull();
494 else
495 setControlValue( Any(), _eInstigator );
496
497 return sal_True;
498 }
499
500 //------------------------------------------------------------------------------
commitControlValueToDbColumn(bool _bPostReset)501 sal_Bool OImageControlModel::commitControlValueToDbColumn( bool _bPostReset )
502 {
503 if ( _bPostReset )
504 {
505 // since this is a "commit after reset", we can simply update the column
506 // with null - this is our "default" which we were just reset to
507 if ( m_xColumnUpdate.is() )
508 m_xColumnUpdate->updateNull();
509 }
510 else
511 {
512 ::osl::MutexGuard aGuard(m_aMutex);
513 return impl_handleNewImageURL_lck( eDbColumnBinding );
514 }
515
516 return sal_True;
517 }
518
519 //------------------------------------------------------------------------------
520 namespace
521 {
lcl_isValidDocumentURL(const::rtl::OUString & _rDocURL)522 bool lcl_isValidDocumentURL( const ::rtl::OUString& _rDocURL )
523 {
524 return ( _rDocURL.getLength() && !_rDocURL.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:object" ) ) );
525 }
526 }
527
528 //------------------------------------------------------------------------------
onConnectedDbColumn(const Reference<XInterface> & _rxForm)529 void OImageControlModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
530 {
531 OBoundControlModel::onConnectedDbColumn( _rxForm );
532
533 try
534 {
535 Reference< XModel > xDocument( getXModel( *this ) );
536 if ( xDocument.is() )
537 {
538 m_sDocumentURL = xDocument->getURL();
539 if ( !lcl_isValidDocumentURL( m_sDocumentURL ) )
540 {
541 Reference< XChild > xAsChild( xDocument, UNO_QUERY );
542 while ( xAsChild.is() && !lcl_isValidDocumentURL( m_sDocumentURL ) )
543 {
544 xDocument.set( xAsChild->getParent(), UNO_QUERY );
545 if ( xDocument.is() )
546 m_sDocumentURL = xDocument->getURL();
547 xAsChild.set( xDocument, UNO_QUERY );
548 }
549 }
550 }
551 }
552 catch( const Exception& )
553 {
554 DBG_UNHANDLED_EXCEPTION();
555 }
556 }
557
558 //------------------------------------------------------------------------------
onDisconnectedDbColumn()559 void OImageControlModel::onDisconnectedDbColumn()
560 {
561 OBoundControlModel::onDisconnectedDbColumn();
562
563 m_sDocumentURL = ::rtl::OUString();
564 }
565
566 //------------------------------------------------------------------------------
translateDbColumnToControlValue()567 Any OImageControlModel::translateDbColumnToControlValue()
568 {
569 switch ( lcl_getImageStoreType( getFieldType() ) )
570 {
571 case ImageStoreBinary:
572 {
573 Reference< XInputStream > xImageStream( m_xColumn->getBinaryStream() );
574 if ( m_xColumn->wasNull() )
575 xImageStream.clear();
576 return makeAny( xImageStream );
577 }
578 case ImageStoreLink:
579 {
580 ::rtl::OUString sImageLink( m_xColumn->getString() );
581 if ( m_sDocumentURL.getLength() )
582 sImageLink = INetURLObject::GetAbsURL( m_sDocumentURL, sImageLink );
583 return makeAny( sImageLink );
584 }
585 case ImageStoreInvalid:
586 OSL_ENSURE( false, "OImageControlModel::translateDbColumnToControlValue: invalid field type!" );
587 break;
588 }
589 return Any();
590 }
591
592 //------------------------------------------------------------------------------
getControlValue() const593 Any OImageControlModel::getControlValue( ) const
594 {
595 return makeAny( m_sImageURL );
596 }
597
598 //------------------------------------------------------------------------------
doSetControlValue(const Any & _rValue)599 void OImageControlModel::doSetControlValue( const Any& _rValue )
600 {
601 DBG_ASSERT( GetImageProducer() && m_xImageProducer.is(), "OImageControlModel::doSetControlValue: no image producer!" );
602 if ( !GetImageProducer() || !m_xImageProducer.is() )
603 return;
604
605 bool bStartProduction = false;
606 switch ( lcl_getImageStoreType( getFieldType() ) )
607 {
608 case ImageStoreBinary:
609 {
610 // give the image producer the stream
611 Reference< XInputStream > xInStream;
612 _rValue >>= xInStream;
613 GetImageProducer()->setImage( xInStream );
614 bStartProduction = true;
615 }
616 break;
617
618 case ImageStoreLink:
619 {
620 ::rtl::OUString sImageURL;
621 _rValue >>= sImageURL;
622 GetImageProducer()->SetImage( sImageURL );
623 bStartProduction = true;
624 }
625 break;
626
627 case ImageStoreInvalid:
628 OSL_ENSURE( false, "OImageControlModel::doSetControlValue: invalid field type!" );
629 break;
630
631 } // switch ( lcl_getImageStoreType( getFieldType() ) )
632
633 if ( bStartProduction )
634 {
635 // start production
636 Reference< XImageProducer > xProducer = m_xImageProducer;
637 {
638 // release our mutex once (it's acquired in the calling method!), as starting the image production may
639 // result in the locking of the solar mutex (unfortunally the default implementation of our aggregate,
640 // VCLXImageControl, does this locking)
641 // FS - 74438 - 30.03.00
642 MutexRelease aRelease(m_aMutex);
643 xProducer->startProduction();
644 }
645 }
646 }
647
648 // OComponentHelper
649 //------------------------------------------------------------------
disposing()650 void SAL_CALL OImageControlModel::disposing()
651 {
652 OBoundControlModel::disposing();
653 }
654
655 //------------------------------------------------------------------------------
resetNoBroadcast()656 void OImageControlModel::resetNoBroadcast()
657 {
658 if ( hasField() ) // only reset when we are connected to a column
659 OBoundControlModel::resetNoBroadcast( );
660 }
661
662 //--------------------------------------------------------------------
getImageProducer()663 Reference< XImageProducer > SAL_CALL OImageControlModel::getImageProducer()
664 {
665 return this;
666 }
667
668 //--------------------------------------------------------------------
addConsumer(const Reference<XImageConsumer> & _rxConsumer)669 void SAL_CALL OImageControlModel::addConsumer( const Reference< XImageConsumer >& _rxConsumer )
670 {
671 GetImageProducer()->addConsumer( _rxConsumer );
672 }
673
674 //--------------------------------------------------------------------
removeConsumer(const Reference<XImageConsumer> & _rxConsumer)675 void SAL_CALL OImageControlModel::removeConsumer( const Reference< XImageConsumer >& _rxConsumer )
676 {
677 GetImageProducer()->removeConsumer( _rxConsumer );
678 }
679
680 //--------------------------------------------------------------------
startProduction()681 void SAL_CALL OImageControlModel::startProduction( )
682 {
683 GetImageProducer()->startProduction();
684 }
685
686 //------------------------------------------------------------------------------
IMPL_LINK(OImageControlModel,OnImageImportDone,::Graphic *,i_pGraphic)687 IMPL_LINK( OImageControlModel, OnImageImportDone, ::Graphic*, i_pGraphic )
688 {
689 const Reference< XGraphic > xGraphic( i_pGraphic != NULL ? Image( i_pGraphic->GetBitmapEx() ).GetXGraphic() : NULL );
690 m_bExternalGraphic = false;
691 try
692 {
693 setPropertyValue( PROPERTY_GRAPHIC, makeAny( xGraphic ) );
694 }
695 catch ( const Exception& )
696 {
697 DBG_UNHANDLED_EXCEPTION();
698 }
699 m_bExternalGraphic = true;
700 return 1L;
701 }
702
703 //==================================================================
704 // OImageControlControl
705 //==================================================================
706
707 //------------------------------------------------------------------
OImageControlControl_CreateInstance(const Reference<XMultiServiceFactory> & _rxFactory)708 InterfaceRef SAL_CALL OImageControlControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
709 {
710 return *(new OImageControlControl(_rxFactory));
711 }
712
713 //------------------------------------------------------------------------------
_getTypes()714 Sequence<Type> OImageControlControl::_getTypes()
715 {
716 return concatSequences(
717 OBoundControl::_getTypes(),
718 OImageControlControl_Base::getTypes()
719 );
720 }
721
722 //------------------------------------------------------------------------------
OImageControlControl(const Reference<XMultiServiceFactory> & _rxFactory)723 OImageControlControl::OImageControlControl(const Reference<XMultiServiceFactory>& _rxFactory)
724 :OBoundControl(_rxFactory, VCL_CONTROL_IMAGECONTROL)
725 ,m_aModifyListeners( m_aMutex )
726 {
727 increment(m_refCount);
728 {
729 // als Focus- und MouseListener anmelden
730 Reference< XWindow > xComp;
731 query_aggregation( m_xAggregate, xComp );
732 if ( xComp.is() )
733 xComp->addMouseListener( this );
734 }
735 decrement(m_refCount);
736 }
737
738 //------------------------------------------------------------------------------
queryAggregation(const Type & _rType)739 Any SAL_CALL OImageControlControl::queryAggregation(const Type& _rType)
740 {
741 Any aReturn = OBoundControl::queryAggregation( _rType );
742 if ( !aReturn.hasValue() )
743 aReturn = ::cppu::queryInterface(
744 _rType,
745 static_cast< XMouseListener* >( this ),
746 static_cast< XModifyBroadcaster* >( this )
747 );
748
749 return aReturn;
750 }
751
752 //------------------------------------------------------------------------------
getSupportedServiceNames()753 StringSequence OImageControlControl::getSupportedServiceNames() throw()
754 {
755 StringSequence aSupported = OBoundControl::getSupportedServiceNames();
756 aSupported.realloc(aSupported.getLength() + 1);
757
758 ::rtl::OUString*pArray = aSupported.getArray();
759 pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_IMAGECONTROL;
760 return aSupported;
761 }
762
763 //------------------------------------------------------------------------------
addModifyListener(const Reference<XModifyListener> & _Listener)764 void SAL_CALL OImageControlControl::addModifyListener( const Reference< XModifyListener >& _Listener )
765 {
766 m_aModifyListeners.addInterface( _Listener );
767 }
768
769 //------------------------------------------------------------------------------
removeModifyListener(const Reference<XModifyListener> & _Listener)770 void SAL_CALL OImageControlControl::removeModifyListener( const Reference< XModifyListener >& _Listener )
771 {
772 m_aModifyListeners.removeInterface( _Listener );
773 }
774
775 //------------------------------------------------------------------------------
disposing()776 void SAL_CALL OImageControlControl::disposing()
777 {
778 EventObject aEvent( *this );
779 m_aModifyListeners.disposeAndClear( aEvent );
780
781 OBoundControl::disposing();
782 }
783
784 //------------------------------------------------------------------------------
disposing(const EventObject & _Event)785 void SAL_CALL OImageControlControl::disposing( const EventObject& _Event )
786 {
787 OBoundControl::disposing( _Event );
788 }
789
790 //------------------------------------------------------------------------------
implClearGraphics(sal_Bool _bForce)791 void OImageControlControl::implClearGraphics( sal_Bool _bForce )
792 {
793 Reference< XPropertySet > xSet( getModel(), UNO_QUERY );
794 if ( xSet.is() )
795 {
796 if ( _bForce )
797 {
798 ::rtl::OUString sOldImageURL;
799 xSet->getPropertyValue( PROPERTY_IMAGE_URL ) >>= sOldImageURL;
800
801 if ( !sOldImageURL.getLength() )
802 // the ImageURL is already empty, so simply setting a new empty one would not suffice
803 // (since it would be ignored)
804 xSet->setPropertyValue( PROPERTY_IMAGE_URL, makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "private:emptyImage" ) ) ) );
805 // (the concrete URL we're passing here doens't matter. It's important that
806 // the model cannot resolve it to a a valid resource describing an image stream
807 }
808
809 xSet->setPropertyValue( PROPERTY_IMAGE_URL, makeAny( ::rtl::OUString() ) );
810 }
811 }
812
813 //------------------------------------------------------------------------------
implInsertGraphics()814 bool OImageControlControl::implInsertGraphics()
815 {
816 Reference< XPropertySet > xSet( getModel(), UNO_QUERY );
817 if ( !xSet.is() )
818 return false;
819
820 ::rtl::OUString sTitle = FRM_RES_STRING(RID_STR_IMPORT_GRAPHIC);
821 // build some arguments for the upcoming dialog
822 try
823 {
824 ::sfx2::FileDialogHelper aDialog( TemplateDescription::FILEOPEN_LINK_PREVIEW, SFXWB_GRAPHIC );
825 aDialog.SetTitle( sTitle );
826
827 Reference< XFilePickerControlAccess > xController( aDialog.GetFilePicker(), UNO_QUERY_THROW );
828 xController->setValue(ExtendedFilePickerElementIds::CHECKBOX_PREVIEW, 0, ::cppu::bool2any(sal_True));
829
830 Reference<XPropertySet> xBoundField;
831 if ( hasProperty( PROPERTY_BOUNDFIELD, xSet ) )
832 xSet->getPropertyValue( PROPERTY_BOUNDFIELD ) >>= xBoundField;
833 sal_Bool bHasField = xBoundField.is();
834
835 // if the control is bound to a DB field, then it's not possible to decide whether or not to link
836 xController->enableControl(ExtendedFilePickerElementIds::CHECKBOX_LINK, !bHasField );
837
838 // if the control is bound to a DB field, then linking of the image depends on the type of the field
839 sal_Bool bImageIsLinked = sal_True;
840 if ( bHasField )
841 {
842 sal_Int32 nFieldType = DataType::OTHER;
843 OSL_VERIFY( xBoundField->getPropertyValue( PROPERTY_FIELDTYPE ) >>= nFieldType );
844 bImageIsLinked = ( lcl_getImageStoreType( nFieldType ) == ImageStoreLink );
845 }
846 xController->setValue(ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, makeAny( bImageIsLinked ) );
847
848 if ( ERRCODE_NONE == aDialog.Execute() )
849 {
850 // reset the url property in case it already has the value we're about to set - in this case
851 // our propertyChanged would not get called without this.
852 implClearGraphics( sal_False );
853 sal_Bool bIsLink = sal_False;
854 xController->getValue(ExtendedFilePickerElementIds::CHECKBOX_LINK, 0) >>= bIsLink;
855 // Force bIsLink to be sal_True if we're bound to a field. Though we initialized the file picker with IsLink=TRUE
856 // in this case, and disabled the respective control, there might be picker implementations which do not
857 // respect this, and return IsLink=FALSE here. In this case, "normalize" the flag.
858 // #i112659# / 2010-08-26 / frank.schoenheit@oracle.com
859 bIsLink |= bHasField;
860 if ( !bIsLink )
861 {
862 Graphic aGraphic;
863 aDialog.GetGraphic( aGraphic );
864 xSet->setPropertyValue( PROPERTY_GRAPHIC, makeAny( aGraphic.GetXGraphic() ) );
865 }
866 else
867 xSet->setPropertyValue( PROPERTY_IMAGE_URL, makeAny( ::rtl::OUString( aDialog.GetPath() ) ) );
868
869 return true;
870 }
871 }
872 catch(Exception&)
873 {
874 DBG_ERROR("OImageControlControl::implInsertGraphics: caught an exception while attempting to execute the FilePicker!");
875 }
876 return false;
877 }
878
879 //------------------------------------------------------------------------------
impl_isEmptyGraphics_nothrow() const880 bool OImageControlControl::impl_isEmptyGraphics_nothrow() const
881 {
882 bool bIsEmpty = true;
883
884 try
885 {
886 Reference< XPropertySet > xModelProps( const_cast< OImageControlControl* >( this )->getModel(), UNO_QUERY_THROW );
887 Reference< XGraphic > xGraphic;
888 OSL_VERIFY( xModelProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Graphic" ) ) ) >>= xGraphic );
889 bIsEmpty = !xGraphic.is();
890 }
891 catch( const Exception& )
892 {
893 DBG_UNHANDLED_EXCEPTION();
894 }
895
896 return bIsEmpty;
897 }
898
899 // MouseListener
900 //------------------------------------------------------------------------------
mousePressed(const::com::sun::star::awt::MouseEvent & e)901 void OImageControlControl::mousePressed(const ::com::sun::star::awt::MouseEvent& e)
902 {
903 ::vos::OGuard aGuard( Application::GetSolarMutex() );
904
905 if (e.Buttons != MouseButton::LEFT)
906 return;
907
908 bool bModified = false;
909 // is this a request for a context menu?
910 if ( e.PopupTrigger )
911 {
912 Reference< XPopupMenu > xMenu( m_aContext.createComponent( "com.sun.star.awt.PopupMenu" ), UNO_QUERY );
913 DBG_ASSERT( xMenu.is(), "OImageControlControl::mousePressed: could not create a popup menu!" );
914
915 Reference< XWindowPeer > xWindowPeer = getPeer();
916 DBG_ASSERT( xWindowPeer.is(), "OImageControlControl::mousePressed: no window!" );
917
918 if ( xMenu.is() && xWindowPeer.is() )
919 {
920 xMenu->insertItem( ID_OPEN_GRAPHICS, FRM_RES_STRING( RID_STR_OPEN_GRAPHICS ), 0, 0 );
921 xMenu->insertItem( ID_CLEAR_GRAPHICS, FRM_RES_STRING( RID_STR_CLEAR_GRAPHICS ), 0, 1 );
922
923 // check if the ImageURL is empty
924 if ( impl_isEmptyGraphics_nothrow() )
925 xMenu->enableItem( ID_CLEAR_GRAPHICS, sal_False );
926
927 awt::Rectangle aRect( e.X, e.Y, 0, 0 );
928 if ( ( e.X < 0 ) || ( e.Y < 0 ) )
929 { // context menu triggered by keyboard
930 // position it in the center of the control
931 // 102205 - 16.08.2002 - fs@openoffice.org
932 Reference< XWindow > xWindow( static_cast< ::cppu::OWeakObject* >( this ), UNO_QUERY );
933 OSL_ENSURE( xWindow.is(), "OImageControlControl::mousePressed: me not a window? How this?" );
934 if ( xWindow.is() )
935 {
936 awt::Rectangle aPosSize = xWindow->getPosSize();
937 aRect.X = aPosSize.Width / 2;
938 aRect.Y = aPosSize.Height / 2;
939 }
940 }
941
942 const sal_Int16 nResult = xMenu->execute( xWindowPeer, aRect, PopupMenuDirection::EXECUTE_DEFAULT );
943
944 switch ( nResult )
945 {
946 case ID_OPEN_GRAPHICS:
947 implInsertGraphics();
948 bModified = true;
949 break;
950
951 case ID_CLEAR_GRAPHICS:
952 implClearGraphics( sal_True );
953 bModified = true;
954 break;
955 }
956 }
957 }
958 else
959 {
960 //////////////////////////////////////////////////////////////////////
961 // Doppelclick
962 if (e.ClickCount == 2)
963 {
964
965 Reference<XPropertySet> xSet(getModel(), UNO_QUERY);
966 if (!xSet.is())
967 return;
968
969 // wenn Control nicht gebunden ist, kein Dialog (da die zu schickende URL hinterher sowieso
970 // versanden wuerde)
971 // FS - #64946# - 19.04.99
972 Reference<XPropertySet> xBoundField;
973 if (hasProperty(PROPERTY_BOUNDFIELD, xSet))
974 ::cppu::extractInterface(xBoundField, xSet->getPropertyValue(PROPERTY_BOUNDFIELD));
975 if (!xBoundField.is())
976 {
977 // but only if our IMAGE_URL property is handled as if it is transient, which is equivalent to
978 // an empty control source
979 if (!hasProperty(PROPERTY_CONTROLSOURCE, xSet) || (::comphelper::getString(xSet->getPropertyValue(PROPERTY_CONTROLSOURCE)).getLength() != 0))
980 return;
981 }
982
983 sal_Bool bReadOnly = false;
984 xSet->getPropertyValue(PROPERTY_READONLY) >>= bReadOnly;
985 if (bReadOnly)
986 return;
987
988 if ( implInsertGraphics() )
989 bModified = true;
990 }
991 }
992
993 if ( bModified )
994 {
995 EventObject aEvent( *this );
996 m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
997 }
998 }
999
1000 //------------------------------------------------------------------------------
mouseReleased(const awt::MouseEvent &)1001 void SAL_CALL OImageControlControl::mouseReleased(const awt::MouseEvent& /*e*/)
1002 {
1003 }
1004
1005 //------------------------------------------------------------------------------
mouseEntered(const awt::MouseEvent &)1006 void SAL_CALL OImageControlControl::mouseEntered(const awt::MouseEvent& /*e*/)
1007 {
1008 }
1009
1010 //------------------------------------------------------------------------------
mouseExited(const awt::MouseEvent &)1011 void SAL_CALL OImageControlControl::mouseExited(const awt::MouseEvent& /*e*/)
1012 {
1013 }
1014
1015 //.........................................................................
1016 } // namespace frm
1017 //.........................................................................
1018