Release 960728
[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 "string32.h"
17 #include "toolhelp.h"
18 #include "winproc.h"
19 #include "stddebug.h"
20 #include "debug.h"
21
22
23 static CLASS *firstClass = NULL;
24
25
26 /***********************************************************************
27  *           CLASS_DumpClass
28  *
29  * Dump the content of a class structure to stderr.
30  */
31 void CLASS_DumpClass( CLASS *ptr )
32 {
33     char className[80];
34     int i;
35
36     if (ptr->magic != CLASS_MAGIC)
37     {
38         fprintf( stderr, "%p is not a class\n", ptr );
39         return;
40     }
41
42     GlobalGetAtomName32A( ptr->atomName, className, sizeof(className) );
43
44     fprintf( stderr, "Class %p:\n", ptr );
45     fprintf( stderr,
46              "next=%p  name=%04x '%s'  style=%08x  wndProc=%08x\n"
47              "inst=%04x  hdce=%04x  icon=%04x  cursor=%04x  bkgnd=%04x\n"
48              "clsExtra=%d  winExtra=%d  #windows=%d\n",
49              ptr->next, ptr->atomName, className, ptr->style,
50              (UINT32)ptr->winproc, ptr->hInstance, ptr->hdce,
51              ptr->hIcon, ptr->hCursor, ptr->hbrBackground,
52              ptr->cbClsExtra, ptr->cbWndExtra, ptr->cWindows );
53     if (ptr->cbClsExtra)
54     {
55         fprintf( stderr, "extra bytes:" );
56         for (i = 0; i < ptr->cbClsExtra; i++)
57             fprintf( stderr, " %02x", *((BYTE *)ptr->wExtra+i) );
58         fprintf( stderr, "\n" );
59     }
60     fprintf( stderr, "\n" );
61 }
62
63
64 /***********************************************************************
65  *           CLASS_WalkClasses
66  *
67  * Walk the class list and print each class on stderr.
68  */
69 void CLASS_WalkClasses(void)
70 {
71     CLASS *ptr;
72     char className[80];
73
74     fprintf( stderr, " Class   Name                  Style   WndProc\n" );
75     for (ptr = firstClass; ptr; ptr = ptr->next)
76     {
77         GlobalGetAtomName32A( ptr->atomName, className, sizeof(className) );
78         fprintf( stderr, "%08x %-20.20s %08x %08x\n", (UINT32)ptr, className,
79                  ptr->style, (UINT32)ptr->winproc );
80     }
81     fprintf( stderr, "\n" );
82 }
83
84
85 /***********************************************************************
86  *           CLASS_GetMenuNameA
87  *
88  * Get the menu name as a ASCII string.
89  */
90 static LPSTR CLASS_GetMenuNameA( CLASS *classPtr )
91 {
92     if (!classPtr->menuNameA && classPtr->menuNameW)
93     {
94         /* We need to copy the Unicode string */
95         if ((classPtr->menuNameA = SEGPTR_ALLOC(
96                                         lstrlen32W(classPtr->menuNameW) + 1 )))
97             STRING32_UniToAnsi( classPtr->menuNameA, 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         if ((classPtr->menuNameW = HeapAlloc( SystemHeap, 0,
116                               (strlen(classPtr->menuNameA)+1)*sizeof(WCHAR) )))
117             STRING32_AnsiToUni( classPtr->menuNameW, 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 BOOL 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->hdce) DCE_FreeDCE( classPtr->hdce );
185     if (classPtr->hbrBackground) DeleteObject( classPtr->hbrBackground );
186     GlobalDeleteAtom( classPtr->atomName );
187     CLASS_SetMenuNameA( classPtr, NULL );
188     WINPROC_FreeProc( classPtr->winproc );
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->hdce        = (style&CS_CLASSDC) ? DCE_AllocDCE(0, DCE_CLASS_DC): 0;
292     WINPROC_SetProc( &classPtr->winproc, wndProc, wndProcType );
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 RegisterClass16( const WNDCLASS16 *wc )
306 {
307     ATOM atom;
308     CLASS *classPtr;
309
310     HINSTANCE32 hInstance = (HINSTANCE32)GetExePtr( wc->hInstance );
311
312     if (!(atom = GlobalAddAtom16( wc->lpszClassName ))) return 0;
313     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
314                                           wc->cbClsExtra, wc->cbWndExtra,
315                                           wc->lpfnWndProc, WIN_PROC_16 )))
316     {
317         GlobalDeleteAtom( atom );
318         return 0;
319     }
320
321     dprintf_class( stddeb, "RegisterClass16: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
322                    atom, (DWORD)wc->lpfnWndProc, hInstance,
323                    wc->hbrBackground, wc->style, wc->cbClsExtra,
324                    wc->cbWndExtra, classPtr );
325
326     classPtr->hIcon         = wc->hIcon;
327     classPtr->hIconSm       = 0;
328     classPtr->hCursor       = wc->hCursor;
329     classPtr->hbrBackground = wc->hbrBackground;
330
331     CLASS_SetMenuNameA( classPtr, HIWORD(wc->lpszMenuName) ?
332                  PTR_SEG_TO_LIN(wc->lpszMenuName) : (LPCSTR)wc->lpszMenuName );
333     return atom;
334 }
335
336
337 /***********************************************************************
338  *           RegisterClass32A      (USER32.426)
339  */
340 ATOM RegisterClass32A( const WNDCLASS32A* wc )
341 {
342     ATOM atom;
343     CLASS *classPtr;
344
345     /* FIXME: this should not be necessary for Win32 */
346     HINSTANCE32 hInstance = (HINSTANCE32)GetExePtr( wc->hInstance );
347
348     if (!(atom = GlobalAddAtom32A( wc->lpszClassName ))) return 0;
349     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
350                                           wc->cbClsExtra, wc->cbWndExtra,
351                                           (WNDPROC16)wc->lpfnWndProc,
352                                           WIN_PROC_32A )))
353     {
354         GlobalDeleteAtom( atom );
355         return 0;
356     }
357
358     dprintf_class( stddeb, "RegisterClass32A: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
359                    atom, (DWORD)wc->lpfnWndProc, hInstance,
360                    wc->hbrBackground, wc->style, wc->cbClsExtra,
361                    wc->cbWndExtra, classPtr );
362     
363     classPtr->hIcon         = (HICON16)wc->hIcon;
364     classPtr->hIconSm       = 0;
365     classPtr->hCursor       = (HCURSOR16)wc->hCursor;
366     classPtr->hbrBackground = (HBRUSH16)wc->hbrBackground;
367     CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
368     return atom;
369 }
370
371
372 /***********************************************************************
373  *           RegisterClass32W      (USER32.429)
374  */
375 ATOM RegisterClass32W( const WNDCLASS32W* wc )
376 {
377     ATOM atom;
378     CLASS *classPtr;
379
380     /* FIXME: this should not be necessary for Win32 */
381     HINSTANCE32 hInstance = (HINSTANCE32)GetExePtr( wc->hInstance );
382
383     if (!(atom = GlobalAddAtom32W( wc->lpszClassName ))) return 0;
384     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
385                                           wc->cbClsExtra, wc->cbWndExtra,
386                                           (WNDPROC16)wc->lpfnWndProc,
387                                           WIN_PROC_32W )))
388     {
389         GlobalDeleteAtom( atom );
390         return 0;
391     }
392
393     dprintf_class( stddeb, "RegisterClass32W: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
394                    atom, (DWORD)wc->lpfnWndProc, hInstance,
395                    wc->hbrBackground, wc->style, wc->cbClsExtra,
396                    wc->cbWndExtra, classPtr );
397     
398     classPtr->hIcon         = (HICON16)wc->hIcon;
399     classPtr->hIconSm       = 0;
400     classPtr->hCursor       = (HCURSOR16)wc->hCursor;
401     classPtr->hbrBackground = (HBRUSH16)wc->hbrBackground;
402     CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
403     return atom;
404 }
405
406
407 /***********************************************************************
408  *           RegisterClassEx16    (USER.397)
409  */
410 ATOM RegisterClassEx16( const WNDCLASSEX16 *wc )
411 {
412     ATOM atom;
413     CLASS *classPtr;
414
415     HINSTANCE32 hInstance = (HINSTANCE32)GetExePtr( wc->hInstance );
416
417     if (!(atom = GlobalAddAtom16( wc->lpszClassName ))) return 0;
418     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
419                                           wc->cbClsExtra, wc->cbWndExtra,
420                                           wc->lpfnWndProc, WIN_PROC_16 )))
421     {
422         GlobalDeleteAtom( atom );
423         return 0;
424     }
425
426     dprintf_class( stddeb, "RegisterClassEx16: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
427                    atom, (DWORD)wc->lpfnWndProc, hInstance,
428                    wc->hbrBackground, wc->style, wc->cbClsExtra,
429                    wc->cbWndExtra, classPtr );
430     
431     classPtr->hIcon         = wc->hIcon;
432     classPtr->hIconSm       = wc->hIconSm;
433     classPtr->hCursor       = wc->hCursor;
434     classPtr->hbrBackground = wc->hbrBackground;
435
436     CLASS_SetMenuNameA( classPtr, HIWORD(wc->lpszMenuName) ?
437                  PTR_SEG_TO_LIN(wc->lpszMenuName) : (LPCSTR)wc->lpszMenuName );
438     return atom;
439 }
440
441
442 /***********************************************************************
443  *           RegisterClassEx32A      (USER32.427)
444  */
445 ATOM RegisterClassEx32A( const WNDCLASSEX32A* wc )
446 {
447     ATOM atom;
448     CLASS *classPtr;
449
450     /* FIXME: this should not be necessary for Win32 */
451     HINSTANCE32 hInstance = (HINSTANCE32)GetExePtr( wc->hInstance );
452
453     if (!(atom = GlobalAddAtom32A( wc->lpszClassName ))) return 0;
454     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
455                                           wc->cbClsExtra, wc->cbWndExtra,
456                                           (WNDPROC16)wc->lpfnWndProc,
457                                           WIN_PROC_32A )))
458     {
459         GlobalDeleteAtom( atom );
460         return 0;
461     }
462
463     dprintf_class( stddeb, "RegisterClassEx32A: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
464                    atom, (DWORD)wc->lpfnWndProc, hInstance,
465                    wc->hbrBackground, wc->style, wc->cbClsExtra,
466                    wc->cbWndExtra, classPtr );
467     
468     classPtr->hIcon         = (HICON16)wc->hIcon;
469     classPtr->hIconSm       = (HICON16)wc->hIconSm;
470     classPtr->hCursor       = (HCURSOR16)wc->hCursor;
471     classPtr->hbrBackground = (HBRUSH16)wc->hbrBackground;
472     CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
473     return atom;
474 }
475
476
477 /***********************************************************************
478  *           RegisterClassEx32W      (USER32.428)
479  */
480 ATOM RegisterClassEx32W( const WNDCLASSEX32W* wc )
481 {
482     ATOM atom;
483     CLASS *classPtr;
484
485     /* FIXME: this should not be necessary for Win32 */
486     HINSTANCE32 hInstance = (HINSTANCE32)GetExePtr( wc->hInstance );
487
488     if (!(atom = GlobalAddAtom32W( wc->lpszClassName ))) return 0;
489     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
490                                           wc->cbClsExtra, wc->cbWndExtra,
491                                           (WNDPROC16)wc->lpfnWndProc,
492                                           WIN_PROC_32W )))
493     {
494         GlobalDeleteAtom( atom );
495         return 0;
496     }
497
498     dprintf_class( stddeb, "RegisterClassEx32W: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
499                    atom, (DWORD)wc->lpfnWndProc, hInstance,
500                    wc->hbrBackground, wc->style, wc->cbClsExtra,
501                    wc->cbWndExtra, classPtr );
502     
503     classPtr->hIcon         = (HICON16)wc->hIcon;
504     classPtr->hIconSm       = (HICON16)wc->hIconSm;
505     classPtr->hCursor       = (HCURSOR16)wc->hCursor;
506     classPtr->hbrBackground = (HBRUSH16)wc->hbrBackground;
507     CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
508     return atom;
509 }
510
511
512 /***********************************************************************
513  *           UnregisterClass16    (USER.403)
514  */
515 BOOL16 UnregisterClass16( SEGPTR className, HINSTANCE16 hInstance )
516 {
517     CLASS *classPtr;
518     ATOM atom;
519
520     hInstance = GetExePtr( hInstance );
521     if (!(atom = GlobalFindAtom16( className ))) return FALSE;
522     if (!(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
523         (classPtr->hInstance != hInstance)) return FALSE;
524     return CLASS_FreeClass( classPtr );
525 }
526
527
528 /***********************************************************************
529  *           UnregisterClass32A    (USER32.562)
530  */
531 BOOL32 UnregisterClass32A( LPCSTR className, HINSTANCE32 hInstance )
532 {
533     CLASS *classPtr;
534     ATOM atom;
535
536     hInstance = GetExePtr( hInstance );  /* FIXME: not needed in Win32 */
537     if (!(atom = GlobalFindAtom32A( className ))) return FALSE;
538     if (!(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
539         (classPtr->hInstance != hInstance)) return FALSE;
540     return CLASS_FreeClass( classPtr );
541 }
542
543
544 /***********************************************************************
545  *           UnregisterClass32W    (USER32.563)
546  */
547 BOOL32 UnregisterClass32W( LPCWSTR className, HINSTANCE32 hInstance )
548 {
549     CLASS *classPtr;
550     ATOM atom;
551
552     hInstance = GetExePtr( hInstance );  /* FIXME: not needed in Win32 */
553     if (!(atom = GlobalFindAtom32W( className ))) return FALSE;
554     if (!(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
555         (classPtr->hInstance != hInstance)) return FALSE;
556     return CLASS_FreeClass( classPtr );
557 }
558
559
560 /***********************************************************************
561  *           GetClassWord    (USER.129) (USER32.218)
562  */
563 WORD GetClassWord( HWND32 hwnd, INT32 offset )
564 {
565     WND * wndPtr;
566     
567     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
568     if (offset >= 0)
569     {
570         if (offset <= wndPtr->class->cbClsExtra - sizeof(WORD))
571             return GET_WORD(((char *)wndPtr->class->wExtra) + offset);
572     }
573     else switch(offset)
574     {
575         case GCW_HBRBACKGROUND: return wndPtr->class->hbrBackground;
576         case GCW_HCURSOR:       return wndPtr->class->hCursor;
577         case GCW_HICON:         return wndPtr->class->hIcon;
578         case GCW_HICONSM:       return wndPtr->class->hIconSm;
579         case GCW_ATOM:          return wndPtr->class->atomName;
580         case GCW_STYLE:
581         case GCW_CBWNDEXTRA:
582         case GCW_CBCLSEXTRA:
583         case GCW_HMODULE:
584             return (WORD)GetClassLong32A( hwnd, offset );
585     }
586     fprintf(stderr, "Warning: invalid offset %d for GetClassWord()\n", offset);
587     return 0;
588 }
589
590
591 /***********************************************************************
592  *           GetClassLong16    (USER.131)
593  */
594 LONG GetClassLong16( HWND16 hwnd, INT16 offset )
595 {
596     WND *wndPtr;
597     LONG ret;
598
599     switch( offset )
600     {
601     case GCL_WNDPROC:
602         if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
603         return (LONG)WINPROC_GetProc( wndPtr->class->winproc, WIN_PROC_16 );
604     case GCL_MENUNAME:
605         ret = GetClassLong32A( hwnd, offset );
606         return (LONG)SEGPTR_GET( (void *)ret );
607     default:
608         return GetClassLong32A( hwnd, offset );
609     }
610 }
611
612
613 /***********************************************************************
614  *           GetClassLong32A    (USER32.214)
615  */
616 LONG GetClassLong32A( HWND32 hwnd, INT32 offset )
617 {
618     WND * wndPtr;
619     
620     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
621     if (offset >= 0)
622     {
623         if (offset <= wndPtr->class->cbClsExtra - sizeof(LONG))
624             return GET_DWORD(((char *)wndPtr->class->wExtra) + offset);
625     }
626     switch(offset)
627     {
628         case GCL_STYLE:      return (LONG)wndPtr->class->style;
629         case GCL_CBWNDEXTRA: return (LONG)wndPtr->class->cbWndExtra;
630         case GCL_CBCLSEXTRA: return (LONG)wndPtr->class->cbClsExtra;
631         case GCL_HMODULE:    return (LONG)wndPtr->class->hInstance;
632         case GCL_WNDPROC:
633             return (LONG)WINPROC_GetProc(wndPtr->class->winproc, WIN_PROC_32A);
634         case GCL_MENUNAME:
635             return (LONG)CLASS_GetMenuNameA( wndPtr->class );
636         case GCL_HBRBACKGROUND:
637         case GCL_HCURSOR:
638         case GCL_HICON:
639         case GCL_HICONSM:
640             return GetClassWord( hwnd, offset );
641     }
642     fprintf(stderr, "Warning: invalid offset %d for GetClassLong()\n", offset);
643     return 0;
644 }
645
646
647 /***********************************************************************
648  *           GetClassLong32W    (USER32.215)
649  */
650 LONG GetClassLong32W( HWND32 hwnd, INT32 offset )
651 {
652     WND * wndPtr;
653
654     switch(offset)
655     {
656     case GCL_WNDPROC:
657         if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
658         return (LONG)WINPROC_GetProc( wndPtr->class->winproc, WIN_PROC_32W );
659     case GCL_MENUNAME:
660         if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
661         return (LONG)CLASS_GetMenuNameW( wndPtr->class );
662     default:
663         return GetClassLong32A( hwnd, offset );
664     }
665 }
666
667
668 /***********************************************************************
669  *           SetClassWord    (USER.130) (USER32.468)
670  */
671 WORD SetClassWord( HWND32 hwnd, INT32 offset, WORD newval )
672 {
673     WND * wndPtr;
674     WORD retval = 0;
675     void *ptr;
676     
677     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
678     if (offset >= 0)
679     {
680         if (offset + sizeof(WORD) <= wndPtr->class->cbClsExtra)
681             ptr = ((char *)wndPtr->class->wExtra) + offset;
682         else
683         {
684             fprintf( stderr, "Warning: invalid offset %d for SetClassWord()\n",
685                      offset );
686             return 0;
687         }
688     }
689     else switch(offset)
690     {
691         case GCW_STYLE:
692         case GCW_CBWNDEXTRA:
693         case GCW_CBCLSEXTRA:
694         case GCW_HMODULE:
695             return (WORD)SetClassLong32A( hwnd, offset, (LONG)newval );
696         case GCW_HBRBACKGROUND: ptr = &wndPtr->class->hbrBackground; break;
697         case GCW_HCURSOR:       ptr = &wndPtr->class->hCursor; break;
698         case GCW_HICON:         ptr = &wndPtr->class->hIcon; break;
699         case GCW_HICONSM:       ptr = &wndPtr->class->hIconSm; break;
700         case GCW_ATOM:          ptr = &wndPtr->class->atomName; break;
701         default:
702             fprintf( stderr, "Warning: invalid offset %d for SetClassWord()\n",
703                      offset);
704             return 0;
705     }
706     retval = GET_WORD(ptr);
707     PUT_WORD( ptr, newval );
708     return retval;
709 }
710
711
712 /***********************************************************************
713  *           SetClassLong16    (USER.132)
714  */
715 LONG SetClassLong16( HWND16 hwnd, INT16 offset, LONG newval )
716 {
717     WND *wndPtr;
718     LONG retval;
719
720     switch(offset)
721     {
722     case GCL_WNDPROC:
723         if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
724         retval = (LONG)WINPROC_GetProc( wndPtr->class->winproc, WIN_PROC_16 );
725         WINPROC_SetProc( &wndPtr->class->winproc, (WNDPROC16)newval,
726                          WIN_PROC_16 );
727         return retval;
728     case GCL_MENUNAME:
729         return SetClassLong32A( hwnd, offset, (LONG)PTR_SEG_TO_LIN(newval) );
730     default:
731         return SetClassLong32A( hwnd, offset, newval );
732     }
733 }
734
735
736 /***********************************************************************
737  *           SetClassLong32A    (USER32.466)
738  */
739 LONG SetClassLong32A( HWND32 hwnd, INT32 offset, LONG newval )
740 {
741     WND * wndPtr;
742     LONG retval = 0;
743     void *ptr;
744     
745     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
746     if (offset >= 0)
747     {
748         if (offset + sizeof(LONG) <= wndPtr->class->cbClsExtra)
749             ptr = ((char *)wndPtr->class->wExtra) + offset;
750         else
751         {
752             fprintf( stderr, "Warning: invalid offset %d for SetClassLong()\n",
753                      offset );
754             return 0;
755         }
756     }
757     else switch(offset)
758     {
759         case GCL_MENUNAME:
760             CLASS_SetMenuNameA( wndPtr->class, (LPCSTR)newval );
761             return 0;  /* Old value is now meaningless anyway */
762         case GCL_WNDPROC:
763             retval = (LONG)WINPROC_GetProc( wndPtr->class->winproc,
764                                             WIN_PROC_32A );
765             WINPROC_SetProc( &wndPtr->class->winproc, (WNDPROC16)newval,
766                              WIN_PROC_32A );
767             return retval;
768         case GCL_HBRBACKGROUND:
769         case GCL_HCURSOR:
770         case GCL_HICON:
771         case GCL_HICONSM:
772             return SetClassWord( hwnd, offset, (WORD)newval );
773         case GCL_STYLE:      ptr = &wndPtr->class->style; break;
774         case GCL_CBWNDEXTRA: ptr = &wndPtr->class->cbWndExtra; break;
775         case GCL_CBCLSEXTRA: ptr = &wndPtr->class->cbClsExtra; break;
776         case GCL_HMODULE:    ptr = &wndPtr->class->hInstance; break;
777         default:
778             fprintf( stderr, "Warning: invalid offset %d for SetClassLong()\n",
779                      offset);
780             return 0;
781     }
782     retval = GET_DWORD(ptr);
783     PUT_DWORD( ptr, newval );
784     return retval;
785 }
786
787
788 /***********************************************************************
789  *           SetClassLong32W    (USER32.467)
790  */
791 LONG SetClassLong32W( HWND32 hwnd, INT32 offset, LONG newval )
792 {
793     WND *wndPtr;
794     LONG retval;
795
796     switch(offset)
797     {
798     case GCL_WNDPROC:
799         if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
800         retval = (LONG)WINPROC_GetProc( wndPtr->class->winproc, WIN_PROC_32W );
801         WINPROC_SetProc( &wndPtr->class->winproc, (WNDPROC16)newval,
802                          WIN_PROC_32W );
803         return retval;
804     case GCL_MENUNAME:
805         if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
806         CLASS_SetMenuNameW( wndPtr->class, (LPCWSTR)newval );
807         return 0;  /* Old value is now meaningless anyway */
808     default:
809         return SetClassLong32A( hwnd, offset, newval );
810     }
811 }
812
813
814 /***********************************************************************
815  *           GetClassName16      (USER.58)
816  */
817 INT16 GetClassName16( HWND16 hwnd, LPSTR buffer, INT16 count )
818 {
819     WND *wndPtr;
820     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
821     return GlobalGetAtomName16( wndPtr->class->atomName, buffer, count );
822 }
823
824
825 /***********************************************************************
826  *           GetClassName32A      (USER32.216)
827  */
828 INT32 GetClassName32A( HWND32 hwnd, LPSTR buffer, INT32 count )
829 {
830     WND *wndPtr;
831     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
832     return GlobalGetAtomName32A( wndPtr->class->atomName, buffer, count );
833 }
834
835
836 /***********************************************************************
837  *           GetClassName32W      (USER32.217)
838  */
839 INT32 GetClassName32W( HWND32 hwnd, LPWSTR buffer, INT32 count )
840 {
841     WND *wndPtr;
842     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
843     return GlobalGetAtomName32W( wndPtr->class->atomName, buffer, count );
844 }
845
846
847 /***********************************************************************
848  *           GetClassInfo16      (USER.404)
849  */
850 BOOL16 GetClassInfo16( HINSTANCE16 hInstance, SEGPTR name, WNDCLASS16 *wc )
851 {
852     ATOM atom;
853     CLASS *classPtr;
854
855     hInstance = GetExePtr( hInstance );
856     if (!(atom = GlobalFindAtom16( name )) ||
857         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
858         (hInstance != classPtr->hInstance)) return FALSE;
859     wc->style         = (UINT16)classPtr->style;
860     wc->lpfnWndProc   = WINPROC_GetProc( classPtr->winproc, WIN_PROC_16 );
861     wc->cbClsExtra    = (INT16)classPtr->cbClsExtra;
862     wc->cbWndExtra    = (INT16)classPtr->cbWndExtra;
863     wc->hInstance     = (HINSTANCE16)classPtr->hInstance;
864     wc->hIcon         = classPtr->hIcon;
865     wc->hCursor       = classPtr->hCursor;
866     wc->hbrBackground = classPtr->hbrBackground;
867     wc->lpszClassName = (SEGPTR)0;
868     wc->lpszMenuName  = (SEGPTR)CLASS_GetMenuNameA( classPtr );
869     if (HIWORD(wc->lpszMenuName))  /* Make it a SEGPTR */
870         wc->lpszMenuName = SEGPTR_GET( (LPSTR)wc->lpszMenuName );
871     return TRUE;
872 }
873
874
875 /***********************************************************************
876  *           GetClassInfo32A      (USER32.210)
877  */
878 BOOL32 GetClassInfo32A( HINSTANCE32 hInstance, LPCSTR name, WNDCLASS32A *wc )
879 {
880     ATOM atom;
881     CLASS *classPtr;
882
883     hInstance = GetExePtr( hInstance );  /* FIXME: not needed in Win32 */
884     if (!(atom = GlobalFindAtom32A( name )) ||
885         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
886         (hInstance != classPtr->hInstance)) return FALSE;
887     wc->style         = classPtr->style;
888     wc->lpfnWndProc   = (WNDPROC32)WINPROC_GetProc( classPtr->winproc,
889                                                     WIN_PROC_32A );
890     wc->cbClsExtra    = classPtr->cbClsExtra;
891     wc->cbWndExtra    = classPtr->cbWndExtra;
892     wc->hInstance     = classPtr->hInstance;
893     wc->hIcon         = (HICON32)classPtr->hIcon;
894     wc->hCursor       = (HCURSOR32)classPtr->hCursor;
895     wc->hbrBackground = (HBRUSH32)classPtr->hbrBackground;
896     wc->lpszMenuName  = CLASS_GetMenuNameA( classPtr );
897     wc->lpszClassName = NULL;
898     return TRUE;
899 }
900
901
902 /***********************************************************************
903  *           GetClassInfo32W      (USER32.213)
904  */
905 BOOL32 GetClassInfo32W( HINSTANCE32 hInstance, LPCWSTR name, WNDCLASS32W *wc )
906 {
907     ATOM atom;
908     CLASS *classPtr;
909
910     hInstance = GetExePtr( hInstance );  /* FIXME: not needed in Win32 */
911     if (!(atom = GlobalFindAtom32W( name )) ||
912         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
913         (hInstance != classPtr->hInstance)) return FALSE;
914     wc->style         = classPtr->style;
915     wc->lpfnWndProc   = (WNDPROC32)WINPROC_GetProc( classPtr->winproc,
916                                                     WIN_PROC_32W );
917     wc->cbClsExtra    = classPtr->cbClsExtra;
918     wc->cbWndExtra    = classPtr->cbWndExtra;
919     wc->hInstance     = classPtr->hInstance;
920     wc->hIcon         = (HICON32)classPtr->hIcon;
921     wc->hCursor       = (HCURSOR32)classPtr->hCursor;
922     wc->hbrBackground = (HBRUSH32)classPtr->hbrBackground;
923     wc->lpszMenuName  = CLASS_GetMenuNameW( classPtr );
924     wc->lpszClassName = NULL;
925     return TRUE;
926 }
927
928
929 /***********************************************************************
930  *           GetClassInfoEx16      (USER.398)
931  *
932  * FIXME: this is just a guess, I have no idea if GetClassInfoEx() is the
933  * same in Win16 as in Win32. --AJ
934  */
935 BOOL16 GetClassInfoEx16( HINSTANCE16 hInstance, SEGPTR name, WNDCLASSEX16 *wc )
936 {
937     ATOM atom;
938     CLASS *classPtr;
939
940     hInstance = GetExePtr( hInstance );
941     if (!(atom = GlobalFindAtom16( name )) ||
942         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
943         (hInstance != classPtr->hInstance)) return FALSE;
944     wc->style         = classPtr->style;
945     wc->lpfnWndProc   = WINPROC_GetProc( classPtr->winproc, WIN_PROC_16 );
946     wc->cbClsExtra    = (INT16)classPtr->cbClsExtra;
947     wc->cbWndExtra    = (INT16)classPtr->cbWndExtra;
948     wc->hInstance     = (HINSTANCE16)classPtr->hInstance;
949     wc->hIcon         = classPtr->hIcon;
950     wc->hIconSm       = classPtr->hIconSm;
951     wc->hCursor       = classPtr->hCursor;
952     wc->hbrBackground = classPtr->hbrBackground;
953     wc->lpszClassName = (SEGPTR)0;
954     wc->lpszMenuName  = (SEGPTR)CLASS_GetMenuNameA( classPtr );
955     if (HIWORD(wc->lpszMenuName))  /* Make it a SEGPTR */
956         wc->lpszMenuName = SEGPTR_GET( (LPSTR)wc->lpszMenuName );
957     return TRUE;
958 }
959
960
961 /***********************************************************************
962  *           GetClassInfoEx32A      (USER32.211)
963  */
964 BOOL32 GetClassInfoEx32A( HINSTANCE32 hInstance, LPCSTR name,
965                           WNDCLASSEX32A *wc )
966 {
967     ATOM atom;
968     CLASS *classPtr;
969
970     hInstance = GetExePtr( hInstance );  /* FIXME: not needed in Win32 */
971     if (!(atom = GlobalFindAtom32A( name )) ||
972         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
973         (hInstance != classPtr->hInstance)) return FALSE;
974     wc->style         = classPtr->style;
975     wc->lpfnWndProc   = (WNDPROC32)WINPROC_GetProc( classPtr->winproc,
976                                                     WIN_PROC_32A );
977     wc->cbClsExtra    = classPtr->cbClsExtra;
978     wc->cbWndExtra    = classPtr->cbWndExtra;
979     wc->hInstance     = classPtr->hInstance;
980     wc->hIcon         = (HICON32)classPtr->hIcon;
981     wc->hIconSm       = (HICON32)classPtr->hIconSm;
982     wc->hCursor       = (HCURSOR32)classPtr->hCursor;
983     wc->hbrBackground = (HBRUSH32)classPtr->hbrBackground;
984     wc->lpszMenuName  = CLASS_GetMenuNameA( classPtr );
985     wc->lpszClassName = NULL;
986     return TRUE;
987 }
988
989
990 /***********************************************************************
991  *           GetClassInfoEx32W      (USER32.212)
992  */
993 BOOL32 GetClassInfoEx32W( HINSTANCE32 hInstance, LPCWSTR name,
994                           WNDCLASSEX32W *wc )
995 {
996     ATOM atom;
997     CLASS *classPtr;
998
999     hInstance = GetExePtr( hInstance );  /* FIXME: not needed in Win32 */
1000     if (!(atom = GlobalFindAtom32W( name )) ||
1001         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
1002         (hInstance != classPtr->hInstance)) return FALSE;
1003     wc->style         = classPtr->style;
1004     wc->lpfnWndProc   = (WNDPROC32)WINPROC_GetProc( classPtr->winproc,
1005                                                     WIN_PROC_32W );
1006     wc->cbClsExtra    = classPtr->cbClsExtra;
1007     wc->cbWndExtra    = classPtr->cbWndExtra;
1008     wc->hInstance     = classPtr->hInstance;
1009     wc->hIcon         = (HICON32)classPtr->hIcon;
1010     wc->hIconSm       = (HICON32)classPtr->hIconSm;
1011     wc->hCursor       = (HCURSOR32)classPtr->hCursor;
1012     wc->hbrBackground = (HBRUSH32)classPtr->hbrBackground;
1013     wc->lpszMenuName  = CLASS_GetMenuNameW( classPtr );
1014     wc->lpszClassName = NULL;
1015     return TRUE;
1016 }
1017
1018
1019 /***********************************************************************
1020  *           ClassFirst      (TOOLHELP.69)
1021  */
1022 BOOL16 ClassFirst( CLASSENTRY *pClassEntry )
1023 {
1024     pClassEntry->wNext = 1;
1025     return ClassNext( pClassEntry );
1026 }
1027
1028
1029 /***********************************************************************
1030  *           ClassNext      (TOOLHELP.70)
1031  */
1032 BOOL16 ClassNext( CLASSENTRY *pClassEntry )
1033 {
1034     int i;
1035     CLASS *class = firstClass;
1036
1037     if (!pClassEntry->wNext) return FALSE;
1038     for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1039     if (!class)
1040     {
1041         pClassEntry->wNext = 0;
1042         return FALSE;
1043     }
1044     pClassEntry->hInst = class->hInstance;
1045     pClassEntry->wNext++;
1046     GlobalGetAtomName32A( class->atomName, pClassEntry->szClassName,
1047                           sizeof(pClassEntry->szClassName) );
1048     return TRUE;
1049 }