Release 980301
[wine] / windows / class.c
1 /*
2  * Window classes functions
3  *
4  * Copyright 1993, 1996 Alexandre Julliard
5  *
6  * FIXME: In win32 all classes are local. They are registered at 
7  *        program start. Processes CANNOT share classes. (Source: some
8  *        win31->NT migration book)
9  */
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include "class.h"
15 #include "heap.h"
16 #include "win.h"
17 #include "dce.h"
18 #include "atom.h"
19 #include "ldt.h"
20 #include "toolhelp.h"
21 #include "winproc.h"
22 #include "debug.h"
23
24
25 static CLASS *firstClass = NULL;
26
27
28 /***********************************************************************
29  *           CLASS_DumpClass
30  *
31  * Dump the content of a class structure to stderr.
32  */
33 void CLASS_DumpClass( CLASS *ptr )
34 {
35     char className[80];
36     int i;
37
38     if (ptr->magic != CLASS_MAGIC)
39     {
40         fprintf( stderr, "%p is not a class\n", ptr );
41         return;
42     }
43
44     GlobalGetAtomName32A( ptr->atomName, className, sizeof(className) );
45
46     fprintf( stderr, "Class %p:\n", ptr );
47     fprintf( stderr,
48              "next=%p  name=%04x '%s'  style=%08x  wndProc=%08x\n"
49              "inst=%04x  dce=%08x  icon=%04x  cursor=%04x  bkgnd=%04x\n"
50              "clsExtra=%d  winExtra=%d  #windows=%d\n",
51              ptr->next, ptr->atomName, className, ptr->style,
52              (UINT32)ptr->winproc, ptr->hInstance, (UINT32)ptr->dce,
53              ptr->hIcon, ptr->hCursor, ptr->hbrBackground,
54              ptr->cbClsExtra, ptr->cbWndExtra, ptr->cWindows );
55     if (ptr->cbClsExtra)
56     {
57         fprintf( stderr, "extra bytes:" );
58         for (i = 0; i < ptr->cbClsExtra; i++)
59             fprintf( stderr, " %02x", *((BYTE *)ptr->wExtra+i) );
60         fprintf( stderr, "\n" );
61     }
62     fprintf( stderr, "\n" );
63 }
64
65
66 /***********************************************************************
67  *           CLASS_WalkClasses
68  *
69  * Walk the class list and print each class on stderr.
70  */
71 void CLASS_WalkClasses(void)
72 {
73     CLASS *ptr;
74     char className[80];
75
76     fprintf( stderr, " Class   Name                  Style   WndProc\n" );
77     for (ptr = firstClass; ptr; ptr = ptr->next)
78     {
79         GlobalGetAtomName32A( ptr->atomName, className, sizeof(className) );
80         fprintf( stderr, "%08x %-20.20s %08x %08x\n", (UINT32)ptr, className,
81                  ptr->style, (UINT32)ptr->winproc );
82     }
83     fprintf( stderr, "\n" );
84 }
85
86
87 /***********************************************************************
88  *           CLASS_GetMenuNameA
89  *
90  * Get the menu name as a ASCII string.
91  */
92 static LPSTR CLASS_GetMenuNameA( CLASS *classPtr )
93 {
94     if (!classPtr->menuNameA && classPtr->menuNameW)
95     {
96         /* We need to copy the Unicode string */
97         classPtr->menuNameA = SEGPTR_STRDUP_WtoA( classPtr->menuNameW );
98     }
99     return classPtr->menuNameA;
100 }
101
102
103 /***********************************************************************
104  *           CLASS_GetMenuNameW
105  *
106  * Get the menu name as a Unicode string.
107  */
108 static LPWSTR CLASS_GetMenuNameW( CLASS *classPtr )
109 {
110     if (!classPtr->menuNameW && classPtr->menuNameA)
111     {
112         if (!HIWORD(classPtr->menuNameA))
113             return (LPWSTR)classPtr->menuNameA;
114         /* Now we need to copy the ASCII string */
115         classPtr->menuNameW = HEAP_strdupAtoW( SystemHeap, 0,
116                                                classPtr->menuNameA );
117     }
118     return classPtr->menuNameW;
119 }
120
121
122 /***********************************************************************
123  *           CLASS_SetMenuNameA
124  *
125  * Set the menu name in a class structure by copying the string.
126  */
127 static void CLASS_SetMenuNameA( CLASS *classPtr, LPCSTR name )
128 {
129     if (HIWORD(classPtr->menuNameA)) SEGPTR_FREE( classPtr->menuNameA );
130     if (classPtr->menuNameW) HeapFree( SystemHeap, 0, classPtr->menuNameW );
131     classPtr->menuNameA = SEGPTR_STRDUP( name );
132     classPtr->menuNameW = 0;
133 }
134
135
136 /***********************************************************************
137  *           CLASS_SetMenuNameW
138  *
139  * Set the menu name in a class structure by copying the string.
140  */
141 static void CLASS_SetMenuNameW( CLASS *classPtr, LPCWSTR name )
142 {
143     if (!HIWORD(name))
144     {
145         CLASS_SetMenuNameA( classPtr, (LPCSTR)name );
146         return;
147     }
148     if (HIWORD(classPtr->menuNameA)) SEGPTR_FREE( classPtr->menuNameA );
149     if (classPtr->menuNameW) HeapFree( SystemHeap, 0, classPtr->menuNameW );
150     if ((classPtr->menuNameW = HeapAlloc( SystemHeap, 0,
151                                          (lstrlen32W(name)+1)*sizeof(WCHAR) )))
152         lstrcpy32W( classPtr->menuNameW, name );
153     classPtr->menuNameA = 0;
154 }
155
156
157 /***********************************************************************
158  *           CLASS_FreeClass
159  *
160  * Free a class structure.
161  */
162 static BOOL32 CLASS_FreeClass( CLASS *classPtr )
163 {
164     CLASS **ppClass;
165
166     /* Check if we can remove this class */
167
168     if (classPtr->cWindows > 0) return FALSE;
169
170     /* Remove the class from the linked list */
171
172     for (ppClass = &firstClass; *ppClass; ppClass = &(*ppClass)->next)
173         if (*ppClass == classPtr) break;
174     if (!*ppClass)
175     {
176         fprintf(stderr, "ERROR: Class list corrupted\n" );
177         return FALSE;
178     }
179     *ppClass = classPtr->next;
180
181     /* Delete the class */
182
183     if (classPtr->dce) DCE_FreeDCE( classPtr->dce );
184     if (classPtr->hbrBackground) DeleteObject32( classPtr->hbrBackground );
185     GlobalDeleteAtom( classPtr->atomName );
186     CLASS_SetMenuNameA( classPtr, NULL );
187     WINPROC_FreeProc( classPtr->winproc, WIN_PROC_CLASS );
188     HeapFree( SystemHeap, 0, classPtr );
189     return TRUE;
190 }
191
192
193 /***********************************************************************
194  *           CLASS_FreeModuleClasses
195  */
196 void CLASS_FreeModuleClasses( HMODULE16 hModule )
197 {
198     CLASS *ptr, *next;
199   
200     for (ptr = firstClass; ptr; ptr = next)
201     {
202         next = ptr->next;
203         if (ptr->hInstance == hModule) CLASS_FreeClass( ptr );
204     }
205 }
206
207
208 /***********************************************************************
209  *           CLASS_FindClassByAtom
210  *
211  * Return a pointer to the class.
212  * hinstance has been normalized by the caller.
213  */
214 CLASS *CLASS_FindClassByAtom( ATOM atom, HINSTANCE32 hinstance )
215 {
216     CLASS * class;
217
218     /* First search task-specific classes */
219
220     for (class = firstClass; (class); class = class->next)
221     {
222         if (class->style & CS_GLOBALCLASS) continue;
223         if ((class->atomName == atom) && 
224             ((hinstance == 0xffff) ||
225              (hinstance == class->hInstance))) return class;
226     }
227     
228       /* Then search global classes */
229
230     for (class = firstClass; (class); class = class->next)
231     {
232         if (!(class->style & CS_GLOBALCLASS)) continue;
233         if (class->atomName == atom) return class;
234     }
235
236     return 0;
237 }
238
239
240 /***********************************************************************
241  *           CLASS_RegisterClass
242  *
243  * The real RegisterClass() functionality.
244  */
245 static CLASS *CLASS_RegisterClass( ATOM atom, HINSTANCE32 hInstance,
246                                    DWORD style, INT32 classExtra,
247                                    INT32 winExtra, WNDPROC16 wndProc,
248                                    WINDOWPROCTYPE wndProcType )
249 {
250     CLASS *classPtr;
251
252     /* Check if a class with this name already exists */
253
254     classPtr = CLASS_FindClassByAtom( atom, hInstance );
255     if (classPtr)
256     {
257         /* Class can be created only if it is local and */
258         /* if the class with the same name is global.   */
259
260         if (style & CS_GLOBALCLASS) return NULL;
261         if (!(classPtr->style & CS_GLOBALCLASS)) return NULL;
262     }
263
264     /* Fix the extra bytes value */
265
266     if (classExtra < 0) classExtra = 0;
267     else if (classExtra > 40)  /* Extra bytes are limited to 40 in Win32 */
268         fprintf(stderr, "Warning: class extra bytes %d is > 40\n", classExtra);
269     if (winExtra < 0) winExtra = 0;
270     else if (winExtra > 40)    /* Extra bytes are limited to 40 in Win32 */
271         fprintf( stderr, "Warning: win extra bytes %d is > 40\n", winExtra );
272
273     /* Create the class */
274
275     classPtr = (CLASS *)HeapAlloc( SystemHeap, 0, sizeof(CLASS) +
276                                        classExtra - sizeof(classPtr->wExtra) );
277     if (!classPtr) return NULL;
278     classPtr->next        = firstClass;
279     classPtr->magic       = CLASS_MAGIC;
280     classPtr->cWindows    = 0;  
281     classPtr->style       = style;
282     classPtr->winproc     = (HWINDOWPROC)0;
283     classPtr->cbWndExtra  = winExtra;
284     classPtr->cbClsExtra  = classExtra;
285     classPtr->hInstance   = hInstance;
286     classPtr->atomName    = atom;
287     classPtr->menuNameA   = 0;
288     classPtr->menuNameW   = 0;
289     classPtr->dce         = (style & CS_CLASSDC) ?
290                                  DCE_AllocDCE( 0, DCE_CLASS_DC ) : NULL;
291
292     WINPROC_SetProc( &classPtr->winproc, wndProc, wndProcType, WIN_PROC_CLASS);
293
294     /* Other values must be set by caller */
295
296     if (classExtra) memset( classPtr->wExtra, 0, classExtra );
297     firstClass = classPtr;
298     return classPtr;
299 }
300
301
302 /***********************************************************************
303  *           RegisterClass16    (USER.57)
304  */
305 ATOM WINAPI RegisterClass16( const WNDCLASS16 *wc )
306 {
307     ATOM atom;
308     CLASS *classPtr;
309     HINSTANCE16 hInstance=GetExePtr(wc->hInstance);
310
311     if (!(atom = GlobalAddAtom16( wc->lpszClassName ))) return 0;
312     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
313                                           wc->cbClsExtra, wc->cbWndExtra,
314                                           wc->lpfnWndProc, WIN_PROC_16 )))
315     {
316         GlobalDeleteAtom( atom );
317         return 0;
318     }
319
320     dprintf_info(class, "RegisterClass16: atom=%04x wndproc=%08lx hinst=%04x
321 bg=%04x style=%08x clsExt=%d winExt=%d class=%p name='%s'\n",
322                    atom, (DWORD)wc->lpfnWndProc, hInstance,
323                    wc->hbrBackground, wc->style, wc->cbClsExtra,
324                    wc->cbWndExtra, classPtr,
325                    HIWORD(wc->lpszClassName) ?
326                        (char *)PTR_SEG_TO_LIN(wc->lpszClassName) : "" );
327
328     classPtr->hIcon         = wc->hIcon;
329     classPtr->hIconSm       = 0;
330     classPtr->hCursor       = wc->hCursor;
331     classPtr->hbrBackground = wc->hbrBackground;
332
333     CLASS_SetMenuNameA( classPtr, HIWORD(wc->lpszMenuName) ?
334                  PTR_SEG_TO_LIN(wc->lpszMenuName) : (LPCSTR)wc->lpszMenuName );
335     return atom;
336 }
337
338
339 /***********************************************************************
340  *           RegisterClass32A      (USER32.426)
341  */
342 ATOM WINAPI RegisterClass32A( const WNDCLASS32A* wc )
343 {
344     ATOM atom;
345     CLASS *classPtr;
346
347     if (!(atom = GlobalAddAtom32A( wc->lpszClassName ))) return 0;
348     if (!(classPtr = CLASS_RegisterClass( atom, wc->hInstance, wc->style,
349                                           wc->cbClsExtra, wc->cbWndExtra,
350                                           (WNDPROC16)wc->lpfnWndProc,
351                                           WIN_PROC_32A )))
352     {
353         GlobalDeleteAtom( atom );
354         return 0;
355     }
356
357     dprintf_info(class, "RegisterClass32A: atom=%04x wndproc=%08lx
358 hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p name='%s'\n",
359                    atom, (DWORD)wc->lpfnWndProc, wc->hInstance,
360                    wc->hbrBackground, wc->style, wc->cbClsExtra,
361                    wc->cbWndExtra, classPtr,
362                    HIWORD(wc->lpszClassName) ? wc->lpszClassName : "" );
363     
364     classPtr->hIcon         = (HICON16)wc->hIcon;
365     classPtr->hIconSm       = 0;
366     classPtr->hCursor       = (HCURSOR16)wc->hCursor;
367     classPtr->hbrBackground = (HBRUSH16)wc->hbrBackground;
368     CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
369     return atom;
370 }
371
372
373 /***********************************************************************
374  *           RegisterClass32W      (USER32.429)
375  */
376 ATOM WINAPI RegisterClass32W( const WNDCLASS32W* wc )
377 {
378     ATOM atom;
379     CLASS *classPtr;
380
381     if (!(atom = GlobalAddAtom32W( wc->lpszClassName ))) return 0;
382     if (!(classPtr = CLASS_RegisterClass( atom, wc->hInstance, wc->style,
383                                           wc->cbClsExtra, wc->cbWndExtra,
384                                           (WNDPROC16)wc->lpfnWndProc,
385                                           WIN_PROC_32W )))
386     {
387         GlobalDeleteAtom( atom );
388         return 0;
389     }
390
391     dprintf_info(class, "RegisterClass32W: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
392                    atom, (DWORD)wc->lpfnWndProc, wc->hInstance,
393                    wc->hbrBackground, wc->style, wc->cbClsExtra,
394                    wc->cbWndExtra, classPtr );
395     
396     classPtr->hIcon         = (HICON16)wc->hIcon;
397     classPtr->hIconSm       = 0;
398     classPtr->hCursor       = (HCURSOR16)wc->hCursor;
399     classPtr->hbrBackground = (HBRUSH16)wc->hbrBackground;
400     CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
401     return atom;
402 }
403
404
405 /***********************************************************************
406  *           RegisterClassEx16    (USER.397)
407  */
408 ATOM WINAPI RegisterClassEx16( const WNDCLASSEX16 *wc )
409 {
410     ATOM atom;
411     CLASS *classPtr;
412     HINSTANCE16 hInstance = GetExePtr( wc->hInstance );
413
414     if (!(atom = GlobalAddAtom16( wc->lpszClassName ))) return 0;
415     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
416                                           wc->cbClsExtra, wc->cbWndExtra,
417                                           wc->lpfnWndProc, WIN_PROC_16 )))
418     {
419         GlobalDeleteAtom( atom );
420         return 0;
421     }
422
423     dprintf_info(class, "RegisterClassEx16: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
424                    atom, (DWORD)wc->lpfnWndProc, hInstance,
425                    wc->hbrBackground, wc->style, wc->cbClsExtra,
426                    wc->cbWndExtra, classPtr );
427     
428     classPtr->hIcon         = wc->hIcon;
429     classPtr->hIconSm       = wc->hIconSm;
430     classPtr->hCursor       = wc->hCursor;
431     classPtr->hbrBackground = wc->hbrBackground;
432
433     CLASS_SetMenuNameA( classPtr, HIWORD(wc->lpszMenuName) ?
434                  PTR_SEG_TO_LIN(wc->lpszMenuName) : (LPCSTR)wc->lpszMenuName );
435     return atom;
436 }
437
438
439 /***********************************************************************
440  *           RegisterClassEx32A      (USER32.427)
441  */
442 ATOM WINAPI RegisterClassEx32A( const WNDCLASSEX32A* wc )
443 {
444     ATOM atom;
445     CLASS *classPtr;
446
447     if (!(atom = GlobalAddAtom32A( wc->lpszClassName ))) return 0;
448     if (!(classPtr = CLASS_RegisterClass( atom, wc->hInstance, wc->style,
449                                           wc->cbClsExtra, wc->cbWndExtra,
450                                           (WNDPROC16)wc->lpfnWndProc,
451                                           WIN_PROC_32A )))
452     {
453         GlobalDeleteAtom( atom );
454         return 0;
455     }
456
457     dprintf_info(class, "RegisterClassEx32A: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
458                    atom, (DWORD)wc->lpfnWndProc, wc->hInstance,
459                    wc->hbrBackground, wc->style, wc->cbClsExtra,
460                    wc->cbWndExtra, classPtr );
461     
462     classPtr->hIcon         = (HICON16)wc->hIcon;
463     classPtr->hIconSm       = (HICON16)wc->hIconSm;
464     classPtr->hCursor       = (HCURSOR16)wc->hCursor;
465     classPtr->hbrBackground = (HBRUSH16)wc->hbrBackground;
466     CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
467     return atom;
468 }
469
470
471 /***********************************************************************
472  *           RegisterClassEx32W      (USER32.428)
473  */
474 ATOM WINAPI RegisterClassEx32W( const WNDCLASSEX32W* wc )
475 {
476     ATOM atom;
477     CLASS *classPtr;
478
479     if (!(atom = GlobalAddAtom32W( wc->lpszClassName ))) return 0;
480     if (!(classPtr = CLASS_RegisterClass( atom, wc->hInstance, wc->style,
481                                           wc->cbClsExtra, wc->cbWndExtra,
482                                           (WNDPROC16)wc->lpfnWndProc,
483                                           WIN_PROC_32W )))
484     {
485         GlobalDeleteAtom( atom );
486         return 0;
487     }
488
489     dprintf_info(class, "RegisterClassEx32W: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
490                    atom, (DWORD)wc->lpfnWndProc, wc->hInstance,
491                    wc->hbrBackground, wc->style, wc->cbClsExtra,
492                    wc->cbWndExtra, classPtr );
493     
494     classPtr->hIcon         = (HICON16)wc->hIcon;
495     classPtr->hIconSm       = (HICON16)wc->hIconSm;
496     classPtr->hCursor       = (HCURSOR16)wc->hCursor;
497     classPtr->hbrBackground = (HBRUSH16)wc->hbrBackground;
498     CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
499     return atom;
500 }
501
502
503 /***********************************************************************
504  *           UnregisterClass16    (USER.403)
505  */
506 BOOL16 WINAPI UnregisterClass16( SEGPTR className, HINSTANCE16 hInstance )
507 {
508     CLASS *classPtr;
509     ATOM atom;
510
511     hInstance = GetExePtr( hInstance );
512     if (!(atom = GlobalFindAtom16( className ))) return FALSE;
513     if (!(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
514         (classPtr->hInstance != hInstance)) return FALSE;
515     return CLASS_FreeClass( classPtr );
516 }
517
518
519 /***********************************************************************
520  *           UnregisterClass32A    (USER32.562)
521  */
522 BOOL32 WINAPI UnregisterClass32A( LPCSTR className, HINSTANCE32 hInstance )
523 {
524     CLASS *classPtr;
525     ATOM atom;
526
527     if (!(atom = GlobalFindAtom32A( className ))) return FALSE;
528     if (!(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
529         (classPtr->hInstance != hInstance)) return FALSE;
530     return CLASS_FreeClass( classPtr );
531 }
532
533
534 /***********************************************************************
535  *           UnregisterClass32W    (USER32.563)
536  */
537 BOOL32 WINAPI UnregisterClass32W( LPCWSTR className, HINSTANCE32 hInstance )
538 {
539     CLASS *classPtr;
540     ATOM atom;
541
542     if (!(atom = GlobalFindAtom32W( className ))) return FALSE;
543     if (!(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
544         (classPtr->hInstance != hInstance)) return FALSE;
545     return CLASS_FreeClass( classPtr );
546 }
547
548
549 /***********************************************************************
550  *           GetClassWord16    (USER.129)
551  */
552 WORD WINAPI GetClassWord16( HWND16 hwnd, INT16 offset )
553 {
554     return GetClassWord32( hwnd, offset );
555 }
556
557
558 /***********************************************************************
559  *           GetClassWord32    (USER32.218)
560  */
561 WORD WINAPI GetClassWord32( HWND32 hwnd, INT32 offset )
562 {
563     WND * wndPtr;
564     
565     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
566     if (offset >= 0)
567     {
568         if (offset <= wndPtr->class->cbClsExtra - sizeof(WORD))
569             return GET_WORD(((char *)wndPtr->class->wExtra) + offset);
570     }
571     else switch(offset)
572     {
573         case GCW_HBRBACKGROUND: return wndPtr->class->hbrBackground;
574         case GCW_HCURSOR:       return wndPtr->class->hCursor;
575         case GCW_HICON:         return wndPtr->class->hIcon;
576         case GCW_HICONSM:       return wndPtr->class->hIconSm;
577         case GCW_ATOM:          return wndPtr->class->atomName;
578         case GCW_STYLE:
579         case GCW_CBWNDEXTRA:
580         case GCW_CBCLSEXTRA:
581         case GCW_HMODULE:
582             return (WORD)GetClassLong32A( hwnd, offset );
583     }
584     fprintf(stderr, "Warning: invalid offset %d for GetClassWord()\n", offset);
585     return 0;
586 }
587
588
589 /***********************************************************************
590  *           GetClassLong16    (USER.131)
591  */
592 LONG WINAPI GetClassLong16( HWND16 hwnd, INT16 offset )
593 {
594     WND *wndPtr;
595     LONG ret;
596
597     switch( offset )
598     {
599     case GCL_WNDPROC:
600         if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
601         return (LONG)WINPROC_GetProc( wndPtr->class->winproc, WIN_PROC_16 );
602     case GCL_MENUNAME:
603         ret = GetClassLong32A( hwnd, offset );
604         return (LONG)SEGPTR_GET( (void *)ret );
605     default:
606         return GetClassLong32A( hwnd, offset );
607     }
608 }
609
610
611 /***********************************************************************
612  *           GetClassLong32A    (USER32.214)
613  */
614 LONG WINAPI GetClassLong32A( HWND32 hwnd, INT32 offset )
615 {
616     WND * wndPtr;
617     
618     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
619     if (offset >= 0)
620     {
621         if (offset <= wndPtr->class->cbClsExtra - sizeof(LONG))
622             return GET_DWORD(((char *)wndPtr->class->wExtra) + offset);
623     }
624     switch(offset)
625     {
626         case GCL_STYLE:      return (LONG)wndPtr->class->style;
627         case GCL_CBWNDEXTRA: return (LONG)wndPtr->class->cbWndExtra;
628         case GCL_CBCLSEXTRA: return (LONG)wndPtr->class->cbClsExtra;
629         case GCL_HMODULE:    return (LONG)wndPtr->class->hInstance;
630         case GCL_WNDPROC:
631             return (LONG)WINPROC_GetProc(wndPtr->class->winproc, WIN_PROC_32A);
632         case GCL_MENUNAME:
633             return (LONG)CLASS_GetMenuNameA( wndPtr->class );
634         case GCL_HBRBACKGROUND:
635         case GCL_HCURSOR:
636         case GCL_HICON:
637         case GCL_HICONSM:
638             return GetClassWord32( hwnd, offset );
639     }
640     fprintf(stderr, "Warning: invalid offset %d for GetClassLong()\n", offset);
641     return 0;
642 }
643
644
645 /***********************************************************************
646  *           GetClassLong32W    (USER32.215)
647  */
648 LONG WINAPI GetClassLong32W( HWND32 hwnd, INT32 offset )
649 {
650     WND * wndPtr;
651
652     switch(offset)
653     {
654     case GCL_WNDPROC:
655         if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
656         return (LONG)WINPROC_GetProc( wndPtr->class->winproc, WIN_PROC_32W );
657     case GCL_MENUNAME:
658         if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
659         return (LONG)CLASS_GetMenuNameW( wndPtr->class );
660     default:
661         return GetClassLong32A( hwnd, offset );
662     }
663 }
664
665
666 /***********************************************************************
667  *           SetClassWord16    (USER.130)
668  */
669 WORD WINAPI SetClassWord16( HWND16 hwnd, INT16 offset, WORD newval )
670 {
671     return SetClassWord32( hwnd, offset, newval );
672 }
673
674
675 /***********************************************************************
676  *           SetClassWord32    (USER32.468)
677  */
678 WORD WINAPI SetClassWord32( HWND32 hwnd, INT32 offset, WORD newval )
679 {
680     WND * wndPtr;
681     WORD retval = 0;
682     void *ptr;
683     
684     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
685     if (offset >= 0)
686     {
687         if (offset + sizeof(WORD) <= wndPtr->class->cbClsExtra)
688             ptr = ((char *)wndPtr->class->wExtra) + offset;
689         else
690         {
691             fprintf( stderr, "Warning: invalid offset %d for SetClassWord()\n",
692                      offset );
693             return 0;
694         }
695     }
696     else switch(offset)
697     {
698         case GCW_STYLE:
699         case GCW_CBWNDEXTRA:
700         case GCW_CBCLSEXTRA:
701         case GCW_HMODULE:
702             return (WORD)SetClassLong32A( hwnd, offset, (LONG)newval );
703         case GCW_HBRBACKGROUND: ptr = &wndPtr->class->hbrBackground; break;
704         case GCW_HCURSOR:       ptr = &wndPtr->class->hCursor; break;
705         case GCW_HICON:         ptr = &wndPtr->class->hIcon; break;
706         case GCW_HICONSM:       ptr = &wndPtr->class->hIconSm; break;
707         case GCW_ATOM:          ptr = &wndPtr->class->atomName; break;
708         default:
709             fprintf( stderr, "Warning: invalid offset %d for SetClassWord()\n",
710                      offset);
711             return 0;
712     }
713     retval = GET_WORD(ptr);
714     PUT_WORD( ptr, newval );
715     return retval;
716 }
717
718
719 /***********************************************************************
720  *           SetClassLong16    (USER.132)
721  */
722 LONG WINAPI SetClassLong16( HWND16 hwnd, INT16 offset, LONG newval )
723 {
724     WND *wndPtr;
725     LONG retval;
726
727     switch(offset)
728     {
729     case GCL_WNDPROC:
730         if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
731         retval = (LONG)WINPROC_GetProc( wndPtr->class->winproc, WIN_PROC_16 );
732         WINPROC_SetProc( &wndPtr->class->winproc, (WNDPROC16)newval,
733                          WIN_PROC_16, WIN_PROC_CLASS );
734         return retval;
735     case GCL_MENUNAME:
736         return SetClassLong32A( hwnd, offset, (LONG)PTR_SEG_TO_LIN(newval) );
737     default:
738         return SetClassLong32A( hwnd, offset, newval );
739     }
740 }
741
742
743 /***********************************************************************
744  *           SetClassLong32A    (USER32.466)
745  */
746 LONG WINAPI SetClassLong32A( HWND32 hwnd, INT32 offset, LONG newval )
747 {
748     WND * wndPtr;
749     LONG retval = 0;
750     void *ptr;
751     
752     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
753     if (offset >= 0)
754     {
755         if (offset + sizeof(LONG) <= wndPtr->class->cbClsExtra)
756             ptr = ((char *)wndPtr->class->wExtra) + offset;
757         else
758         {
759             fprintf( stderr, "Warning: invalid offset %d for SetClassLong()\n",
760                      offset );
761             return 0;
762         }
763     }
764     else switch(offset)
765     {
766         case GCL_MENUNAME:
767             CLASS_SetMenuNameA( wndPtr->class, (LPCSTR)newval );
768             return 0;  /* Old value is now meaningless anyway */
769         case GCL_WNDPROC:
770             retval = (LONG)WINPROC_GetProc( wndPtr->class->winproc,
771                                             WIN_PROC_32A );
772             WINPROC_SetProc( &wndPtr->class->winproc, (WNDPROC16)newval,
773                              WIN_PROC_32A, WIN_PROC_CLASS );
774             return retval;
775         case GCL_HBRBACKGROUND:
776         case GCL_HCURSOR:
777         case GCL_HICON:
778         case GCL_HICONSM:
779             return SetClassWord32( hwnd, offset, (WORD)newval );
780         case GCL_STYLE:      ptr = &wndPtr->class->style; break;
781         case GCL_CBWNDEXTRA: ptr = &wndPtr->class->cbWndExtra; break;
782         case GCL_CBCLSEXTRA: ptr = &wndPtr->class->cbClsExtra; break;
783         case GCL_HMODULE:    ptr = &wndPtr->class->hInstance; break;
784         default:
785             fprintf( stderr, "Warning: invalid offset %d for SetClassLong()\n",
786                      offset);
787             return 0;
788     }
789     retval = GET_DWORD(ptr);
790     PUT_DWORD( ptr, newval );
791     return retval;
792 }
793
794
795 /***********************************************************************
796  *           SetClassLong32W    (USER32.467)
797  */
798 LONG WINAPI SetClassLong32W( HWND32 hwnd, INT32 offset, LONG newval )
799 {
800     WND *wndPtr;
801     LONG retval;
802
803     switch(offset)
804     {
805     case GCL_WNDPROC:
806         if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
807         retval = (LONG)WINPROC_GetProc( wndPtr->class->winproc, WIN_PROC_32W );
808         WINPROC_SetProc( &wndPtr->class->winproc, (WNDPROC16)newval,
809                          WIN_PROC_32W, WIN_PROC_CLASS );
810         return retval;
811     case GCL_MENUNAME:
812         if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
813         CLASS_SetMenuNameW( wndPtr->class, (LPCWSTR)newval );
814         return 0;  /* Old value is now meaningless anyway */
815     default:
816         return SetClassLong32A( hwnd, offset, newval );
817     }
818 }
819
820
821 /***********************************************************************
822  *           GetClassName16      (USER.58)
823  */
824 INT16 WINAPI GetClassName16( HWND16 hwnd, LPSTR buffer, INT16 count )
825 {
826     WND *wndPtr;
827     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
828     return GlobalGetAtomName16( wndPtr->class->atomName, buffer, count );
829 }
830
831
832 /***********************************************************************
833  *           GetClassName32A      (USER32.216)
834  */
835 INT32 WINAPI GetClassName32A( HWND32 hwnd, LPSTR buffer, INT32 count )
836 {
837     WND *wndPtr;
838     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
839     return GlobalGetAtomName32A( wndPtr->class->atomName, buffer, count );
840 }
841
842
843 /***********************************************************************
844  *           GetClassName32W      (USER32.217)
845  */
846 INT32 WINAPI GetClassName32W( HWND32 hwnd, LPWSTR buffer, INT32 count )
847 {
848     WND *wndPtr;
849     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
850     return GlobalGetAtomName32W( wndPtr->class->atomName, buffer, count );
851 }
852
853
854 /***********************************************************************
855  *           GetClassInfo16      (USER.404)
856  */
857 BOOL16 WINAPI GetClassInfo16( HINSTANCE16 hInstance, SEGPTR name,
858                               WNDCLASS16 *wc )
859 {
860     ATOM atom;
861     CLASS *classPtr;
862
863     hInstance = GetExePtr( hInstance );
864     if (!(atom = GlobalFindAtom16( name )) ||
865         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )))
866         return FALSE;
867     if ((hInstance != classPtr->hInstance) &&
868         !(classPtr->style & CS_GLOBALCLASS)) /*BWCC likes to pass hInstance=0*/
869         return FALSE;
870     wc->style         = (UINT16)classPtr->style;
871     wc->lpfnWndProc   = WINPROC_GetProc( classPtr->winproc, WIN_PROC_16 );
872     wc->cbClsExtra    = (INT16)classPtr->cbClsExtra;
873     wc->cbWndExtra    = (INT16)classPtr->cbWndExtra;
874     wc->hInstance     = (HINSTANCE16)classPtr->hInstance;
875     wc->hIcon         = classPtr->hIcon;
876     wc->hCursor       = classPtr->hCursor;
877     wc->hbrBackground = classPtr->hbrBackground;
878     wc->lpszClassName = (SEGPTR)0;
879     wc->lpszMenuName  = (SEGPTR)CLASS_GetMenuNameA( classPtr );
880     if (HIWORD(wc->lpszMenuName))  /* Make it a SEGPTR */
881         wc->lpszMenuName = SEGPTR_GET( (LPSTR)wc->lpszMenuName );
882     return TRUE;
883 }
884
885
886 /***********************************************************************
887  *           GetClassInfo32A      (USER32.210)
888  */
889 BOOL32 WINAPI GetClassInfo32A( HINSTANCE32 hInstance, LPCSTR name,
890                                WNDCLASS32A *wc )
891 {
892     ATOM atom;
893     CLASS *classPtr;
894
895     if (!(atom = GlobalFindAtom32A( name )) ||
896         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
897         (classPtr->hInstance && (hInstance != classPtr->hInstance)))
898         return FALSE;
899
900     wc->style         = classPtr->style;
901     wc->lpfnWndProc   = (WNDPROC32)WINPROC_GetProc( classPtr->winproc,
902                                                     WIN_PROC_32A );
903     wc->cbClsExtra    = classPtr->cbClsExtra;
904     wc->cbWndExtra    = classPtr->cbWndExtra;
905     wc->hInstance     = classPtr->hInstance;
906     wc->hIcon         = (HICON32)classPtr->hIcon;
907     wc->hCursor       = (HCURSOR32)classPtr->hCursor;
908     wc->hbrBackground = (HBRUSH32)classPtr->hbrBackground;
909     wc->lpszMenuName  = CLASS_GetMenuNameA( classPtr );
910     wc->lpszClassName = NULL;
911     return TRUE;
912 }
913
914
915 /***********************************************************************
916  *           GetClassInfo32W      (USER32.213)
917  */
918 BOOL32 WINAPI GetClassInfo32W( HINSTANCE32 hInstance, LPCWSTR name,
919                                WNDCLASS32W *wc )
920 {
921     ATOM atom;
922     CLASS *classPtr;
923
924     if (!(atom = GlobalFindAtom32W( name )) ||
925         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
926         (classPtr->hInstance && (hInstance != classPtr->hInstance)))
927         return FALSE;
928
929     wc->style         = classPtr->style;
930     wc->lpfnWndProc   = (WNDPROC32)WINPROC_GetProc( classPtr->winproc,
931                                                     WIN_PROC_32W );
932     wc->cbClsExtra    = classPtr->cbClsExtra;
933     wc->cbWndExtra    = classPtr->cbWndExtra;
934     wc->hInstance     = classPtr->hInstance;
935     wc->hIcon         = (HICON32)classPtr->hIcon;
936     wc->hCursor       = (HCURSOR32)classPtr->hCursor;
937     wc->hbrBackground = (HBRUSH32)classPtr->hbrBackground;
938     wc->lpszMenuName  = CLASS_GetMenuNameW( classPtr );
939     wc->lpszClassName = NULL;
940     return TRUE;
941 }
942
943
944 /***********************************************************************
945  *           GetClassInfoEx16      (USER.398)
946  *
947  * FIXME: this is just a guess, I have no idea if GetClassInfoEx() is the
948  * same in Win16 as in Win32. --AJ
949  */
950 BOOL16 WINAPI GetClassInfoEx16( HINSTANCE16 hInstance, SEGPTR name,
951                                 WNDCLASSEX16 *wc )
952 {
953     ATOM atom;
954     CLASS *classPtr;
955
956     hInstance = GetExePtr( hInstance );
957     if (!(atom = GlobalFindAtom16( name )) ||
958         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
959         (hInstance != classPtr->hInstance)) return FALSE;
960     wc->style         = classPtr->style;
961     wc->lpfnWndProc   = WINPROC_GetProc( classPtr->winproc, WIN_PROC_16 );
962     wc->cbClsExtra    = (INT16)classPtr->cbClsExtra;
963     wc->cbWndExtra    = (INT16)classPtr->cbWndExtra;
964     wc->hInstance     = (HINSTANCE16)classPtr->hInstance;
965     wc->hIcon         = classPtr->hIcon;
966     wc->hIconSm       = classPtr->hIconSm;
967     wc->hCursor       = classPtr->hCursor;
968     wc->hbrBackground = classPtr->hbrBackground;
969     wc->lpszClassName = (SEGPTR)0;
970     wc->lpszMenuName  = (SEGPTR)CLASS_GetMenuNameA( classPtr );
971     if (HIWORD(wc->lpszMenuName))  /* Make it a SEGPTR */
972         wc->lpszMenuName = SEGPTR_GET( (LPSTR)wc->lpszMenuName );
973     return TRUE;
974 }
975
976
977 /***********************************************************************
978  *           GetClassInfoEx32A      (USER32.211)
979  */
980 BOOL32 WINAPI GetClassInfoEx32A( HINSTANCE32 hInstance, LPCSTR name,
981                                  WNDCLASSEX32A *wc )
982 {
983     ATOM atom;
984     CLASS *classPtr;
985
986     if (!(atom = GlobalFindAtom32A( name )) ||
987         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
988         (hInstance != classPtr->hInstance)) return FALSE;
989     wc->style         = classPtr->style;
990     wc->lpfnWndProc   = (WNDPROC32)WINPROC_GetProc( classPtr->winproc,
991                                                     WIN_PROC_32A );
992     wc->cbClsExtra    = classPtr->cbClsExtra;
993     wc->cbWndExtra    = classPtr->cbWndExtra;
994     wc->hInstance     = classPtr->hInstance;
995     wc->hIcon         = (HICON32)classPtr->hIcon;
996     wc->hIconSm       = (HICON32)classPtr->hIconSm;
997     wc->hCursor       = (HCURSOR32)classPtr->hCursor;
998     wc->hbrBackground = (HBRUSH32)classPtr->hbrBackground;
999     wc->lpszMenuName  = CLASS_GetMenuNameA( classPtr );
1000     wc->lpszClassName = NULL;
1001     return TRUE;
1002 }
1003
1004
1005 /***********************************************************************
1006  *           GetClassInfoEx32W      (USER32.212)
1007  */
1008 BOOL32 WINAPI GetClassInfoEx32W( HINSTANCE32 hInstance, LPCWSTR name,
1009                                  WNDCLASSEX32W *wc )
1010 {
1011     ATOM atom;
1012     CLASS *classPtr;
1013
1014     if (!(atom = GlobalFindAtom32W( name )) ||
1015         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
1016         (hInstance != classPtr->hInstance)) return FALSE;
1017     wc->style         = classPtr->style;
1018     wc->lpfnWndProc   = (WNDPROC32)WINPROC_GetProc( classPtr->winproc,
1019                                                     WIN_PROC_32W );
1020     wc->cbClsExtra    = classPtr->cbClsExtra;
1021     wc->cbWndExtra    = classPtr->cbWndExtra;
1022     wc->hInstance     = classPtr->hInstance;
1023     wc->hIcon         = (HICON32)classPtr->hIcon;
1024     wc->hIconSm       = (HICON32)classPtr->hIconSm;
1025     wc->hCursor       = (HCURSOR32)classPtr->hCursor;
1026     wc->hbrBackground = (HBRUSH32)classPtr->hbrBackground;
1027     wc->lpszMenuName  = CLASS_GetMenuNameW( classPtr );
1028     wc->lpszClassName = NULL;
1029     return TRUE;
1030 }
1031
1032
1033 /***********************************************************************
1034  *           ClassFirst      (TOOLHELP.69)
1035  */
1036 BOOL16 WINAPI ClassFirst( CLASSENTRY *pClassEntry )
1037 {
1038     pClassEntry->wNext = 1;
1039     return ClassNext( pClassEntry );
1040 }
1041
1042
1043 /***********************************************************************
1044  *           ClassNext      (TOOLHELP.70)
1045  */
1046 BOOL16 WINAPI ClassNext( CLASSENTRY *pClassEntry )
1047 {
1048     int i;
1049     CLASS *class = firstClass;
1050
1051     if (!pClassEntry->wNext) return FALSE;
1052     for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1053     if (!class)
1054     {
1055         pClassEntry->wNext = 0;
1056         return FALSE;
1057     }
1058     pClassEntry->hInst = class->hInstance;
1059     pClassEntry->wNext++;
1060     GlobalGetAtomName32A( class->atomName, pClassEntry->szClassName,
1061                           sizeof(pClassEntry->szClassName) );
1062     return TRUE;
1063 }