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