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