xref: /trunk/main/sysui/source/win32/misc/WinImplHelper.cxx (revision 9d37da743abb7db0a497688391f6e55a19f27c44)
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 
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 
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 
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 
123 void SAL_CALL ListboxAddItem( HWND hwnd, const Any& aItem, const Reference< XInterface >& rXInterface, sal_Int16 aArgPos )
124 {
125     OSL_ASSERT( IsWindow( hwnd ) );
126 
127     if ( !aItem.hasValue( ) ||
128          aItem.getValueType( ) != getCppuType((OUString*)0) )
129          throw IllegalArgumentException(
130             OUString::createFromAscii( "invalid value type or any has no value" ),
131             rXInterface,
132             aArgPos );
133 
134     OUString cbItem;
135     aItem >>= cbItem;
136 
137     ListboxAddString( hwnd, cbItem );
138 }
139 
140 //------------------------------------------------------------
141 //
142 //------------------------------------------------------------
143 
144 void SAL_CALL ListboxAddItems( HWND hwnd, const Any& aItemList, const Reference< XInterface >& rXInterface, sal_Int16 aArgPos )
145 {
146     OSL_ASSERT( IsWindow( hwnd ) );
147 
148     if ( !aItemList.hasValue( ) ||
149          aItemList.getValueType( ) != getCppuType((Sequence<OUString>*)0) )
150          throw IllegalArgumentException(
151             OUString::createFromAscii( "invalid value type or any has no value" ),
152             rXInterface,
153             aArgPos );
154 
155     Sequence< OUString > aStringList;
156     aItemList >>= aStringList;
157 
158     sal_Int32 nItemCount = aStringList.getLength( );
159     for( sal_Int32 i = 0; i < nItemCount; i++ )
160     {
161         ListboxAddString( hwnd, aStringList[i] );
162     }
163 }
164 
165 //------------------------------------------------------------
166 //
167 //------------------------------------------------------------
168 
169 void SAL_CALL ListboxDeleteItem( HWND hwnd, const Any& aPosition, const Reference< XInterface >& rXInterface, sal_Int16 aArgPos )
170 {
171     OSL_ASSERT( IsWindow( hwnd ) );
172 
173     if ( !aPosition.hasValue( ) ||
174          ( (aPosition.getValueType( ) != getCppuType((sal_Int32*)0)) &&
175            (aPosition.getValueType( ) != getCppuType((sal_Int16*)0)) &&
176            (aPosition.getValueType( ) != getCppuType((sal_Int8*)0)) ) )
177          throw IllegalArgumentException(
178             OUString::createFromAscii( "invalid value type or any has no value" ),
179             rXInterface,
180             aArgPos );
181 
182     sal_Int32 nPos;
183     aPosition >>= nPos;
184 
185     LRESULT lRet = SendMessage( hwnd, CB_DELETESTRING, nPos, 0 );
186 
187     // if the return value is CB_ERR the given
188     // index was not correct
189     if ( CB_ERR == lRet )
190         throw IllegalArgumentException(
191             OUString::createFromAscii( "invalid item position" ),
192             rXInterface,
193             aArgPos );
194 }
195 
196 //------------------------------------------------------------
197 //
198 //------------------------------------------------------------
199 
200 void SAL_CALL ListboxDeleteItems( HWND hwnd, const Any& /*unused*/, const Reference< XInterface >& rXInterface, sal_Int16 aArgPos )
201 {
202     OSL_ASSERT( IsWindow( hwnd ) );
203 
204     LRESULT lRet = 0;
205 
206     do
207     {
208         // the return value on success is the number
209         // of remaining elements in the listbox
210         lRet = SendMessageW( hwnd, CB_DELETESTRING, 0, 0 );
211     }
212     while ( (lRet != CB_ERR) && (lRet > 0) );
213 }
214 
215 //------------------------------------------------------------
216 //
217 //------------------------------------------------------------
218 
219 void SAL_CALL ListboxSetSelectedItem( HWND hwnd, const Any& aPosition, const Reference< XInterface >& rXInterface, sal_Int16 aArgPos )
220 {
221     OSL_ASSERT( IsWindow( hwnd ) );
222 
223     if ( !aPosition.hasValue( ) ||
224          ( (aPosition.getValueType( ) != getCppuType((sal_Int32*)0)) &&
225            (aPosition.getValueType( ) != getCppuType((sal_Int16*)0)) &&
226            (aPosition.getValueType( ) != getCppuType((sal_Int8*)0)) ) )
227          throw IllegalArgumentException(
228             OUString::createFromAscii( "invalid value type or any has no value" ),
229             rXInterface,
230             aArgPos );
231 
232     sal_Int32 nPos;
233     aPosition >>= nPos;
234 
235     if ( nPos < -1 )
236         throw IllegalArgumentException(
237             OUString::createFromAscii("invalid index"),
238             rXInterface,
239             aArgPos );
240 
241     LRESULT lRet = SendMessageW( hwnd, CB_SETCURSEL, nPos, 0 );
242 
243     if ( (CB_ERR == lRet) && (-1 != nPos) )
244         throw IllegalArgumentException(
245             OUString::createFromAscii("invalid index"),
246             rXInterface,
247             aArgPos );
248 }
249 
250 //------------------------------------------------------------
251 //
252 //------------------------------------------------------------
253 
254 Any SAL_CALL ListboxGetItems( HWND hwnd )
255 {
256     OSL_ASSERT( IsWindow( hwnd ) );
257 
258     LRESULT nItemCount = SendMessageW( hwnd, CB_GETCOUNT, 0, 0 );
259 
260     Sequence< OUString > aItemList;
261 
262     if ( CB_ERR != nItemCount )
263     {
264         aItemList.realloc( nItemCount );
265 
266         for ( sal_Int32 i = 0; i < nItemCount; i++ )
267         {
268             aItemList[i] = ListboxGetString( hwnd, i );
269         }
270     }
271 
272     Any aAny;
273     aAny <<= aItemList;
274 
275     return aAny;
276 }
277 
278 //------------------------------------------------------------
279 //
280 //------------------------------------------------------------
281 
282 Any SAL_CALL ListboxGetSelectedItem( HWND hwnd )
283 {
284     OSL_ASSERT( IsWindow( hwnd ) );
285 
286     LRESULT idxItem = SendMessageW( hwnd, CB_GETCURSEL, 0, 0 );
287 
288     Any aAny;
289     aAny <<= ListboxGetString( hwnd, idxItem );
290 
291     return aAny;
292 }
293 
294 //------------------------------------------------------------
295 //
296 //------------------------------------------------------------
297 
298 Any SAL_CALL CheckboxGetState( HWND hwnd )
299 {
300     OSL_ASSERT( IsWindow( hwnd ) );
301 
302     LRESULT lChkState = SendMessageW( hwnd, BM_GETCHECK, 0, 0 );
303     sal_Bool bChkState = (lChkState == BST_CHECKED) ? sal_True : sal_False;
304     Any aAny;
305     aAny.setValue( &bChkState, getCppuType((sal_Bool*)0) );
306     return aAny;
307 }
308 
309 //------------------------------------------------------------
310 //
311 //------------------------------------------------------------
312 
313 void SAL_CALL CheckboxSetState(
314     HWND hwnd, const ::com::sun::star::uno::Any& aState, const Reference< XInterface >& rXInterface, sal_Int16 aArgPos )
315 {
316     OSL_ASSERT( IsWindow( hwnd ) );
317 
318     if ( !aState.hasValue( ) ||
319          aState.getValueType( ) != getCppuType((sal_Bool*)0) )
320          throw IllegalArgumentException(
321             OUString::createFromAscii( "invalid value type or any has no value" ),
322             rXInterface,
323             aArgPos );
324 
325     sal_Bool bCheckState = *reinterpret_cast< const sal_Bool* >( aState.getValue( ) );
326     WPARAM wParam = bCheckState ? BST_CHECKED : BST_UNCHECKED;
327     SendMessageW( hwnd, BM_SETCHECK, wParam, 0 );
328 }
329 
330 //------------------------------------------------------------
331 //
332 //------------------------------------------------------------
333 
334 sal_uInt32 SAL_CALL _wcslenex( const sal_Unicode* pStr )
335 {
336     if ( !pStr )
337         return 0;
338 
339     const sal_Unicode* pTemp = pStr;
340     sal_uInt32 strLen = 0;
341     while( *pTemp || *(pTemp + 1) )
342     {
343         pTemp++;
344         strLen++;
345     }
346 
347     return strLen;
348 }
349 
350 /* vim: set noet sw=4 ts=4: */
351