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