dsound: Add support for 24/32 bits input sound buffers.
[wine] / dlls / user32 / class.c
1 /*
2  * Window classes functions
3  *
4  * Copyright 1993, 1996, 2003 Alexandre Julliard
5  * Copyright 1998 Juergen Schmied (jsch)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "winerror.h"
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "wine/unicode.h"
35 #include "win.h"
36 #include "user_private.h"
37 #include "controls.h"
38 #include "wine/server.h"
39 #include "wine/list.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(class);
43
44 #define MAX_ATOM_LEN 255 /* from dlls/kernel32/atom.c */
45
46 typedef struct tagCLASS
47 {
48     struct list      entry;         /* Entry in class list */
49     UINT             style;         /* Class style */
50     BOOL             local;         /* Local class? */
51     WNDPROC          winproc;       /* Window procedure */
52     INT              cbClsExtra;    /* Class extra bytes */
53     INT              cbWndExtra;    /* Window extra bytes */
54     LPWSTR           menuName;      /* Default menu name (Unicode followed by ASCII) */
55     HINSTANCE        hInstance;     /* Module that created the task */
56     HICON            hIcon;         /* Default icon */
57     HICON            hIconSm;       /* Default small icon */
58     HCURSOR          hCursor;       /* Default cursor */
59     HBRUSH           hbrBackground; /* Default background */
60     ATOM             atomName;      /* Name of the class */
61     WCHAR            name[MAX_ATOM_LEN + 1];
62 } CLASS;
63
64 static struct list class_list = LIST_INIT( class_list );
65
66 #define CLASS_OTHER_PROCESS ((CLASS *)1)
67
68 /***********************************************************************
69  *           get_class_ptr
70  */
71 static CLASS *get_class_ptr( HWND hwnd, BOOL write_access )
72 {
73     WND *ptr = WIN_GetPtr( hwnd );
74
75     if (ptr)
76     {
77         if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP) return ptr->class;
78         if (!write_access) return CLASS_OTHER_PROCESS;
79
80         /* modifying classes in other processes is not allowed */
81         if (ptr == WND_DESKTOP || IsWindow( hwnd ))
82         {
83             SetLastError( ERROR_ACCESS_DENIED );
84             return NULL;
85         }
86     }
87     SetLastError( ERROR_INVALID_WINDOW_HANDLE );
88     return NULL;
89 }
90
91
92 /***********************************************************************
93  *           release_class_ptr
94  */
95 static inline void release_class_ptr( CLASS *ptr )
96 {
97     USER_Unlock();
98 }
99
100
101 /***********************************************************************
102  *           get_int_atom_value
103  */
104 ATOM get_int_atom_value( LPCWSTR name )
105 {
106     UINT ret = 0;
107
108     if (IS_INTRESOURCE(name)) return LOWORD(name);
109     if (*name++ != '#') return 0;
110     while (*name)
111     {
112         if (*name < '0' || *name > '9') return 0;
113         ret = ret * 10 + *name++ - '0';
114         if (ret > 0xffff) return 0;
115     }
116     return ret;
117 }
118
119
120 /***********************************************************************
121  *           set_server_info
122  *
123  * Set class info with the wine server.
124  */
125 static BOOL set_server_info( HWND hwnd, INT offset, LONG_PTR newval, UINT size )
126 {
127     BOOL ret;
128
129     SERVER_START_REQ( set_class_info )
130     {
131         req->window = hwnd;
132         req->extra_offset = -1;
133         switch(offset)
134         {
135         case GCW_ATOM:
136             req->flags = SET_CLASS_ATOM;
137             req->atom = newval;
138         case GCL_STYLE:
139             req->flags = SET_CLASS_STYLE;
140             req->style = newval;
141             break;
142         case GCL_CBWNDEXTRA:
143             req->flags = SET_CLASS_WINEXTRA;
144             req->win_extra = newval;
145             break;
146         case GCLP_HMODULE:
147             req->flags = SET_CLASS_INSTANCE;
148             req->instance = (void *)newval;
149             break;
150         default:
151             assert( offset >= 0 );
152             req->flags = SET_CLASS_EXTRA;
153             req->extra_offset = offset;
154             req->extra_size = size;
155             if ( size == sizeof(LONG) )
156             {
157                 LONG newlong = newval;
158                 memcpy( &req->extra_value, &newlong, sizeof(LONG) );
159             }
160             else
161                 memcpy( &req->extra_value, &newval, sizeof(LONG_PTR) );
162             break;
163         }
164         ret = !wine_server_call_err( req );
165     }
166     SERVER_END_REQ;
167     return ret;
168 }
169
170
171 /***********************************************************************
172  *           CLASS_GetMenuNameA
173  *
174  * Get the menu name as a ASCII string.
175  */
176 static inline LPSTR CLASS_GetMenuNameA( CLASS *classPtr )
177 {
178     if (!HIWORD(classPtr->menuName)) return (LPSTR)classPtr->menuName;
179     return (LPSTR)(classPtr->menuName + strlenW(classPtr->menuName) + 1);
180 }
181
182
183 /***********************************************************************
184  *           CLASS_GetMenuNameW
185  *
186  * Get the menu name as a Unicode string.
187  */
188 static inline LPWSTR CLASS_GetMenuNameW( CLASS *classPtr )
189 {
190     return classPtr->menuName;
191 }
192
193
194 /***********************************************************************
195  *           CLASS_SetMenuNameA
196  *
197  * Set the menu name in a class structure by copying the string.
198  */
199 static void CLASS_SetMenuNameA( CLASS *classPtr, LPCSTR name )
200 {
201     if (HIWORD(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
202     if (HIWORD(name))
203     {
204         DWORD lenA = strlen(name) + 1;
205         DWORD lenW = MultiByteToWideChar( CP_ACP, 0, name, lenA, NULL, 0 );
206         classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
207         MultiByteToWideChar( CP_ACP, 0, name, lenA, classPtr->menuName, lenW );
208         memcpy( classPtr->menuName + lenW, name, lenA );
209     }
210     else classPtr->menuName = (LPWSTR)name;
211 }
212
213
214 /***********************************************************************
215  *           CLASS_SetMenuNameW
216  *
217  * Set the menu name in a class structure by copying the string.
218  */
219 static void CLASS_SetMenuNameW( CLASS *classPtr, LPCWSTR name )
220 {
221     if (HIWORD(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
222     if (HIWORD(name))
223     {
224         DWORD lenW = strlenW(name) + 1;
225         DWORD lenA = WideCharToMultiByte( CP_ACP, 0, name, lenW, NULL, 0, NULL, NULL );
226         classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
227         memcpy( classPtr->menuName, name, lenW*sizeof(WCHAR) );
228         WideCharToMultiByte( CP_ACP, 0, name, lenW,
229                              (char *)(classPtr->menuName + lenW), lenA, NULL, NULL );
230     }
231     else classPtr->menuName = (LPWSTR)name;
232 }
233
234
235 /***********************************************************************
236  *           CLASS_FreeClass
237  *
238  * Free a class structure.
239  */
240 static void CLASS_FreeClass( CLASS *classPtr )
241 {
242     TRACE("%p\n", classPtr);
243
244     USER_Lock();
245
246     list_remove( &classPtr->entry );
247     if (classPtr->hbrBackground > (HBRUSH)(COLOR_GRADIENTINACTIVECAPTION + 1))
248         DeleteObject( classPtr->hbrBackground );
249     HeapFree( GetProcessHeap(), 0, classPtr->menuName );
250     HeapFree( GetProcessHeap(), 0, classPtr );
251     USER_Unlock();
252 }
253
254
255 /***********************************************************************
256  *           CLASS_FreeModuleClasses
257  */
258 void CLASS_FreeModuleClasses( HMODULE16 hModule )
259 {
260     struct list *ptr, *next;
261
262     TRACE("0x%08x\n", hModule);
263
264     USER_Lock();
265     for (ptr = list_head( &class_list ); ptr; ptr = next)
266     {
267         CLASS *class = LIST_ENTRY( ptr, CLASS, entry );
268         next = list_next( &class_list, ptr );
269         if (class->hInstance == HINSTANCE_32(hModule))
270         {
271             BOOL ret;
272
273             SERVER_START_REQ( destroy_class )
274             {
275                 req->atom = class->atomName;
276                 req->instance = class->hInstance;
277                 ret = !wine_server_call_err( req );
278             }
279             SERVER_END_REQ;
280             if (ret) CLASS_FreeClass( class );
281         }
282     }
283     USER_Unlock();
284 }
285
286
287 /***********************************************************************
288  *           CLASS_FindClass
289  *
290  * Return a pointer to the class.
291  * hinstance has been normalized by the caller.
292  */
293 static CLASS *CLASS_FindClass( LPCWSTR name, HINSTANCE hinstance )
294 {
295     struct list *ptr;
296     ATOM atom = get_int_atom_value( name );
297
298     USER_Lock();
299
300     LIST_FOR_EACH( ptr, &class_list )
301     {
302         CLASS *class = LIST_ENTRY( ptr, CLASS, entry );
303         if (atom)
304         {
305             if (class->atomName != atom) continue;
306         }
307         else
308         {
309             if (!name || strcmpiW( class->name, name )) continue;
310         }
311         if (!hinstance || !class->local || class->hInstance == hinstance)
312         {
313             TRACE("%s %p -> %p\n", debugstr_w(name), hinstance, class);
314             return class;
315         }
316     }
317     USER_Unlock();
318     TRACE("%s %p -> not found\n", debugstr_w(name), hinstance);
319     return NULL;
320 }
321
322
323 /***********************************************************************
324  *           CLASS_RegisterClass
325  *
326  * The real RegisterClass() functionality.
327  */
328 static CLASS *CLASS_RegisterClass( LPCWSTR name, HINSTANCE hInstance, BOOL local,
329                                    DWORD style, INT classExtra, INT winExtra )
330 {
331     CLASS *classPtr;
332     BOOL ret;
333
334     TRACE("name=%s hinst=%p style=0x%x clExtr=0x%x winExtr=0x%x\n",
335           debugstr_w(name), hInstance, style, classExtra, winExtra );
336
337     /* Fix the extra bytes value */
338
339     if (classExtra < 0 || winExtra < 0)
340     {
341          SetLastError( ERROR_INVALID_PARAMETER );
342          return NULL;
343     }
344     if (classExtra > 40)  /* Extra bytes are limited to 40 in Win32 */
345         WARN("Class extra bytes %d is > 40\n", classExtra);
346     if (winExtra > 40)    /* Extra bytes are limited to 40 in Win32 */
347         WARN("Win extra bytes %d is > 40\n", winExtra );
348
349     classPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLASS) + classExtra );
350     if (!classPtr) return NULL;
351
352     classPtr->atomName = get_int_atom_value( name );
353     if (!classPtr->atomName && name) strcpyW( classPtr->name, name );
354     else GlobalGetAtomNameW( classPtr->atomName, classPtr->name, sizeof(classPtr->name)/sizeof(WCHAR) );
355
356     SERVER_START_REQ( create_class )
357     {
358         req->local      = local;
359         req->style      = style;
360         req->instance   = hInstance;
361         req->extra      = classExtra;
362         req->win_extra  = winExtra;
363         req->client_ptr = classPtr;
364         req->atom       = classPtr->atomName;
365         if (!req->atom && name) wine_server_add_data( req, name, strlenW(name) * sizeof(WCHAR) );
366         ret = !wine_server_call_err( req );
367         classPtr->atomName = reply->atom;
368     }
369     SERVER_END_REQ;
370     if (!ret)
371     {
372         HeapFree( GetProcessHeap(), 0, classPtr );
373         return NULL;
374     }
375
376     classPtr->style       = style;
377     classPtr->local       = local;
378     classPtr->cbWndExtra  = winExtra;
379     classPtr->cbClsExtra  = classExtra;
380     classPtr->hInstance   = hInstance;
381
382     /* Other non-null values must be set by caller */
383
384     USER_Lock();
385     if (local) list_add_head( &class_list, &classPtr->entry );
386     else list_add_tail( &class_list, &classPtr->entry );
387     return classPtr;
388 }
389
390
391 /***********************************************************************
392  *           register_builtin
393  *
394  * Register a builtin control class.
395  * This allows having both ASCII and Unicode winprocs for the same class.
396  */
397 static WNDPROC register_builtin( const struct builtin_class_descr *descr )
398 {
399     CLASS *classPtr;
400
401     if (!(classPtr = CLASS_RegisterClass( descr->name, user32_module, FALSE,
402                                           descr->style, 0, descr->extra ))) return 0;
403
404     classPtr->hCursor       = LoadCursorA( 0, (LPSTR)descr->cursor );
405     classPtr->hbrBackground = descr->brush;
406     classPtr->winproc       = WINPROC_AllocProc( descr->procA, descr->procW );
407     release_class_ptr( classPtr );
408     return classPtr->winproc;
409 }
410
411
412 /***********************************************************************
413  *           CLASS_RegisterBuiltinClasses
414  */
415 void CLASS_RegisterBuiltinClasses(void)
416 {
417     register_builtin( &DESKTOP_builtin_class );
418     register_builtin( &BUTTON_builtin_class );
419     register_builtin( &COMBO_builtin_class );
420     register_builtin( &COMBOLBOX_builtin_class );
421     register_builtin( &DIALOG_builtin_class );
422     EDIT_winproc_handle = register_builtin( &EDIT_builtin_class );
423     register_builtin( &ICONTITLE_builtin_class );
424     register_builtin( &LISTBOX_builtin_class );
425     register_builtin( &MDICLIENT_builtin_class );
426     register_builtin( &MENU_builtin_class );
427     register_builtin( &SCROLL_builtin_class );
428     register_builtin( &STATIC_builtin_class );
429
430     /* the DefWindowProc winprocs are magic too */
431     WINPROC_AllocProc( DefWindowProcA, DefWindowProcW );
432 }
433
434
435 /***********************************************************************
436  *           CLASS_AddWindow
437  *
438  * Add a new window using this class, and set the necessary
439  * information inside the window structure.
440  */
441 void CLASS_AddWindow( CLASS *class, WND *win, BOOL unicode )
442 {
443     win->class    = class;
444     win->clsStyle = class->style;
445     win->winproc  = class->winproc;
446     if (WINPROC_IsUnicode( win->winproc, unicode )) win->flags |= WIN_ISUNICODE;
447 }
448
449
450 /***********************************************************************
451  *              RegisterClassA (USER32.@)
452  *
453  * Register a window class.
454  *
455  * RETURNS
456  *      >0: Unique identifier
457  *      0: Failure
458  */
459 ATOM WINAPI RegisterClassA( const WNDCLASSA* wc ) /* [in] Address of structure with class data */
460 {
461     WNDCLASSEXA wcex;
462
463     wcex.cbSize        = sizeof(wcex);
464     wcex.style         = wc->style;
465     wcex.lpfnWndProc   = wc->lpfnWndProc;
466     wcex.cbClsExtra    = wc->cbClsExtra;
467     wcex.cbWndExtra    = wc->cbWndExtra;
468     wcex.hInstance     = wc->hInstance;
469     wcex.hIcon         = wc->hIcon;
470     wcex.hCursor       = wc->hCursor;
471     wcex.hbrBackground = wc->hbrBackground;
472     wcex.lpszMenuName  = wc->lpszMenuName;
473     wcex.lpszClassName = wc->lpszClassName;
474     wcex.hIconSm       = 0;
475     return RegisterClassExA( &wcex );
476 }
477
478
479 /***********************************************************************
480  *              RegisterClassW (USER32.@)
481  *
482  * See RegisterClassA.
483  */
484 ATOM WINAPI RegisterClassW( const WNDCLASSW* wc )
485 {
486     WNDCLASSEXW wcex;
487
488     wcex.cbSize        = sizeof(wcex);
489     wcex.style         = wc->style;
490     wcex.lpfnWndProc   = wc->lpfnWndProc;
491     wcex.cbClsExtra    = wc->cbClsExtra;
492     wcex.cbWndExtra    = wc->cbWndExtra;
493     wcex.hInstance     = wc->hInstance;
494     wcex.hIcon         = wc->hIcon;
495     wcex.hCursor       = wc->hCursor;
496     wcex.hbrBackground = wc->hbrBackground;
497     wcex.lpszMenuName  = wc->lpszMenuName;
498     wcex.lpszClassName = wc->lpszClassName;
499     wcex.hIconSm       = 0;
500     return RegisterClassExW( &wcex );
501 }
502
503
504 /***********************************************************************
505  *              RegisterClassExA (USER32.@)
506  */
507 ATOM WINAPI RegisterClassExA( const WNDCLASSEXA* wc )
508 {
509     ATOM atom;
510     CLASS *classPtr;
511     HINSTANCE instance;
512
513     if (wc->hInstance == user32_module)
514     {
515         /* we can't register a class for user32 */
516         SetLastError( ERROR_INVALID_PARAMETER );
517         return 0;
518     }
519     if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
520
521     if (!IS_INTRESOURCE(wc->lpszClassName))
522     {
523         WCHAR name[MAX_ATOM_LEN + 1];
524
525         if (!MultiByteToWideChar( CP_ACP, 0, wc->lpszClassName, -1, name, MAX_ATOM_LEN + 1 )) return 0;
526         classPtr = CLASS_RegisterClass( name, instance, !(wc->style & CS_GLOBALCLASS),
527                                         wc->style, wc->cbClsExtra, wc->cbWndExtra );
528     }
529     else
530     {
531         classPtr = CLASS_RegisterClass( (LPCWSTR)wc->lpszClassName, instance,
532                                         !(wc->style & CS_GLOBALCLASS), wc->style,
533                                         wc->cbClsExtra, wc->cbWndExtra );
534     }
535     if (!classPtr) return 0;
536     atom = classPtr->atomName;
537
538     TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
539           debugstr_a(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
540           wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
541
542     classPtr->hIcon         = wc->hIcon;
543     classPtr->hIconSm       = wc->hIconSm;
544     classPtr->hCursor       = wc->hCursor;
545     classPtr->hbrBackground = wc->hbrBackground;
546     classPtr->winproc       = WINPROC_AllocProc( wc->lpfnWndProc, NULL );
547     CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
548     release_class_ptr( classPtr );
549     return atom;
550 }
551
552
553 /***********************************************************************
554  *              RegisterClassExW (USER32.@)
555  */
556 ATOM WINAPI RegisterClassExW( const WNDCLASSEXW* wc )
557 {
558     ATOM atom;
559     CLASS *classPtr;
560     HINSTANCE instance;
561
562     if (wc->hInstance == user32_module)
563     {
564         /* we can't register a class for user32 */
565         SetLastError( ERROR_INVALID_PARAMETER );
566         return 0;
567     }
568     if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
569
570     if (!(classPtr = CLASS_RegisterClass( wc->lpszClassName, instance, !(wc->style & CS_GLOBALCLASS),
571                                           wc->style, wc->cbClsExtra, wc->cbWndExtra )))
572         return 0;
573
574     atom = classPtr->atomName;
575
576     TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
577           debugstr_w(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
578           wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
579
580     classPtr->hIcon         = wc->hIcon;
581     classPtr->hIconSm       = wc->hIconSm;
582     classPtr->hCursor       = wc->hCursor;
583     classPtr->hbrBackground = wc->hbrBackground;
584     classPtr->winproc       = WINPROC_AllocProc( NULL, wc->lpfnWndProc );
585     CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
586     release_class_ptr( classPtr );
587     return atom;
588 }
589
590
591 /***********************************************************************
592  *              UnregisterClassA (USER32.@)
593  */
594 BOOL WINAPI UnregisterClassA( LPCSTR className, HINSTANCE hInstance )
595 {
596     if (!IS_INTRESOURCE(className))
597     {
598         WCHAR name[MAX_ATOM_LEN + 1];
599
600         if (!MultiByteToWideChar( CP_ACP, 0, className, -1, name, MAX_ATOM_LEN + 1 ))
601             return FALSE;
602         return UnregisterClassW( name, hInstance );
603     }
604     return UnregisterClassW( (LPCWSTR)className, hInstance );
605 }
606
607 /***********************************************************************
608  *              UnregisterClassW (USER32.@)
609  */
610 BOOL WINAPI UnregisterClassW( LPCWSTR className, HINSTANCE hInstance )
611 {
612     CLASS *classPtr = NULL;
613
614     TRACE("%s %p\n",debugstr_w(className), hInstance);
615
616     SERVER_START_REQ( destroy_class )
617     {
618         req->instance = hInstance;
619         if (!(req->atom = get_int_atom_value(className)) && className)
620             wine_server_add_data( req, className, strlenW(className) * sizeof(WCHAR) );
621         if (!wine_server_call_err( req )) classPtr = reply->client_ptr;
622     }
623     SERVER_END_REQ;
624
625     if (classPtr) CLASS_FreeClass( classPtr );
626     return (classPtr != NULL);
627 }
628
629
630 /***********************************************************************
631  *              GetClassWord (USER32.@)
632  */
633 WORD WINAPI GetClassWord( HWND hwnd, INT offset )
634 {
635     CLASS *class;
636     WORD retvalue = 0;
637
638     if (offset < 0) return GetClassLongA( hwnd, offset );
639
640     TRACE("%p %x\n",hwnd, offset);
641
642     if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
643
644     if (class == CLASS_OTHER_PROCESS)
645     {
646         SERVER_START_REQ( set_class_info )
647         {
648             req->window = hwnd;
649             req->flags = 0;
650             req->extra_offset = offset;
651             req->extra_size = sizeof(retvalue);
652             if (!wine_server_call_err( req ))
653                 memcpy( &retvalue, &reply->old_extra_value, sizeof(retvalue) );
654         }
655         SERVER_END_REQ;
656         return retvalue;
657     }
658
659     if (offset <= class->cbClsExtra - sizeof(WORD))
660         memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(retvalue) );
661     else
662         SetLastError( ERROR_INVALID_INDEX );
663     release_class_ptr( class );
664     return retvalue;
665 }
666
667
668 /***********************************************************************
669  *             CLASS_GetClassLong
670  *
671  * Implementation of GetClassLong(Ptr)A/W
672  */
673 static ULONG_PTR CLASS_GetClassLong( HWND hwnd, INT offset, UINT size,
674                                      BOOL unicode )
675 {
676     CLASS *class;
677     ULONG_PTR retvalue = 0;
678
679     TRACE("%p %d\n", hwnd, offset);
680
681     if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
682
683     if (class == CLASS_OTHER_PROCESS)
684     {
685         SERVER_START_REQ( set_class_info )
686         {
687             req->window = hwnd;
688             req->flags = 0;
689             req->extra_offset = (offset >= 0) ? offset : -1;
690             req->extra_size = (offset >= 0) ? size : 0;
691             if (!wine_server_call_err( req ))
692             {
693                 switch(offset)
694                 {
695                 case GCLP_HBRBACKGROUND:
696                 case GCLP_HCURSOR:
697                 case GCLP_HICON:
698                 case GCLP_HICONSM:
699                 case GCLP_WNDPROC:
700                 case GCLP_MENUNAME:
701                     FIXME( "offset %d (%s) not supported on other process window %p\n",
702                            offset, SPY_GetClassLongOffsetName(offset), hwnd );
703                     SetLastError( ERROR_INVALID_HANDLE );
704                     break;
705                 case GCL_STYLE:
706                     retvalue = reply->old_style;
707                     break;
708                 case GCL_CBWNDEXTRA:
709                     retvalue = reply->old_win_extra;
710                     break;
711                 case GCL_CBCLSEXTRA:
712                     retvalue = reply->old_extra;
713                     break;
714                 case GCLP_HMODULE:
715                     retvalue = (ULONG_PTR)reply->old_instance;
716                     break;
717                 case GCW_ATOM:
718                     retvalue = reply->old_atom;
719                     break;
720                 default:
721                     if (offset >= 0)
722                     {
723                         if (size == sizeof(DWORD))
724                         {
725                             DWORD retdword;
726                             memcpy( &retdword, &reply->old_extra_value, sizeof(DWORD) );
727                             retvalue = retdword;
728                         }
729                         else
730                             memcpy( &retvalue, &reply->old_extra_value,
731                                     sizeof(ULONG_PTR) );
732                     }
733                     else SetLastError( ERROR_INVALID_INDEX );
734                     break;
735                 }
736             }
737         }
738         SERVER_END_REQ;
739         return retvalue;
740     }
741
742     if (offset >= 0)
743     {
744         if (offset <= class->cbClsExtra - size)
745         {
746             if (size == sizeof(DWORD))
747             {
748                 DWORD retdword;
749                 memcpy( &retdword, (char *)(class + 1) + offset, sizeof(DWORD) );
750                 retvalue = retdword;
751             }
752             else
753                 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(ULONG_PTR) );
754         }
755         else
756             SetLastError( ERROR_INVALID_INDEX );
757         release_class_ptr( class );
758         return retvalue;
759     }
760
761     switch(offset)
762     {
763     case GCLP_HBRBACKGROUND:
764         retvalue = (ULONG_PTR)class->hbrBackground;
765         break;
766     case GCLP_HCURSOR:
767         retvalue = (ULONG_PTR)class->hCursor;
768         break;
769     case GCLP_HICON:
770         retvalue = (ULONG_PTR)class->hIcon;
771         break;
772     case GCLP_HICONSM:
773         retvalue = (ULONG_PTR)class->hIconSm;
774         break;
775     case GCL_STYLE:
776         retvalue = class->style;
777         break;
778     case GCL_CBWNDEXTRA:
779         retvalue = class->cbWndExtra;
780         break;
781     case GCL_CBCLSEXTRA:
782         retvalue = class->cbClsExtra;
783         break;
784     case GCLP_HMODULE:
785         retvalue = (ULONG_PTR)class->hInstance;
786         break;
787     case GCLP_WNDPROC:
788         retvalue = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
789         break;
790     case GCLP_MENUNAME:
791         retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
792         if (unicode)
793             retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
794         else
795             retvalue = (ULONG_PTR)CLASS_GetMenuNameA( class );
796         break;
797     case GCW_ATOM:
798         retvalue = class->atomName;
799         break;
800     default:
801         SetLastError( ERROR_INVALID_INDEX );
802         break;
803     }
804     release_class_ptr( class );
805     return retvalue;
806 }
807
808
809 /***********************************************************************
810  *              GetClassLongW (USER32.@)
811  */
812 DWORD WINAPI GetClassLongW( HWND hwnd, INT offset )
813 {
814     return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), TRUE );
815 }
816
817
818
819 /***********************************************************************
820  *              GetClassLongA (USER32.@)
821  */
822 DWORD WINAPI GetClassLongA( HWND hwnd, INT offset )
823 {
824     return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), FALSE );
825 }
826
827
828 /***********************************************************************
829  *              SetClassWord (USER32.@)
830  */
831 WORD WINAPI SetClassWord( HWND hwnd, INT offset, WORD newval )
832 {
833     CLASS *class;
834     WORD retval = 0;
835
836     if (offset < 0) return SetClassLongA( hwnd, offset, (DWORD)newval );
837
838     TRACE("%p %d %x\n", hwnd, offset, newval);
839
840     if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
841
842     SERVER_START_REQ( set_class_info )
843     {
844         req->window = hwnd;
845         req->flags = SET_CLASS_EXTRA;
846         req->extra_offset = offset;
847         req->extra_size = sizeof(newval);
848         memcpy( &req->extra_value, &newval, sizeof(newval) );
849         if (!wine_server_call_err( req ))
850         {
851             void *ptr = (char *)(class + 1) + offset;
852             memcpy( &retval, ptr, sizeof(retval) );
853             memcpy( ptr, &newval, sizeof(newval) );
854         }
855     }
856     SERVER_END_REQ;
857     release_class_ptr( class );
858     return retval;
859 }
860
861
862 /***********************************************************************
863  *             CLASS_SetClassLong
864  *
865  * Implementation of SetClassLong(Ptr)A/W
866  */
867 static ULONG_PTR CLASS_SetClassLong( HWND hwnd, INT offset, LONG_PTR newval,
868                                      UINT size, BOOL unicode )
869 {
870     CLASS *class;
871     ULONG_PTR retval = 0;
872
873     TRACE("%p %d %lx\n", hwnd, offset, newval);
874
875     if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
876
877     if (offset >= 0)
878     {
879         if (set_server_info( hwnd, offset, newval, size ))
880         {
881             void *ptr = (char *)(class + 1) + offset;
882             if ( size == sizeof(LONG) )
883             {
884                 DWORD retdword;
885                 LONG newlong = newval;
886                 memcpy( &retdword, ptr, sizeof(DWORD) );
887                 memcpy( ptr, &newlong, sizeof(LONG) );
888                 retval = retdword;
889             }
890             else
891             {
892                 memcpy( &retval, ptr, sizeof(ULONG_PTR) );
893                 memcpy( ptr, &newval, sizeof(LONG_PTR) );
894             }
895         }
896     }
897     else switch(offset)
898     {
899     case GCLP_MENUNAME:
900         if ( unicode )
901             CLASS_SetMenuNameW( class, (LPCWSTR)newval );
902         else
903             CLASS_SetMenuNameA( class, (LPCSTR)newval );
904         retval = 0;  /* Old value is now meaningless anyway */
905         break;
906     case GCLP_WNDPROC:
907         retval = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
908         class->winproc = WINPROC_AllocProc( unicode ? NULL : (WNDPROC)newval,
909                                             unicode ? (WNDPROC)newval : NULL );
910         break;
911     case GCLP_HBRBACKGROUND:
912         retval = (ULONG_PTR)class->hbrBackground;
913         class->hbrBackground = (HBRUSH)newval;
914         break;
915     case GCLP_HCURSOR:
916         retval = (ULONG_PTR)class->hCursor;
917         class->hCursor = (HCURSOR)newval;
918         break;
919     case GCLP_HICON:
920         retval = (ULONG_PTR)class->hIcon;
921         class->hIcon = (HICON)newval;
922         break;
923     case GCLP_HICONSM:
924         retval = (ULONG_PTR)class->hIconSm;
925         class->hIconSm = (HICON)newval;
926         break;
927     case GCL_STYLE:
928         if (!set_server_info( hwnd, offset, newval, size )) break;
929         retval = class->style;
930         class->style = newval;
931         break;
932     case GCL_CBWNDEXTRA:
933         if (!set_server_info( hwnd, offset, newval, size )) break;
934         retval = class->cbWndExtra;
935         class->cbWndExtra = newval;
936         break;
937     case GCLP_HMODULE:
938         if (!set_server_info( hwnd, offset, newval, size )) break;
939         retval = (ULONG_PTR)class->hInstance;
940         class->hInstance = (HINSTANCE)newval;
941         break;
942     case GCW_ATOM:
943         if (!set_server_info( hwnd, offset, newval, size )) break;
944         retval = class->atomName;
945         class->atomName = newval;
946         GlobalGetAtomNameW( newval, class->name, sizeof(class->name)/sizeof(WCHAR) );
947         break;
948     case GCL_CBCLSEXTRA:  /* cannot change this one */
949         SetLastError( ERROR_INVALID_PARAMETER );
950         break;
951     default:
952         SetLastError( ERROR_INVALID_INDEX );
953         break;
954     }
955     release_class_ptr( class );
956     return retval;
957 }
958
959
960 /***********************************************************************
961  *              SetClassLongW (USER32.@)
962  */
963 DWORD WINAPI SetClassLongW( HWND hwnd, INT offset, LONG newval )
964 {
965     TRACE("%p %d %x\n", hwnd, offset, newval);
966
967     return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), TRUE );
968 }
969
970
971 /***********************************************************************
972  *              SetClassLongA (USER32.@)
973  */
974 DWORD WINAPI SetClassLongA( HWND hwnd, INT offset, LONG newval )
975 {
976     TRACE("%p %d %x\n", hwnd, offset, newval);
977
978     return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), FALSE );
979 }
980
981
982 /***********************************************************************
983  *              GetClassNameA (USER32.@)
984  */
985 INT WINAPI GetClassNameA( HWND hwnd, LPSTR buffer, INT count )
986 {
987     WCHAR tmpbuf[MAX_ATOM_LEN + 1];
988     DWORD len;
989
990     if (count <= 0) return 0;
991     if (!GetClassNameW( hwnd, tmpbuf, sizeof(tmpbuf)/sizeof(WCHAR) )) return 0;
992     RtlUnicodeToMultiByteN( buffer, count - 1, &len, tmpbuf, strlenW(tmpbuf) * sizeof(WCHAR) );
993     buffer[len] = 0;
994     return len;
995 }
996
997
998 /***********************************************************************
999  *              GetClassNameW (USER32.@)
1000  */
1001 INT WINAPI GetClassNameW( HWND hwnd, LPWSTR buffer, INT count )
1002 {
1003     CLASS *class;
1004     INT ret;
1005
1006     TRACE("%p %p %d\n", hwnd, buffer, count);
1007
1008     if (count <= 0) return 0;
1009
1010     if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
1011
1012     if (class == CLASS_OTHER_PROCESS)
1013     {
1014         WCHAR tmpbuf[MAX_ATOM_LEN + 1];
1015
1016         ret = GlobalGetAtomNameW( GetClassLongW( hwnd, GCW_ATOM ), tmpbuf, MAX_ATOM_LEN + 1 );
1017         if (ret)
1018         {
1019             ret = min(count - 1, ret);
1020             memcpy(buffer, tmpbuf, ret * sizeof(WCHAR));
1021             buffer[ret] = 0;
1022         }
1023     }
1024     else
1025     {
1026         lstrcpynW( buffer, class->name, count );
1027         release_class_ptr( class );
1028         ret = strlenW( buffer );
1029     }
1030     return ret;
1031 }
1032
1033
1034 /***********************************************************************
1035  *              RealGetWindowClassA (USER32.@)
1036  */
1037 UINT WINAPI RealGetWindowClassA( HWND hwnd, LPSTR buffer, UINT count )
1038 {
1039     return GetClassNameA( hwnd, buffer, count );
1040 }
1041
1042
1043 /***********************************************************************
1044  *              RealGetWindowClassW (USER32.@)
1045  */
1046 UINT WINAPI RealGetWindowClassW( HWND hwnd, LPWSTR buffer, UINT count )
1047 {
1048     return GetClassNameW( hwnd, buffer, count );
1049 }
1050
1051
1052 /***********************************************************************
1053  *              GetClassInfoA (USER32.@)
1054  */
1055 BOOL WINAPI GetClassInfoA( HINSTANCE hInstance, LPCSTR name, WNDCLASSA *wc )
1056 {
1057     WNDCLASSEXA wcex;
1058     UINT ret = GetClassInfoExA( hInstance, name, &wcex );
1059
1060     if (ret)
1061     {
1062         wc->style         = wcex.style;
1063         wc->lpfnWndProc   = wcex.lpfnWndProc;
1064         wc->cbClsExtra    = wcex.cbClsExtra;
1065         wc->cbWndExtra    = wcex.cbWndExtra;
1066         wc->hInstance     = wcex.hInstance;
1067         wc->hIcon         = wcex.hIcon;
1068         wc->hCursor       = wcex.hCursor;
1069         wc->hbrBackground = wcex.hbrBackground;
1070         wc->lpszMenuName  = wcex.lpszMenuName;
1071         wc->lpszClassName = wcex.lpszClassName;
1072     }
1073     return ret;
1074 }
1075
1076
1077 /***********************************************************************
1078  *              GetClassInfoW (USER32.@)
1079  */
1080 BOOL WINAPI GetClassInfoW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSW *wc )
1081 {
1082     WNDCLASSEXW wcex;
1083     UINT ret = GetClassInfoExW( hInstance, name, &wcex );
1084
1085     if (ret)
1086     {
1087         wc->style         = wcex.style;
1088         wc->lpfnWndProc   = wcex.lpfnWndProc;
1089         wc->cbClsExtra    = wcex.cbClsExtra;
1090         wc->cbWndExtra    = wcex.cbWndExtra;
1091         wc->hInstance     = wcex.hInstance;
1092         wc->hIcon         = wcex.hIcon;
1093         wc->hCursor       = wcex.hCursor;
1094         wc->hbrBackground = wcex.hbrBackground;
1095         wc->lpszMenuName  = wcex.lpszMenuName;
1096         wc->lpszClassName = wcex.lpszClassName;
1097     }
1098     return ret;
1099 }
1100
1101
1102 /***********************************************************************
1103  *              GetClassInfoExA (USER32.@)
1104  */
1105 BOOL WINAPI GetClassInfoExA( HINSTANCE hInstance, LPCSTR name, WNDCLASSEXA *wc )
1106 {
1107     ATOM atom;
1108     CLASS *classPtr;
1109
1110     TRACE("%p %s %p\n", hInstance, debugstr_a(name), wc);
1111
1112     if (!hInstance) hInstance = user32_module;
1113
1114     if (!IS_INTRESOURCE(name))
1115     {
1116         WCHAR nameW[MAX_ATOM_LEN + 1];
1117         if (!MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, sizeof(nameW)/sizeof(WCHAR) ))
1118             return FALSE;
1119         classPtr = CLASS_FindClass( nameW, hInstance );
1120     }
1121     else classPtr = CLASS_FindClass( (LPCWSTR)name, hInstance );
1122
1123     if (!classPtr)
1124     {
1125         SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1126         return FALSE;
1127     }
1128     wc->style         = classPtr->style;
1129     wc->lpfnWndProc   = WINPROC_GetProc( classPtr->winproc, FALSE );
1130     wc->cbClsExtra    = classPtr->cbClsExtra;
1131     wc->cbWndExtra    = classPtr->cbWndExtra;
1132     wc->hInstance     = (hInstance == user32_module) ? 0 : hInstance;
1133     wc->hIcon         = (HICON)classPtr->hIcon;
1134     wc->hIconSm       = (HICON)classPtr->hIconSm;
1135     wc->hCursor       = (HCURSOR)classPtr->hCursor;
1136     wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1137     wc->lpszMenuName  = CLASS_GetMenuNameA( classPtr );
1138     wc->lpszClassName = name;
1139     atom = classPtr->atomName;
1140     release_class_ptr( classPtr );
1141
1142     /* We must return the atom of the class here instead of just TRUE. */
1143     return atom;
1144 }
1145
1146
1147 /***********************************************************************
1148  *              GetClassInfoExW (USER32.@)
1149  */
1150 BOOL WINAPI GetClassInfoExW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSEXW *wc )
1151 {
1152     ATOM atom;
1153     CLASS *classPtr;
1154
1155     TRACE("%p %s %p\n", hInstance, debugstr_w(name), wc);
1156
1157     if (!hInstance) hInstance = user32_module;
1158
1159     if (!(classPtr = CLASS_FindClass( name, hInstance )))
1160     {
1161         SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1162         return FALSE;
1163     }
1164     wc->style         = classPtr->style;
1165     wc->lpfnWndProc   = WINPROC_GetProc( classPtr->winproc, TRUE );
1166     wc->cbClsExtra    = classPtr->cbClsExtra;
1167     wc->cbWndExtra    = classPtr->cbWndExtra;
1168     wc->hInstance     = (hInstance == user32_module) ? 0 : hInstance;
1169     wc->hIcon         = (HICON)classPtr->hIcon;
1170     wc->hIconSm       = (HICON)classPtr->hIconSm;
1171     wc->hCursor       = (HCURSOR)classPtr->hCursor;
1172     wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1173     wc->lpszMenuName  = CLASS_GetMenuNameW( classPtr );
1174     wc->lpszClassName = name;
1175     atom = classPtr->atomName;
1176     release_class_ptr( classPtr );
1177
1178     /* We must return the atom of the class here instead of just TRUE. */
1179     return atom;
1180 }
1181
1182
1183 #if 0  /* toolhelp is in kernel, so this cannot work */
1184
1185 /***********************************************************************
1186  *              ClassFirst (TOOLHELP.69)
1187  */
1188 BOOL16 WINAPI ClassFirst16( CLASSENTRY *pClassEntry )
1189 {
1190     TRACE("%p\n",pClassEntry);
1191     pClassEntry->wNext = 1;
1192     return ClassNext16( pClassEntry );
1193 }
1194
1195
1196 /***********************************************************************
1197  *              ClassNext (TOOLHELP.70)
1198  */
1199 BOOL16 WINAPI ClassNext16( CLASSENTRY *pClassEntry )
1200 {
1201     int i;
1202     CLASS *class = firstClass;
1203
1204     TRACE("%p\n",pClassEntry);
1205
1206     if (!pClassEntry->wNext) return FALSE;
1207     for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1208     if (!class)
1209     {
1210         pClassEntry->wNext = 0;
1211         return FALSE;
1212     }
1213     pClassEntry->hInst = class->hInstance;
1214     pClassEntry->wNext++;
1215     GlobalGetAtomNameA( class->atomName, pClassEntry->szClassName,
1216                           sizeof(pClassEntry->szClassName) );
1217     return TRUE;
1218 }
1219 #endif
1220
1221 /* 64bit versions */
1222
1223 #ifdef GetClassLongPtrA
1224 #undef GetClassLongPtrA
1225 #endif
1226
1227 #ifdef GetClassLongPtrW
1228 #undef GetClassLongPtrW
1229 #endif
1230
1231 #ifdef SetClassLongPtrA
1232 #undef SetClassLongPtrA
1233 #endif
1234
1235 #ifdef SetClassLongPtrW
1236 #undef SetClassLongPtrW
1237 #endif
1238
1239 /***********************************************************************
1240  *              GetClassLongPtrA (USER32.@)
1241  */
1242 ULONG_PTR WINAPI GetClassLongPtrA( HWND hwnd, INT offset )
1243 {
1244     return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), FALSE );
1245 }
1246
1247 /***********************************************************************
1248  *              GetClassLongPtrW (USER32.@)
1249  */
1250 ULONG_PTR WINAPI GetClassLongPtrW( HWND hwnd, INT offset )
1251 {
1252     return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), TRUE );
1253 }
1254
1255 /***********************************************************************
1256  *              SetClassLongPtrW (USER32.@)
1257  */
1258 ULONG_PTR WINAPI SetClassLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
1259 {
1260     return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), TRUE );
1261 }
1262
1263 /***********************************************************************
1264  *              SetClassLongPtrA (USER32.@)
1265  */
1266 ULONG_PTR WINAPI SetClassLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
1267 {
1268     return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), FALSE );
1269 }