LibreOffice Module forms (master) 1
ImageControl.cxx
Go to the documentation of this file.
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 <strings.hrc>
23#include <frm_resource.hxx>
24#include <property.hxx>
25#include <services.hxx>
26#include <componenttools.hxx>
27
30#include <com/sun/star/awt/PopupMenu.hpp>
31#include <com/sun/star/awt/XPopupMenu.hpp>
32#include <com/sun/star/awt/PopupMenuDirection.hpp>
33#include <com/sun/star/beans/PropertyAttribute.hpp>
34#include <com/sun/star/form/FormComponentType.hpp>
35#include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
36#include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
37#include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
38#include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
39#include <com/sun/star/sdbc/DataType.hpp>
40#include <com/sun/star/awt/MouseButton.hpp>
41#include <com/sun/star/awt/XWindow.hpp>
42#include <com/sun/star/graphic/XGraphic.hpp>
43#include <com/sun/star/graphic/GraphicObject.hpp>
44#include <tools/urlobj.hxx>
45#include <tools/stream.hxx>
46#include <tools/debug.hxx>
48#include <vcl/graph.hxx>
49#include <vcl/svapp.hxx>
53#include <comphelper/types.hxx>
56#include <svl/urihelper.hxx>
57
58#include <memory>
59
60#define ID_OPEN_GRAPHICS 1
61#define ID_CLEAR_GRAPHICS 2
62
63namespace frm
64{
65
66using namespace ::com::sun::star;
67using namespace ::com::sun::star::uno;
68using namespace ::com::sun::star::sdb;
69using namespace ::com::sun::star::sdbc;
70using namespace ::com::sun::star::beans;
71using namespace ::com::sun::star::container;
72using namespace ::com::sun::star::form;
73using namespace ::com::sun::star::awt;
74using namespace ::com::sun::star::io;
75using namespace ::com::sun::star::ui::dialogs;
76using namespace ::com::sun::star::lang;
77using namespace ::com::sun::star::util;
78using namespace ::com::sun::star::graphic;
79using namespace ::com::sun::star::frame;
80
81
82//= OImageControlModel
83
84namespace
85{
86 enum ImageStoreType
87 {
88 ImageStoreBinary,
89 ImageStoreLink,
90
91 ImageStoreInvalid
92 };
93
94 ImageStoreType lcl_getImageStoreType( const sal_Int32 _nFieldType )
95 {
96 // binary/longvarchar types could be used to store images in binary representation
97 if ( ( _nFieldType == DataType::BINARY )
98 || ( _nFieldType == DataType::VARBINARY )
99 || ( _nFieldType == DataType::LONGVARBINARY )
100 || ( _nFieldType == DataType::OTHER )
101 || ( _nFieldType == DataType::OBJECT )
102 || ( _nFieldType == DataType::BLOB )
103 || ( _nFieldType == DataType::LONGVARCHAR )
104 || ( _nFieldType == DataType::CLOB )
105 )
106 return ImageStoreBinary;
107
108 // char types could be used to store links to images
109 if ( ( _nFieldType == DataType::CHAR )
110 || ( _nFieldType == DataType::VARCHAR )
111 )
112 return ImageStoreLink;
113
114 return ImageStoreInvalid;
115 }
116}
117
118
119// OImageControlModel
120
122{
123 return concatSequences(
126 );
127}
128
129
130OImageControlModel::OImageControlModel(const Reference<XComponentContext>& _rxFactory)
132 // use the old control name for compytibility reasons
133 ,m_bExternalGraphic( true )
134 ,m_bReadOnly( false )
135{
136 m_nClassId = FormComponentType::IMAGECONTROL;
138
140}
141
142
143OImageControlModel::OImageControlModel( const OImageControlModel* _pOriginal, const Reference< XComponentContext >& _rxFactory )
144 :OBoundControlModel( _pOriginal, _rxFactory )
145 // use the old control name for compytibility reasons
146 ,m_bExternalGraphic( true )
147 ,m_bReadOnly( _pOriginal->m_bReadOnly )
148 ,m_sImageURL( _pOriginal->m_sImageURL )
149 ,m_xGraphicObject( _pOriginal->m_xGraphicObject )
150{
152
153 osl_atomic_increment( &m_refCount );
154 {
155 // simulate a propertyChanged event for the ImageURL
156 ::osl::MutexGuard aGuard( m_aMutex );
158 }
159 osl_atomic_decrement( &m_refCount );
160}
161
162
164{
166 m_xImageProducer->SetDoneHdl( LINK( this, OImageControlModel, OnImageImportDone ) );
167}
168
169
171{
172 if (!OComponentHelper::rBHelper.bDisposed)
173 {
174 acquire();
175 dispose();
176 }
177
178}
179
180// XCloneable
181
182css::uno::Reference< css::util::XCloneable > SAL_CALL OImageControlModel::createClone()
183{
185 pClone->clonedFrom(this);
186 return pClone;
187}
188
189// XServiceInfo
190
192{
193 css::uno::Sequence<OUString> aSupported = OBoundControlModel::getSupportedServiceNames();
194 aSupported.realloc(aSupported.getLength() + 2);
195
196 OUString*pArray = aSupported.getArray();
197 pArray[aSupported.getLength()-2] = FRM_SUN_COMPONENT_IMAGECONTROL;
198 pArray[aSupported.getLength()-1] = FRM_COMPONENT_IMAGECONTROL;
199 return aSupported;
200}
201
202
204{
205 // Order matters: we want to "override" the XImageProducer interface of the aggregate without
206 // own XImageProducer interface, thus we need to query OImageControlModel_Base first
208
209 // BUT: _don't_ let it feel responsible for the XTypeProvider interface
210 // (as this is implemented by our base class in the proper way)
211 if ( _rType.equals( cppu::UnoType<XTypeProvider>::get() )
212 || !aReturn.hasValue()
213 )
214 aReturn = OBoundControlModel::queryAggregation( _rType );
215
216 return aReturn;
217}
218
219
220bool OImageControlModel::approveDbColumnType( sal_Int32 _nColumnType )
221{
222 return ImageStoreInvalid != lcl_getImageStoreType( _nColumnType );
223}
224
225
226void OImageControlModel::getFastPropertyValue(Any& rValue, sal_Int32 nHandle) const
227{
228 switch (nHandle)
229 {
231 rValue <<= m_bReadOnly;
232 break;
234 rValue <<= m_sImageURL;
235 break;
237 rValue <<= m_xGraphicObject.is() ? m_xGraphicObject->getGraphic() : Reference< XGraphic >();
238 break;
239 default:
241 }
242}
243
244
245void OImageControlModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const Any& rValue)
246{
247 switch (nHandle)
248 {
250 DBG_ASSERT(rValue.getValueType().getTypeClass() == TypeClass_BOOLEAN, "OImageControlModel::setFastPropertyValue_NoBroadcast : invalid type !" );
251 m_bReadOnly = getBOOL(rValue);
252 break;
253
255 OSL_VERIFY( rValue >>= m_sImageURL );
257 {
258 ControlModelLock aLock( *this );
259 // that's a fake ... onValuePropertyChange expects to receive the only lock to our instance,
260 // but we're already called with our mutex locked ...
261 onValuePropertyChange( aLock );
262 }
263 break;
264
266 {
267 Reference< XGraphic > xGraphic;
268 OSL_VERIFY( rValue >>= xGraphic );
269 if ( !xGraphic.is() )
270 m_xGraphicObject.clear();
271 else
272 {
273 m_xGraphicObject = graphic::GraphicObject::create( m_xContext );
274 m_xGraphicObject->setGraphic( xGraphic );
275 }
276
277 if ( m_bExternalGraphic )
278 {
279 m_sImageURL = OUString();
280 // TODO: speaking strictly, this would need to be notified, since ImageURL is a bound property. However,
281 // this method here is called with a locked mutex, so we cannot simply call listeners ...
282 // I think the missing notification (and thus clients which potentially cannot observe the change)
283 // is less severe than the potential deadlock ...
284 }
285 }
286 break;
287
288 default:
290 break;
291 }
292}
293
294
295sal_Bool OImageControlModel::convertFastPropertyValue(Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle, const Any& rValue)
296{
297 switch (nHandle)
298 {
300 return tryPropertyValue(rConvertedValue, rOldValue, rValue, m_bReadOnly);
301
303 return tryPropertyValue( rConvertedValue, rOldValue, rValue, m_sImageURL );
304
306 {
307 const Reference< XGraphic > xGraphic( getFastPropertyValue( PROPERTY_ID_GRAPHIC ), UNO_QUERY );
308 return tryPropertyValue( rConvertedValue, rOldValue, rValue, xGraphic );
309 }
310
311 default:
312 return OBoundControlModel::convertFastPropertyValue(rConvertedValue, rOldValue, nHandle, rValue);
313 }
314}
315
316
317void OImageControlModel::describeFixedProperties( Sequence< Property >& _rProps ) const
318{
320 sal_Int32 nOldCount = _rProps.getLength();
321 _rProps.realloc( nOldCount + 4);
322 css::beans::Property* pProperties = _rProps.getArray() + nOldCount;
323 *pProperties++ = css::beans::Property(PROPERTY_GRAPHIC, PROPERTY_ID_GRAPHIC, cppu::UnoType<XGraphic>::get(),
324 css::beans::PropertyAttribute::BOUND | css::beans::PropertyAttribute::TRANSIENT);
325 *pProperties++ = css::beans::Property(PROPERTY_IMAGE_URL, PROPERTY_ID_IMAGE_URL, cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::BOUND);
326 *pProperties++ = css::beans::Property(PROPERTY_READONLY, PROPERTY_ID_READONLY, cppu::UnoType<bool>::get(),
327 css::beans::PropertyAttribute::BOUND);
328 *pProperties++ = css::beans::Property(PROPERTY_TABINDEX, PROPERTY_ID_TABINDEX, cppu::UnoType<sal_Int16>::get(), css::beans::PropertyAttribute::BOUND);
329 DBG_ASSERT( pProperties == _rProps.getArray() + _rProps.getLength(), "<...>::describeFixedProperties/getInfoHelper: forgot to adjust the count ?");
330}
331
332
333void OImageControlModel::describeAggregateProperties( Sequence< Property >& /* [out] */ o_rAggregateProperties ) const
334{
335 OBoundControlModel::describeAggregateProperties( o_rAggregateProperties );
336 // remove ImageURL and Graphic properties, we "override" them.
337 // This is because our aggregate synchronizes those
338 // two, but we have an own synchronization mechanism.
339 RemoveProperty( o_rAggregateProperties, PROPERTY_IMAGE_URL );
340 RemoveProperty( o_rAggregateProperties, PROPERTY_GRAPHIC );
341}
342
343
345{
346 return FRM_COMPONENT_IMAGECONTROL; // old (non-sun) name for compatibility !
347}
348
349
350void OImageControlModel::write(const Reference<XObjectOutputStream>& _rxOutStream)
351{
352 // Base class
353 OBoundControlModel::write(_rxOutStream);
354 // Version
355 _rxOutStream->writeShort(0x0003);
356 // Name
357 _rxOutStream->writeBoolean(m_bReadOnly);
358 writeHelpTextCompatibly(_rxOutStream);
359 // from version 0x0003 : common properties
360 writeCommonProperties(_rxOutStream);
361}
362
363
364void OImageControlModel::read(const Reference<XObjectInputStream>& _rxInStream)
365{
366 OBoundControlModel::read(_rxInStream);
367
368 // Version
369 sal_uInt16 nVersion = _rxInStream->readShort();
370 switch (nVersion)
371 {
372 case 0x0001:
373 m_bReadOnly = _rxInStream->readBoolean();
374 break;
375 case 0x0002:
376 m_bReadOnly = _rxInStream->readBoolean();
377 readHelpTextCompatibly(_rxInStream);
378 break;
379 case 0x0003:
380 m_bReadOnly = _rxInStream->readBoolean();
381 readHelpTextCompatibly(_rxInStream);
382 readCommonProperties(_rxInStream);
383 break;
384 default :
385 OSL_FAIL("OImageControlModel::read : unknown version !");
386 m_bReadOnly = false;
388 break;
389 }
390 // Display default values after read
391 if ( !getControlSource().isEmpty() )
392 { // (not if we don't have a control source - the "State" property acts like it is persistent, then
393 ::osl::MutexGuard aGuard(m_aMutex); // resetNoBroadcast expects this mutex guarding
395 }
396}
397
398
400{
401 // create a stream for the image specified by the URL
402 std::unique_ptr< SvStream > pImageStream;
403 Reference< XInputStream > xImageStream;
404
405 if ( ::svt::GraphicAccess::isSupportedURL( _rURL ) )
406 {
407 xImageStream = ::svt::GraphicAccess::getImageXStream( getContext(), _rURL );
408 }
409 else
410 {
411 pImageStream = ::utl::UcbStreamHelper::CreateStream( _rURL, StreamMode::READ );
412 bool bSetNull = (pImageStream == nullptr) || (ERRCODE_NONE != pImageStream->GetErrorCode());
413
414 if ( !bSetNull )
415 {
416 // get the size of the stream
417 sal_uInt64 const nSize = pImageStream->remainingSize();
418 if (pImageStream->GetBufferSize() < 8192)
419 pImageStream->SetBufferSize(8192);
420 pImageStream->Seek(STREAM_SEEK_TO_BEGIN);
421
422 xImageStream = new ::utl::OInputStreamHelper( new SvLockBytes( pImageStream.get(), false ), nSize );
423 }
424 }
425
426 if ( xImageStream.is() )
427 {
428 if ( m_xColumnUpdate.is() )
429 m_xColumnUpdate->updateBinaryStream( xImageStream, xImageStream->available() );
430 else
431 setControlValue( Any( xImageStream ), _eInstigator );
432 xImageStream->closeInput();
433 return true;
434 }
435
436 return false;
437}
438
439
441{
442 switch ( lcl_getImageStoreType( getFieldType() ) )
443 {
444 case ImageStoreBinary:
445 if ( impl_updateStreamForURL_lck( m_sImageURL, _eInstigator ) )
446 return;
447 break;
448
449 case ImageStoreLink:
450 {
451 OUString sCommitURL( m_sImageURL );
452 if ( !m_sDocumentURL.isEmpty() )
454 OSL_ENSURE( m_xColumnUpdate.is(), "OImageControlModel::impl_handleNewImageURL_lck: no bound field, but ImageStoreLink?!" );
455 if ( m_xColumnUpdate.is() )
456 {
457 m_xColumnUpdate->updateString( sCommitURL );
458 return;
459 }
460 }
461 break;
462
463 case ImageStoreInvalid:
464 OSL_FAIL( "OImageControlModel::impl_handleNewImageURL_lck: image storage type type!" );
465 break;
466 }
467
468 // if we're here, then the above code was unable to update our field/control from the given URL
469 // => fall back to NULL/VOID
470 if ( m_xColumnUpdate.is() )
471 m_xColumnUpdate->updateNull();
472 else
473 setControlValue( Any(), _eInstigator );
474}
475
476
478{
479 if ( _bPostReset )
480 {
481 // since this is a "commit after reset", we can simply update the column
482 // with null - this is our "default" which we were just reset to
483 if ( m_xColumnUpdate.is() )
484 m_xColumnUpdate->updateNull();
485 }
486 else
487 {
488 ::osl::MutexGuard aGuard(m_aMutex);
490 }
491
492 return true;
493}
494
495
496namespace
497{
498 bool lcl_isValidDocumentURL( std::u16string_view _rDocURL )
499 {
500 return ( !_rDocURL.empty() && _rDocURL != u"private:object" );
501 }
502}
503
504
505void OImageControlModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
506{
508
509 try
510 {
511 Reference< XModel > xDocument( getXModel( *this ) );
512 if ( xDocument.is() )
513 {
514 m_sDocumentURL = xDocument->getURL();
515 if ( !lcl_isValidDocumentURL( m_sDocumentURL ) )
516 {
517 Reference< XChild > xAsChild( xDocument, UNO_QUERY );
518 while ( xAsChild.is() && !lcl_isValidDocumentURL( m_sDocumentURL ) )
519 {
520 xDocument.set( xAsChild->getParent(), UNO_QUERY );
521 if ( xDocument.is() )
522 m_sDocumentURL = xDocument->getURL();
523 xAsChild.set( xDocument, UNO_QUERY );
524 }
525 }
526 }
527 }
528 catch( const Exception& )
529 {
530 DBG_UNHANDLED_EXCEPTION("forms.component");
531 }
532}
533
534
536{
538
539 m_sDocumentURL.clear();
540}
541
542
544{
545 switch ( lcl_getImageStoreType( getFieldType() ) )
546 {
547 case ImageStoreBinary:
548 {
549 Reference< XInputStream > xImageStream( m_xColumn->getBinaryStream() );
550 if ( m_xColumn->wasNull() )
551 xImageStream.clear();
552 return Any( xImageStream );
553 }
554 case ImageStoreLink:
555 {
556 OUString sImageLink( m_xColumn->getString() );
557 if ( !m_sDocumentURL.isEmpty() )
558 sImageLink = INetURLObject::GetAbsURL( m_sDocumentURL, sImageLink );
559 return Any( sImageLink );
560 }
561 case ImageStoreInvalid:
562 OSL_FAIL( "OImageControlModel::translateDbColumnToControlValue: invalid field type!" );
563 break;
564 }
565 return Any();
566}
567
568
570{
571 return Any( m_sImageURL );
572}
573
574
576{
577 DBG_ASSERT( GetImageProducer() && m_xImageProducer.is(), "OImageControlModel::doSetControlValue: no image producer!" );
578 if ( !GetImageProducer() || !m_xImageProducer.is() )
579 return;
580
581 bool bStartProduction = false;
582 switch ( lcl_getImageStoreType( getFieldType() ) )
583 {
584 case ImageStoreBinary:
585 {
586 // give the image producer the stream
587 Reference< XInputStream > xInStream;
588 _rValue >>= xInStream;
589 GetImageProducer()->setImage( xInStream );
590 bStartProduction = true;
591 }
592 break;
593
594 case ImageStoreLink:
595 {
596 OUString sImageURL;
597 _rValue >>= sImageURL;
598 GetImageProducer()->SetImage( sImageURL );
599 bStartProduction = true;
600 }
601 break;
602
603 case ImageStoreInvalid:
604 OSL_FAIL( "OImageControlModel::doSetControlValue: invalid field type!" );
605 break;
606
607 } // switch ( lcl_getImageStoreType( getFieldType() ) )
608
609 if ( bStartProduction )
610 {
611 // start production
613 {
614 // release our mutex once (it's acquired in the calling method!), as starting the image production may
615 // result in the locking of the solar mutex (unfortunately the default implementation of our aggregate,
616 // VCLXImageControl, does this locking)
617 MutexRelease aRelease(m_aMutex);
618 xProducer->startProduction();
619 }
620 }
621}
622
624{
625 if ( hasField() ) // only reset when we are connected to a column
627}
628
629
630Reference< XImageProducer > SAL_CALL OImageControlModel::getImageProducer()
631{
632 return this;
633}
634
635
636void SAL_CALL OImageControlModel::addConsumer( const Reference< XImageConsumer >& _rxConsumer )
637{
638 GetImageProducer()->addConsumer( _rxConsumer );
639}
640
641
642void SAL_CALL OImageControlModel::removeConsumer( const Reference< XImageConsumer >& _rxConsumer )
643{
644 GetImageProducer()->removeConsumer( _rxConsumer );
645}
646
647
649{
651}
652
653
654IMPL_LINK( OImageControlModel, OnImageImportDone, ::Graphic*, i_pGraphic, void )
655{
656 const Reference< XGraphic > xGraphic(i_pGraphic != nullptr ? i_pGraphic->GetXGraphic() : nullptr);
657 m_bExternalGraphic = false;
658 try
659 {
660 setPropertyValue( PROPERTY_GRAPHIC, Any( xGraphic ) );
661 }
662 catch ( const Exception& )
663 {
664 DBG_UNHANDLED_EXCEPTION("forms.component");
665 }
666 m_bExternalGraphic = true;
667}
668
669
670// OImageControlControl
671
673{
674 return concatSequences(
677 );
678}
679
680
681OImageControlControl::OImageControlControl(const Reference<XComponentContext>& _rxFactory)
683 ,m_aModifyListeners( m_aMutex )
684{
685 osl_atomic_increment(&m_refCount);
686 {
687 // Add as Focus- and MouseListener
688 Reference< XWindow > xComp;
690 if ( xComp.is() )
691 xComp->addMouseListener( this );
692 }
693 osl_atomic_decrement(&m_refCount);
694}
695
696
698{
699 Any aReturn = OBoundControl::queryAggregation( _rType );
700 if ( !aReturn.hasValue() )
701 aReturn = ::cppu::queryInterface(
702 _rType,
703 static_cast< XMouseListener* >( this ),
704 static_cast< XModifyBroadcaster* >( this )
705 );
706
707 return aReturn;
708}
709
710
712{
713 css::uno::Sequence<OUString> aSupported = OBoundControl::getSupportedServiceNames();
714 aSupported.realloc(aSupported.getLength() + 2);
715
716 OUString*pArray = aSupported.getArray();
717 pArray[aSupported.getLength()-2] = FRM_SUN_CONTROL_IMAGECONTROL;
718 pArray[aSupported.getLength()-1] = STARDIV_ONE_FORM_CONTROL_IMAGECONTROL;
719 return aSupported;
720}
721
722
723void SAL_CALL OImageControlControl::addModifyListener( const Reference< XModifyListener >& Listener )
724{
726}
727
728
729void SAL_CALL OImageControlControl::removeModifyListener( const Reference< XModifyListener >& Listener )
730{
732}
733
734
736{
737 EventObject aEvent( *this );
739
741}
742
743
744void SAL_CALL OImageControlControl::disposing( const EventObject& Event )
745{
747}
748
749
751{
752 Reference< XPropertySet > xSet( getModel(), UNO_QUERY );
753 if ( !xSet.is() )
754 return;
755
756 if ( _bForce )
757 {
758 OUString sOldImageURL;
759 xSet->getPropertyValue( PROPERTY_IMAGE_URL ) >>= sOldImageURL;
760
761 if ( sOldImageURL.isEmpty() )
762 // the ImageURL is already empty, so simply setting a new empty one would not suffice
763 // (since it would be ignored)
764 xSet->setPropertyValue( PROPERTY_IMAGE_URL, Any( OUString( "private:emptyImage" ) ) );
765 // (the concrete URL we're passing here doesn't matter. It's important that
766 // the model cannot resolve it to a valid resource describing an image stream
767 }
768
769 xSet->setPropertyValue( PROPERTY_IMAGE_URL, Any( OUString() ) );
770}
771
772
774{
775 Reference< XPropertySet > xSet( getModel(), UNO_QUERY );
776 if ( !xSet.is() )
777 return false;
778
779 OUString sTitle = ResourceManager::loadString(RID_STR_IMPORT_GRAPHIC);
780 // build some arguments for the upcoming dialog
781 try
782 {
783 Reference< XWindow > xWindow( static_cast< ::cppu::OWeakObject* >( this ), UNO_QUERY );
784 ::sfx2::FileDialogHelper aDialog(TemplateDescription::FILEOPEN_LINK_PREVIEW, FileDialogFlags::Graphic,
787 aDialog.SetTitle( sTitle );
788
789 Reference< XFilePickerControlAccess > xController( aDialog.GetFilePicker(), UNO_QUERY_THROW );
790 xController->setValue(ExtendedFilePickerElementIds::CHECKBOX_PREVIEW, 0, css::uno::Any(true));
791
792 Reference<XPropertySet> xBoundField;
793 if ( hasProperty( PROPERTY_BOUNDFIELD, xSet ) )
794 xSet->getPropertyValue( PROPERTY_BOUNDFIELD ) >>= xBoundField;
795 bool bHasField = xBoundField.is();
796
797 // if the control is bound to a DB field, then it's not possible to decide whether or not to link
798 xController->enableControl(ExtendedFilePickerElementIds::CHECKBOX_LINK, !bHasField );
799
800 // if the control is bound to a DB field, then linking of the image depends on the type of the field
801 bool bImageIsLinked = true;
802 if ( bHasField )
803 {
804 sal_Int32 nFieldType = DataType::OTHER;
805 OSL_VERIFY( xBoundField->getPropertyValue( PROPERTY_FIELDTYPE ) >>= nFieldType );
806 bImageIsLinked = ( lcl_getImageStoreType( nFieldType ) == ImageStoreLink );
807 }
808 xController->setValue(ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, Any( bImageIsLinked ) );
809
810 if ( ERRCODE_NONE == aDialog.Execute() )
811 {
812 // reset the url property in case it already has the value we're about to set - in this case
813 // our propertyChanged would not get called without this.
814 implClearGraphics( false );
815 bool bIsLink = false;
816 xController->getValue(ExtendedFilePickerElementIds::CHECKBOX_LINK, 0) >>= bIsLink;
817 // Force bIsLink to be sal_True if we're bound to a field. Though we initialized the file picker with IsLink=TRUE
818 // in this case, and disabled the respective control, there might be picker implementations which do not
819 // respect this, and return IsLink=FALSE here. In this case, "normalize" the flag.
820 // #i112659#
821 bIsLink |= bHasField;
822 if ( !bIsLink )
823 {
824 Graphic aGraphic;
825 aDialog.GetGraphic( aGraphic );
826 xSet->setPropertyValue( PROPERTY_GRAPHIC, Any( aGraphic.GetXGraphic() ) );
827 }
828 else
829 xSet->setPropertyValue( PROPERTY_IMAGE_URL, Any( aDialog.GetPath() ) );
830
831 return true;
832 }
833 }
834 catch(const Exception&)
835 {
836 TOOLS_WARN_EXCEPTION( "forms.component", "OImageControlControl::implInsertGraphics: caught an exception while attempting to execute the FilePicker!");
837 }
838 return false;
839}
840
841
843{
844 bool bIsEmpty = true;
845
846 try
847 {
848 Reference< XPropertySet > xModelProps( const_cast< OImageControlControl* >( this )->getModel(), UNO_QUERY_THROW );
849 Reference< XGraphic > xGraphic;
850 OSL_VERIFY( xModelProps->getPropertyValue("Graphic") >>= xGraphic );
851 bIsEmpty = !xGraphic.is();
852 }
853 catch( const Exception& )
854 {
855 DBG_UNHANDLED_EXCEPTION("forms.component");
856 }
857
858 return bIsEmpty;
859}
860
861// MouseListener
862
863void OImageControlControl::mousePressed(const css::awt::MouseEvent& e)
864{
865 SolarMutexGuard aGuard;
866
867 if (e.Buttons != MouseButton::LEFT)
868 return;
869
870 bool bModified = false;
871 // is this a request for a context menu?
872 if ( e.PopupTrigger )
873 {
874 Reference< XPopupMenu > xMenu( awt::PopupMenu::create( m_xContext ) );
875 DBG_ASSERT( xMenu.is(), "OImageControlControl::mousePressed: could not create a popup menu!" );
876
877 Reference< XWindowPeer > xWindowPeer = getPeer();
878 DBG_ASSERT( xWindowPeer.is(), "OImageControlControl::mousePressed: no window!" );
879
880 if ( xMenu.is() && xWindowPeer.is() )
881 {
882 xMenu->insertItem( ID_OPEN_GRAPHICS, ResourceManager::loadString(RID_STR_OPEN_GRAPHICS), 0, 0 );
883 xMenu->insertItem( ID_CLEAR_GRAPHICS, ResourceManager::loadString(RID_STR_CLEAR_GRAPHICS), 0, 1 );
884
885 // check if the ImageURL is empty
887 xMenu->enableItem( ID_CLEAR_GRAPHICS, false );
888
889 awt::Rectangle aRect( e.X, e.Y, 0, 0 );
890 if ( ( e.X < 0 ) || ( e.Y < 0 ) )
891 { // context menu triggered by keyboard
892 // position it in the center of the control
893 Reference< XWindow > xWindow( static_cast< ::cppu::OWeakObject* >( this ), UNO_QUERY );
894 OSL_ENSURE( xWindow.is(), "OImageControlControl::mousePressed: me not a window? How this?" );
895 if ( xWindow.is() )
896 {
897 awt::Rectangle aPosSize = xWindow->getPosSize();
898 aRect.X = aPosSize.Width / 2;
899 aRect.Y = aPosSize.Height / 2;
900 }
901 }
902
903 const sal_Int16 nResult = xMenu->execute( xWindowPeer, aRect, PopupMenuDirection::EXECUTE_DEFAULT );
904
905 switch ( nResult )
906 {
907 case ID_OPEN_GRAPHICS:
909 bModified = true;
910 break;
911
913 implClearGraphics( true );
914 bModified = true;
915 break;
916 }
917 }
918 }
919 else
920 {
921
922 // Double click
923 if (e.ClickCount == 2)
924 {
925
926 Reference<XPropertySet> xSet(getModel(), UNO_QUERY);
927 if (!xSet.is())
928 return;
929
930 // If the Control is not bound, do not display a dialog (because the to-be-sent URL would be invalid anyway)
931 Reference<XPropertySet> xBoundField;
933 xBoundField.set(
934 xSet->getPropertyValue(PROPERTY_BOUNDFIELD),
935 css::uno::UNO_QUERY);
936 if (!xBoundField.is())
937 {
938 // but only if our IMAGE_URL property is handled as if it is transient, which is equivalent to
939 // an empty control source
940 if ( !hasProperty(PROPERTY_CONTROLSOURCE, xSet) || !::comphelper::getString(xSet->getPropertyValue(PROPERTY_CONTROLSOURCE)).isEmpty() )
941 return;
942 }
943
944 bool bReadOnly = false;
945 xSet->getPropertyValue(PROPERTY_READONLY) >>= bReadOnly;
946 if (bReadOnly)
947 return;
948
949 if ( implInsertGraphics() )
950 bModified = true;
951 }
952 }
953
954 if ( bModified )
955 {
956 EventObject aEvent( *this );
957 m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
958 }
959}
960
961
962void SAL_CALL OImageControlControl::mouseReleased(const awt::MouseEvent& /*e*/)
963{
964}
965
966
967void SAL_CALL OImageControlControl::mouseEntered(const awt::MouseEvent& /*e*/)
968{
969}
970
971
972void SAL_CALL OImageControlControl::mouseExited(const awt::MouseEvent& /*e*/)
973{
974}
975
976} // namespace frm
977
978extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
979com_sun_star_form_OImageControlModel_get_implementation(css::uno::XComponentContext* component,
980 css::uno::Sequence<css::uno::Any> const &)
981{
982 return cppu::acquire(new frm::OImageControlModel(component));
983}
984
985extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
987 css::uno::Sequence<css::uno::Any> const &)
988{
989 return cppu::acquire(new frm::OImageControlControl(component));
990}
991
992/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_form_OImageControlModel_get_implementation(css::uno::XComponentContext *component, css::uno::Sequence< css::uno::Any > const &)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_form_OImageControlControl_get_implementation(css::uno::XComponentContext *component, css::uno::Sequence< css::uno::Any > const &)
#define ID_OPEN_GRAPHICS
#define ID_CLEAR_GRAPHICS
AnyEventRef aEvent
static weld::Window * GetFrameWeld(const css::uno::Reference< css::awt::XWindow > &rWindow)
css::uno::Reference< css::graphic::XGraphic > GetXGraphic() const
static OUString GetAbsURL(std::u16string_view rTheBaseURIRef, OUString const &rTheRelURIRef, EncodeMechanism eEncodeMechanism=EncodeMechanism::WasEncoded, DecodeMechanism eDecodeMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
void SAL_CALL removeConsumer(const css::uno::Reference< css::awt::XImageConsumer > &rxConsumer) override
Definition: imgprod.cxx:194
void SAL_CALL startProduction() override
Definition: imgprod.cxx:252
void SAL_CALL addConsumer(const css::uno::Reference< css::awt::XImageConsumer > &rxConsumer) override
Definition: imgprod.cxx:186
void SetImage(const OUString &rPath)
Definition: imgprod.cxx:203
void setImage(css::uno::Reference< css::io::XInputStream > const &rStmRef)
Definition: imgprod.cxx:233
sal_Int32 addInterface(const css::uno::Reference< ListenerT > &rxIFace)
void disposeAndClear(const css::lang::EventObject &rEvt)
sal_Int32 removeInterface(const css::uno::Reference< ListenerT > &rxIFace)
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() SAL_OVERRIDE
virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const &rType) SAL_OVERRIDE
virtual void SAL_CALL acquire() SAL_NOEXCEPT SAL_OVERRIDE
virtual void SAL_CALL dispose() SAL_OVERRIDE
oslInterlockedCount m_refCount
class whose instances lock an OControlModel
void writeCommonProperties(const css::uno::Reference< css::io::XObjectOutputStream > &_rxOutStream)
We can't write (new) common properties in this base class, as the file format doesn't allow this (unf...
sal_Int32 getFieldType() const
virtual void onConnectedDbColumn(const css::uno::Reference< css::uno::XInterface > &_rxForm)
called whenever a connection to a database column has been established
void onValuePropertyChange(ControlModelLock &i_rControLock)
(to be) called when the value property changed
virtual css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &_rType) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
void setControlValue(const css::uno::Any &_rValue, ValueChangeInstigator _eInstigator)
sets the given value as new current value for the control
virtual void SAL_CALL setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const css::uno::Any &rValue) override
css::uno::Reference< css::sdb::XColumn > m_xColumn
virtual void describeFixedProperties(css::uno::Sequence< css::beans::Property > &_rProps) const override
describes the properties provided by this class, or its respective derived class
virtual void resetNoBroadcast()
called to reset the control to some kind of default.
const OUString & getControlSource() const
virtual css::uno::Sequence< css::uno::Type > _getTypes() override
virtual void SAL_CALL write(const css::uno::Reference< css::io::XObjectOutputStream > &OutStream) override
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const override
virtual sal_Bool SAL_CALL convertFastPropertyValue(css::uno::Any &_rConvertedValue, css::uno::Any &_rOldValue, sal_Int32 _nHandle, const css::uno::Any &_rValue) override
virtual void SAL_CALL read(const css::uno::Reference< css::io::XObjectInputStream > &InStream) override
void readCommonProperties(const css::uno::Reference< css::io::XObjectInputStream > &_rxInStream)
css::uno::Reference< css::sdb::XColumnUpdate > m_xColumnUpdate
virtual void onDisconnectedDbColumn()
called whenever a connection to a database column has been suspended
void initOwnValueProperty(const OUString &i_rValuePropertyName)
initializes the part of the class which is related to the control value.
virtual css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &_rType) override
virtual void SAL_CALL disposing() override
virtual css::uno::Sequence< css::uno::Type > _getTypes() override
css::uno::Reference< css::uno::XComponentContext > m_xContext
::osl::Mutex m_aMutex
const css::uno::Reference< css::uno::XComponentContext > & getContext() const
virtual void describeAggregateProperties(css::uno::Sequence< css::beans::Property > &_rAggregateProps) const
describes the properties of our aggregate
void writeHelpTextCompatibly(const css::uno::Reference< css::io::XObjectOutputStream > &_rxOutStream)
void readHelpTextCompatibly(const css::uno::Reference< css::io::XObjectInputStream > &_rxInStream)
css::uno::Reference< css::uno::XAggregation > m_xAggregate
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual css::uno::Reference< css::awt::XWindowPeer > SAL_CALL getPeer() override
virtual css::uno::Reference< css::awt::XControlModel > SAL_CALL getModel() override
css::uno::Reference< css::uno::XComponentContext > m_xContext
OImageControlControl(const css::uno::Reference< css::uno::XComponentContext > &_rxFactory)
void implClearGraphics(bool _bForce)
::comphelper::OInterfaceContainerHelper3< css::util::XModifyListener > m_aModifyListeners
virtual void SAL_CALL disposing() override
virtual void SAL_CALL mouseEntered(const css::awt::MouseEvent &e) override
bool impl_isEmptyGraphics_nothrow() const
determines whether the control does currently have an empty graphic set
virtual void SAL_CALL mouseExited(const css::awt::MouseEvent &e) override
virtual css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &_rType) override
virtual css::uno::Sequence< css::uno::Type > _getTypes() override
virtual void SAL_CALL addModifyListener(const css::uno::Reference< css::util::XModifyListener > &aListener) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL mousePressed(const css::awt::MouseEvent &e) override
virtual void SAL_CALL removeModifyListener(const css::uno::Reference< css::util::XModifyListener > &aListener) override
virtual void SAL_CALL mouseReleased(const css::awt::MouseEvent &e) override
bool impl_updateStreamForURL_lck(const OUString &_rURL, ValueChangeInstigator _eInstigator)
updates the binary stream, created from loading the file which the given URL points to,...
virtual OUString SAL_CALL getServiceName() override
virtual css::uno::Any translateDbColumnToControlValue() override
translates a db column value into a control value.
virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone() override
virtual css::uno::Sequence< css::uno::Type > _getTypes() override
ImageProducer * GetImageProducer()
virtual void SAL_CALL addConsumer(const css::uno::Reference< css::awt::XImageConsumer > &xConsumer) override
virtual ~OImageControlModel() override
virtual void SAL_CALL write(const css::uno::Reference< css::io::XObjectOutputStream > &_rxOutStream) override
virtual void resetNoBroadcast() override
called to reset the control to some kind of default.
virtual css::uno::Any getControlValue() const override
retrieves the current value of the control
virtual void SAL_CALL setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const css::uno::Any &rValue) override
virtual void describeFixedProperties(css::uno::Sequence< css::beans::Property > &_rProps) const override
describes the properties provided by this class, or its respective derived class
virtual void doSetControlValue(const css::uno::Any &_rValue) override
virtual sal_Bool SAL_CALL convertFastPropertyValue(css::uno::Any &rConvertedValue, css::uno::Any &rOldValue, sal_Int32 nHandle, const css::uno::Any &rValue) override
virtual css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &_rType) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
rtl::Reference< ImageProducer > m_xImageProducer
virtual void onDisconnectedDbColumn() override
called whenever a connection to a database column has been suspended
virtual bool commitControlValueToDbColumn(bool _bPostReset) override
commits the current control value to the database column we're bound to @precond we're properly bound...
virtual void describeAggregateProperties(css::uno::Sequence< css::beans::Property > &_rAggregateProps) const override
describes the properties of our aggregate
virtual bool approveDbColumnType(sal_Int32 _nColumnType) override
nFieldType is the type of the field, on which the model will be linked.
virtual void onConnectedDbColumn(const css::uno::Reference< css::uno::XInterface > &_rxForm) override
called whenever a connection to a database column has been established
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const override
void impl_handleNewImageURL_lck(ValueChangeInstigator _eInstigator)
displays the image described by the given URL @precond our own mutex is locked
virtual css::uno::Reference< css::awt::XImageProducer > SAL_CALL getImageProducer() override
virtual void SAL_CALL removeConsumer(const css::uno::Reference< css::awt::XImageConsumer > &xConsumer) override
virtual void SAL_CALL startProduction() override
OImageControlModel(const css::uno::Reference< css::uno::XComponentContext > &_rxFactory)
css::uno::Reference< css::graphic::XGraphicObject > m_xGraphicObject
virtual void SAL_CALL read(const css::uno::Reference< css::io::XObjectInputStream > &_rxInStream) override
OUString GetPath() const
const css::uno::Reference< css::ui::dialogs::XFilePicker3 > & GetFilePicker() const
void SetTitle(const OUString &rNewTitle)
void SetContext(Context _eNewContext)
ErrCode GetGraphic(Graphic &rGraphic) const
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
#define ERRCODE_NONE
sal_Int16 nVersion
bool bReadOnly
constexpr OUStringLiteral PROPERTY_TABINDEX
Definition: frm_strings.hxx:26
constexpr OUStringLiteral PROPERTY_BOUNDFIELD
constexpr OUStringLiteral PROPERTY_CONTROLSOURCE
Definition: frm_strings.hxx:38
constexpr OUStringLiteral PROPERTY_READONLY
Definition: frm_strings.hxx:41
constexpr OUStringLiteral PROPERTY_IMAGE_URL
Definition: frm_strings.hxx:72
constexpr OUStringLiteral PROPERTY_FIELDTYPE
Definition: frm_strings.hxx:90
constexpr OUStringLiteral PROPERTY_GRAPHIC
Definition: frm_strings.hxx:73
std::mutex m_aMutex
bool m_bReadOnly
SVL_DLLPUBLIC OUString simpleNormalizedMakeRelative(OUString const &baseUriReference, OUString const &uriReference)
@ Exception
bool hasProperty(const OUString &_rName, const Reference< XPropertySet > &_rxSet)
css::uno::Sequence< T > concatSequences(const css::uno::Sequence< T > &rS1, const Ss &... rSn)
bool getBOOL(const Any &_rAny)
bool query_aggregation(const css::uno::Reference< css::uno::XAggregation > &_rxAggregate, css::uno::Reference< iface > &_rxOut)
bool tryPropertyValue(Any &_rConvertedValue, Any &_rOldValue, const Any &_rValueToSet, const Any &_rCurrentValue, const Type &_rExpectedType)
void RemoveProperty(Sequence< Property > &_rProps, const OUString &_rPropName)
Type
OUString loadString(TranslateId aResId)
loads the string with the specified resource id from the FormLayer mo file
ListBox is a bit confusing / different from other form components, so here are a few notes:
Definition: BaseListBox.hxx:25
IMPL_LINK(OClickableImageBaseModel, OnImageImportDone, Graphic *, i_pGraphic, void)
css::uno::Reference< css::frame::XModel > getXModel(const css::uno::Reference< css::uno::XInterface > &_rxComponent)
VBAHELPER_DLLPUBLIC bool setPropertyValue(css::uno::Sequence< css::beans::PropertyValue > &aProp, const OUString &aName, const css::uno::Any &aValue)
#define PROPERTY_ID_READONLY
Definition: property.hxx:47
#define PROPERTY_ID_GRAPHIC
Definition: property.hxx:58
#define PROPERTY_ID_IMAGE_URL
Definition: property.hxx:114
#define PROPERTY_ID_TABINDEX
Definition: property.hxx:41
sal_Int32 nHandle
constexpr OUStringLiteral FRM_COMPONENT_IMAGECONTROL
Definition: services.hxx:85
constexpr OUStringLiteral FRM_SUN_COMPONENT_IMAGECONTROL
Definition: services.hxx:137
constexpr OUStringLiteral VCL_CONTROL_IMAGECONTROL
Definition: services.hxx:37
constexpr OUStringLiteral VCL_CONTROLMODEL_IMAGECONTROL
Definition: services.hxx:55
constexpr OUStringLiteral FRM_SUN_CONTROL_IMAGECONTROL
Definition: services.hxx:164
constexpr OUStringLiteral STARDIV_ONE_FORM_CONTROL_IMAGECONTROL
Definition: services.hxx:104
#define STREAM_SEEK_TO_BEGIN
Reference< XController > xController
unsigned char sal_Bool