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