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