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