Release 960705
[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       /* First search task-specific classes */
219
220     for (class = firstClass; (class); class = class->next)
221     {
222         if (class->style & CS_GLOBALCLASS) continue;
223         if ((class->atomName == atom) && 
224             ((hinstance==(HINSTANCE16)0xffff) ||
225              (hinstance == class->hInstance))) return class;
226     }
227     
228       /* Then search global classes */
229
230     for (class = firstClass; (class); class = class->next)
231     {
232         if (!(class->style & CS_GLOBALCLASS)) continue;
233         if (class->atomName == atom) return class;
234     }
235
236     return 0;
237 }
238
239
240 /***********************************************************************
241  *           CLASS_FindClassByName
242  *
243  * Return a pointer to the class.
244  */
245 CLASS *CLASS_FindClassByName( SEGPTR name, HINSTANCE hinstance )
246 {
247     ATOM atom;
248
249     if (!(atom = GlobalFindAtom16( name ))) return 0;
250     return CLASS_FindClassByAtom( atom, hinstance );
251 }
252
253
254 /***********************************************************************
255  *           CLASS_RegisterClass
256  *
257  * The real RegisterClass() functionality.
258  */
259 static CLASS *CLASS_RegisterClass( ATOM atom, HINSTANCE32 hInstance,
260                                    DWORD style, INT32 classExtra,
261                                    INT32 winExtra, WNDPROC16 wndProc,
262                                    WINDOWPROCTYPE wndProcType )
263 {
264     CLASS *classPtr;
265
266     /* Check if a class with this name already exists */
267
268     classPtr = CLASS_FindClassByAtom( atom, hInstance );
269     if (classPtr)
270     {
271         /* Class can be created only if it is local and */
272         /* if the class with the same name is global.   */
273
274         if (style & CS_GLOBALCLASS) return NULL;
275         if (!(classPtr->style & CS_GLOBALCLASS)) return NULL;
276     }
277
278     /* Fix the extra bytes value */
279
280     if (classExtra < 0) classExtra = 0;
281     else if (classExtra > 40)  /* Extra bytes are limited to 40 in Win32 */
282         fprintf(stderr, "Warning: class extra bytes %d is > 40\n", classExtra);
283     if (winExtra < 0) winExtra = 0;
284     else if (winExtra > 40)    /* Extra bytes are limited to 40 in Win32 */
285         fprintf( stderr, "Warning: win extra bytes %d is > 40\n", winExtra );
286
287     /* Create the class */
288
289     classPtr = (CLASS *)HeapAlloc( SystemHeap, 0, sizeof(CLASS) +
290                                        classExtra - sizeof(classPtr->wExtra) );
291     if (!classPtr) return NULL;
292     classPtr->next        = firstClass;
293     classPtr->magic       = CLASS_MAGIC;
294     classPtr->cWindows    = 0;  
295     classPtr->style       = style;
296     classPtr->winproc     = (HWINDOWPROC)0;
297     classPtr->cbWndExtra  = winExtra;
298     classPtr->cbClsExtra  = classExtra;
299     classPtr->hInstance   = hInstance;
300     classPtr->atomName    = atom;
301     classPtr->menuNameA   = 0;
302     classPtr->menuNameW   = 0;
303     classPtr->hdce        = (style&CS_CLASSDC) ? DCE_AllocDCE(0, DCE_CLASS_DC): 0;
304     WINPROC_SetProc( &classPtr->winproc, wndProc, wndProcType );
305
306     /* Other values must be set by caller */
307
308     if (classExtra) memset( classPtr->wExtra, 0, classExtra );
309     firstClass = classPtr;
310     return classPtr;
311 }
312
313
314 /***********************************************************************
315  *           RegisterClass16    (USER.57)
316  */
317 ATOM RegisterClass16( const WNDCLASS16 *wc )
318 {
319     ATOM atom;
320     CLASS *classPtr;
321
322     HINSTANCE32 hInstance = (HINSTANCE32)GetExePtr( wc->hInstance );
323
324     if (!(atom = GlobalAddAtom16( wc->lpszClassName ))) return 0;
325     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
326                                           wc->cbClsExtra, wc->cbWndExtra,
327                                           wc->lpfnWndProc, WIN_PROC_16 )))
328     {
329         GlobalDeleteAtom( atom );
330         return 0;
331     }
332
333     dprintf_class( stddeb, "RegisterClass16: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
334                    atom, (DWORD)wc->lpfnWndProc, hInstance,
335                    wc->hbrBackground, wc->style, wc->cbClsExtra,
336                    wc->cbWndExtra, classPtr );
337
338     classPtr->hIcon         = wc->hIcon;
339     classPtr->hIconSm       = 0;
340     classPtr->hCursor       = wc->hCursor;
341     classPtr->hbrBackground = wc->hbrBackground;
342
343     CLASS_SetMenuNameA( classPtr, HIWORD(wc->lpszMenuName) ?
344                  PTR_SEG_TO_LIN(wc->lpszMenuName) : (LPCSTR)wc->lpszMenuName );
345     return atom;
346 }
347
348
349 /***********************************************************************
350  *           RegisterClass32A      (USER32.426)
351  */
352 ATOM RegisterClass32A( const WNDCLASS32A* wc )
353 {
354     ATOM atom;
355     CLASS *classPtr;
356
357     /* FIXME: this should not be necessary for Win32 */
358     HINSTANCE32 hInstance = (HINSTANCE32)GetExePtr( wc->hInstance );
359
360     if (!(atom = GlobalAddAtom32A( wc->lpszClassName ))) return 0;
361     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
362                                           wc->cbClsExtra, wc->cbWndExtra,
363                                           (WNDPROC16)wc->lpfnWndProc,
364                                           WIN_PROC_32A )))
365     {
366         GlobalDeleteAtom( atom );
367         return 0;
368     }
369
370     dprintf_class( stddeb, "RegisterClass32A: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
371                    atom, (DWORD)wc->lpfnWndProc, hInstance,
372                    wc->hbrBackground, wc->style, wc->cbClsExtra,
373                    wc->cbWndExtra, classPtr );
374     
375     classPtr->hIcon         = (HICON16)wc->hIcon;
376     classPtr->hIconSm       = 0;
377     classPtr->hCursor       = (HCURSOR16)wc->hCursor;
378     classPtr->hbrBackground = (HBRUSH16)wc->hbrBackground;
379     CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
380     return atom;
381 }
382
383
384 /***********************************************************************
385  *           RegisterClass32W      (USER32.429)
386  */
387 ATOM RegisterClass32W( const WNDCLASS32W* wc )
388 {
389     ATOM atom;
390     CLASS *classPtr;
391
392     /* FIXME: this should not be necessary for Win32 */
393     HINSTANCE32 hInstance = (HINSTANCE32)GetExePtr( wc->hInstance );
394
395     if (!(atom = GlobalAddAtom32W( wc->lpszClassName ))) return 0;
396     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
397                                           wc->cbClsExtra, wc->cbWndExtra,
398                                           (WNDPROC16)wc->lpfnWndProc,
399                                           WIN_PROC_32W )))
400     {
401         GlobalDeleteAtom( atom );
402         return 0;
403     }
404
405     dprintf_class( stddeb, "RegisterClass32W: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
406                    atom, (DWORD)wc->lpfnWndProc, hInstance,
407                    wc->hbrBackground, wc->style, wc->cbClsExtra,
408                    wc->cbWndExtra, classPtr );
409     
410     classPtr->hIcon         = (HICON16)wc->hIcon;
411     classPtr->hIconSm       = 0;
412     classPtr->hCursor       = (HCURSOR16)wc->hCursor;
413     classPtr->hbrBackground = (HBRUSH16)wc->hbrBackground;
414     CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
415     return atom;
416 }
417
418
419 /***********************************************************************
420  *           RegisterClassEx16    (USER.397)
421  */
422 ATOM RegisterClassEx16( const WNDCLASSEX16 *wc )
423 {
424     ATOM atom;
425     CLASS *classPtr;
426
427     HINSTANCE32 hInstance = (HINSTANCE32)GetExePtr( wc->hInstance );
428
429     if (!(atom = GlobalAddAtom16( wc->lpszClassName ))) return 0;
430     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
431                                           wc->cbClsExtra, wc->cbWndExtra,
432                                           wc->lpfnWndProc, WIN_PROC_16 )))
433     {
434         GlobalDeleteAtom( atom );
435         return 0;
436     }
437
438     dprintf_class( stddeb, "RegisterClassEx16: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
439                    atom, (DWORD)wc->lpfnWndProc, hInstance,
440                    wc->hbrBackground, wc->style, wc->cbClsExtra,
441                    wc->cbWndExtra, classPtr );
442     
443     classPtr->hIcon         = wc->hIcon;
444     classPtr->hIconSm       = wc->hIconSm;
445     classPtr->hCursor       = wc->hCursor;
446     classPtr->hbrBackground = wc->hbrBackground;
447
448     CLASS_SetMenuNameA( classPtr, HIWORD(wc->lpszMenuName) ?
449                  PTR_SEG_TO_LIN(wc->lpszMenuName) : (LPCSTR)wc->lpszMenuName );
450     return atom;
451 }
452
453
454 /***********************************************************************
455  *           RegisterClassEx32A      (USER32.427)
456  */
457 ATOM RegisterClassEx32A( const WNDCLASSEX32A* wc )
458 {
459     ATOM atom;
460     CLASS *classPtr;
461
462     /* FIXME: this should not be necessary for Win32 */
463     HINSTANCE32 hInstance = (HINSTANCE32)GetExePtr( wc->hInstance );
464
465     if (!(atom = GlobalAddAtom32A( wc->lpszClassName ))) return 0;
466     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
467                                           wc->cbClsExtra, wc->cbWndExtra,
468                                           (WNDPROC16)wc->lpfnWndProc,
469                                           WIN_PROC_32A )))
470     {
471         GlobalDeleteAtom( atom );
472         return 0;
473     }
474
475     dprintf_class( stddeb, "RegisterClassEx32A: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
476                    atom, (DWORD)wc->lpfnWndProc, hInstance,
477                    wc->hbrBackground, wc->style, wc->cbClsExtra,
478                    wc->cbWndExtra, classPtr );
479     
480     classPtr->hIcon         = (HICON16)wc->hIcon;
481     classPtr->hIconSm       = (HICON16)wc->hIconSm;
482     classPtr->hCursor       = (HCURSOR16)wc->hCursor;
483     classPtr->hbrBackground = (HBRUSH16)wc->hbrBackground;
484     CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
485     return atom;
486 }
487
488
489 /***********************************************************************
490  *           RegisterClassEx32W      (USER32.428)
491  */
492 ATOM RegisterClassEx32W( const WNDCLASSEX32W* wc )
493 {
494     ATOM atom;
495     CLASS *classPtr;
496
497     /* FIXME: this should not be necessary for Win32 */
498     HINSTANCE32 hInstance = (HINSTANCE32)GetExePtr( wc->hInstance );
499
500     if (!(atom = GlobalAddAtom32W( wc->lpszClassName ))) return 0;
501     if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
502                                           wc->cbClsExtra, wc->cbWndExtra,
503                                           (WNDPROC16)wc->lpfnWndProc,
504                                           WIN_PROC_32W )))
505     {
506         GlobalDeleteAtom( atom );
507         return 0;
508     }
509
510     dprintf_class( stddeb, "RegisterClassEx32W: atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
511                    atom, (DWORD)wc->lpfnWndProc, hInstance,
512                    wc->hbrBackground, wc->style, wc->cbClsExtra,
513                    wc->cbWndExtra, classPtr );
514     
515     classPtr->hIcon         = (HICON16)wc->hIcon;
516     classPtr->hIconSm       = (HICON16)wc->hIconSm;
517     classPtr->hCursor       = (HCURSOR16)wc->hCursor;
518     classPtr->hbrBackground = (HBRUSH16)wc->hbrBackground;
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( HWND32 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( HWND16 hwnd, INT16 offset )
607 {
608     WND *wndPtr;
609     LONG ret;
610
611     switch( offset )
612     {
613     case GCL_WNDPROC:
614         if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
615         return (LONG)WINPROC_GetProc( wndPtr->class->winproc, WIN_PROC_16 );
616     case GCL_MENUNAME:
617         ret = GetClassLong32A( hwnd, offset );
618         return (LONG)SEGPTR_GET( (void *)ret );
619     default:
620         return GetClassLong32A( hwnd, offset );
621     }
622 }
623
624
625 /***********************************************************************
626  *           GetClassLong32A    (USER32.214)
627  */
628 LONG GetClassLong32A( HWND32 hwnd, INT32 offset )
629 {
630     WND * wndPtr;
631     
632     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
633     if (offset >= 0)
634     {
635         if (offset <= wndPtr->class->cbClsExtra - sizeof(LONG))
636             return GET_DWORD(((char *)wndPtr->class->wExtra) + offset);
637     }
638     switch(offset)
639     {
640         case GCL_STYLE:      return (LONG)wndPtr->class->style;
641         case GCL_CBWNDEXTRA: return (LONG)wndPtr->class->cbWndExtra;
642         case GCL_CBCLSEXTRA: return (LONG)wndPtr->class->cbClsExtra;
643         case GCL_HMODULE:    return (LONG)wndPtr->class->hInstance;
644         case GCL_WNDPROC:
645             return (LONG)WINPROC_GetProc(wndPtr->class->winproc, WIN_PROC_32A);
646         case GCL_MENUNAME:
647             return (LONG)CLASS_GetMenuNameA( wndPtr->class );
648         case GCL_HBRBACKGROUND:
649         case GCL_HCURSOR:
650         case GCL_HICON:
651         case GCL_HICONSM:
652             return GetClassWord( hwnd, offset );
653     }
654     fprintf(stderr, "Warning: invalid offset %d for GetClassLong()\n", offset);
655     return 0;
656 }
657
658
659 /***********************************************************************
660  *           GetClassLong32W    (USER32.215)
661  */
662 LONG GetClassLong32W( HWND32 hwnd, INT32 offset )
663 {
664     WND * wndPtr;
665
666     switch(offset)
667     {
668     case GCL_WNDPROC:
669         if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
670         return (LONG)WINPROC_GetProc( wndPtr->class->winproc, WIN_PROC_32W );
671     case GCL_MENUNAME:
672         if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
673         return (LONG)CLASS_GetMenuNameW( wndPtr->class );
674     default:
675         return GetClassLong32A( hwnd, offset );
676     }
677 }
678
679
680 /***********************************************************************
681  *           SetClassWord    (USER.130) (USER32.468)
682  */
683 WORD SetClassWord( HWND32 hwnd, INT32 offset, WORD newval )
684 {
685     WND * wndPtr;
686     WORD retval = 0;
687     void *ptr;
688     
689     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
690     if (offset >= 0)
691     {
692         if (offset + sizeof(WORD) <= wndPtr->class->cbClsExtra)
693             ptr = ((char *)wndPtr->class->wExtra) + offset;
694         else
695         {
696             fprintf( stderr, "Warning: invalid offset %d for SetClassWord()\n",
697                      offset );
698             return 0;
699         }
700     }
701     else switch(offset)
702     {
703         case GCW_STYLE:
704         case GCW_CBWNDEXTRA:
705         case GCW_CBCLSEXTRA:
706         case GCW_HMODULE:
707             return (WORD)SetClassLong32A( hwnd, offset, (LONG)newval );
708         case GCW_HBRBACKGROUND: ptr = &wndPtr->class->hbrBackground; break;
709         case GCW_HCURSOR:       ptr = &wndPtr->class->hCursor; break;
710         case GCW_HICON:         ptr = &wndPtr->class->hIcon; break;
711         case GCW_HICONSM:       ptr = &wndPtr->class->hIconSm; break;
712         case GCW_ATOM:          ptr = &wndPtr->class->atomName; break;
713         default:
714             fprintf( stderr, "Warning: invalid offset %d for SetClassWord()\n",
715                      offset);
716             return 0;
717     }
718     retval = GET_WORD(ptr);
719     PUT_WORD( ptr, newval );
720     return retval;
721 }
722
723
724 /***********************************************************************
725  *           SetClassLong16    (USER.132)
726  */
727 LONG SetClassLong16( HWND16 hwnd, INT16 offset, LONG newval )
728 {
729     WND *wndPtr;
730     LONG retval;
731
732     switch(offset)
733     {
734     case GCL_WNDPROC:
735         if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
736         retval = (LONG)WINPROC_GetProc( wndPtr->class->winproc, WIN_PROC_16 );
737         WINPROC_SetProc( &wndPtr->class->winproc, (WNDPROC16)newval,
738                          WIN_PROC_16 );
739         return retval;
740     case GCL_MENUNAME:
741         return SetClassLong32A( hwnd, offset, (LONG)PTR_SEG_TO_LIN(newval) );
742     default:
743         return SetClassLong32A( hwnd, offset, newval );
744     }
745 }
746
747
748 /***********************************************************************
749  *           SetClassLong32A    (USER32.466)
750  */
751 LONG SetClassLong32A( HWND32 hwnd, INT32 offset, LONG newval )
752 {
753     WND * wndPtr;
754     LONG retval = 0;
755     void *ptr;
756     
757     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
758     if (offset >= 0)
759     {
760         if (offset + sizeof(LONG) <= wndPtr->class->cbClsExtra)
761             ptr = ((char *)wndPtr->class->wExtra) + offset;
762         else
763         {
764             fprintf( stderr, "Warning: invalid offset %d for SetClassLong()\n",
765                      offset );
766             return 0;
767         }
768     }
769     else switch(offset)
770     {
771         case GCL_MENUNAME:
772             CLASS_SetMenuNameA( wndPtr->class, (LPCSTR)newval );
773             return 0;  /* Old value is now meaningless anyway */
774         case GCL_WNDPROC:
775             retval = (LONG)WINPROC_GetProc( wndPtr->class->winproc,
776                                             WIN_PROC_32A );
777             WINPROC_SetProc( &wndPtr->class->winproc, (WNDPROC16)newval,
778                              WIN_PROC_32A );
779             return retval;
780         case GCL_HBRBACKGROUND:
781         case GCL_HCURSOR:
782         case GCL_HICON:
783         case GCL_HICONSM:
784             return SetClassWord( hwnd, offset, (WORD)newval );
785         case GCL_STYLE:      ptr = &wndPtr->class->style; break;
786         case GCL_CBWNDEXTRA: ptr = &wndPtr->class->cbWndExtra; break;
787         case GCL_CBCLSEXTRA: ptr = &wndPtr->class->cbClsExtra; break;
788         case GCL_HMODULE:    ptr = &wndPtr->class->hInstance; break;
789         default:
790             fprintf( stderr, "Warning: invalid offset %d for SetClassLong()\n",
791                      offset);
792             return 0;
793     }
794     retval = GET_DWORD(ptr);
795     PUT_DWORD( ptr, newval );
796     return retval;
797 }
798
799
800 /***********************************************************************
801  *           SetClassLong32W    (USER32.467)
802  */
803 LONG SetClassLong32W( HWND32 hwnd, INT32 offset, LONG newval )
804 {
805     WND *wndPtr;
806     LONG retval;
807
808     switch(offset)
809     {
810     case GCL_WNDPROC:
811         if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
812         retval = (LONG)WINPROC_GetProc( wndPtr->class->winproc, WIN_PROC_32W );
813         WINPROC_SetProc( &wndPtr->class->winproc, (WNDPROC16)newval,
814                          WIN_PROC_32W );
815         return retval;
816     case GCL_MENUNAME:
817         if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
818         CLASS_SetMenuNameW( wndPtr->class, (LPCWSTR)newval );
819         return 0;  /* Old value is now meaningless anyway */
820     default:
821         return SetClassLong32A( hwnd, offset, newval );
822     }
823 }
824
825
826 /***********************************************************************
827  *           GetClassName16      (USER.58)
828  */
829 INT16 GetClassName16( HWND16 hwnd, LPSTR buffer, INT16 count )
830 {
831     WND *wndPtr;
832     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
833     return GlobalGetAtomName16( wndPtr->class->atomName, buffer, count );
834 }
835
836
837 /***********************************************************************
838  *           GetClassName32A      (USER32.216)
839  */
840 INT32 GetClassName32A( HWND32 hwnd, LPSTR buffer, INT32 count )
841 {
842     WND *wndPtr;
843     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
844     return GlobalGetAtomName32A( wndPtr->class->atomName, buffer, count );
845 }
846
847
848 /***********************************************************************
849  *           GetClassName32W      (USER32.217)
850  */
851 INT32 GetClassName32W( HWND32 hwnd, LPWSTR buffer, INT32 count )
852 {
853     WND *wndPtr;
854     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
855     return GlobalGetAtomName32W( wndPtr->class->atomName, buffer, count );
856 }
857
858
859 /***********************************************************************
860  *           GetClassInfo16      (USER.404)
861  */
862 BOOL16 GetClassInfo16( HINSTANCE16 hInstance, SEGPTR name, WNDCLASS16 *wc )
863 {
864     ATOM atom;
865     CLASS *classPtr;
866
867     hInstance = GetExePtr( hInstance );
868     if (!(atom = GlobalFindAtom16( name )) ||
869         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
870         (hInstance != classPtr->hInstance)) return FALSE;
871     wc->style         = (UINT16)classPtr->style;
872     wc->lpfnWndProc   = WINPROC_GetProc( classPtr->winproc, WIN_PROC_16 );
873     wc->cbClsExtra    = (INT16)classPtr->cbClsExtra;
874     wc->cbWndExtra    = (INT16)classPtr->cbWndExtra;
875     wc->hInstance     = (HINSTANCE16)classPtr->hInstance;
876     wc->hIcon         = classPtr->hIcon;
877     wc->hCursor       = classPtr->hCursor;
878     wc->hbrBackground = classPtr->hbrBackground;
879     wc->lpszClassName = (SEGPTR)0;
880     wc->lpszMenuName  = (SEGPTR)CLASS_GetMenuNameA( classPtr );
881     if (HIWORD(wc->lpszMenuName))  /* Make it a SEGPTR */
882         wc->lpszMenuName = SEGPTR_GET( (LPSTR)wc->lpszMenuName );
883     return TRUE;
884 }
885
886
887 /***********************************************************************
888  *           GetClassInfo32A      (USER32.210)
889  */
890 BOOL32 GetClassInfo32A( HINSTANCE32 hInstance, LPCSTR name, WNDCLASS32A *wc )
891 {
892     ATOM atom;
893     CLASS *classPtr;
894
895     hInstance = GetExePtr( hInstance );  /* FIXME: not needed in Win32 */
896     if (!(atom = GlobalFindAtom32A( name )) ||
897         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
898         (hInstance != classPtr->hInstance)) return FALSE;
899     wc->style         = classPtr->style;
900     wc->lpfnWndProc   = (WNDPROC32)WINPROC_GetProc( classPtr->winproc,
901                                                     WIN_PROC_32A );
902     wc->cbClsExtra    = classPtr->cbClsExtra;
903     wc->cbWndExtra    = classPtr->cbWndExtra;
904     wc->hInstance     = classPtr->hInstance;
905     wc->hIcon         = (HICON32)classPtr->hIcon;
906     wc->hCursor       = (HCURSOR32)classPtr->hCursor;
907     wc->hbrBackground = (HBRUSH32)classPtr->hbrBackground;
908     wc->lpszMenuName  = CLASS_GetMenuNameA( classPtr );
909     wc->lpszClassName = NULL;
910     return TRUE;
911 }
912
913
914 /***********************************************************************
915  *           GetClassInfo32W      (USER32.213)
916  */
917 BOOL32 GetClassInfo32W( HINSTANCE32 hInstance, LPCWSTR name, WNDCLASS32W *wc )
918 {
919     ATOM atom;
920     CLASS *classPtr;
921
922     hInstance = GetExePtr( hInstance );  /* FIXME: not needed in Win32 */
923     if (!(atom = GlobalFindAtom32W( name )) ||
924         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
925         (hInstance != classPtr->hInstance)) return FALSE;
926     wc->style         = classPtr->style;
927     wc->lpfnWndProc   = (WNDPROC32)WINPROC_GetProc( classPtr->winproc,
928                                                     WIN_PROC_32W );
929     wc->cbClsExtra    = classPtr->cbClsExtra;
930     wc->cbWndExtra    = classPtr->cbWndExtra;
931     wc->hInstance     = classPtr->hInstance;
932     wc->hIcon         = (HICON32)classPtr->hIcon;
933     wc->hCursor       = (HCURSOR32)classPtr->hCursor;
934     wc->hbrBackground = (HBRUSH32)classPtr->hbrBackground;
935     wc->lpszMenuName  = CLASS_GetMenuNameW( classPtr );
936     wc->lpszClassName = NULL;
937     return TRUE;
938 }
939
940
941 /***********************************************************************
942  *           GetClassInfoEx16      (USER.398)
943  *
944  * FIXME: this is just a guess, I have no idea if GetClassInfoEx() is the
945  * same in Win16 as in Win32. --AJ
946  */
947 BOOL16 GetClassInfoEx16( HINSTANCE16 hInstance, SEGPTR name, WNDCLASSEX16 *wc )
948 {
949     ATOM atom;
950     CLASS *classPtr;
951
952     hInstance = GetExePtr( hInstance );
953     if (!(atom = GlobalFindAtom16( name )) ||
954         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
955         (hInstance != classPtr->hInstance)) return FALSE;
956     wc->style         = classPtr->style;
957     wc->lpfnWndProc   = WINPROC_GetProc( classPtr->winproc, WIN_PROC_16 );
958     wc->cbClsExtra    = (INT16)classPtr->cbClsExtra;
959     wc->cbWndExtra    = (INT16)classPtr->cbWndExtra;
960     wc->hInstance     = (HINSTANCE16)classPtr->hInstance;
961     wc->hIcon         = classPtr->hIcon;
962     wc->hIconSm       = classPtr->hIconSm;
963     wc->hCursor       = classPtr->hCursor;
964     wc->hbrBackground = classPtr->hbrBackground;
965     wc->lpszClassName = (SEGPTR)0;
966     wc->lpszMenuName  = (SEGPTR)CLASS_GetMenuNameA( classPtr );
967     if (HIWORD(wc->lpszMenuName))  /* Make it a SEGPTR */
968         wc->lpszMenuName = SEGPTR_GET( (LPSTR)wc->lpszMenuName );
969     return TRUE;
970 }
971
972
973 /***********************************************************************
974  *           GetClassInfoEx32A      (USER32.211)
975  */
976 BOOL32 GetClassInfoEx32A( HINSTANCE32 hInstance, LPCSTR name,
977                           WNDCLASSEX32A *wc )
978 {
979     ATOM atom;
980     CLASS *classPtr;
981
982     hInstance = GetExePtr( hInstance );  /* FIXME: not needed in Win32 */
983     if (!(atom = GlobalFindAtom32A( name )) ||
984         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
985         (hInstance != classPtr->hInstance)) return FALSE;
986     wc->style         = classPtr->style;
987     wc->lpfnWndProc   = (WNDPROC32)WINPROC_GetProc( classPtr->winproc,
988                                                     WIN_PROC_32A );
989     wc->cbClsExtra    = classPtr->cbClsExtra;
990     wc->cbWndExtra    = classPtr->cbWndExtra;
991     wc->hInstance     = classPtr->hInstance;
992     wc->hIcon         = (HICON32)classPtr->hIcon;
993     wc->hIconSm       = (HICON32)classPtr->hIconSm;
994     wc->hCursor       = (HCURSOR32)classPtr->hCursor;
995     wc->hbrBackground = (HBRUSH32)classPtr->hbrBackground;
996     wc->lpszMenuName  = CLASS_GetMenuNameA( classPtr );
997     wc->lpszClassName = NULL;
998     return TRUE;
999 }
1000
1001
1002 /***********************************************************************
1003  *           GetClassInfoEx32W      (USER32.212)
1004  */
1005 BOOL32 GetClassInfoEx32W( HINSTANCE32 hInstance, LPCWSTR name,
1006                           WNDCLASSEX32W *wc )
1007 {
1008     ATOM atom;
1009     CLASS *classPtr;
1010
1011     hInstance = GetExePtr( hInstance );  /* FIXME: not needed in Win32 */
1012     if (!(atom = GlobalFindAtom32W( name )) ||
1013         !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
1014         (hInstance != classPtr->hInstance)) return FALSE;
1015     wc->style         = classPtr->style;
1016     wc->lpfnWndProc   = (WNDPROC32)WINPROC_GetProc( classPtr->winproc,
1017                                                     WIN_PROC_32W );
1018     wc->cbClsExtra    = classPtr->cbClsExtra;
1019     wc->cbWndExtra    = classPtr->cbWndExtra;
1020     wc->hInstance     = classPtr->hInstance;
1021     wc->hIcon         = (HICON32)classPtr->hIcon;
1022     wc->hIconSm       = (HICON32)classPtr->hIconSm;
1023     wc->hCursor       = (HCURSOR32)classPtr->hCursor;
1024     wc->hbrBackground = (HBRUSH32)classPtr->hbrBackground;
1025     wc->lpszMenuName  = CLASS_GetMenuNameW( classPtr );
1026     wc->lpszClassName = NULL;
1027     return TRUE;
1028 }
1029
1030
1031 /***********************************************************************
1032  *           ClassFirst      (TOOLHELP.69)
1033  */
1034 BOOL16 ClassFirst( CLASSENTRY *pClassEntry )
1035 {
1036     pClassEntry->wNext = 1;
1037     return ClassNext( pClassEntry );
1038 }
1039
1040
1041 /***********************************************************************
1042  *           ClassNext      (TOOLHELP.70)
1043  */
1044 BOOL16 ClassNext( CLASSENTRY *pClassEntry )
1045 {
1046     int i;
1047     CLASS *class = firstClass;
1048
1049     if (!pClassEntry->wNext) return FALSE;
1050     for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1051     if (!class)
1052     {
1053         pClassEntry->wNext = 0;
1054         return FALSE;
1055     }
1056     pClassEntry->hInst = class->hInstance;
1057     pClassEntry->wNext++;
1058     GlobalGetAtomName32A( class->atomName, pClassEntry->szClassName,
1059                           sizeof(pClassEntry->szClassName) );
1060     return TRUE;
1061 }