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 #include "vbacombobox.hxx"
25 #include "vbanewfont.hxx"
26 #include <ooo/vba/msforms/fmStyle.hpp>
27 #include <ooo/vba/msforms/fmDropButtonStyle.hpp>
28 #include <ooo/vba/msforms/fmDragBehavior.hpp>
29 #include <ooo/vba/msforms/fmEnterFieldBehavior.hpp>
30 #include <ooo/vba/msforms/fmListStyle.hpp>
31 #include <ooo/vba/msforms/fmTextAlign.hpp>
32 
33 using namespace com::sun::star;
34 using namespace ooo::vba;
35 
36 
37 //SelectedItems list of integer indexes
38 //StringItemList list of items
39 
40 const static rtl::OUString TEXT( RTL_CONSTASCII_USTRINGPARAM("Text") );
41 const static rtl::OUString SELECTEDITEMS( RTL_CONSTASCII_USTRINGPARAM("SelectedItems") );
42 const static rtl::OUString ITEMS( RTL_CONSTASCII_USTRINGPARAM("StringItemList") );
43 const static rtl::OUString CONTROLSOURCEPROP( RTL_CONSTASCII_USTRINGPARAM("DataFieldProperty") );
44 
ScVbaComboBox(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<uno::XInterface> & xControl,const uno::Reference<frame::XModel> & xModel,AbstractGeometryAttributes * pGeomHelper,bool bDialogType)45 ScVbaComboBox::ScVbaComboBox( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< uno::XInterface >& xControl, const uno::Reference< frame::XModel >& xModel, AbstractGeometryAttributes* pGeomHelper, bool bDialogType ) : ComboBoxImpl_BASE( xParent, xContext, xControl, xModel, pGeomHelper ), mbDialogType( bDialogType )
46 {
47     mpListHelper.reset( new ListControlHelper( m_xProps ) );
48 	try
49 	{
50 	   // grab the default value property name
51 	   m_xProps->getPropertyValue( CONTROLSOURCEPROP ) >>= sSourceName;
52 	}
53     catch( uno::Exception& )
54     {
55     }
56     if( sSourceName.getLength() == 0 )
57         sSourceName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ) );
58 }
59 
60 // Attributes
61 
62 
63 // Value, [read] e.g. getValue returns the value of ooo Text propery e.g. the value in
64 // the drop down
65 uno::Any SAL_CALL
getValue()66 ScVbaComboBox::getValue() throw (uno::RuntimeException)
67 {
68 	return m_xProps->getPropertyValue( sSourceName );
69 }
70 
71 void SAL_CALL
setListIndex(const uno::Any & _value)72 ScVbaComboBox::setListIndex( const uno::Any& _value ) throw (uno::RuntimeException)
73 {
74     sal_Int16 nIndex = 0;
75     if( _value >>= nIndex )
76     {
77 	    uno::Sequence< rtl::OUString > sItems;
78     	m_xProps->getPropertyValue( ITEMS ) >>= sItems;
79         if( ( nIndex >= 0 ) && ( sItems.getLength() > nIndex ) )
80         {
81             rtl::OUString sText = sItems[ nIndex ];
82             m_xProps->setPropertyValue( TEXT, uno::makeAny( sText ) );
83         }
84     }
85 }
86 
87 uno::Any SAL_CALL
getListIndex()88 ScVbaComboBox::getListIndex() throw (uno::RuntimeException)
89 {
90 	uno::Sequence< rtl::OUString > sItems;
91 	m_xProps->getPropertyValue( ITEMS ) >>= sItems;
92 	// should really return the item that has focus regardless of
93 	// it been selected
94 	if ( sItems.getLength() > 0 )
95 	{
96 		rtl::OUString sText = getText();
97 		sal_Int32 nLen = sItems.getLength();
98 		for ( sal_Int32 index = 0; sText.getLength() && index < nLen; ++index )
99 		{
100 			if ( sItems[ index ].equals( sText ) )
101 			{
102 				OSL_TRACE("getListIndex returning %d", index );
103 				return uno::makeAny( index );
104 			}
105 
106 		}
107  	}
108 	OSL_TRACE("getListIndex returning %d", -1 );
109 	return uno::makeAny( sal_Int32( -1 ) );
110 }
111 
112 // Value, [write]e.g. setValue sets the value in the drop down, and if the value is one
113 // of the values in the list then the selection is also set
114 void SAL_CALL
setValue(const uno::Any & _value)115 ScVbaComboBox::setValue( const uno::Any& _value ) throw (uno::RuntimeException)
116 {
117     // booleans are converted to uppercase strings
118 	m_xProps->setPropertyValue( sSourceName, uno::Any( extractStringFromAny( _value, ::rtl::OUString(), true ) ) );
119 }
120 
121 // see Value
122 
123 ::rtl::OUString SAL_CALL
getText()124 ScVbaComboBox::getText() throw (uno::RuntimeException)
125 {
126 	rtl::OUString result;
127 	getValue() >>= result;
128 	return result;
129 }
130 
131 void SAL_CALL
setText(const::rtl::OUString & _text)132 ScVbaComboBox::setText( const ::rtl::OUString& _text ) throw (uno::RuntimeException)
133 {
134 	setValue( uno::makeAny( _text ) ); // seems the same
135 }
136 
137 // Methods
138 void SAL_CALL
AddItem(const uno::Any & pvargItem,const uno::Any & pvargIndex)139 ScVbaComboBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex ) throw (uno::RuntimeException)
140 {
141 	mpListHelper->AddItem( pvargItem, pvargIndex );
142 }
143 
144 void SAL_CALL
removeItem(const uno::Any & index)145 ScVbaComboBox::removeItem( const uno::Any& index ) throw (uno::RuntimeException)
146 {
147 	mpListHelper->removeItem( index );
148 }
149 
150 void SAL_CALL
Clear()151 ScVbaComboBox::Clear(  ) throw (uno::RuntimeException)
152 {
153 	mpListHelper->Clear();
154 }
155 
156 void SAL_CALL
setRowSource(const rtl::OUString & _rowsource)157 ScVbaComboBox::setRowSource( const rtl::OUString& _rowsource ) throw (css::uno::RuntimeException)
158 {
159 	ScVbaControl::setRowSource( _rowsource );
160 	mpListHelper->setRowSource( _rowsource );
161 }
162 
163 sal_Int32 SAL_CALL
getListCount()164 ScVbaComboBox::getListCount() throw (uno::RuntimeException)
165 {
166 	return mpListHelper->getListCount();
167 }
168 
169 uno::Any SAL_CALL
List(const::uno::Any & pvargIndex,const uno::Any & pvarColumn)170 ScVbaComboBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn ) throw (uno::RuntimeException)
171 {
172 	return mpListHelper->List( pvargIndex, pvarColumn );
173 }
174 
getStyle()175 sal_Int32 SAL_CALL ScVbaComboBox::getStyle() throw (uno::RuntimeException)
176 {
177     return msforms::fmStyle::fmStyleDropDownCombo;
178 }
179 
setStyle(sal_Int32)180 void SAL_CALL ScVbaComboBox::setStyle( sal_Int32 /*nStyle*/ ) throw (uno::RuntimeException)
181 {
182 }
183 
getDropButtonStyle()184 sal_Int32 SAL_CALL ScVbaComboBox::getDropButtonStyle() throw (uno::RuntimeException)
185 {
186     return msforms::fmDropButtonStyle::fmDropButtonStyleArrow;
187 }
188 
setDropButtonStyle(sal_Int32)189 void SAL_CALL ScVbaComboBox::setDropButtonStyle( sal_Int32 /*nDropButtonStyle*/ ) throw (uno::RuntimeException)
190 {
191 }
192 
getDragBehavior()193 sal_Int32 SAL_CALL ScVbaComboBox::getDragBehavior() throw (uno::RuntimeException)
194 {
195     return msforms::fmDragBehavior::fmDragBehaviorDisabled;
196 }
197 
setDragBehavior(sal_Int32)198 void SAL_CALL ScVbaComboBox::setDragBehavior( sal_Int32 /*nDragBehavior*/ ) throw (uno::RuntimeException)
199 {
200 }
201 
getEnterFieldBehavior()202 sal_Int32 SAL_CALL ScVbaComboBox::getEnterFieldBehavior() throw (uno::RuntimeException)
203 {
204     return msforms::fmEnterFieldBehavior::fmEnterFieldBehaviorSelectAll;
205 }
206 
setEnterFieldBehavior(sal_Int32)207 void SAL_CALL ScVbaComboBox::setEnterFieldBehavior( sal_Int32 /*nEnterFieldBehavior*/ ) throw (uno::RuntimeException)
208 {
209 }
210 
getListStyle()211 sal_Int32 SAL_CALL ScVbaComboBox::getListStyle() throw (uno::RuntimeException)
212 {
213     return msforms::fmListStyle::fmListStylePlain;
214 }
215 
setListStyle(sal_Int32)216 void SAL_CALL ScVbaComboBox::setListStyle( sal_Int32 /*nListStyle*/ ) throw (uno::RuntimeException)
217 {
218 }
219 
getTextAlign()220 sal_Int32 SAL_CALL ScVbaComboBox::getTextAlign() throw (uno::RuntimeException)
221 {
222     return msforms::fmTextAlign::fmTextAlignLeft;
223 }
224 
setTextAlign(sal_Int32)225 void SAL_CALL ScVbaComboBox::setTextAlign( sal_Int32 /*nTextAlign*/ ) throw (uno::RuntimeException)
226 {
227 }
228 
getTextLength()229 sal_Int32 SAL_CALL ScVbaComboBox::getTextLength() throw (uno::RuntimeException)
230 {
231     return getText().getLength();
232 }
233 
getFont()234 uno::Reference< msforms::XNewFont > SAL_CALL ScVbaComboBox::getFont() throw (uno::RuntimeException)
235 {
236     return new VbaNewFont( this, mxContext, m_xProps );
237 }
238 
239 rtl::OUString&
getServiceImplName()240 ScVbaComboBox::getServiceImplName()
241 {
242 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaComboBox") );
243 	return sImplName;
244 }
245 
246 uno::Sequence< rtl::OUString >
getServiceNames()247 ScVbaComboBox::getServiceNames()
248 {
249 	static uno::Sequence< rtl::OUString > aServiceNames;
250 	if ( aServiceNames.getLength() == 0 )
251 	{
252 		aServiceNames.realloc( 1 );
253 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.msforms.ComboBox" ) );
254 	}
255 	return aServiceNames;
256 }
257