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