HeapReAlloc doesn't allocate memory.
[wine.git] / windows / class.c
blob9272acf3bba1bb7fec518e3687f6b7345f7996bb
1 /*
2 * Window classes functions
4 * Copyright 1993, 1996 Alexandre Julliard
5 * 1998 Juergen Schmied (jsch)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * FIXME: In win32 all classes are local. They are registered at
22 * program start. Processes CANNOT share classes. (Source: some
23 * win31->NT migration book)
25 * FIXME: There seems to be a general problem with hInstance in WINE
26 * classes are getting registered with wrong hInstance.
29 #include "config.h"
30 #include "wine/port.h"
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include "wine/winbase16.h"
37 #include "winerror.h"
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wingdi.h"
41 #include "wine/winuser16.h"
42 #include "wownt32.h"
43 #include "wine/unicode.h"
44 #include "win.h"
45 #include "user.h"
46 #include "controls.h"
47 #include "dce.h"
48 #include "winproc.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(class);
53 typedef struct tagCLASS
55 struct tagCLASS *next; /* Next class */
56 struct tagCLASS *prev; /* Prev class */
57 UINT cWindows; /* Count of existing windows */
58 UINT style; /* Class style */
59 WNDPROC winprocA; /* Window procedure (ASCII) */
60 WNDPROC winprocW; /* Window procedure (Unicode) */
61 INT cbClsExtra; /* Class extra bytes */
62 INT cbWndExtra; /* Window extra bytes */
63 LPWSTR menuName; /* Default menu name (Unicode followed by ASCII) */
64 SEGPTR segMenuName; /* Default menu name as SEGPTR */
65 struct tagDCE *dce; /* Class DCE (if CS_CLASSDC) */
66 HINSTANCE hInstance; /* Module that created the task */
67 HICON hIcon; /* Default icon */
68 HICON hIconSm; /* Default small icon */
69 HCURSOR hCursor; /* Default cursor */
70 HBRUSH hbrBackground; /* Default background */
71 ATOM atomName; /* Name of the class */
72 } CLASS;
74 static CLASS *firstClass;
76 /***********************************************************************
77 * get_class_ptr
79 static CLASS *get_class_ptr( HWND hwnd, BOOL write_access )
81 WND *ptr = WIN_GetPtr( hwnd );
83 if (ptr)
85 if (ptr != WND_OTHER_PROCESS) return ptr->class;
86 if (IsWindow( hwnd )) /* check other processes */
88 if (write_access)
90 /* modifying classes in other processes is not allowed */
91 SetLastError( ERROR_ACCESS_DENIED );
92 return NULL;
94 FIXME( "reading from class of other process window %p\n", hwnd );
95 /* DbgBreakPoint(); */
98 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
99 return NULL;
103 /***********************************************************************
104 * release_class_ptr
106 inline static void release_class_ptr( CLASS *ptr )
108 USER_Unlock();
112 /***********************************************************************
113 * CLASS_GetProc
115 * Get the class winproc for a given proc type
117 static WNDPROC16 CLASS_GetProc( CLASS *classPtr, WINDOWPROCTYPE type )
119 WNDPROC proc = classPtr->winprocA;
121 if (classPtr->winprocW)
123 /* if we have a Unicode proc, use it if we have no ASCII proc
124 * or if we have both and Unicode was requested
126 if (!proc || type == WIN_PROC_32W) proc = classPtr->winprocW;
128 return WINPROC_GetProc( proc, type );
132 /***********************************************************************
133 * CLASS_SetProc
135 * Set the class winproc for a given proc type.
136 * Returns the previous window proc.
138 static WNDPROC16 CLASS_SetProc( CLASS *classPtr, WNDPROC newproc, WINDOWPROCTYPE type )
140 WNDPROC *proc = &classPtr->winprocA;
141 WNDPROC16 ret;
143 if (classPtr->winprocW)
145 /* if we have a Unicode proc, use it if we have no ASCII proc
146 * or if we have both and Unicode was requested
148 if (!*proc || type == WIN_PROC_32W) proc = &classPtr->winprocW;
150 ret = WINPROC_GetProc( *proc, type );
151 WINPROC_SetProc( proc, newproc, type, WIN_PROC_CLASS );
152 /* now free the one that we didn't set */
153 if (classPtr->winprocA && classPtr->winprocW)
155 if (proc == &classPtr->winprocA)
157 WINPROC_FreeProc( classPtr->winprocW, WIN_PROC_CLASS );
158 classPtr->winprocW = 0;
160 else
162 WINPROC_FreeProc( classPtr->winprocA, WIN_PROC_CLASS );
163 classPtr->winprocA = 0;
166 return ret;
170 /***********************************************************************
171 * CLASS_GetMenuNameA
173 * Get the menu name as a ASCII string.
175 inline static LPSTR CLASS_GetMenuNameA( CLASS *classPtr )
177 if (!HIWORD(classPtr->menuName)) return (LPSTR)classPtr->menuName;
178 return (LPSTR)(classPtr->menuName + strlenW(classPtr->menuName) + 1);
182 /***********************************************************************
183 * CLASS_GetMenuName16
185 * Get the menu name as a SEGPTR.
187 inline static SEGPTR CLASS_GetMenuName16( CLASS *classPtr )
189 if (!HIWORD(classPtr->menuName)) return (SEGPTR)classPtr->menuName;
190 if (!classPtr->segMenuName)
191 classPtr->segMenuName = MapLS( CLASS_GetMenuNameA(classPtr) );
192 return classPtr->segMenuName;
196 /***********************************************************************
197 * CLASS_GetMenuNameW
199 * Get the menu name as a Unicode string.
201 inline static LPWSTR CLASS_GetMenuNameW( CLASS *classPtr )
203 return classPtr->menuName;
207 /***********************************************************************
208 * CLASS_SetMenuNameA
210 * Set the menu name in a class structure by copying the string.
212 static void CLASS_SetMenuNameA( CLASS *classPtr, LPCSTR name )
214 UnMapLS( classPtr->segMenuName );
215 classPtr->segMenuName = 0;
216 if (HIWORD(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
217 if (HIWORD(name))
219 DWORD lenA = strlen(name) + 1;
220 DWORD lenW = MultiByteToWideChar( CP_ACP, 0, name, lenA, NULL, 0 );
221 classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
222 MultiByteToWideChar( CP_ACP, 0, name, lenA, classPtr->menuName, lenW );
223 memcpy( classPtr->menuName + lenW, name, lenA );
225 else classPtr->menuName = (LPWSTR)name;
229 /***********************************************************************
230 * CLASS_SetMenuNameW
232 * Set the menu name in a class structure by copying the string.
234 static void CLASS_SetMenuNameW( CLASS *classPtr, LPCWSTR name )
236 UnMapLS( classPtr->segMenuName );
237 classPtr->segMenuName = 0;
238 if (HIWORD(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
239 if (HIWORD(name))
241 DWORD lenW = strlenW(name) + 1;
242 DWORD lenA = WideCharToMultiByte( CP_ACP, 0, name, lenW, NULL, 0, NULL, NULL );
243 classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
244 memcpy( classPtr->menuName, name, lenW*sizeof(WCHAR) );
245 WideCharToMultiByte( CP_ACP, 0, name, lenW,
246 (char *)(classPtr->menuName + lenW), lenA, NULL, NULL );
248 else classPtr->menuName = (LPWSTR)name;
252 /***********************************************************************
253 * CLASS_FreeClass
255 * Free a class structure.
257 static BOOL CLASS_FreeClass( CLASS *classPtr )
259 TRACE("%p\n", classPtr);
261 /* Check if we can remove this class */
263 if (classPtr->cWindows > 0)
265 SetLastError( ERROR_CLASS_HAS_WINDOWS );
266 return FALSE;
269 /* Remove the class from the linked list */
271 if (classPtr->next) classPtr->next->prev = classPtr->prev;
272 if (classPtr->prev) classPtr->prev->next = classPtr->next;
273 else firstClass = classPtr->next;
275 /* Delete the class */
277 if (classPtr->dce) DCE_FreeDCE( classPtr->dce );
278 if (classPtr->hbrBackground > (HBRUSH)(COLOR_GRADIENTINACTIVECAPTION + 1))
279 DeleteObject( classPtr->hbrBackground );
280 GlobalDeleteAtom( classPtr->atomName );
281 WINPROC_FreeProc( classPtr->winprocA, WIN_PROC_CLASS );
282 WINPROC_FreeProc( classPtr->winprocW, WIN_PROC_CLASS );
283 UnMapLS( classPtr->segMenuName );
284 HeapFree( GetProcessHeap(), 0, classPtr->menuName );
285 HeapFree( GetProcessHeap(), 0, classPtr );
286 return TRUE;
290 /***********************************************************************
291 * CLASS_FreeModuleClasses
293 void CLASS_FreeModuleClasses( HMODULE16 hModule )
295 CLASS *ptr, *next;
297 TRACE("0x%08x\n", hModule);
299 USER_Lock();
300 for (ptr = firstClass; ptr; ptr = next)
302 next = ptr->next;
303 if (ptr->hInstance == HINSTANCE_32(hModule)) CLASS_FreeClass( ptr );
305 USER_Unlock();
309 /***********************************************************************
310 * CLASS_FindClassByAtom
312 * Return a pointer to the class.
313 * hinstance has been normalized by the caller.
315 * NOTES
316 * 980805 a local class will be found now if registred with hInst=0
317 * and looed up with a hInst!=0. msmoney does it (jsch)
319 * Local class registered with a USER instance handle are found as if
320 * they were global classes.
322 static CLASS *CLASS_FindClassByAtom( ATOM atom, HINSTANCE hinstance )
324 CLASS * class, *tclass = 0, *user_class = 0;
325 HINSTANCE16 hUser = GetModuleHandle16("USER");
327 TRACE("0x%08x %p\n", atom, hinstance);
329 /* First search task-specific classes */
331 for (class = firstClass; (class); class = class->next)
333 if (class->style & CS_GLOBALCLASS) continue;
334 if (class->atomName == atom)
336 if (hinstance==class->hInstance || hinstance == (HINSTANCE)0xffff)
338 TRACE("-- found local %p\n", class);
339 return class;
341 if (class->hInstance == 0) tclass = class;
342 else if(class->hInstance == HINSTANCE_32(hUser))
344 user_class = class;
349 /* Then search global classes */
351 for (class = firstClass; (class); class = class->next)
353 if (!(class->style & CS_GLOBALCLASS)) continue;
354 if (class->atomName == atom)
356 TRACE("-- found global %p\n", class);
357 return class;
361 /* Check if there was a local class registered with USER */
362 if( user_class )
364 TRACE("--found local USER class %p\n", user_class);
365 return user_class;
368 /* Then check if there was a local class with hInst=0*/
369 if ( tclass )
371 WARN("-- found local Class registred with hInst=0\n");
372 return tclass;
375 TRACE("-- not found\n");
376 return 0;
380 /***********************************************************************
381 * CLASS_RegisterClass
383 * The real RegisterClass() functionality.
385 static CLASS *CLASS_RegisterClass( ATOM atom, HINSTANCE hInstance,
386 DWORD style, INT classExtra, INT winExtra )
388 CLASS *classPtr;
390 TRACE("atom=0x%x hinst=%p style=0x%lx clExtr=0x%x winExtr=0x%x\n",
391 atom, hInstance, style, classExtra, winExtra );
393 /* Check if a class with this name already exists */
394 classPtr = CLASS_FindClassByAtom( atom, hInstance );
395 if (classPtr)
397 /* Class can be created only if it is local and */
398 /* if the class with the same name is global. */
400 if ((style & CS_GLOBALCLASS) || !(classPtr->style & CS_GLOBALCLASS))
402 SetLastError( ERROR_CLASS_ALREADY_EXISTS );
403 return NULL;
407 /* Fix the extra bytes value */
409 if (classExtra < 0) classExtra = 0;
410 else if (classExtra > 40) /* Extra bytes are limited to 40 in Win32 */
411 WARN("Class extra bytes %d is > 40\n", classExtra);
412 if (winExtra < 0) winExtra = 0;
413 else if (winExtra > 40) /* Extra bytes are limited to 40 in Win32 */
414 WARN("Win extra bytes %d is > 40\n", winExtra );
416 /* Create the class */
418 classPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLASS) + classExtra );
419 if (!classPtr) return NULL;
420 classPtr->style = style;
421 classPtr->cbWndExtra = winExtra;
422 classPtr->cbClsExtra = classExtra;
423 classPtr->hInstance = hInstance;
424 classPtr->atomName = atom;
425 classPtr->dce = (style & CS_CLASSDC) ? DCE_AllocDCE( 0, DCE_CLASS_DC ) : NULL;
427 /* Other non-null values must be set by caller */
429 if ((classPtr->next = firstClass)) firstClass->prev = classPtr;
430 firstClass = classPtr;
431 return classPtr;
435 /***********************************************************************
436 * CLASS_UnregisterClass
438 * The real UnregisterClass() functionality.
440 static BOOL CLASS_UnregisterClass( ATOM atom, HINSTANCE hInstance )
442 CLASS *classPtr;
443 BOOL ret = FALSE;
445 USER_Lock();
446 if (atom &&
447 (classPtr = CLASS_FindClassByAtom( atom, hInstance )) &&
448 (!hInstance || classPtr->hInstance == hInstance))
450 ret = CLASS_FreeClass( classPtr );
452 else SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
454 USER_Unlock();
455 return ret;
459 /***********************************************************************
460 * CLASS_RegisterBuiltinClass
462 * Register a builtin control class.
463 * This allows having both ASCII and Unicode winprocs for the same class.
465 ATOM CLASS_RegisterBuiltinClass( const struct builtin_class_descr *descr )
467 ATOM atom;
468 CLASS *classPtr;
470 if (!(atom = GlobalAddAtomA( descr->name ))) return 0;
472 if (!(classPtr = CLASS_RegisterClass( atom, 0, descr->style, 0, descr->extra )))
474 GlobalDeleteAtom( atom );
475 return 0;
478 classPtr->hCursor = LoadCursorA( 0, (LPSTR)descr->cursor );
479 classPtr->hbrBackground = descr->brush;
481 if (descr->procA) WINPROC_SetProc( &classPtr->winprocA, descr->procA,
482 WIN_PROC_32A, WIN_PROC_CLASS );
483 if (descr->procW) WINPROC_SetProc( &classPtr->winprocW, descr->procW,
484 WIN_PROC_32W, WIN_PROC_CLASS );
485 return atom;
489 /***********************************************************************
490 * CLASS_AddWindow
492 * Add a new window using this class, and return the necessary
493 * information for creating the window.
495 CLASS *CLASS_AddWindow( ATOM atom, HINSTANCE inst, WINDOWPROCTYPE type,
496 INT *winExtra, WNDPROC *winproc, DWORD *style, struct tagDCE **dce )
498 CLASS *class;
499 if (type == WIN_PROC_16) inst = HINSTANCE_32(GetExePtr(HINSTANCE_16(inst)));
501 if (!(class = CLASS_FindClassByAtom( atom, inst ))) return NULL;
502 class->cWindows++;
504 if (type == WIN_PROC_32W)
506 if (!(*winproc = class->winprocW)) *winproc = class->winprocA;
508 else
510 if (!(*winproc = class->winprocA)) *winproc = class->winprocW;
512 *winExtra = class->cbWndExtra;
513 *style = class->style;
514 *dce = class->dce;
515 return class;
519 /***********************************************************************
520 * CLASS_RemoveWindow
522 * Remove a window from the class window count.
524 void CLASS_RemoveWindow( CLASS *cls )
526 if (cls && cls->cWindows) cls->cWindows--;
530 /***********************************************************************
531 * RegisterClass (USER.57)
533 ATOM WINAPI RegisterClass16( const WNDCLASS16 *wc )
535 ATOM atom;
536 CLASS *classPtr;
537 int iSmIconWidth, iSmIconHeight;
538 HINSTANCE hInstance = HINSTANCE_32(GetExePtr(wc->hInstance));
540 if (!(atom = GlobalAddAtomA( MapSL(wc->lpszClassName) ))) return 0;
541 if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
542 wc->cbClsExtra, wc->cbWndExtra )))
544 GlobalDeleteAtom( atom );
545 return 0;
548 TRACE("atom=%04x wndproc=%08lx hinst=%p bg=%04x style=%08x clsExt=%d winExt=%d class=%p name='%s'\n",
549 atom, (DWORD)wc->lpfnWndProc, hInstance,
550 wc->hbrBackground, wc->style, wc->cbClsExtra,
551 wc->cbWndExtra, classPtr,
552 HIWORD(wc->lpszClassName) ? (char *)MapSL(wc->lpszClassName) : "" );
554 iSmIconWidth = GetSystemMetrics(SM_CXSMICON);
555 iSmIconHeight = GetSystemMetrics(SM_CYSMICON);
557 classPtr->hIcon = HICON_32(wc->hIcon);
558 classPtr->hIconSm = CopyImage(classPtr->hIcon, IMAGE_ICON,
559 iSmIconWidth, iSmIconHeight,
560 LR_COPYFROMRESOURCE);
561 classPtr->hCursor = HCURSOR_32(wc->hCursor);
562 classPtr->hbrBackground = HBRUSH_32(wc->hbrBackground);
564 WINPROC_SetProc( &classPtr->winprocA, (WNDPROC)wc->lpfnWndProc,
565 WIN_PROC_16, WIN_PROC_CLASS );
566 CLASS_SetMenuNameA( classPtr, MapSL(wc->lpszMenuName) );
568 return atom;
572 /***********************************************************************
573 * RegisterClassA (USER32.@)
574 * RETURNS
575 * >0: Unique identifier
576 * 0: Failure
578 ATOM WINAPI RegisterClassA( const WNDCLASSA* wc ) /* [in] Address of structure with class data */
580 ATOM atom;
581 int iSmIconWidth, iSmIconHeight;
582 CLASS *classPtr;
584 if (!(atom = GlobalAddAtomA( wc->lpszClassName ))) return 0;
586 if (!(classPtr = CLASS_RegisterClass( atom, wc->hInstance, wc->style,
587 wc->cbClsExtra, wc->cbWndExtra )))
589 GlobalDeleteAtom( atom );
590 return 0;
593 TRACE("atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p name='%s'\n",
594 atom, wc->lpfnWndProc, wc->hInstance,
595 wc->hbrBackground, wc->style, wc->cbClsExtra,
596 wc->cbWndExtra, classPtr,
597 HIWORD(wc->lpszClassName) ? wc->lpszClassName : "" );
599 iSmIconWidth = GetSystemMetrics(SM_CXSMICON);
600 iSmIconHeight = GetSystemMetrics(SM_CYSMICON);
602 classPtr->hIcon = wc->hIcon;
603 classPtr->hIconSm = CopyImage(wc->hIcon, IMAGE_ICON,
604 iSmIconWidth, iSmIconHeight,
605 LR_COPYFROMRESOURCE);
606 classPtr->hCursor = wc->hCursor;
607 classPtr->hbrBackground = wc->hbrBackground;
609 WINPROC_SetProc( &classPtr->winprocA, wc->lpfnWndProc,
610 WIN_PROC_32A, WIN_PROC_CLASS );
611 CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
612 return atom;
616 /***********************************************************************
617 * RegisterClassW (USER32.@)
619 ATOM WINAPI RegisterClassW( const WNDCLASSW* wc )
621 ATOM atom;
622 int iSmIconWidth, iSmIconHeight;
623 CLASS *classPtr;
625 if (!(atom = GlobalAddAtomW( wc->lpszClassName ))) return 0;
627 if (!(classPtr = CLASS_RegisterClass( atom, wc->hInstance, wc->style,
628 wc->cbClsExtra, wc->cbWndExtra )))
630 GlobalDeleteAtom( atom );
631 return 0;
634 TRACE("atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
635 atom, wc->lpfnWndProc, wc->hInstance,
636 wc->hbrBackground, wc->style, wc->cbClsExtra,
637 wc->cbWndExtra, classPtr );
639 iSmIconWidth = GetSystemMetrics(SM_CXSMICON);
640 iSmIconHeight = GetSystemMetrics(SM_CYSMICON);
642 classPtr->hIcon = wc->hIcon;
643 classPtr->hIconSm = CopyImage(wc->hIcon, IMAGE_ICON,
644 iSmIconWidth, iSmIconHeight,
645 LR_COPYFROMRESOURCE);
646 classPtr->hCursor = wc->hCursor;
647 classPtr->hbrBackground = wc->hbrBackground;
649 WINPROC_SetProc( &classPtr->winprocW, wc->lpfnWndProc,
650 WIN_PROC_32W, WIN_PROC_CLASS );
651 CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
652 return atom;
656 /***********************************************************************
657 * RegisterClassEx (USER.397)
659 ATOM WINAPI RegisterClassEx16( const WNDCLASSEX16 *wc )
661 ATOM atom;
662 CLASS *classPtr;
663 HINSTANCE hInstance = HINSTANCE_32(GetExePtr( wc->hInstance ));
665 if (!(atom = GlobalAddAtomA( MapSL(wc->lpszClassName) ))) return 0;
666 if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
667 wc->cbClsExtra, wc->cbWndExtra )))
669 GlobalDeleteAtom( atom );
670 return 0;
673 TRACE("atom=%04x wndproc=%p hinst=%p bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
674 atom, wc->lpfnWndProc, hInstance,
675 wc->hbrBackground, wc->style, wc->cbClsExtra,
676 wc->cbWndExtra, classPtr );
678 classPtr->hIcon = HICON_32(wc->hIcon);
679 classPtr->hIconSm = HICON_32(wc->hIconSm);
680 classPtr->hCursor = HCURSOR_32(wc->hCursor);
681 classPtr->hbrBackground = HBRUSH_32(wc->hbrBackground);
683 WINPROC_SetProc( &classPtr->winprocA, (WNDPROC)wc->lpfnWndProc,
684 WIN_PROC_16, WIN_PROC_CLASS );
685 CLASS_SetMenuNameA( classPtr, MapSL(wc->lpszMenuName) );
686 return atom;
690 /***********************************************************************
691 * RegisterClassExA (USER32.@)
693 ATOM WINAPI RegisterClassExA( const WNDCLASSEXA* wc )
695 ATOM atom;
696 CLASS *classPtr;
698 if (!(atom = GlobalAddAtomA( wc->lpszClassName ))) return 0;
700 if (!(classPtr = CLASS_RegisterClass( atom, wc->hInstance, wc->style,
701 wc->cbClsExtra, wc->cbWndExtra )))
703 GlobalDeleteAtom( atom );
704 return 0;
707 TRACE("atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
708 atom, wc->lpfnWndProc, wc->hInstance,
709 wc->hbrBackground, wc->style, wc->cbClsExtra,
710 wc->cbWndExtra, classPtr );
712 classPtr->hIcon = wc->hIcon;
713 classPtr->hIconSm = wc->hIconSm;
714 classPtr->hCursor = wc->hCursor;
715 classPtr->hbrBackground = wc->hbrBackground;
716 WINPROC_SetProc( &classPtr->winprocA, wc->lpfnWndProc, WIN_PROC_32A, WIN_PROC_CLASS );
717 CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
718 return atom;
722 /***********************************************************************
723 * RegisterClassExW (USER32.@)
725 ATOM WINAPI RegisterClassExW( const WNDCLASSEXW* wc )
727 ATOM atom;
728 CLASS *classPtr;
730 if (!(atom = GlobalAddAtomW( wc->lpszClassName ))) return 0;
732 if (!(classPtr = CLASS_RegisterClass( atom, wc->hInstance, wc->style,
733 wc->cbClsExtra, wc->cbWndExtra )))
735 GlobalDeleteAtom( atom );
736 return 0;
739 TRACE("atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
740 atom, wc->lpfnWndProc, wc->hInstance,
741 wc->hbrBackground, wc->style, wc->cbClsExtra,
742 wc->cbWndExtra, classPtr );
744 classPtr->hIcon = wc->hIcon;
745 classPtr->hIconSm = wc->hIconSm;
746 classPtr->hCursor = wc->hCursor;
747 classPtr->hbrBackground = wc->hbrBackground;
748 WINPROC_SetProc( &classPtr->winprocW, wc->lpfnWndProc, WIN_PROC_32W, WIN_PROC_CLASS );
749 CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
750 return atom;
754 /***********************************************************************
755 * UnregisterClass (USER.403)
757 BOOL16 WINAPI UnregisterClass16( LPCSTR className, HINSTANCE16 hInstance )
759 return UnregisterClassA( className, HINSTANCE_32(GetExePtr( hInstance )) );
762 /***********************************************************************
763 * UnregisterClassA (USER32.@)
766 BOOL WINAPI UnregisterClassA( LPCSTR className, HINSTANCE hInstance )
768 TRACE("%s %p\n",debugstr_a(className), hInstance);
769 return CLASS_UnregisterClass( GlobalFindAtomA( className ), hInstance );
772 /***********************************************************************
773 * UnregisterClassW (USER32.@)
775 BOOL WINAPI UnregisterClassW( LPCWSTR className, HINSTANCE hInstance )
777 TRACE("%s %p\n",debugstr_w(className), hInstance);
778 return CLASS_UnregisterClass( GlobalFindAtomW( className ), hInstance );
782 /***********************************************************************
783 * GetClassWord (USER32.@)
785 WORD WINAPI GetClassWord( HWND hwnd, INT offset )
787 CLASS *class;
788 WORD retvalue = 0;
790 if (offset < 0) return GetClassLongA( hwnd, offset );
792 TRACE("%p %x\n",hwnd, offset);
794 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
796 if (offset <= class->cbClsExtra - sizeof(WORD))
797 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(retvalue) );
798 else
799 SetLastError( ERROR_INVALID_INDEX );
800 release_class_ptr( class );
801 return retvalue;
805 /***********************************************************************
806 * GetClassLong (USER.131)
808 LONG WINAPI GetClassLong16( HWND16 hwnd16, INT16 offset )
810 CLASS *class;
811 LONG ret;
812 HWND hwnd = (HWND)(ULONG_PTR)hwnd16; /* no need for full handle */
814 TRACE("%p %d\n",hwnd, offset);
816 switch( offset )
818 case GCL_WNDPROC:
819 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
820 ret = (LONG)CLASS_GetProc( class, WIN_PROC_16 );
821 release_class_ptr( class );
822 return ret;
823 case GCL_MENUNAME:
824 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
825 ret = (LONG)CLASS_GetMenuName16( class );
826 release_class_ptr( class );
827 return ret;
828 default:
829 return GetClassLongA( hwnd, offset );
834 /***********************************************************************
835 * GetClassLongW (USER32.@)
837 LONG WINAPI GetClassLongW( HWND hwnd, INT offset )
839 CLASS *class;
840 LONG retvalue = 0;
842 TRACE("%p %d\n", hwnd, offset);
844 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
846 if (offset >= 0)
848 if (offset <= class->cbClsExtra - sizeof(LONG))
849 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(retvalue) );
850 else
851 SetLastError( ERROR_INVALID_INDEX );
852 release_class_ptr( class );
853 return retvalue;
856 switch(offset)
858 case GCL_HBRBACKGROUND:
859 retvalue = (LONG)class->hbrBackground;
860 break;
861 case GCL_HCURSOR:
862 retvalue = (LONG)class->hCursor;
863 break;
864 case GCL_HICON:
865 retvalue = (LONG)class->hIcon;
866 break;
867 case GCL_HICONSM:
868 retvalue = (LONG)class->hIconSm;
869 break;
870 case GCL_STYLE:
871 retvalue = (LONG)class->style;
872 break;
873 case GCL_CBWNDEXTRA:
874 retvalue = (LONG)class->cbWndExtra;
875 break;
876 case GCL_CBCLSEXTRA:
877 retvalue = (LONG)class->cbClsExtra;
878 break;
879 case GCL_HMODULE:
880 retvalue = (LONG)class->hInstance;
881 break;
882 case GCL_WNDPROC:
883 retvalue = (LONG)CLASS_GetProc( class, WIN_PROC_32W );
884 break;
885 case GCL_MENUNAME:
886 retvalue = (LONG)CLASS_GetMenuNameW( class );
887 break;
888 case GCW_ATOM:
889 retvalue = (DWORD)class->atomName;
890 break;
891 default:
892 SetLastError( ERROR_INVALID_INDEX );
893 break;
895 release_class_ptr( class );
896 return retvalue;
900 /***********************************************************************
901 * GetClassLongA (USER32.@)
903 LONG WINAPI GetClassLongA( HWND hwnd, INT offset )
905 CLASS *class;
906 LONG retvalue;
908 if (offset != GCL_WNDPROC && offset != GCL_MENUNAME)
909 return GetClassLongW( hwnd, offset );
911 TRACE("%p %d\n", hwnd, offset);
913 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
915 if (offset == GCL_WNDPROC)
916 retvalue = (LONG)CLASS_GetProc( class, WIN_PROC_32A );
917 else /* GCL_MENUNAME */
918 retvalue = (LONG)CLASS_GetMenuNameA( class );
920 release_class_ptr( class );
921 return retvalue;
925 /***********************************************************************
926 * SetClassWord (USER32.@)
928 WORD WINAPI SetClassWord( HWND hwnd, INT offset, WORD newval )
930 CLASS *class;
931 WORD retval = 0;
933 if (offset < 0) return SetClassLongA( hwnd, offset, (DWORD)newval );
935 TRACE("%p %d %x\n", hwnd, offset, newval);
937 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
939 if (offset <= class->cbClsExtra - sizeof(WORD))
941 void *ptr = (char *)(class + 1) + offset;
942 memcpy( &retval, ptr, sizeof(retval) );
943 memcpy( ptr, &newval, sizeof(newval) );
945 else SetLastError( ERROR_INVALID_INDEX );
947 release_class_ptr( class );
948 return retval;
952 /***********************************************************************
953 * SetClassLong (USER.132)
955 LONG WINAPI SetClassLong16( HWND16 hwnd16, INT16 offset, LONG newval )
957 CLASS *class;
958 LONG retval;
959 HWND hwnd = (HWND)(ULONG_PTR)hwnd16; /* no need for full handle */
961 TRACE("%p %d %lx\n", hwnd, offset, newval);
963 switch(offset)
965 case GCL_WNDPROC:
966 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
967 retval = (LONG)CLASS_SetProc( class, (WNDPROC)newval, WIN_PROC_16 );
968 release_class_ptr( class );
969 return retval;
970 case GCL_MENUNAME:
971 newval = (LONG)MapSL( newval );
972 /* fall through */
973 default:
974 return SetClassLongA( hwnd, offset, newval );
979 /***********************************************************************
980 * SetClassLongW (USER32.@)
982 LONG WINAPI SetClassLongW( HWND hwnd, INT offset, LONG newval )
984 CLASS *class;
985 LONG retval = 0;
987 TRACE("%p %d %lx\n", hwnd, offset, newval);
989 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
991 if (offset >= 0)
993 if (offset <= class->cbClsExtra - sizeof(LONG))
995 void *ptr = (char *)(class + 1) + offset;
996 memcpy( &retval, ptr, sizeof(retval) );
997 memcpy( ptr, &newval, sizeof(newval) );
999 else SetLastError( ERROR_INVALID_INDEX );
1001 else switch(offset)
1003 case GCL_MENUNAME:
1004 CLASS_SetMenuNameW( class, (LPCWSTR)newval );
1005 retval = 0; /* Old value is now meaningless anyway */
1006 break;
1007 case GCL_WNDPROC:
1008 retval = (LONG)CLASS_SetProc( class, (WNDPROC)newval, WIN_PROC_32W );
1009 break;
1010 case GCL_HBRBACKGROUND:
1011 retval = (LONG)class->hbrBackground;
1012 class->hbrBackground = (HBRUSH)newval;
1013 break;
1014 case GCL_HCURSOR:
1015 retval = (LONG)class->hCursor;
1016 class->hCursor = (HCURSOR)newval;
1017 break;
1018 case GCL_HICON:
1019 retval = (LONG)class->hIcon;
1020 class->hIcon = (HICON)newval;
1021 break;
1022 case GCL_HICONSM:
1023 retval = (LONG)class->hIconSm;
1024 class->hIconSm = (HICON)newval;
1025 break;
1026 case GCL_STYLE:
1027 retval = (LONG)class->style;
1028 class->style = newval;
1029 break;
1030 case GCL_CBWNDEXTRA:
1031 retval = (LONG)class->cbWndExtra;
1032 class->cbWndExtra = newval;
1033 break;
1034 case GCL_HMODULE:
1035 retval = (LONG)class->hInstance;
1036 class->hInstance = (HINSTANCE)newval;
1037 break;
1038 case GCW_ATOM:
1039 retval = (DWORD)class->atomName;
1040 class->atomName = newval;
1041 break;
1042 case GCL_CBCLSEXTRA: /* cannot change this one */
1043 SetLastError( ERROR_INVALID_PARAMETER );
1044 break;
1045 default:
1046 SetLastError( ERROR_INVALID_INDEX );
1047 break;
1049 release_class_ptr( class );
1050 return retval;
1054 /***********************************************************************
1055 * SetClassLongA (USER32.@)
1057 LONG WINAPI SetClassLongA( HWND hwnd, INT offset, LONG newval )
1059 CLASS *class;
1060 LONG retval;
1062 if (offset != GCL_WNDPROC && offset != GCL_MENUNAME)
1063 return SetClassLongW( hwnd, offset, newval );
1065 TRACE("%p %d %lx\n", hwnd, offset, newval);
1067 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
1069 if (offset == GCL_WNDPROC)
1070 retval = (LONG)CLASS_SetProc( class, (WNDPROC)newval, WIN_PROC_32A );
1071 else /* GCL_MENUNAME */
1073 CLASS_SetMenuNameA( class, (LPCSTR)newval );
1074 retval = 0; /* Old value is now meaningless anyway */
1076 release_class_ptr( class );
1077 return retval;
1081 /***********************************************************************
1082 * GetClassNameA (USER32.@)
1084 INT WINAPI GetClassNameA( HWND hwnd, LPSTR buffer, INT count )
1086 INT ret = GlobalGetAtomNameA( GetClassLongA( hwnd, GCW_ATOM ), buffer, count );
1088 TRACE("%p %s %x\n",hwnd, debugstr_a(buffer), count);
1089 return ret;
1093 /***********************************************************************
1094 * GetClassNameW (USER32.@)
1096 INT WINAPI GetClassNameW( HWND hwnd, LPWSTR buffer, INT count )
1098 INT ret = GlobalGetAtomNameW( GetClassLongW( hwnd, GCW_ATOM ), buffer, count );
1100 TRACE("%p %s %x\n",hwnd, debugstr_w(buffer), count);
1101 return ret;
1105 /***********************************************************************
1106 * RealGetWindowClassA (USER32.@)
1108 UINT WINAPI RealGetWindowClassA( HWND hwnd, LPSTR buffer, UINT count )
1110 return GetClassNameA( hwnd, buffer, count );
1114 /***********************************************************************
1115 * RealGetWindowClassW (USER32.@)
1117 UINT WINAPI RealGetWindowClassW( HWND hwnd, LPWSTR buffer, UINT count )
1119 return GetClassNameW( hwnd, buffer, count );
1123 /***********************************************************************
1124 * GetClassInfo (USER.404)
1126 BOOL16 WINAPI GetClassInfo16( HINSTANCE16 hInst16, SEGPTR name, WNDCLASS16 *wc )
1128 ATOM atom;
1129 CLASS *classPtr;
1130 HINSTANCE hInstance = HINSTANCE_32(GetExePtr( hInst16 ));
1132 TRACE("%p %s %p\n",hInstance, debugstr_a(MapSL(name)), wc);
1134 if (!(atom = GlobalFindAtomA( MapSL(name) )) ||
1135 !(classPtr = CLASS_FindClassByAtom( atom, hInstance )))
1136 return FALSE;
1137 if ((hInstance != classPtr->hInstance) &&
1138 !(classPtr->style & CS_GLOBALCLASS)) /*BWCC likes to pass hInstance=0*/
1139 return FALSE;
1140 wc->style = (UINT16)classPtr->style;
1141 wc->lpfnWndProc = CLASS_GetProc( classPtr, WIN_PROC_16 );
1142 wc->cbClsExtra = (INT16)classPtr->cbClsExtra;
1143 wc->cbWndExtra = (INT16)classPtr->cbWndExtra;
1144 wc->hInstance = classPtr->style & CS_GLOBALCLASS ? GetModuleHandle16("USER") : HINSTANCE_16(classPtr->hInstance);
1145 wc->hIcon = HICON_16(classPtr->hIcon);
1146 wc->hCursor = HCURSOR_16(classPtr->hCursor);
1147 wc->hbrBackground = HBRUSH_16(classPtr->hbrBackground);
1148 wc->lpszClassName = name;
1149 wc->lpszMenuName = CLASS_GetMenuName16( classPtr );
1151 /* We must return the atom of the class here instead of just TRUE. */
1152 return atom;
1156 /***********************************************************************
1157 * GetClassInfoA (USER32.@)
1159 BOOL WINAPI GetClassInfoA( HINSTANCE hInstance, LPCSTR name,
1160 WNDCLASSA *wc )
1162 ATOM atom;
1163 CLASS *classPtr;
1165 TRACE("%p %p %p\n",hInstance, name, wc);
1167 /* workaround: if hInstance=NULL you expect to get the system classes
1168 but this classes (as example from comctl32.dll SysListView) won't be
1169 registered with hInstance=NULL in WINE because of the late loading
1170 of this dll. fixes file dialogs in WinWord95 (jsch)*/
1172 if (!(atom=GlobalFindAtomA(name)) || !(classPtr=CLASS_FindClassByAtom(atom,hInstance)))
1173 return FALSE;
1175 if (!(classPtr->style & CS_GLOBALCLASS) &&
1176 classPtr->hInstance &&
1177 (hInstance != classPtr->hInstance))
1179 if (hInstance) return FALSE;
1180 WARN("systemclass %s (hInst=0) demanded but only class with hInst!=0 found\n",name);
1183 wc->style = classPtr->style;
1184 wc->lpfnWndProc = (WNDPROC)CLASS_GetProc( classPtr, WIN_PROC_32A );
1185 wc->cbClsExtra = classPtr->cbClsExtra;
1186 wc->cbWndExtra = classPtr->cbWndExtra;
1187 wc->hInstance = hInstance;
1188 wc->hIcon = (HICON)classPtr->hIcon;
1189 wc->hCursor = (HCURSOR)classPtr->hCursor;
1190 wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1191 wc->lpszMenuName = CLASS_GetMenuNameA( classPtr );
1192 wc->lpszClassName = name;
1194 /* We must return the atom of the class here instead of just TRUE. */
1195 return atom;
1199 /***********************************************************************
1200 * GetClassInfoW (USER32.@)
1202 BOOL WINAPI GetClassInfoW( HINSTANCE hInstance, LPCWSTR name,
1203 WNDCLASSW *wc )
1205 ATOM atom;
1206 CLASS *classPtr;
1208 TRACE("%p %p %p\n",hInstance, name, wc);
1210 if ( !(atom=GlobalFindAtomW(name)) ||
1211 !(classPtr=CLASS_FindClassByAtom(atom,hInstance))
1213 return FALSE;
1215 if (!(classPtr->style & CS_GLOBALCLASS) &&
1216 classPtr->hInstance &&
1217 (hInstance != classPtr->hInstance))
1219 if (hInstance) return FALSE;
1220 WARN("systemclass %s (hInst=0) demanded but only class with hInst!=0 found\n",debugstr_w(name));
1222 wc->style = classPtr->style;
1223 wc->lpfnWndProc = (WNDPROC)CLASS_GetProc( classPtr, WIN_PROC_32W );
1224 wc->cbClsExtra = classPtr->cbClsExtra;
1225 wc->cbWndExtra = classPtr->cbWndExtra;
1226 wc->hInstance = hInstance;
1227 wc->hIcon = (HICON)classPtr->hIcon;
1228 wc->hCursor = (HCURSOR)classPtr->hCursor;
1229 wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1230 wc->lpszMenuName = CLASS_GetMenuNameW( classPtr );
1231 wc->lpszClassName = name;
1233 /* We must return the atom of the class here instead of just TRUE. */
1234 return atom;
1238 /***********************************************************************
1239 * GetClassInfoEx (USER.398)
1241 * FIXME: this is just a guess, I have no idea if GetClassInfoEx() is the
1242 * same in Win16 as in Win32. --AJ
1244 BOOL16 WINAPI GetClassInfoEx16( HINSTANCE16 hInst16, SEGPTR name, WNDCLASSEX16 *wc )
1246 ATOM atom;
1247 CLASS *classPtr;
1248 HINSTANCE hInstance = HINSTANCE_32(GetExePtr( hInst16 ));
1250 TRACE("%p %s %p\n",hInstance,debugstr_a( MapSL(name) ), wc);
1252 if (!(atom = GlobalFindAtomA( MapSL(name) )) ||
1253 !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
1254 (hInstance != classPtr->hInstance)) return FALSE;
1255 wc->style = classPtr->style;
1256 wc->lpfnWndProc = CLASS_GetProc( classPtr, WIN_PROC_16 );
1257 wc->cbClsExtra = (INT16)classPtr->cbClsExtra;
1258 wc->cbWndExtra = (INT16)classPtr->cbWndExtra;
1259 wc->hInstance = HINSTANCE_16(classPtr->hInstance);
1260 wc->hIcon = HICON_16(classPtr->hIcon);
1261 wc->hIconSm = HICON_16(classPtr->hIconSm);
1262 wc->hCursor = HCURSOR_16(classPtr->hCursor);
1263 wc->hbrBackground = HBRUSH_16(classPtr->hbrBackground);
1264 wc->lpszClassName = (SEGPTR)0;
1265 wc->lpszMenuName = CLASS_GetMenuName16( classPtr );
1266 wc->lpszClassName = name;
1268 /* We must return the atom of the class here instead of just TRUE. */
1269 return atom;
1273 /***********************************************************************
1274 * GetClassInfoExA (USER32.@)
1276 BOOL WINAPI GetClassInfoExA( HINSTANCE hInstance, LPCSTR name,
1277 WNDCLASSEXA *wc )
1279 ATOM atom;
1280 CLASS *classPtr;
1282 TRACE("%p %p %p\n",hInstance, name, wc);
1284 if (!(atom = GlobalFindAtomA( name )) ||
1285 !(classPtr = CLASS_FindClassByAtom( atom, hInstance ))
1286 /*|| (hInstance != classPtr->hInstance) */ ) return FALSE;
1287 wc->style = classPtr->style;
1288 wc->lpfnWndProc = (WNDPROC)CLASS_GetProc( classPtr, WIN_PROC_32A );
1289 wc->cbClsExtra = classPtr->cbClsExtra;
1290 wc->cbWndExtra = classPtr->cbWndExtra;
1291 wc->hInstance = classPtr->hInstance;
1292 wc->hIcon = (HICON)classPtr->hIcon;
1293 wc->hIconSm = (HICON)classPtr->hIconSm;
1294 wc->hCursor = (HCURSOR)classPtr->hCursor;
1295 wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1296 wc->lpszMenuName = CLASS_GetMenuNameA( classPtr );
1297 wc->lpszClassName = name;
1299 /* We must return the atom of the class here instead of just TRUE. */
1300 return atom;
1304 /***********************************************************************
1305 * GetClassInfoExW (USER32.@)
1307 BOOL WINAPI GetClassInfoExW( HINSTANCE hInstance, LPCWSTR name,
1308 WNDCLASSEXW *wc )
1310 ATOM atom;
1311 CLASS *classPtr;
1313 TRACE("%p %p %p\n",hInstance, name, wc);
1315 if (!(atom = GlobalFindAtomW( name )) ||
1316 !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
1317 (hInstance != classPtr->hInstance)) return FALSE;
1318 wc->style = classPtr->style;
1319 wc->lpfnWndProc = (WNDPROC)CLASS_GetProc( classPtr, WIN_PROC_32W );
1320 wc->cbClsExtra = classPtr->cbClsExtra;
1321 wc->cbWndExtra = classPtr->cbWndExtra;
1322 wc->hInstance = classPtr->hInstance;
1323 wc->hIcon = (HICON)classPtr->hIcon;
1324 wc->hIconSm = (HICON)classPtr->hIconSm;
1325 wc->hCursor = (HCURSOR)classPtr->hCursor;
1326 wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1327 wc->lpszMenuName = CLASS_GetMenuNameW( classPtr );
1328 wc->lpszClassName = name;
1330 /* We must return the atom of the class here instead of just TRUE. */
1331 return atom;
1335 #if 0 /* toolhelp is in kernel, so this cannot work */
1337 /***********************************************************************
1338 * ClassFirst (TOOLHELP.69)
1340 BOOL16 WINAPI ClassFirst16( CLASSENTRY *pClassEntry )
1342 TRACE("%p\n",pClassEntry);
1343 pClassEntry->wNext = 1;
1344 return ClassNext16( pClassEntry );
1348 /***********************************************************************
1349 * ClassNext (TOOLHELP.70)
1351 BOOL16 WINAPI ClassNext16( CLASSENTRY *pClassEntry )
1353 int i;
1354 CLASS *class = firstClass;
1356 TRACE("%p\n",pClassEntry);
1358 if (!pClassEntry->wNext) return FALSE;
1359 for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1360 if (!class)
1362 pClassEntry->wNext = 0;
1363 return FALSE;
1365 pClassEntry->hInst = class->hInstance;
1366 pClassEntry->wNext++;
1367 GlobalGetAtomNameA( class->atomName, pClassEntry->szClassName,
1368 sizeof(pClassEntry->szClassName) );
1369 return TRUE;
1371 #endif