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