1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "vbacombobox.hxx"
29 #include "vbanewfont.hxx"
30 #include <ooo/vba/msforms/fmStyle.hpp>
31 #include <ooo/vba/msforms/fmDropButtonStyle.hpp>
32 #include <ooo/vba/msforms/fmDragBehavior.hpp>
33 #include <ooo/vba/msforms/fmEnterFieldBehavior.hpp>
34 #include <ooo/vba/msforms/fmListStyle.hpp>
35 #include <ooo/vba/msforms/fmTextAlign.hpp>
36 
37 using namespace com::sun::star;
38 using namespace ooo::vba;
39 
40 
41 //SelectedItems list of integer indexes
42 //StringItemList list of items
43 
44 const static rtl::OUString TEXT( RTL_CONSTASCII_USTRINGPARAM("Text") );
45 const static rtl::OUString SELECTEDITEMS( RTL_CONSTASCII_USTRINGPARAM("SelectedItems") );
46 const static rtl::OUString ITEMS( RTL_CONSTASCII_USTRINGPARAM("StringItemList") );
47 const static rtl::OUString CONTROLSOURCEPROP( RTL_CONSTASCII_USTRINGPARAM("DataFieldProperty") );
48 
49 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 )
50 {
51     mpListHelper.reset( new ListControlHelper( m_xProps ) );
52 	try
53 	{
54 	   // grab the default value property name
55 	   m_xProps->getPropertyValue( CONTROLSOURCEPROP ) >>= sSourceName;
56 	}
57     catch( uno::Exception& )
58     {
59     }
60     if( sSourceName.getLength() == 0 )
61         sSourceName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ) );
62 }
63 
64 // Attributes
65 
66 
67 // Value, [read] e.g. getValue returns the value of ooo Text propery e.g. the value in
68 // the drop down
69 uno::Any SAL_CALL
70 ScVbaComboBox::getValue() throw (uno::RuntimeException)
71 {
72 	return m_xProps->getPropertyValue( sSourceName );
73 }
74 
75 void SAL_CALL
76 ScVbaComboBox::setListIndex( const uno::Any& _value ) throw (uno::RuntimeException)
77 {
78     sal_Int16 nIndex = 0;
79     if( _value >>= nIndex )
80     {
81 	    uno::Sequence< rtl::OUString > sItems;
82     	m_xProps->getPropertyValue( ITEMS ) >>= sItems;
83         if( ( nIndex >= 0 ) && ( sItems.getLength() > nIndex ) )
84         {
85             rtl::OUString sText = sItems[ nIndex ];
86             m_xProps->setPropertyValue( TEXT, uno::makeAny( sText ) );
87         }
88     }
89 }
90 
91 uno::Any SAL_CALL
92 ScVbaComboBox::getListIndex() throw (uno::RuntimeException)
93 {
94 	uno::Sequence< rtl::OUString > sItems;
95 	m_xProps->getPropertyValue( ITEMS ) >>= sItems;
96 	// should really return the item that has focus regardless of
97 	// it been selected
98 	if ( sItems.getLength() > 0 )
99 	{
100 		rtl::OUString sText = getText();
101 		sal_Int32 nLen = sItems.getLength();
102 		for ( sal_Int32 index = 0; sText.getLength() && index < nLen; ++index )
103 		{
104 			if ( sItems[ index ].equals( sText ) )
105 			{
106 				OSL_TRACE("getListIndex returning %d", index );
107 				return uno::makeAny( index );
108 			}
109 
110 		}
111  	}
112 	OSL_TRACE("getListIndex returning %d", -1 );
113 	return uno::makeAny( sal_Int32( -1 ) );
114 }
115 
116 // Value, [write]e.g. setValue sets the value in the drop down, and if the value is one
117 // of the values in the list then the selection is also set
118 void SAL_CALL
119 ScVbaComboBox::setValue( const uno::Any& _value ) throw (uno::RuntimeException)
120 {
121     // booleans are converted to uppercase strings
122 	m_xProps->setPropertyValue( sSourceName, uno::Any( extractStringFromAny( _value, ::rtl::OUString(), true ) ) );
123 }
124 
125 // see Value
126 
127 ::rtl::OUString SAL_CALL
128 ScVbaComboBox::getText() throw (uno::RuntimeException)
129 {
130 	rtl::OUString result;
131 	getValue() >>= result;
132 	return result;
133 }
134 
135 void SAL_CALL
136 ScVbaComboBox::setText( const ::rtl::OUString& _text ) throw (uno::RuntimeException)
137 {
138 	setValue( uno::makeAny( _text ) ); // seems the same
139 }
140 
141 // Methods
142 void SAL_CALL
143 ScVbaComboBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex ) throw (uno::RuntimeException)
144 {
145 	mpListHelper->AddItem( pvargItem, pvargIndex );
146 }
147 
148 void SAL_CALL
149 ScVbaComboBox::removeItem( const uno::Any& index ) throw (uno::RuntimeException)
150 {
151 	mpListHelper->removeItem( index );
152 }
153 
154 void SAL_CALL
155 ScVbaComboBox::Clear(  ) throw (uno::RuntimeException)
156 {
157 	mpListHelper->Clear();
158 }
159 
160 void SAL_CALL
161 ScVbaComboBox::setRowSource( const rtl::OUString& _rowsource ) throw (css::uno::RuntimeException)
162 {
163 	ScVbaControl::setRowSource( _rowsource );
164 	mpListHelper->setRowSource( _rowsource );
165 }
166 
167 sal_Int32 SAL_CALL
168 ScVbaComboBox::getListCount() throw (uno::RuntimeException)
169 {
170 	return mpListHelper->getListCount();
171 }
172 
173 uno::Any SAL_CALL
174 ScVbaComboBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn ) throw (uno::RuntimeException)
175 {
176 	return mpListHelper->List( pvargIndex, pvarColumn );
177 }
178 
179 sal_Int32 SAL_CALL ScVbaComboBox::getStyle() throw (uno::RuntimeException)
180 {
181     return msforms::fmStyle::fmStyleDropDownCombo;
182 }
183 
184 void SAL_CALL ScVbaComboBox::setStyle( sal_Int32 /*nStyle*/ ) throw (uno::RuntimeException)
185 {
186 }
187 
188 sal_Int32 SAL_CALL ScVbaComboBox::getDropButtonStyle() throw (uno::RuntimeException)
189 {
190     return msforms::fmDropButtonStyle::fmDropButtonStyleArrow;
191 }
192 
193 void SAL_CALL ScVbaComboBox::setDropButtonStyle( sal_Int32 /*nDropButtonStyle*/ ) throw (uno::RuntimeException)
194 {
195 }
196 
197 sal_Int32 SAL_CALL ScVbaComboBox::getDragBehavior() throw (uno::RuntimeException)
198 {
199     return msforms::fmDragBehavior::fmDragBehaviorDisabled;
200 }
201 
202 void SAL_CALL ScVbaComboBox::setDragBehavior( sal_Int32 /*nDragBehavior*/ ) throw (uno::RuntimeException)
203 {
204 }
205 
206 sal_Int32 SAL_CALL ScVbaComboBox::getEnterFieldBehavior() throw (uno::RuntimeException)
207 {
208     return msforms::fmEnterFieldBehavior::fmEnterFieldBehaviorSelectAll;
209 }
210 
211 void SAL_CALL ScVbaComboBox::setEnterFieldBehavior( sal_Int32 /*nEnterFieldBehavior*/ ) throw (uno::RuntimeException)
212 {
213 }
214 
215 sal_Int32 SAL_CALL ScVbaComboBox::getListStyle() throw (uno::RuntimeException)
216 {
217     return msforms::fmListStyle::fmListStylePlain;
218 }
219 
220 void SAL_CALL ScVbaComboBox::setListStyle( sal_Int32 /*nListStyle*/ ) throw (uno::RuntimeException)
221 {
222 }
223 
224 sal_Int32 SAL_CALL ScVbaComboBox::getTextAlign() throw (uno::RuntimeException)
225 {
226     return msforms::fmTextAlign::fmTextAlignLeft;
227 }
228 
229 void SAL_CALL ScVbaComboBox::setTextAlign( sal_Int32 /*nTextAlign*/ ) throw (uno::RuntimeException)
230 {
231 }
232 
233 sal_Int32 SAL_CALL ScVbaComboBox::getTextLength() throw (uno::RuntimeException)
234 {
235     return getText().getLength();
236 }
237 
238 uno::Reference< msforms::XNewFont > SAL_CALL ScVbaComboBox::getFont() throw (uno::RuntimeException)
239 {
240     return new VbaNewFont( this, mxContext, m_xProps );
241 }
242 
243 rtl::OUString&
244 ScVbaComboBox::getServiceImplName()
245 {
246 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaComboBox") );
247 	return sImplName;
248 }
249 
250 uno::Sequence< rtl::OUString >
251 ScVbaComboBox::getServiceNames()
252 {
253 	static uno::Sequence< rtl::OUString > aServiceNames;
254 	if ( aServiceNames.getLength() == 0 )
255 	{
256 		aServiceNames.realloc( 1 );
257 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.msforms.ComboBox" ) );
258 	}
259 	return aServiceNames;
260 }
261