Bump version to 5.0-14
[LibreOffice.git] / svtools / source / misc / bindablecontrolhelper.cxx
blobab22b2b50cb4b7768a3e4c5f5e6eab12135b7728
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <svtools/bindablecontrolhelper.hxx>
21 #include <com/sun/star/form/binding/XBindableValue.hpp>
22 #include <com/sun/star/form/binding/XValueBinding.hpp>
23 #include <com/sun/star/form/binding/XListEntrySink.hpp>
24 #include <com/sun/star/form/binding/XListEntrySource.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/container/XNameAccess.hpp>
27 #include <com/sun/star/table/CellRangeAddress.hpp>
28 #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
29 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/table/CellAddress.hpp>
32 #include <com/sun/star/beans/NamedValue.hpp>
35 namespace svt
39 using namespace ::com::sun::star;
41 bool lcl_isNamedRange( const OUString& sAddress, const uno::Reference< frame::XModel >& xModel, table::CellRangeAddress& aAddress )
43 bool bRes = false;
44 uno::Reference< sheet::XCellRangeReferrer > xReferrer;
45 try
47 uno::Reference< beans::XPropertySet > xPropSet( xModel, uno::UNO_QUERY_THROW );
48 uno::Reference< container::XNameAccess > xNamed( xPropSet->getPropertyValue( "NamedRanges" ), uno::UNO_QUERY_THROW );
49 xReferrer.set ( xNamed->getByName( sAddress ), uno::UNO_QUERY );
51 catch( uno::Exception& /*e*/ )
53 // do nothing
55 if ( xReferrer.is() )
57 uno::Reference< sheet::XCellRangeAddressable > xRangeAddressable( xReferrer->getReferredCells(), uno::UNO_QUERY );
58 if ( xRangeAddressable.is() )
60 aAddress = xRangeAddressable->getRangeAddress();
61 bRes = true;
64 return bRes;
68 void
69 BindableControlHelper::ApplyListSourceAndBindableData( const com::sun::star::uno::Reference< com::sun::star::frame::XModel >& xModel, const com::sun::star::uno::Reference< com::sun::star::uno::XInterface >& rObj, const OUString& rsCtrlSource, const OUString& rsRowSource, sal_uInt16 nRefTab )
71 // XBindable etc.
72 uno::Reference< lang::XMultiServiceFactory > xFac;
73 if ( xModel.is() )
74 xFac.set( xModel, uno::UNO_QUERY );
75 uno::Reference< form::binding::XBindableValue > xBindable( rObj, uno::UNO_QUERY );
76 if ( xFac.is() && rsCtrlSource.getLength() && xBindable.is() )
79 // OOo address structures
80 // RefCell - convert from XL
81 // pretend we converted the imported string address into the
82 // appropriate address structure
83 uno::Reference< beans::XPropertySet > xConvertor( xFac->createInstance( "com.sun.star.table.CellAddressConversion"), uno::UNO_QUERY );
84 table::CellAddress aAddress;
85 if ( xConvertor.is() )
87 // we need this service to properly convert XL notation also
88 // Should be easy to extend
89 xConvertor->setPropertyValue( "ReferenceSheet", uno::makeAny( nRefTab ) );
90 xConvertor->setPropertyValue( "XLA1Representation", uno::makeAny( rsCtrlSource ) );
91 xConvertor->getPropertyValue( "Address" ) >>= aAddress;
94 beans::NamedValue aArg1;
95 aArg1.Name = "BoundCell";
96 aArg1.Value <<= aAddress;
98 uno::Sequence< uno::Any > aArgs(1);
99 aArgs[ 0 ] <<= aArg1;
101 uno::Reference< form::binding::XValueBinding > xBinding( xFac->createInstanceWithArguments( "com.sun.star.table.CellValueBinding", aArgs ), uno::UNO_QUERY );
102 xBindable->setValueBinding( xBinding );
104 else if ( xBindable.is() ) // reset it
105 xBindable->setValueBinding( uno::Reference< form::binding::XValueBinding >() );
106 uno::Reference< form::binding::XListEntrySink > xListEntrySink( rObj, uno::UNO_QUERY );
107 if ( xFac.is() && rsRowSource.getLength() && xListEntrySink.is() )
110 // OOo address structures
111 // RefCell - convert from XL
112 // pretend we converted the imported string address into the
113 // appropriate address structure
114 uno::Reference< beans::XPropertySet > xConvertor( xFac->createInstance( "com.sun.star.table.CellRangeAddressConversion"), uno::UNO_QUERY );
115 table::CellRangeAddress aAddress;
116 if ( xConvertor.is() )
118 if ( !lcl_isNamedRange( rsRowSource, xModel, aAddress ) )
120 // we need this service to properly convert XL notation also
121 // Should be easy to extend
122 xConvertor->setPropertyValue( "ReferenceSheet", uno::makeAny( nRefTab ) );
123 xConvertor->setPropertyValue( "XLA1Representation", uno::makeAny( rsRowSource ) );
124 xConvertor->getPropertyValue( "Address" ) >>= aAddress;
128 beans::NamedValue aArg1;
129 aArg1.Name = "CellRange";
130 aArg1.Value <<= aAddress;
132 uno::Sequence< uno::Any > aArgs(1);
133 aArgs[ 0 ] <<= aArg1;
135 uno::Reference< form::binding::XListEntrySource > xSource( xFac->createInstanceWithArguments( "com.sun.star.table.CellRangeListSource", aArgs ), uno::UNO_QUERY );
136 xListEntrySink->setListEntrySource( xSource );
138 else if ( xListEntrySink.is() ) // reset
139 xListEntrySink->setListEntrySource( uno::Reference< form::binding::XListEntrySource >() );
144 } // namespace svt
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */