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