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