Release 20021031.
[wine/multimedia.git] / windows / class.c
blob3326782c192775b1992f1e70688342da023f3019
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 <stdlib.h>
33 #include <string.h>
35 #include "wine/winbase16.h"
36 #include "winerror.h"
37 #include "windef.h"
38 #include "wingdi.h"
39 #include "wine/winuser16.h"
40 #include "wownt32.h"
41 #include "wine/unicode.h"
42 #include "win.h"
43 #include "user.h"
44 #include "controls.h"
45 #include "dce.h"
46 #include "winproc.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(class);
51 typedef struct tagCLASS
53 struct tagCLASS *next; /* Next class */
54 struct tagCLASS *prev; /* Prev class */
55 UINT cWindows; /* Count of existing windows */
56 UINT style; /* Class style */
57 HWINDOWPROC winprocA; /* Window procedure (ASCII) */
58 HWINDOWPROC winprocW; /* Window procedure (Unicode) */
59 INT cbClsExtra; /* Class extra bytes */
60 INT cbWndExtra; /* Window extra bytes */
61 LPWSTR menuName; /* Default menu name (Unicode followed by ASCII) */
62 SEGPTR segMenuName; /* Default menu name as SEGPTR */
63 struct tagDCE *dce; /* Class DCE (if CS_CLASSDC) */
64 HINSTANCE hInstance; /* Module that created the task */
65 HICON hIcon; /* Default icon */
66 HICON hIconSm; /* Default small icon */
67 HCURSOR hCursor; /* Default cursor */
68 HBRUSH hbrBackground; /* Default background */
69 ATOM atomName; /* Name of the class */
70 } CLASS;
72 static CLASS *firstClass;
74 /***********************************************************************
75 * get_class_ptr
77 static CLASS *get_class_ptr( HWND hwnd, BOOL write_access )
79 WND *ptr = WIN_GetPtr( hwnd );
81 if (ptr)
83 if (ptr != WND_OTHER_PROCESS) return ptr->class;
84 if (IsWindow( hwnd )) /* check other processes */
86 if (write_access)
88 /* modifying classes in other processes is not allowed */
89 SetLastError( ERROR_ACCESS_DENIED );
90 return NULL;
92 FIXME( "reading from class of other process window %04x\n", hwnd );
93 /* DbgBreakPoint(); */
96 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
97 return NULL;
101 /***********************************************************************
102 * release_class_ptr
104 inline static void release_class_ptr( CLASS *ptr )
106 USER_Unlock();
110 /***********************************************************************
111 * CLASS_GetProc
113 * Get the class winproc for a given proc type
115 static WNDPROC16 CLASS_GetProc( CLASS *classPtr, WINDOWPROCTYPE type )
117 HWINDOWPROC proc = classPtr->winprocA;
119 if (classPtr->winprocW)
121 /* if we have a Unicode proc, use it if we have no ASCII proc
122 * or if we have both and Unicode was requested
124 if (!proc || type == WIN_PROC_32W) proc = classPtr->winprocW;
126 return WINPROC_GetProc( proc, type );
130 /***********************************************************************
131 * CLASS_SetProc
133 * Set the class winproc for a given proc type.
134 * Returns the previous window proc.
136 static WNDPROC16 CLASS_SetProc( CLASS *classPtr, WNDPROC newproc, WINDOWPROCTYPE type )
138 HWINDOWPROC *proc = &classPtr->winprocA;
139 WNDPROC16 ret;
141 if (classPtr->winprocW)
143 /* if we have a Unicode proc, use it if we have no ASCII proc
144 * or if we have both and Unicode was requested
146 if (!*proc || type == WIN_PROC_32W) proc = &classPtr->winprocW;
148 ret = WINPROC_GetProc( *proc, type );
149 WINPROC_SetProc( proc, (HWINDOWPROC)newproc, type, WIN_PROC_CLASS );
150 /* now free the one that we didn't set */
151 if (classPtr->winprocA && classPtr->winprocW)
153 if (proc == &classPtr->winprocA)
155 WINPROC_FreeProc( classPtr->winprocW, WIN_PROC_CLASS );
156 classPtr->winprocW = 0;
158 else
160 WINPROC_FreeProc( classPtr->winprocA, WIN_PROC_CLASS );
161 classPtr->winprocA = 0;
164 return ret;
168 /***********************************************************************
169 * CLASS_GetMenuNameA
171 * Get the menu name as a ASCII string.
173 inline static LPSTR CLASS_GetMenuNameA( CLASS *classPtr )
175 if (!HIWORD(classPtr->menuName)) return (LPSTR)classPtr->menuName;
176 return (LPSTR)(classPtr->menuName + strlenW(classPtr->menuName) + 1);
180 /***********************************************************************
181 * CLASS_GetMenuName16
183 * Get the menu name as a SEGPTR.
185 inline static SEGPTR CLASS_GetMenuName16( CLASS *classPtr )
187 if (!HIWORD(classPtr->menuName)) return (SEGPTR)classPtr->menuName;
188 if (!classPtr->segMenuName)
189 classPtr->segMenuName = MapLS( CLASS_GetMenuNameA(classPtr) );
190 return classPtr->segMenuName;
194 /***********************************************************************
195 * CLASS_GetMenuNameW
197 * Get the menu name as a Unicode string.
199 inline static LPWSTR CLASS_GetMenuNameW( CLASS *classPtr )
201 return classPtr->menuName;
205 /***********************************************************************
206 * CLASS_SetMenuNameA
208 * Set the menu name in a class structure by copying the string.
210 static void CLASS_SetMenuNameA( CLASS *classPtr, LPCSTR name )
212 UnMapLS( classPtr->segMenuName );
213 classPtr->segMenuName = 0;
214 if (HIWORD(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
215 if (HIWORD(name))
217 DWORD lenA = strlen(name) + 1;
218 DWORD lenW = MultiByteToWideChar( CP_ACP, 0, name, lenA, NULL, 0 );
219 classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
220 MultiByteToWideChar( CP_ACP, 0, name, lenA, classPtr->menuName, lenW );
221 memcpy( classPtr->menuName + lenW, name, lenA );
223 else classPtr->menuName = (LPWSTR)name;
227 /***********************************************************************
228 * CLASS_SetMenuNameW
230 * Set the menu name in a class structure by copying the string.
232 static void CLASS_SetMenuNameW( CLASS *classPtr, LPCWSTR name )
234 UnMapLS( classPtr->segMenuName );
235 classPtr->segMenuName = 0;
236 if (HIWORD(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
237 if (HIWORD(name))
239 DWORD lenW = strlenW(name) + 1;
240 DWORD lenA = WideCharToMultiByte( CP_ACP, 0, name, lenW, NULL, 0, NULL, NULL );
241 classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
242 memcpy( classPtr->menuName, name, lenW*sizeof(WCHAR) );
243 WideCharToMultiByte( CP_ACP, 0, name, lenW,
244 (char *)(classPtr->menuName + lenW), lenA, NULL, NULL );
246 else classPtr->menuName = (LPWSTR)name;
250 /***********************************************************************
251 * CLASS_FreeClass
253 * Free a class structure.
255 static BOOL CLASS_FreeClass( CLASS *classPtr )
257 TRACE("%p\n", classPtr);
259 /* Check if we can remove this class */
261 if (classPtr->cWindows > 0)
263 SetLastError( ERROR_CLASS_HAS_WINDOWS );
264 return FALSE;
267 /* Remove the class from the linked list */
269 if (classPtr->next) classPtr->next->prev = classPtr->prev;
270 if (classPtr->prev) classPtr->prev->next = classPtr->next;
271 else firstClass = classPtr->next;
273 /* Delete the class */
275 if (classPtr->dce) DCE_FreeDCE( classPtr->dce );
276 if (classPtr->hbrBackground > (HBRUSH)(COLOR_GRADIENTINACTIVECAPTION + 1))
277 DeleteObject( classPtr->hbrBackground );
278 GlobalDeleteAtom( classPtr->atomName );
279 WINPROC_FreeProc( classPtr->winprocA, WIN_PROC_CLASS );
280 WINPROC_FreeProc( classPtr->winprocW, WIN_PROC_CLASS );
281 UnMapLS( classPtr->segMenuName );
282 HeapFree( GetProcessHeap(), 0, classPtr->menuName );
283 HeapFree( GetProcessHeap(), 0, classPtr );
284 return TRUE;
288 /***********************************************************************
289 * CLASS_FreeModuleClasses
291 void CLASS_FreeModuleClasses( HMODULE16 hModule )
293 CLASS *ptr, *next;
295 TRACE("0x%08x\n", hModule);
297 USER_Lock();
298 for (ptr = firstClass; ptr; ptr = next)
300 next = ptr->next;
301 if (ptr->hInstance == hModule) CLASS_FreeClass( ptr );
303 USER_Unlock();
307 /***********************************************************************
308 * CLASS_FindClassByAtom
310 * Return a pointer to the class.
311 * hinstance has been normalized by the caller.
313 * NOTES
314 * 980805 a local class will be found now if registred with hInst=0
315 * and looed up with a hInst!=0. msmoney does it (jsch)
317 * Local class registered with a USER instance handle are found as if
318 * they were global classes.
320 static CLASS *CLASS_FindClassByAtom( ATOM atom, HINSTANCE hinstance )
322 CLASS * class, *tclass = 0, *user_class = 0;
323 HINSTANCE16 hUser = GetModuleHandle16("USER");
325 TRACE("0x%08x 0x%08x\n", atom, hinstance);
327 /* First search task-specific classes */
329 for (class = firstClass; (class); class = class->next)
331 if (class->style & CS_GLOBALCLASS) continue;
332 if (class->atomName == atom)
334 if (hinstance==class->hInstance || hinstance==0xffff)
336 TRACE("-- found local %p\n", class);
337 return class;
339 if (class->hInstance == 0) tclass = class;
340 else if(class->hInstance == hUser)
342 user_class = class;
347 /* Then search global classes */
349 for (class = firstClass; (class); class = class->next)
351 if (!(class->style & CS_GLOBALCLASS)) continue;
352 if (class->atomName == atom)
354 TRACE("-- found global %p\n", class);
355 return class;
359 /* Check if there was a local class registered with USER */
360 if( user_class )
362 TRACE("--found local USER class %p\n", user_class);
363 return user_class;
366 /* Then check if there was a local class with hInst=0*/
367 if ( tclass )
369 WARN("-- found local Class registred with hInst=0\n");
370 return tclass;
373 TRACE("-- not found\n");
374 return 0;
378 /***********************************************************************
379 * CLASS_RegisterClass
381 * The real RegisterClass() functionality.
383 static CLASS *CLASS_RegisterClass( ATOM atom, HINSTANCE hInstance,
384 DWORD style, INT classExtra, INT winExtra )
386 CLASS *classPtr;
388 TRACE("atom=0x%x hinst=0x%x style=0x%lx clExtr=0x%x winExtr=0x%x\n",
389 atom, hInstance, style, classExtra, winExtra );
391 /* Check if a class with this name already exists */
392 classPtr = CLASS_FindClassByAtom( atom, hInstance );
393 if (classPtr)
395 /* Class can be created only if it is local and */
396 /* if the class with the same name is global. */
398 if ((style & CS_GLOBALCLASS) || !(classPtr->style & CS_GLOBALCLASS))
400 SetLastError( ERROR_CLASS_ALREADY_EXISTS );
401 return NULL;
405 /* Fix the extra bytes value */
407 if (classExtra < 0) classExtra = 0;
408 else if (classExtra > 40) /* Extra bytes are limited to 40 in Win32 */
409 WARN("Class extra bytes %d is > 40\n", classExtra);
410 if (winExtra < 0) winExtra = 0;
411 else if (winExtra > 40) /* Extra bytes are limited to 40 in Win32 */
412 WARN("Win extra bytes %d is > 40\n", winExtra );
414 /* Create the class */
416 classPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLASS) + classExtra );
417 if (!classPtr) return NULL;
418 classPtr->style = style;
419 classPtr->cbWndExtra = winExtra;
420 classPtr->cbClsExtra = classExtra;
421 classPtr->hInstance = hInstance;
422 classPtr->atomName = atom;
423 classPtr->dce = (style & CS_CLASSDC) ? DCE_AllocDCE( 0, DCE_CLASS_DC ) : NULL;
425 /* Other non-null values must be set by caller */
427 if ((classPtr->next = firstClass)) firstClass->prev = classPtr;
428 firstClass = classPtr;
429 return classPtr;
433 /***********************************************************************
434 * CLASS_UnregisterClass
436 * The real UnregisterClass() functionality.
438 static BOOL CLASS_UnregisterClass( ATOM atom, HINSTANCE hInstance )
440 CLASS *classPtr;
441 BOOL ret = FALSE;
443 USER_Lock();
444 if (atom &&
445 (classPtr = CLASS_FindClassByAtom( atom, hInstance )) &&
446 (!hInstance || classPtr->hInstance == hInstance))
448 ret = CLASS_FreeClass( classPtr );
450 else SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
452 USER_Unlock();
453 return ret;
457 /***********************************************************************
458 * CLASS_RegisterBuiltinClass
460 * Register a builtin control class.
461 * This allows having both ASCII and Unicode winprocs for the same class.
463 ATOM CLASS_RegisterBuiltinClass( const struct builtin_class_descr *descr )
465 ATOM atom;
466 CLASS *classPtr;
468 if (!(atom = GlobalAddAtomA( descr->name ))) return 0;
470 if (!(classPtr = CLASS_RegisterClass( atom, 0, descr->style, 0, descr->extra )))
472 GlobalDeleteAtom( atom );
473 return 0;
476 classPtr->hCursor = LoadCursorA( 0, descr->cursor );
477 classPtr->hbrBackground = descr->brush;
479 if (descr->procA) WINPROC_SetProc( &classPtr->winprocA, (HWINDOWPROC)descr->procA,
480 WIN_PROC_32A, WIN_PROC_CLASS );
481 if (descr->procW) WINPROC_SetProc( &classPtr->winprocW, (HWINDOWPROC)descr->procW,
482 WIN_PROC_32W, WIN_PROC_CLASS );
483 return atom;
487 /***********************************************************************
488 * CLASS_AddWindow
490 * Add a new window using this class, and return the necessary
491 * information for creating the window.
493 CLASS *CLASS_AddWindow( ATOM atom, HINSTANCE inst, WINDOWPROCTYPE type,
494 INT *winExtra, WNDPROC *winproc, DWORD *style, struct tagDCE **dce )
496 CLASS *class;
497 if (type == WIN_PROC_16) inst = GetExePtr(inst);
499 if (!(class = CLASS_FindClassByAtom( atom, inst ))) return NULL;
500 class->cWindows++;
502 if (type == WIN_PROC_32W)
504 if (!(*winproc = class->winprocW)) *winproc = class->winprocA;
506 else
508 if (!(*winproc = class->winprocA)) *winproc = class->winprocW;
510 *winExtra = class->cbWndExtra;
511 *style = class->style;
512 *dce = class->dce;
513 return class;
517 /***********************************************************************
518 * CLASS_RemoveWindow
520 * Remove a window from the class window count.
522 void CLASS_RemoveWindow( CLASS *cls )
524 if (cls && cls->cWindows) cls->cWindows--;
528 /***********************************************************************
529 * RegisterClass (USER.57)
531 ATOM WINAPI RegisterClass16( const WNDCLASS16 *wc )
533 ATOM atom;
534 CLASS *classPtr;
535 int iSmIconWidth, iSmIconHeight;
536 HINSTANCE16 hInstance=GetExePtr(wc->hInstance);
538 if (!(atom = GlobalAddAtomA( MapSL(wc->lpszClassName) ))) return 0;
539 if (!(classPtr = CLASS_RegisterClass( atom, hInstance, wc->style,
540 wc->cbClsExtra, wc->cbWndExtra )))
542 GlobalDeleteAtom( atom );
543 return 0;
546 TRACE("atom=%04x wndproc=%08lx hinst=%04x "
547 "bg=%04x style=%08x clsExt=%d winExt=%d class=%p name='%s'\n",
548 atom, (DWORD)wc->lpfnWndProc, hInstance,
549 wc->hbrBackground, wc->style, wc->cbClsExtra,
550 wc->cbWndExtra, classPtr,
551 HIWORD(wc->lpszClassName) ?
552 (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(wc->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, (HWINDOWPROC)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=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p name='%s'\n",
594 atom, (DWORD)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, (HWINDOWPROC)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=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
635 atom, (DWORD)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, (HWINDOWPROC)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 HINSTANCE16 hInstance = 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=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
674 atom, (DWORD)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, (HWINDOWPROC)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=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
708 atom, (DWORD)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, (HWINDOWPROC)wc->lpfnWndProc,
717 WIN_PROC_32A, WIN_PROC_CLASS );
718 CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
719 return atom;
723 /***********************************************************************
724 * RegisterClassExW (USER32.@)
726 ATOM WINAPI RegisterClassExW( const WNDCLASSEXW* wc )
728 ATOM atom;
729 CLASS *classPtr;
731 if (!(atom = GlobalAddAtomW( wc->lpszClassName ))) return 0;
733 if (!(classPtr = CLASS_RegisterClass( atom, wc->hInstance, wc->style,
734 wc->cbClsExtra, wc->cbWndExtra )))
736 GlobalDeleteAtom( atom );
737 return 0;
740 TRACE("atom=%04x wndproc=%08lx hinst=%04x bg=%04x style=%08x clsExt=%d winExt=%d class=%p\n",
741 atom, (DWORD)wc->lpfnWndProc, wc->hInstance,
742 wc->hbrBackground, wc->style, wc->cbClsExtra,
743 wc->cbWndExtra, classPtr );
745 classPtr->hIcon = wc->hIcon;
746 classPtr->hIconSm = wc->hIconSm;
747 classPtr->hCursor = wc->hCursor;
748 classPtr->hbrBackground = wc->hbrBackground;
749 WINPROC_SetProc( &classPtr->winprocW, (HWINDOWPROC)wc->lpfnWndProc,
750 WIN_PROC_32W, WIN_PROC_CLASS );
751 CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
752 return atom;
756 /***********************************************************************
757 * UnregisterClass (USER.403)
759 BOOL16 WINAPI UnregisterClass16( LPCSTR className, HINSTANCE16 hInstance )
761 return UnregisterClassA( className, GetExePtr( hInstance ) );
764 /***********************************************************************
765 * UnregisterClassA (USER32.@)
768 BOOL WINAPI UnregisterClassA( LPCSTR className, HINSTANCE hInstance )
770 TRACE("%s %x\n",debugstr_a(className), hInstance);
771 return CLASS_UnregisterClass( GlobalFindAtomA( className ), hInstance );
774 /***********************************************************************
775 * UnregisterClassW (USER32.@)
777 BOOL WINAPI UnregisterClassW( LPCWSTR className, HINSTANCE hInstance )
779 TRACE("%s %x\n",debugstr_w(className), hInstance);
780 return CLASS_UnregisterClass( GlobalFindAtomW( className ), hInstance );
784 /***********************************************************************
785 * GetClassWord (USER32.@)
787 WORD WINAPI GetClassWord( HWND hwnd, INT offset )
789 CLASS *class;
790 WORD retvalue = 0;
792 if (offset < 0) return GetClassLongA( hwnd, offset );
794 TRACE("%x %x\n",hwnd, offset);
796 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
798 if (offset <= class->cbClsExtra - sizeof(WORD))
799 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(retvalue) );
800 else
801 SetLastError( ERROR_INVALID_INDEX );
802 release_class_ptr( class );
803 return retvalue;
807 /***********************************************************************
808 * GetClassLong (USER.131)
810 LONG WINAPI GetClassLong16( HWND16 hwnd16, INT16 offset )
812 CLASS *class;
813 LONG ret;
814 HWND hwnd = (HWND)(ULONG_PTR)hwnd16; /* no need for full handle */
816 TRACE("%x %d\n",hwnd, offset);
818 switch( offset )
820 case GCL_WNDPROC:
821 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
822 ret = (LONG)CLASS_GetProc( class, WIN_PROC_16 );
823 release_class_ptr( class );
824 return ret;
825 case GCL_MENUNAME:
826 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
827 ret = (LONG)CLASS_GetMenuName16( class );
828 release_class_ptr( class );
829 return ret;
830 default:
831 return GetClassLongA( hwnd, offset );
836 /***********************************************************************
837 * GetClassLongA (USER32.@)
839 LONG WINAPI GetClassLongA( HWND hwnd, INT offset )
841 CLASS *class;
842 LONG retvalue = 0;
844 TRACE("%x %d\n", hwnd, offset);
846 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
848 if (offset >= 0)
850 if (offset <= class->cbClsExtra - sizeof(LONG))
851 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(retvalue) );
852 else
853 SetLastError( ERROR_INVALID_INDEX );
854 release_class_ptr( class );
855 return retvalue;
858 switch(offset)
860 case GCL_HBRBACKGROUND:
861 retvalue = (LONG)class->hbrBackground;
862 break;
863 case GCL_HCURSOR:
864 retvalue = (LONG)class->hCursor;
865 break;
866 case GCL_HICON:
867 retvalue = (LONG)class->hIcon;
868 break;
869 case GCL_HICONSM:
870 retvalue = (LONG)class->hIconSm;
871 break;
872 case GCL_STYLE:
873 retvalue = (LONG)class->style;
874 break;
875 case GCL_CBWNDEXTRA:
876 retvalue = (LONG)class->cbWndExtra;
877 break;
878 case GCL_CBCLSEXTRA:
879 retvalue = (LONG)class->cbClsExtra;
880 break;
881 case GCL_HMODULE:
882 retvalue = (LONG)class->hInstance;
883 break;
884 case GCL_WNDPROC:
885 retvalue = (LONG)CLASS_GetProc( class, WIN_PROC_32A );
886 break;
887 case GCL_MENUNAME:
888 retvalue = (LONG)CLASS_GetMenuNameA( class );
889 break;
890 case GCW_ATOM:
891 retvalue = (DWORD)class->atomName;
892 break;
893 default:
894 SetLastError( ERROR_INVALID_INDEX );
895 break;
897 release_class_ptr( class );
898 return retvalue;
902 /***********************************************************************
903 * GetClassLongW (USER32.@)
905 LONG WINAPI GetClassLongW( HWND hwnd, INT offset )
907 CLASS *class;
908 LONG retvalue;
910 if (offset != GCL_WNDPROC && offset != GCL_MENUNAME)
911 return GetClassLongA( hwnd, offset );
913 TRACE("%x %d\n", hwnd, offset);
915 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
917 if (offset == GCL_WNDPROC)
918 retvalue = (LONG)CLASS_GetProc( class, WIN_PROC_32W );
919 else /* GCL_MENUNAME */
920 retvalue = (LONG)CLASS_GetMenuNameW( class );
922 release_class_ptr( class );
923 return retvalue;
927 /***********************************************************************
928 * SetClassWord (USER32.@)
930 WORD WINAPI SetClassWord( HWND hwnd, INT offset, WORD newval )
932 CLASS *class;
933 WORD retval = 0;
935 if (offset < 0) return SetClassLongA( hwnd, offset, (DWORD)newval );
937 TRACE("%x %d %x\n", hwnd, offset, newval);
939 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
941 if (offset <= class->cbClsExtra - sizeof(WORD))
943 void *ptr = (char *)(class + 1) + offset;
944 memcpy( &retval, ptr, sizeof(retval) );
945 memcpy( ptr, &newval, sizeof(newval) );
947 else SetLastError( ERROR_INVALID_INDEX );
949 release_class_ptr( class );
950 return retval;
954 /***********************************************************************
955 * SetClassLong (USER.132)
957 LONG WINAPI SetClassLong16( HWND16 hwnd16, INT16 offset, LONG newval )
959 CLASS *class;
960 LONG retval;
961 HWND hwnd = (HWND)(ULONG_PTR)hwnd16; /* no need for full handle */
963 TRACE("%x %d %lx\n", hwnd, offset, newval);
965 switch(offset)
967 case GCL_WNDPROC:
968 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
969 retval = (LONG)CLASS_SetProc( class, (WNDPROC)newval, WIN_PROC_16 );
970 release_class_ptr( class );
971 return retval;
972 case GCL_MENUNAME:
973 newval = (LONG)MapSL( newval );
974 /* fall through */
975 default:
976 return SetClassLongA( hwnd, offset, newval );
981 /***********************************************************************
982 * SetClassLongA (USER32.@)
984 LONG WINAPI SetClassLongA( HWND hwnd, INT offset, LONG newval )
986 CLASS *class;
987 LONG retval = 0;
989 TRACE("%x %d %lx\n", hwnd, offset, newval);
991 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
993 if (offset >= 0)
995 if (offset <= class->cbClsExtra - sizeof(LONG))
997 void *ptr = (char *)(class + 1) + offset;
998 memcpy( &retval, ptr, sizeof(retval) );
999 memcpy( ptr, &newval, sizeof(newval) );
1001 else SetLastError( ERROR_INVALID_INDEX );
1003 else switch(offset)
1005 case GCL_MENUNAME:
1006 CLASS_SetMenuNameA( class, (LPCSTR)newval );
1007 retval = 0; /* Old value is now meaningless anyway */
1008 break;
1009 case GCL_WNDPROC:
1010 retval = (LONG)CLASS_SetProc( class, (WNDPROC)newval, WIN_PROC_32A );
1011 break;
1012 case GCL_HBRBACKGROUND:
1013 retval = (LONG)class->hbrBackground;
1014 class->hbrBackground = newval;
1015 break;
1016 case GCL_HCURSOR:
1017 retval = (LONG)class->hCursor;
1018 class->hCursor = (HCURSOR)newval;
1019 break;
1020 case GCL_HICON:
1021 retval = (LONG)class->hIcon;
1022 class->hIcon = (HICON)newval;
1023 break;
1024 case GCL_HICONSM:
1025 retval = (LONG)class->hIconSm;
1026 class->hIconSm = (HICON)newval;
1027 break;
1028 case GCL_STYLE:
1029 retval = (LONG)class->style;
1030 class->style = newval;
1031 break;
1032 case GCL_CBWNDEXTRA:
1033 retval = (LONG)class->cbWndExtra;
1034 class->cbWndExtra = newval;
1035 break;
1036 case GCL_HMODULE:
1037 retval = (LONG)class->hInstance;
1038 class->hInstance = newval;
1039 break;
1040 case GCW_ATOM:
1041 retval = (DWORD)class->atomName;
1042 class->atomName = newval;
1043 break;
1044 case GCL_CBCLSEXTRA: /* cannot change this one */
1045 SetLastError( ERROR_INVALID_PARAMETER );
1046 break;
1047 default:
1048 SetLastError( ERROR_INVALID_INDEX );
1049 break;
1051 release_class_ptr( class );
1052 return retval;
1056 /***********************************************************************
1057 * SetClassLongW (USER32.@)
1059 LONG WINAPI SetClassLongW( HWND hwnd, INT offset, LONG newval )
1061 CLASS *class;
1062 LONG retval;
1064 if (offset != GCL_WNDPROC && offset != GCL_MENUNAME)
1065 return SetClassLongA( hwnd, offset, newval );
1067 TRACE("%x %d %lx\n", hwnd, offset, newval);
1069 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
1071 if (offset == GCL_WNDPROC)
1072 retval = (LONG)CLASS_SetProc( class, (WNDPROC)newval, WIN_PROC_32W );
1073 else /* GCL_MENUNAME */
1075 CLASS_SetMenuNameW( class, (LPCWSTR)newval );
1076 retval = 0; /* Old value is now meaningless anyway */
1078 release_class_ptr( class );
1079 return retval;
1083 /***********************************************************************
1084 * GetClassNameA (USER32.@)
1086 INT WINAPI GetClassNameA( HWND hwnd, LPSTR buffer, INT count )
1088 INT ret = GlobalGetAtomNameA( GetClassLongA( hwnd, GCW_ATOM ), buffer, count );
1090 TRACE("%x %s %x\n",hwnd, debugstr_a(buffer), count);
1091 return ret;
1095 /***********************************************************************
1096 * GetClassNameW (USER32.@)
1098 INT WINAPI GetClassNameW( HWND hwnd, LPWSTR buffer, INT count )
1100 INT ret = GlobalGetAtomNameW( GetClassLongW( hwnd, GCW_ATOM ), buffer, count );
1102 TRACE("%x %s %x\n",hwnd, debugstr_w(buffer), count);
1103 return ret;
1107 /***********************************************************************
1108 * GetClassInfo (USER.404)
1110 BOOL16 WINAPI GetClassInfo16( HINSTANCE16 hInstance, SEGPTR name, WNDCLASS16 *wc )
1112 ATOM atom;
1113 CLASS *classPtr;
1115 TRACE("%x %s %p\n",hInstance, debugstr_a(MapSL(name)), wc);
1117 hInstance = GetExePtr( hInstance );
1118 if (!(atom = GlobalFindAtomA( MapSL(name) )) ||
1119 !(classPtr = CLASS_FindClassByAtom( atom, hInstance )))
1120 return FALSE;
1121 if ((hInstance != classPtr->hInstance) &&
1122 !(classPtr->style & CS_GLOBALCLASS)) /*BWCC likes to pass hInstance=0*/
1123 return FALSE;
1124 wc->style = (UINT16)classPtr->style;
1125 wc->lpfnWndProc = CLASS_GetProc( classPtr, WIN_PROC_16 );
1126 wc->cbClsExtra = (INT16)classPtr->cbClsExtra;
1127 wc->cbWndExtra = (INT16)classPtr->cbWndExtra;
1128 wc->hInstance = classPtr->style & CS_GLOBALCLASS ? GetModuleHandle16("USER") : (HINSTANCE16)classPtr->hInstance;
1129 wc->hIcon = HICON_16(classPtr->hIcon);
1130 wc->hCursor = HCURSOR_16(classPtr->hCursor);
1131 wc->hbrBackground = HBRUSH_16(classPtr->hbrBackground);
1132 wc->lpszClassName = name;
1133 wc->lpszMenuName = CLASS_GetMenuName16( classPtr );
1134 return TRUE;
1138 /***********************************************************************
1139 * GetClassInfoA (USER32.@)
1141 BOOL WINAPI GetClassInfoA( HINSTANCE hInstance, LPCSTR name,
1142 WNDCLASSA *wc )
1144 ATOM atom;
1145 CLASS *classPtr;
1147 TRACE("%x %p %p\n",hInstance, name, wc);
1149 /* workaround: if hInstance=NULL you expect to get the system classes
1150 but this classes (as example from comctl32.dll SysListView) won't be
1151 registered with hInstance=NULL in WINE because of the late loading
1152 of this dll. fixes file dialogs in WinWord95 (jsch)*/
1154 if (!(atom=GlobalFindAtomA(name)) || !(classPtr=CLASS_FindClassByAtom(atom,hInstance)))
1155 return FALSE;
1157 if (!(classPtr->style & CS_GLOBALCLASS) &&
1158 classPtr->hInstance &&
1159 (hInstance != classPtr->hInstance))
1161 if (hInstance) return FALSE;
1162 WARN("systemclass %s (hInst=0) demanded but only class with hInst!=0 found\n",name);
1165 wc->style = classPtr->style;
1166 wc->lpfnWndProc = (WNDPROC)CLASS_GetProc( classPtr, WIN_PROC_32A );
1167 wc->cbClsExtra = classPtr->cbClsExtra;
1168 wc->cbWndExtra = classPtr->cbWndExtra;
1169 wc->hInstance = hInstance;
1170 wc->hIcon = (HICON)classPtr->hIcon;
1171 wc->hCursor = (HCURSOR)classPtr->hCursor;
1172 wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1173 wc->lpszMenuName = CLASS_GetMenuNameA( classPtr );
1174 wc->lpszClassName = name;
1175 return TRUE;
1179 /***********************************************************************
1180 * GetClassInfoW (USER32.@)
1182 BOOL WINAPI GetClassInfoW( HINSTANCE hInstance, LPCWSTR name,
1183 WNDCLASSW *wc )
1185 ATOM atom;
1186 CLASS *classPtr;
1188 TRACE("%x %p %p\n",hInstance, name, wc);
1190 if ( !(atom=GlobalFindAtomW(name)) ||
1191 !(classPtr=CLASS_FindClassByAtom(atom,hInstance))
1193 return FALSE;
1195 if (!(classPtr->style & CS_GLOBALCLASS) &&
1196 classPtr->hInstance &&
1197 (hInstance != classPtr->hInstance))
1199 if (hInstance) return FALSE;
1200 WARN("systemclass %s (hInst=0) demanded but only class with hInst!=0 found\n",debugstr_w(name));
1202 wc->style = classPtr->style;
1203 wc->lpfnWndProc = (WNDPROC)CLASS_GetProc( classPtr, WIN_PROC_32W );
1204 wc->cbClsExtra = classPtr->cbClsExtra;
1205 wc->cbWndExtra = classPtr->cbWndExtra;
1206 wc->hInstance = hInstance;
1207 wc->hIcon = (HICON)classPtr->hIcon;
1208 wc->hCursor = (HCURSOR)classPtr->hCursor;
1209 wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1210 wc->lpszMenuName = CLASS_GetMenuNameW( classPtr );
1211 wc->lpszClassName = name;
1212 return TRUE;
1216 /***********************************************************************
1217 * GetClassInfoEx (USER.398)
1219 * FIXME: this is just a guess, I have no idea if GetClassInfoEx() is the
1220 * same in Win16 as in Win32. --AJ
1222 BOOL16 WINAPI GetClassInfoEx16( HINSTANCE16 hInstance, SEGPTR name, WNDCLASSEX16 *wc )
1224 ATOM atom;
1225 CLASS *classPtr;
1227 TRACE("%x %s %p\n",hInstance,debugstr_a( MapSL(name) ), wc);
1229 hInstance = GetExePtr( hInstance );
1230 if (!(atom = GlobalFindAtomA( MapSL(name) )) ||
1231 !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
1232 (hInstance != classPtr->hInstance)) return FALSE;
1233 wc->style = classPtr->style;
1234 wc->lpfnWndProc = CLASS_GetProc( classPtr, WIN_PROC_16 );
1235 wc->cbClsExtra = (INT16)classPtr->cbClsExtra;
1236 wc->cbWndExtra = (INT16)classPtr->cbWndExtra;
1237 wc->hInstance = (HINSTANCE16)classPtr->hInstance;
1238 wc->hIcon = HICON_16(classPtr->hIcon);
1239 wc->hIconSm = HICON_16(classPtr->hIconSm);
1240 wc->hCursor = HCURSOR_16(classPtr->hCursor);
1241 wc->hbrBackground = HBRUSH_16(classPtr->hbrBackground);
1242 wc->lpszClassName = (SEGPTR)0;
1243 wc->lpszMenuName = CLASS_GetMenuName16( classPtr );
1244 wc->lpszClassName = name;
1246 /* We must return the atom of the class here instead of just TRUE. */
1247 return atom;
1251 /***********************************************************************
1252 * GetClassInfoExA (USER32.@)
1254 BOOL WINAPI GetClassInfoExA( HINSTANCE hInstance, LPCSTR name,
1255 WNDCLASSEXA *wc )
1257 ATOM atom;
1258 CLASS *classPtr;
1260 TRACE("%x %p %p\n",hInstance, name, wc);
1262 if (!(atom = GlobalFindAtomA( name )) ||
1263 !(classPtr = CLASS_FindClassByAtom( atom, hInstance ))
1264 /*|| (hInstance != classPtr->hInstance) */ ) return FALSE;
1265 wc->style = classPtr->style;
1266 wc->lpfnWndProc = (WNDPROC)CLASS_GetProc( classPtr, WIN_PROC_32A );
1267 wc->cbClsExtra = classPtr->cbClsExtra;
1268 wc->cbWndExtra = classPtr->cbWndExtra;
1269 wc->hInstance = classPtr->hInstance;
1270 wc->hIcon = (HICON)classPtr->hIcon;
1271 wc->hIconSm = (HICON)classPtr->hIconSm;
1272 wc->hCursor = (HCURSOR)classPtr->hCursor;
1273 wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1274 wc->lpszMenuName = CLASS_GetMenuNameA( classPtr );
1275 wc->lpszClassName = name;
1277 /* We must return the atom of the class here instead of just TRUE. */
1278 return atom;
1282 /***********************************************************************
1283 * GetClassInfoExW (USER32.@)
1285 BOOL WINAPI GetClassInfoExW( HINSTANCE hInstance, LPCWSTR name,
1286 WNDCLASSEXW *wc )
1288 ATOM atom;
1289 CLASS *classPtr;
1291 TRACE("%x %p %p\n",hInstance, name, wc);
1293 if (!(atom = GlobalFindAtomW( name )) ||
1294 !(classPtr = CLASS_FindClassByAtom( atom, hInstance )) ||
1295 (hInstance != classPtr->hInstance)) return FALSE;
1296 wc->style = classPtr->style;
1297 wc->lpfnWndProc = (WNDPROC)CLASS_GetProc( classPtr, WIN_PROC_32W );
1298 wc->cbClsExtra = classPtr->cbClsExtra;
1299 wc->cbWndExtra = classPtr->cbWndExtra;
1300 wc->hInstance = classPtr->hInstance;
1301 wc->hIcon = (HICON)classPtr->hIcon;
1302 wc->hIconSm = (HICON)classPtr->hIconSm;
1303 wc->hCursor = (HCURSOR)classPtr->hCursor;
1304 wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1305 wc->lpszMenuName = CLASS_GetMenuNameW( classPtr );
1306 wc->lpszClassName = name;
1308 /* We must return the atom of the class here instead of just TRUE. */
1309 return atom;
1313 #if 0 /* toolhelp is in kernel, so this cannot work */
1315 /***********************************************************************
1316 * ClassFirst (TOOLHELP.69)
1318 BOOL16 WINAPI ClassFirst16( CLASSENTRY *pClassEntry )
1320 TRACE("%p\n",pClassEntry);
1321 pClassEntry->wNext = 1;
1322 return ClassNext16( pClassEntry );
1326 /***********************************************************************
1327 * ClassNext (TOOLHELP.70)
1329 BOOL16 WINAPI ClassNext16( CLASSENTRY *pClassEntry )
1331 int i;
1332 CLASS *class = firstClass;
1334 TRACE("%p\n",pClassEntry);
1336 if (!pClassEntry->wNext) return FALSE;
1337 for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1338 if (!class)
1340 pClassEntry->wNext = 0;
1341 return FALSE;
1343 pClassEntry->hInst = class->hInstance;
1344 pClassEntry->wNext++;
1345 GlobalGetAtomNameA( class->atomName, pClassEntry->szClassName,
1346 sizeof(pClassEntry->szClassName) );
1347 return TRUE;
1349 #endif