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