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 //------------------------------------------------------------------------
25 // includes
26 //------------------------------------------------------------------------
27 
28 #include <osl/diagnose.h>
29 #include "AutoBuffer.hxx"
30 #include "WinImplHelper.hxx"
31 #include <com/sun/star/uno/Sequence.hxx>
32 
33 #include <systools/win32/user9x.h>
34 
35 //------------------------------------------------------------
36 // namespace directives
37 //------------------------------------------------------------
38 
39 using rtl::OUString;
40 using ::com::sun::star::lang::IllegalArgumentException;
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::XInterface;
43 using ::com::sun::star::uno::Any;
44 using ::com::sun::star::uno::Sequence;
45 
46 //------------------------------------------------------------
47 // determine if we are running under Win2000
48 //------------------------------------------------------------
49 
IsWin2000()50 sal_Bool SAL_CALL IsWin2000( )
51 {
52 	OSVERSIONINFOEX osvi;
53 	BOOL bOsVersionInfoEx;
54 	sal_Bool bRet = sal_False;
55 
56 	osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFOEX );
57 	bOsVersionInfoEx = GetVersionEx( ( OSVERSIONINFO* )&osvi );
58 	if( !bOsVersionInfoEx )
59 	{
60 		// if OSVERSIONINFOEX doesn't work
61 		osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
62 		if( !GetVersionEx( ( OSVERSIONINFO* )&osvi ) )
63 			return sal_False;
64 	}
65 
66 	if( ( VER_PLATFORM_WIN32_NT == osvi.dwPlatformId ) && ( osvi.dwMajorVersion >= 5 ) )
67 		bRet = sal_True;
68 
69 	return bRet;
70 }
71 
72 //------------------------------------------------------------
73 //
74 //------------------------------------------------------------
75 
ListboxAddString(HWND hwnd,const OUString & aString)76 void SAL_CALL ListboxAddString( HWND hwnd, const OUString& aString )
77 {
78     LRESULT rc = SendMessageW(
79         hwnd, CB_ADDSTRING, 0, reinterpret_cast< LPARAM >(aString.getStr( )) );
80 
81     OSL_ASSERT( (CB_ERR != rc) && (CB_ERRSPACE != rc) );
82 }
83 
84 //------------------------------------------------------------
85 //
86 //------------------------------------------------------------
87 
ListboxGetString(HWND hwnd,sal_Int32 aPosition)88 OUString SAL_CALL ListboxGetString( HWND hwnd, sal_Int32 aPosition )
89 {
90     OSL_ASSERT( IsWindow( hwnd ) );
91 
92     OUString aString;
93 
94 	LRESULT lItem =
95         SendMessageW( hwnd, CB_GETLBTEXTLEN, aPosition, 0 );
96 
97 	if ( (CB_ERR != lItem) && (lItem > 0) )
98 	{
99 	    // message returns the len of a combobox item
100 		// without trailing '\0' that's why += 1
101 		lItem++;
102 
103         CAutoUnicodeBuffer aBuff( lItem );
104 
105 		LRESULT lRet =
106             SendMessageW(
107                 hwnd, CB_GETLBTEXT, aPosition,
108                 reinterpret_cast<LPARAM>(&aBuff) );
109 
110         OSL_ASSERT( lRet != CB_ERR );
111 
112 	    if ( CB_ERR != lRet )
113             aString = OUString( aBuff, lRet );
114     }
115 
116     return aString;
117 }
118 
119 //------------------------------------------------------------
120 //
121 //------------------------------------------------------------
122 
ListboxAddItem(HWND hwnd,const Any & aItem,const Reference<XInterface> & rXInterface,sal_Int16 aArgPos)123 void SAL_CALL ListboxAddItem( HWND hwnd, const Any& aItem, const Reference< XInterface >& rXInterface, sal_Int16 aArgPos )
124     throw( IllegalArgumentException )
125 {
126     OSL_ASSERT( IsWindow( hwnd ) );
127 
128     if ( !aItem.hasValue( ) ||
129          aItem.getValueType( ) != getCppuType((OUString*)0) )
130          throw IllegalArgumentException(
131             OUString::createFromAscii( "invalid value type or any has no value" ),
132             rXInterface,
133             aArgPos );
134 
135     OUString cbItem;
136     aItem >>= cbItem;
137 
138     ListboxAddString( hwnd, cbItem );
139 }
140 
141 //------------------------------------------------------------
142 //
143 //------------------------------------------------------------
144 
ListboxAddItems(HWND hwnd,const Any & aItemList,const Reference<XInterface> & rXInterface,sal_Int16 aArgPos)145 void SAL_CALL ListboxAddItems( HWND hwnd, const Any& aItemList, const Reference< XInterface >& rXInterface, sal_Int16 aArgPos )
146     throw( IllegalArgumentException )
147 {
148     OSL_ASSERT( IsWindow( hwnd ) );
149 
150     if ( !aItemList.hasValue( ) ||
151          aItemList.getValueType( ) != getCppuType((Sequence<OUString>*)0) )
152          throw IllegalArgumentException(
153             OUString::createFromAscii( "invalid value type or any has no value" ),
154             rXInterface,
155             aArgPos );
156 
157     Sequence< OUString > aStringList;
158     aItemList >>= aStringList;
159 
160     sal_Int32 nItemCount = aStringList.getLength( );
161     for( sal_Int32 i = 0; i < nItemCount; i++ )
162     {
163         ListboxAddString( hwnd, aStringList[i] );
164     }
165 }
166 
167 //------------------------------------------------------------
168 //
169 //------------------------------------------------------------
170 
ListboxDeleteItem(HWND hwnd,const Any & aPosition,const Reference<XInterface> & rXInterface,sal_Int16 aArgPos)171 void SAL_CALL ListboxDeleteItem( HWND hwnd, const Any& aPosition, const Reference< XInterface >& rXInterface, sal_Int16 aArgPos )
172     throw( IllegalArgumentException )
173 {
174     OSL_ASSERT( IsWindow( hwnd ) );
175 
176     if ( !aPosition.hasValue( ) ||
177          ( (aPosition.getValueType( ) != getCppuType((sal_Int32*)0)) &&
178            (aPosition.getValueType( ) != getCppuType((sal_Int16*)0)) &&
179            (aPosition.getValueType( ) != getCppuType((sal_Int8*)0)) ) )
180          throw IllegalArgumentException(
181             OUString::createFromAscii( "invalid value type or any has no value" ),
182             rXInterface,
183             aArgPos );
184 
185     sal_Int32 nPos;
186     aPosition >>= nPos;
187 
188     LRESULT lRet = SendMessage( hwnd, CB_DELETESTRING, nPos, 0 );
189 
190     // if the return value is CB_ERR the given
191     // index was not correct
192     if ( CB_ERR == lRet )
193         throw IllegalArgumentException(
194             OUString::createFromAscii( "inavlid item position" ),
195             rXInterface,
196             aArgPos );
197 }
198 
199 //------------------------------------------------------------
200 //
201 //------------------------------------------------------------
202 
ListboxDeleteItems(HWND hwnd,const Any &,const Reference<XInterface> & rXInterface,sal_Int16 aArgPos)203 void SAL_CALL ListboxDeleteItems( HWND hwnd, const Any& /*unused*/, const Reference< XInterface >& rXInterface, sal_Int16 aArgPos )
204     throw( IllegalArgumentException )
205 {
206     OSL_ASSERT( IsWindow( hwnd ) );
207 
208     LRESULT lRet = 0;
209 
210     do
211     {
212         // the return value on success is the number
213         // of remaining elements in the listbox
214         lRet = SendMessageW( hwnd, CB_DELETESTRING, 0, 0 );
215     }
216     while ( (lRet != CB_ERR) && (lRet > 0) );
217 }
218 
219 //------------------------------------------------------------
220 //
221 //------------------------------------------------------------
222 
ListboxSetSelectedItem(HWND hwnd,const Any & aPosition,const Reference<XInterface> & rXInterface,sal_Int16 aArgPos)223 void SAL_CALL ListboxSetSelectedItem( HWND hwnd, const Any& aPosition, const Reference< XInterface >& rXInterface, sal_Int16 aArgPos )
224     throw( IllegalArgumentException )
225 {
226     OSL_ASSERT( IsWindow( hwnd ) );
227 
228      if ( !aPosition.hasValue( ) ||
229          ( (aPosition.getValueType( ) != getCppuType((sal_Int32*)0)) &&
230            (aPosition.getValueType( ) != getCppuType((sal_Int16*)0)) &&
231            (aPosition.getValueType( ) != getCppuType((sal_Int8*)0)) ) )
232          throw IllegalArgumentException(
233             OUString::createFromAscii( "invalid value type or any has no value" ),
234             rXInterface,
235             aArgPos );
236 
237     sal_Int32 nPos;
238     aPosition >>= nPos;
239 
240     if ( nPos < -1 )
241         throw IllegalArgumentException(
242             OUString::createFromAscii("invalid index"),
243             rXInterface,
244             aArgPos );
245 
246     LRESULT lRet = SendMessageW( hwnd, CB_SETCURSEL, nPos, 0 );
247 
248     if ( (CB_ERR == lRet) && (-1 != nPos) )
249         throw IllegalArgumentException(
250             OUString::createFromAscii("invalid index"),
251             rXInterface,
252             aArgPos );
253 }
254 
255 //------------------------------------------------------------
256 //
257 //------------------------------------------------------------
258 
ListboxGetItems(HWND hwnd)259 Any SAL_CALL ListboxGetItems( HWND hwnd )
260 {
261     OSL_ASSERT( IsWindow( hwnd ) );
262 
263     LRESULT nItemCount = SendMessageW( hwnd, CB_GETCOUNT, 0, 0 );
264 
265     Sequence< OUString > aItemList;
266 
267     if ( CB_ERR != nItemCount )
268     {
269         aItemList.realloc( nItemCount );
270 
271         for ( sal_Int32 i = 0; i < nItemCount; i++ )
272         {
273             aItemList[i] = ListboxGetString( hwnd, i );
274         }
275     }
276 
277     Any aAny;
278     aAny <<= aItemList;
279 
280     return aAny;
281 }
282 
283 //------------------------------------------------------------
284 //
285 //------------------------------------------------------------
286 
ListboxGetSelectedItem(HWND hwnd)287 Any SAL_CALL ListboxGetSelectedItem( HWND hwnd )
288 {
289     OSL_ASSERT( IsWindow( hwnd ) );
290 
291     LRESULT idxItem = SendMessageW( hwnd, CB_GETCURSEL, 0, 0 );
292 
293     Any aAny;
294     aAny <<= ListboxGetString( hwnd, idxItem );
295 
296     return aAny;
297 }
298 
299 //------------------------------------------------------------
300 //
301 //------------------------------------------------------------
302 
CheckboxGetState(HWND hwnd)303 Any SAL_CALL CheckboxGetState( HWND hwnd )
304 {
305     OSL_ASSERT( IsWindow( hwnd ) );
306 
307     LRESULT lChkState = SendMessageW( hwnd, BM_GETCHECK, 0, 0 );
308     sal_Bool bChkState = (lChkState == BST_CHECKED) ? sal_True : sal_False;
309     Any aAny;
310     aAny.setValue( &bChkState, getCppuType((sal_Bool*)0) );
311     return aAny;
312 }
313 
314 //------------------------------------------------------------
315 //
316 //------------------------------------------------------------
317 
CheckboxSetState(HWND hwnd,const::com::sun::star::uno::Any & aState,const Reference<XInterface> & rXInterface,sal_Int16 aArgPos)318 void SAL_CALL CheckboxSetState(
319     HWND hwnd, const ::com::sun::star::uno::Any& aState, const Reference< XInterface >& rXInterface, sal_Int16 aArgPos )
320     throw( IllegalArgumentException )
321 {
322     OSL_ASSERT( IsWindow( hwnd ) );
323 
324     if ( !aState.hasValue( ) ||
325          aState.getValueType( ) != getCppuType((sal_Bool*)0) )
326          throw IllegalArgumentException(
327             OUString::createFromAscii( "invalid value type or any has no value" ),
328             rXInterface,
329             aArgPos );
330 
331     sal_Bool bCheckState = *reinterpret_cast< const sal_Bool* >( aState.getValue( ) );
332     WPARAM wParam = bCheckState ? BST_CHECKED : BST_UNCHECKED;
333     SendMessageW( hwnd, BM_SETCHECK, wParam, 0 );
334 }
335 
336 //------------------------------------------------------------
337 //
338 //------------------------------------------------------------
339 
_wcslenex(const sal_Unicode * pStr)340 sal_uInt32 SAL_CALL _wcslenex( const sal_Unicode* pStr )
341 {
342     if ( !pStr )
343         return 0;
344 
345     const sal_Unicode* pTemp = pStr;
346     sal_uInt32 strLen = 0;
347     while( *pTemp || *(pTemp + 1) )
348     {
349         pTemp++;
350         strLen++;
351     }
352 
353     return strLen;
354 }
355