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