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 <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/frame/XModel.hpp>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <com/sun/star/container/XNameAccess.hpp>
28 #include <com/sun/star/table/CellRangeAddress.hpp>
29 #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
30 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/table/CellAddress.hpp>
33 #include <com/sun/star/beans/NamedValue.hpp>
34
35
36 namespace svt
37 {
38
39
40 using namespace ::com::sun::star;
41
lcl_isNamedRange(const OUString & sAddress,const uno::Reference<frame::XModel> & xModel,css::table::CellRangeAddress & aAddress)42 static bool lcl_isNamedRange( const OUString& sAddress, const uno::Reference< frame::XModel >& xModel, css::table::CellRangeAddress& aAddress )
43 {
44 bool bRes = false;
45 uno::Reference< sheet::XCellRangeReferrer > xReferrer;
46 try
47 {
48 uno::Reference< beans::XPropertySet > xPropSet( xModel, uno::UNO_QUERY_THROW );
49 uno::Reference< container::XNameAccess > xNamed( xPropSet->getPropertyValue( "NamedRanges" ), uno::UNO_QUERY_THROW );
50 xReferrer.set ( xNamed->getByName( sAddress ), uno::UNO_QUERY );
51 }
52 catch( uno::Exception& /*e*/ )
53 {
54 // do nothing
55 }
56 if ( xReferrer.is() )
57 {
58 uno::Reference< sheet::XCellRangeAddressable > xRangeAddressable( xReferrer->getReferredCells(), uno::UNO_QUERY );
59 if ( xRangeAddressable.is() )
60 {
61 aAddress = xRangeAddressable->getRangeAddress();
62 bRes = true;
63 }
64 }
65 return bRes;
66 }
67
68
69 void
ApplyListSourceAndBindableData(const css::uno::Reference<css::frame::XModel> & xModel,const css::uno::Reference<css::uno::XInterface> & rObj,const OUString & rsCtrlSource,const OUString & rsRowSource,sal_uInt16 nRefTab)70 BindableControlHelper::ApplyListSourceAndBindableData( const css::uno::Reference< css::frame::XModel >& xModel, const css::uno::Reference< css::uno::XInterface >& rObj, const OUString& rsCtrlSource, const OUString& rsRowSource, sal_uInt16 nRefTab )
71 {
72 // XBindable etc.
73 uno::Reference< lang::XMultiServiceFactory > xFac;
74 if ( xModel.is() )
75 xFac.set( xModel, uno::UNO_QUERY );
76 uno::Reference< form::binding::XBindableValue > xBindable( rObj, uno::UNO_QUERY );
77 if ( xFac.is() && rsCtrlSource.getLength() && xBindable.is() )
78 {
79
80 // OOo address structures
81 // RefCell - convert from XL
82 // pretend we converted the imported string address into the
83 // appropriate address structure
84 uno::Reference< beans::XPropertySet > xConvertor( xFac->createInstance( "com.sun.star.table.CellAddressConversion"), uno::UNO_QUERY );
85 css::table::CellAddress aAddress;
86 if ( xConvertor.is() )
87 {
88 // we need this service to properly convert XL notation also
89 // Should be easy to extend
90 xConvertor->setPropertyValue( "ReferenceSheet", uno::makeAny( nRefTab ) );
91 xConvertor->setPropertyValue( "XLA1Representation", uno::makeAny( rsCtrlSource ) );
92 xConvertor->getPropertyValue( "Address" ) >>= aAddress;
93 }
94
95 beans::NamedValue aArg1;
96 aArg1.Name = "BoundCell";
97 aArg1.Value <<= aAddress;
98
99 uno::Sequence< uno::Any > aArgs(1);
100 aArgs[ 0 ] <<= aArg1;
101
102 uno::Reference< form::binding::XValueBinding > xBinding( xFac->createInstanceWithArguments( "com.sun.star.table.CellValueBinding", aArgs ), uno::UNO_QUERY );
103 xBindable->setValueBinding( xBinding );
104 }
105 else if ( xBindable.is() ) // reset it
106 xBindable->setValueBinding( uno::Reference< form::binding::XValueBinding >() );
107 uno::Reference< form::binding::XListEntrySink > xListEntrySink( rObj, uno::UNO_QUERY );
108 if ( xFac.is() && rsRowSource.getLength() && xListEntrySink.is() )
109 {
110
111 // OOo address structures
112 // RefCell - convert from XL
113 // pretend we converted the imported string address into the
114 // appropriate address structure
115 uno::Reference< beans::XPropertySet > xConvertor( xFac->createInstance( "com.sun.star.table.CellRangeAddressConversion"), uno::UNO_QUERY );
116 css::table::CellRangeAddress aAddress;
117 if ( xConvertor.is() )
118 {
119 if ( !lcl_isNamedRange( rsRowSource, xModel, aAddress ) )
120 {
121 // we need this service to properly convert XL notation also
122 // Should be easy to extend
123 xConvertor->setPropertyValue( "ReferenceSheet", uno::makeAny( nRefTab ) );
124 xConvertor->setPropertyValue( "XLA1Representation", uno::makeAny( rsRowSource ) );
125 xConvertor->getPropertyValue( "Address" ) >>= aAddress;
126 }
127 }
128
129 beans::NamedValue aArg1;
130 aArg1.Name = "CellRange";
131 aArg1.Value <<= aAddress;
132
133 uno::Sequence< uno::Any > aArgs(1);
134 aArgs[ 0 ] <<= aArg1;
135
136 uno::Reference< form::binding::XListEntrySource > xSource( xFac->createInstanceWithArguments( "com.sun.star.table.CellRangeListSource", aArgs ), uno::UNO_QUERY );
137 xListEntrySink->setListEntrySource( xSource );
138 }
139 else if ( xListEntrySink.is() ) // reset
140 xListEntrySink->setListEntrySource( uno::Reference< form::binding::XListEntrySource >() );
141
142 }
143
144
145 } // namespace svt
146
147
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
149