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