Various cosmetic changes.
[wine] / dlls / user / property.c
1 /*
2  * Window properties
3  *
4  * Copyright 1995, 1996, 2001 Alexandre Julliard
5  */
6
7 #include <string.h>
8
9 #include "windef.h"
10 #include "wingdi.h"
11 #include "wine/winuser16.h"
12 #include "wine/server.h"
13
14 /* size of buffer needed to store an atom string */
15 #define ATOM_BUFFER_SIZE 256
16
17 /* ### start build ### */
18 extern WORD CALLBACK PROP_CallTo16_word_wlw(PROPENUMPROC16,WORD,LONG,WORD);
19 /* ### stop build ### */
20
21 /***********************************************************************
22  *              get_properties
23  *
24  * Retrieve the list of properties of a given window.
25  * Returned buffer must be freed by caller.
26  */
27 static property_data_t *get_properties( HWND hwnd, int *count )
28 {
29     property_data_t *data;
30     int total = 32;
31
32     while (total)
33     {
34         int res = 0;
35         if (!(data = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*data) ))) break;
36         *count = 0;
37         SERVER_START_REQ( get_window_properties )
38         {
39             req->window = hwnd;
40             wine_server_add_data( req, data, total * sizeof(*data) );
41             if (!wine_server_call( req )) res = reply->total;
42         }
43         SERVER_END_REQ;
44         if (res && res <= total)
45         {
46             *count = res;
47             return data;
48         }
49         HeapFree( GetProcessHeap(), 0, data );
50         total = res;  /* restart with larger buffer */
51     }
52     return NULL;
53 }
54
55
56 /***********************************************************************
57  *              EnumPropsA_relay
58  *
59  * relay to call the EnumProps callback function from EnumPropsEx
60  */
61 static BOOL CALLBACK EnumPropsA_relay( HWND hwnd, LPCSTR str, HANDLE handle, ULONG_PTR lparam )
62 {
63     PROPENUMPROCA func = (PROPENUMPROCA)lparam;
64     return func( hwnd, str, handle );
65 }
66
67
68 /***********************************************************************
69  *              EnumPropsW_relay
70  *
71  * relay to call the EnumProps callback function from EnumPropsEx
72  */
73 static BOOL CALLBACK EnumPropsW_relay( HWND hwnd, LPCWSTR str, HANDLE handle, ULONG_PTR lparam )
74 {
75     PROPENUMPROCW func = (PROPENUMPROCW)lparam;
76     return func( hwnd, str, handle );
77 }
78
79
80 /***********************************************************************
81  *              EnumPropsA   (USER32.@)
82  */
83 INT WINAPI EnumPropsA( HWND hwnd, PROPENUMPROCA func )
84 {
85     return EnumPropsExA( hwnd, EnumPropsA_relay, (LPARAM)func );
86 }
87
88
89 /***********************************************************************
90  *              EnumPropsW   (USER32.@)
91  */
92 INT WINAPI EnumPropsW( HWND hwnd, PROPENUMPROCW func )
93 {
94     return EnumPropsExW( hwnd, EnumPropsW_relay, (LPARAM)func );
95 }
96
97
98 /***********************************************************************
99  *              GetPropA   (USER32.@)
100  */
101 HANDLE WINAPI GetPropA( HWND hwnd, LPCSTR str )
102 {
103     ATOM atom;
104     HANDLE ret = 0;
105
106     if (!HIWORD(str)) atom = LOWORD(str);
107     else if (!(atom = GlobalFindAtomA( str ))) return 0;
108
109     SERVER_START_REQ( get_window_property )
110     {
111         req->window = hwnd;
112         req->atom = atom;
113         if (!wine_server_call_err( req )) ret = reply->handle;
114     }
115     SERVER_END_REQ;
116     return ret;
117 }
118
119
120 /***********************************************************************
121  *              GetPropW   (USER32.@)
122  */
123 HANDLE WINAPI GetPropW( HWND hwnd, LPCWSTR str )
124 {
125     ATOM atom;
126     HANDLE ret = 0;
127
128     if (!HIWORD(str)) atom = LOWORD(str);
129     else if (!(atom = GlobalFindAtomW( str ))) return 0;
130
131     SERVER_START_REQ( get_window_property )
132     {
133         req->window = hwnd;
134         req->atom = atom;
135         if (!wine_server_call_err( req )) ret = reply->handle;
136     }
137     SERVER_END_REQ;
138     return ret;
139 }
140
141
142 /***********************************************************************
143  *              SetPropA   (USER32.@)
144  */
145 BOOL WINAPI SetPropA( HWND hwnd, LPCSTR str, HANDLE handle )
146 {
147     ATOM atom;
148     BOOL ret;
149
150     if (!HIWORD(str)) atom = LOWORD(str);
151     else if (!(atom = GlobalAddAtomA( str ))) return FALSE;
152
153     SERVER_START_REQ( set_window_property )
154     {
155         req->window = hwnd;
156         req->atom   = atom;
157         req->string = (HIWORD(str) != 0);
158         req->handle = handle;
159         ret = !wine_server_call_err( req );
160     }
161     SERVER_END_REQ;
162
163     if (HIWORD(str)) GlobalDeleteAtom( atom );
164     return ret;
165 }
166
167
168 /***********************************************************************
169  *              SetPropW   (USER32.@)
170  */
171 BOOL WINAPI SetPropW( HWND hwnd, LPCWSTR str, HANDLE handle )
172 {
173     ATOM atom;
174     BOOL ret;
175
176     if (!HIWORD(str)) atom = LOWORD(str);
177     else if (!(atom = GlobalAddAtomW( str ))) return FALSE;
178
179     SERVER_START_REQ( set_window_property )
180     {
181         req->window = hwnd;
182         req->atom   = atom;
183         req->string = (HIWORD(str) != 0);
184         req->handle = handle;
185         ret = !wine_server_call_err( req );
186     }
187     SERVER_END_REQ;
188
189     if (HIWORD(str)) GlobalDeleteAtom( atom );
190     return ret;
191 }
192
193
194 /***********************************************************************
195  *              RemovePropA   (USER32.@)
196  */
197 HANDLE WINAPI RemovePropA( HWND hwnd, LPCSTR str )
198 {
199     ATOM atom;
200     HANDLE ret = 0;
201
202     if (!HIWORD(str)) return RemovePropW( hwnd, MAKEINTATOMW(LOWORD(str)) );
203
204     if ((atom = GlobalAddAtomA( str )))
205     {
206         ret = RemovePropW( hwnd, MAKEINTATOMW(atom) );
207         GlobalDeleteAtom( atom );
208     }
209     return ret;
210 }
211
212
213 /***********************************************************************
214  *              RemovePropW   (USER32.@)
215  */
216 HANDLE WINAPI RemovePropW( HWND hwnd, LPCWSTR str )
217 {
218     ATOM atom;
219     HANDLE ret = 0;
220
221     if (!HIWORD(str)) atom = LOWORD(str);
222     else if (!(atom = GlobalAddAtomW( str ))) return 0;
223
224     SERVER_START_REQ( remove_window_property )
225     {
226         req->window = hwnd;
227         req->atom   = atom;
228         if (!wine_server_call_err( req )) ret = reply->handle;
229     }
230     SERVER_END_REQ;
231
232     if (HIWORD(str)) GlobalDeleteAtom( atom );
233     return ret;
234 }
235
236
237 /***********************************************************************
238  *              EnumPropsExA   (USER32.@)
239  */
240 INT WINAPI EnumPropsExA(HWND hwnd, PROPENUMPROCEXA func, LPARAM lParam)
241 {
242     int ret = -1, i, count;
243     property_data_t *list = get_properties( hwnd, &count );
244
245     if (list)
246     {
247         for (i = 0; i < count; i++)
248         {
249             char string[ATOM_BUFFER_SIZE];
250             if (!GlobalGetAtomNameA( list[i].atom, string, ATOM_BUFFER_SIZE )) continue;
251             if (!(ret = func( hwnd, string, list[i].handle, lParam ))) break;
252         }
253         HeapFree( GetProcessHeap(), 0, list );
254     }
255     return ret;
256 }
257
258
259 /***********************************************************************
260  *              EnumPropsExW   (USER32.@)
261  */
262 INT WINAPI EnumPropsExW(HWND hwnd, PROPENUMPROCEXW func, LPARAM lParam)
263 {
264     int ret = -1, i, count;
265     property_data_t *list = get_properties( hwnd, &count );
266
267     if (list)
268     {
269         for (i = 0; i < count; i++)
270         {
271             WCHAR string[ATOM_BUFFER_SIZE];
272             if (!GlobalGetAtomNameW( list[i].atom, string, ATOM_BUFFER_SIZE )) continue;
273             if (!(ret = func( hwnd, string, list[i].handle, lParam ))) break;
274         }
275         HeapFree( GetProcessHeap(), 0, list );
276     }
277     return ret;
278 }
279
280
281 /***********************************************************************
282  *              EnumProps   (USER.27)
283  */
284 INT16 WINAPI EnumProps16( HWND16 hwnd, PROPENUMPROC16 func )
285 {
286     int ret = -1, i, count;
287     property_data_t *list = get_properties( hwnd, &count );
288
289     if (list)
290     {
291         char string[ATOM_BUFFER_SIZE];
292         SEGPTR segptr = MapLS( string );
293         for (i = 0; i < count; i++)
294         {
295             if (list[i].string)  /* it was a string originally */
296             {
297                 if (!GlobalGetAtomNameA( list[i].atom, string, ATOM_BUFFER_SIZE )) continue;
298                 ret = PROP_CallTo16_word_wlw( func, hwnd, segptr, list[i].handle );
299             }
300             else
301                 ret = PROP_CallTo16_word_wlw( func, hwnd, list[i].atom, list[i].handle );
302             if (!ret) break;
303         }
304         UnMapLS( segptr );
305         HeapFree( GetProcessHeap(), 0, list );
306     }
307     return ret;
308 }