server: Add functions for conversions between server user handles and pointer-style...
[wine] / dlls / user32 / property.c
1 /*
2  * Window properties
3  *
4  * Copyright 1995, 1996, 2001 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <string.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wownt32.h"
30 #include "wine/unicode.h"
31 #include "wine/winuser16.h"
32 #include "wine/server.h"
33
34 /* size of buffer needed to store an atom string */
35 #define ATOM_BUFFER_SIZE 256
36
37
38 /***********************************************************************
39  *              get_properties
40  *
41  * Retrieve the list of properties of a given window.
42  * Returned buffer must be freed by caller.
43  */
44 static property_data_t *get_properties( HWND hwnd, int *count )
45 {
46     property_data_t *data;
47     int total = 32;
48
49     while (total)
50     {
51         int res = 0;
52         if (!(data = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*data) ))) break;
53         *count = 0;
54         SERVER_START_REQ( get_window_properties )
55         {
56             req->window = wine_server_user_handle( hwnd );
57             wine_server_set_reply( req, data, total * sizeof(*data) );
58             if (!wine_server_call( req )) res = reply->total;
59         }
60         SERVER_END_REQ;
61         if (res && res <= total)
62         {
63             *count = res;
64             return data;
65         }
66         HeapFree( GetProcessHeap(), 0, data );
67         total = res;  /* restart with larger buffer */
68     }
69     return NULL;
70 }
71
72
73 /***********************************************************************
74  *              EnumPropsA_relay
75  *
76  * relay to call the EnumProps callback function from EnumPropsEx
77  */
78 static BOOL CALLBACK EnumPropsA_relay( HWND hwnd, LPSTR str, HANDLE handle, ULONG_PTR lparam )
79 {
80     PROPENUMPROCA func = (PROPENUMPROCA)lparam;
81     return func( hwnd, str, handle );
82 }
83
84
85 /***********************************************************************
86  *              EnumPropsW_relay
87  *
88  * relay to call the EnumProps callback function from EnumPropsEx
89  */
90 static BOOL CALLBACK EnumPropsW_relay( HWND hwnd, LPWSTR str, HANDLE handle, ULONG_PTR lparam )
91 {
92     PROPENUMPROCW func = (PROPENUMPROCW)lparam;
93     return func( hwnd, str, handle );
94 }
95
96
97 /***********************************************************************
98  *              EnumPropsA   (USER32.@)
99  */
100 INT WINAPI EnumPropsA( HWND hwnd, PROPENUMPROCA func )
101 {
102     return EnumPropsExA( hwnd, EnumPropsA_relay, (LPARAM)func );
103 }
104
105
106 /***********************************************************************
107  *              EnumPropsW   (USER32.@)
108  */
109 INT WINAPI EnumPropsW( HWND hwnd, PROPENUMPROCW func )
110 {
111     return EnumPropsExW( hwnd, EnumPropsW_relay, (LPARAM)func );
112 }
113
114
115 /***********************************************************************
116  *              GetPropA   (USER32.@)
117  */
118 HANDLE WINAPI GetPropA( HWND hwnd, LPCSTR str )
119 {
120     WCHAR buffer[ATOM_BUFFER_SIZE];
121
122     if (!HIWORD(str)) return GetPropW( hwnd, (LPCWSTR)str );
123     if (!MultiByteToWideChar( CP_ACP, 0, str, -1, buffer, ATOM_BUFFER_SIZE )) return 0;
124     return GetPropW( hwnd, buffer );
125 }
126
127
128 /***********************************************************************
129  *              GetPropW   (USER32.@)
130  */
131 HANDLE WINAPI GetPropW( HWND hwnd, LPCWSTR str )
132 {
133     HANDLE ret = 0;
134
135     SERVER_START_REQ( get_window_property )
136     {
137         req->window = wine_server_user_handle( hwnd );
138         if (!HIWORD(str)) req->atom = LOWORD(str);
139         else wine_server_add_data( req, str, strlenW(str) * sizeof(WCHAR) );
140         if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->handle );
141     }
142     SERVER_END_REQ;
143     return ret;
144 }
145
146
147 /***********************************************************************
148  *              SetPropA   (USER32.@)
149  */
150 BOOL WINAPI SetPropA( HWND hwnd, LPCSTR str, HANDLE handle )
151 {
152     WCHAR buffer[ATOM_BUFFER_SIZE];
153
154     if (!HIWORD(str)) return SetPropW( hwnd, (LPCWSTR)str, handle );
155     if (!MultiByteToWideChar( CP_ACP, 0, str, -1, buffer, ATOM_BUFFER_SIZE )) return FALSE;
156     return SetPropW( hwnd, buffer, handle );
157 }
158
159
160 /***********************************************************************
161  *              SetPropW   (USER32.@)
162  */
163 BOOL WINAPI SetPropW( HWND hwnd, LPCWSTR str, HANDLE handle )
164 {
165     BOOL ret;
166
167     SERVER_START_REQ( set_window_property )
168     {
169         req->window = wine_server_user_handle( hwnd );
170         req->handle = wine_server_obj_handle( handle );
171         if (!HIWORD(str)) req->atom = LOWORD(str);
172         else wine_server_add_data( req, str, strlenW(str) * sizeof(WCHAR) );
173         ret = !wine_server_call_err( req );
174     }
175     SERVER_END_REQ;
176     return ret;
177 }
178
179
180 /***********************************************************************
181  *              RemovePropA   (USER32.@)
182  */
183 HANDLE WINAPI RemovePropA( HWND hwnd, LPCSTR str )
184 {
185     WCHAR buffer[ATOM_BUFFER_SIZE];
186
187     if (!HIWORD(str)) return RemovePropW( hwnd, (LPCWSTR)str );
188     if (!MultiByteToWideChar( CP_ACP, 0, str, -1, buffer, ATOM_BUFFER_SIZE )) return 0;
189     return RemovePropW( hwnd, buffer );
190 }
191
192
193 /***********************************************************************
194  *              RemovePropW   (USER32.@)
195  */
196 HANDLE WINAPI RemovePropW( HWND hwnd, LPCWSTR str )
197 {
198     HANDLE ret = 0;
199
200     SERVER_START_REQ( remove_window_property )
201     {
202         req->window = wine_server_user_handle( hwnd );
203         if (!HIWORD(str)) req->atom = LOWORD(str);
204         else wine_server_add_data( req, str, strlenW(str) * sizeof(WCHAR) );
205         if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->handle );
206     }
207     SERVER_END_REQ;
208
209     return ret;
210 }
211
212
213 /***********************************************************************
214  *              EnumPropsExA   (USER32.@)
215  */
216 INT WINAPI EnumPropsExA(HWND hwnd, PROPENUMPROCEXA func, LPARAM lParam)
217 {
218     int ret = -1, i, count;
219     property_data_t *list = get_properties( hwnd, &count );
220
221     if (list)
222     {
223         for (i = 0; i < count; i++)
224         {
225             char string[ATOM_BUFFER_SIZE];
226             if (!GlobalGetAtomNameA( list[i].atom, string, ATOM_BUFFER_SIZE )) continue;
227             if (!(ret = func( hwnd, string, wine_server_ptr_handle(list[i].handle), lParam ))) break;
228         }
229         HeapFree( GetProcessHeap(), 0, list );
230     }
231     return ret;
232 }
233
234
235 /***********************************************************************
236  *              EnumPropsExW   (USER32.@)
237  */
238 INT WINAPI EnumPropsExW(HWND hwnd, PROPENUMPROCEXW func, LPARAM lParam)
239 {
240     int ret = -1, i, count;
241     property_data_t *list = get_properties( hwnd, &count );
242
243     if (list)
244     {
245         for (i = 0; i < count; i++)
246         {
247             WCHAR string[ATOM_BUFFER_SIZE];
248             if (!GlobalGetAtomNameW( list[i].atom, string, ATOM_BUFFER_SIZE )) continue;
249             if (!(ret = func( hwnd, string, wine_server_ptr_handle(list[i].handle), lParam ))) break;
250         }
251         HeapFree( GetProcessHeap(), 0, list );
252     }
253     return ret;
254 }
255
256
257 /***********************************************************************
258  *              EnumProps   (USER.27)
259  */
260 INT16 WINAPI EnumProps16( HWND16 hwnd, PROPENUMPROC16 func )
261 {
262     int ret = -1, i, count;
263     property_data_t *list = get_properties( HWND_32(hwnd), &count );
264
265     if (list)
266     {
267         char string[ATOM_BUFFER_SIZE];
268         SEGPTR segptr = MapLS( string );
269         WORD args[4];
270         DWORD result;
271
272         for (i = 0; i < count; i++)
273         {
274             if (list[i].string)  /* it was a string originally */
275             {
276                 if (!GlobalGetAtomNameA( list[i].atom, string, ATOM_BUFFER_SIZE )) continue;
277                 args[3] = hwnd;
278                 args[2] = SELECTOROF(segptr);
279                 args[1] = OFFSETOF(segptr);
280                 args[0] = LOWORD(wine_server_ptr_handle(list[i].handle));
281             }
282             else
283             {
284                 args[3] = hwnd;
285                 args[2] = 0;
286                 args[1] = list[i].atom;
287                 args[0] = LOWORD(wine_server_ptr_handle(list[i].handle));
288             }
289             WOWCallback16Ex( (DWORD)func, WCB16_PASCAL, sizeof(args), args, &result );
290             if (!(ret = LOWORD(result))) break;
291         }
292         UnMapLS( segptr );
293         HeapFree( GetProcessHeap(), 0, list );
294     }
295     return ret;
296 }