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