user32: Delay registration of the builtin classes until the first window is created.
[wine.git] / dlls / user32 / class.c
blob333cb7641769ab9d1b38cb5eeb6e079eaafc89ff
1 /*
2 * Window classes functions
4 * Copyright 1993, 1996, 2003 Alexandre Julliard
5 * Copyright 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "winerror.h"
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "wine/unicode.h"
35 #include "win.h"
36 #include "user_private.h"
37 #include "controls.h"
38 #include "wine/server.h"
39 #include "wine/list.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(class);
44 #define MAX_ATOM_LEN 255 /* from dlls/kernel32/atom.c */
46 typedef struct tagCLASS
48 struct list entry; /* Entry in class list */
49 UINT style; /* Class style */
50 BOOL local; /* Local class? */
51 WNDPROC winproc; /* Window procedure */
52 INT cbClsExtra; /* Class extra bytes */
53 INT cbWndExtra; /* Window extra bytes */
54 LPWSTR menuName; /* Default menu name (Unicode followed by ASCII) */
55 struct dce *dce; /* Opaque pointer to class DCE */
56 HINSTANCE hInstance; /* Module that created the task */
57 HICON hIcon; /* Default icon */
58 HICON hIconSm; /* Default small icon */
59 HICON hIconSmIntern; /* Internal small icon, derived from hIcon */
60 HCURSOR hCursor; /* Default cursor */
61 HBRUSH hbrBackground; /* Default background */
62 ATOM atomName; /* Name of the class */
63 WCHAR name[MAX_ATOM_LEN + 1];
64 } CLASS;
66 static struct list class_list = LIST_INIT( class_list );
67 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
69 #define CLASS_OTHER_PROCESS ((CLASS *)1)
71 /***********************************************************************
72 * get_class_ptr
74 static CLASS *get_class_ptr( HWND hwnd, BOOL write_access )
76 WND *ptr = WIN_GetPtr( hwnd );
78 if (ptr)
80 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP) return ptr->class;
81 if (!write_access) return CLASS_OTHER_PROCESS;
83 /* modifying classes in other processes is not allowed */
84 if (ptr == WND_DESKTOP || IsWindow( hwnd ))
86 SetLastError( ERROR_ACCESS_DENIED );
87 return NULL;
90 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
91 return NULL;
95 /***********************************************************************
96 * release_class_ptr
98 static inline void release_class_ptr( CLASS *ptr )
100 USER_Unlock();
104 /***********************************************************************
105 * get_int_atom_value
107 ATOM get_int_atom_value( LPCWSTR name )
109 UINT ret = 0;
111 if (IS_INTRESOURCE(name)) return LOWORD(name);
112 if (*name++ != '#') return 0;
113 while (*name)
115 if (*name < '0' || *name > '9') return 0;
116 ret = ret * 10 + *name++ - '0';
117 if (ret > 0xffff) return 0;
119 return ret;
123 /***********************************************************************
124 * set_server_info
126 * Set class info with the wine server.
128 static BOOL set_server_info( HWND hwnd, INT offset, LONG_PTR newval, UINT size )
130 BOOL ret;
132 SERVER_START_REQ( set_class_info )
134 req->window = wine_server_user_handle( hwnd );
135 req->extra_offset = -1;
136 switch(offset)
138 case GCW_ATOM:
139 req->flags = SET_CLASS_ATOM;
140 req->atom = LOWORD(newval);
141 break;
142 case GCL_STYLE:
143 req->flags = SET_CLASS_STYLE;
144 req->style = newval;
145 break;
146 case GCL_CBWNDEXTRA:
147 req->flags = SET_CLASS_WINEXTRA;
148 req->win_extra = newval;
149 break;
150 case GCLP_HMODULE:
151 req->flags = SET_CLASS_INSTANCE;
152 req->instance = wine_server_client_ptr( (void *)newval );
153 break;
154 default:
155 assert( offset >= 0 );
156 req->flags = SET_CLASS_EXTRA;
157 req->extra_offset = offset;
158 req->extra_size = size;
159 if ( size == sizeof(LONG) )
161 LONG newlong = newval;
162 memcpy( &req->extra_value, &newlong, sizeof(LONG) );
164 else
165 memcpy( &req->extra_value, &newval, sizeof(LONG_PTR) );
166 break;
168 ret = !wine_server_call_err( req );
170 SERVER_END_REQ;
171 return ret;
175 /***********************************************************************
176 * CLASS_GetMenuNameA
178 * Get the menu name as a ASCII string.
180 static inline LPSTR CLASS_GetMenuNameA( CLASS *classPtr )
182 if (IS_INTRESOURCE(classPtr->menuName)) return (LPSTR)classPtr->menuName;
183 return (LPSTR)(classPtr->menuName + strlenW(classPtr->menuName) + 1);
187 /***********************************************************************
188 * CLASS_GetMenuNameW
190 * Get the menu name as a Unicode string.
192 static inline LPWSTR CLASS_GetMenuNameW( CLASS *classPtr )
194 return classPtr->menuName;
198 /***********************************************************************
199 * CLASS_SetMenuNameA
201 * Set the menu name in a class structure by copying the string.
203 static void CLASS_SetMenuNameA( CLASS *classPtr, LPCSTR name )
205 if (!IS_INTRESOURCE(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
206 if (!IS_INTRESOURCE(name))
208 DWORD lenA = strlen(name) + 1;
209 DWORD lenW = MultiByteToWideChar( CP_ACP, 0, name, lenA, NULL, 0 );
210 classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
211 MultiByteToWideChar( CP_ACP, 0, name, lenA, classPtr->menuName, lenW );
212 memcpy( classPtr->menuName + lenW, name, lenA );
214 else classPtr->menuName = (LPWSTR)name;
218 /***********************************************************************
219 * CLASS_SetMenuNameW
221 * Set the menu name in a class structure by copying the string.
223 static void CLASS_SetMenuNameW( CLASS *classPtr, LPCWSTR name )
225 if (!IS_INTRESOURCE(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
226 if (!IS_INTRESOURCE(name))
228 DWORD lenW = strlenW(name) + 1;
229 DWORD lenA = WideCharToMultiByte( CP_ACP, 0, name, lenW, NULL, 0, NULL, NULL );
230 classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
231 memcpy( classPtr->menuName, name, lenW*sizeof(WCHAR) );
232 WideCharToMultiByte( CP_ACP, 0, name, lenW,
233 (char *)(classPtr->menuName + lenW), lenA, NULL, NULL );
235 else classPtr->menuName = (LPWSTR)name;
239 /***********************************************************************
240 * CLASS_FreeClass
242 * Free a class structure.
244 static void CLASS_FreeClass( CLASS *classPtr )
246 TRACE("%p\n", classPtr);
248 USER_Lock();
250 if (classPtr->dce) free_dce( classPtr->dce, 0 );
251 list_remove( &classPtr->entry );
252 if (classPtr->hbrBackground > (HBRUSH)(COLOR_GRADIENTINACTIVECAPTION + 1))
253 DeleteObject( classPtr->hbrBackground );
254 HeapFree( GetProcessHeap(), 0, classPtr->menuName );
255 HeapFree( GetProcessHeap(), 0, classPtr );
256 USER_Unlock();
260 /***********************************************************************
261 * CLASS_FindClass
263 * Return a pointer to the class.
264 * hinstance has been normalized by the caller.
266 static CLASS *CLASS_FindClass( LPCWSTR name, HINSTANCE hinstance )
268 struct list *ptr;
269 ATOM atom = get_int_atom_value( name );
271 USER_Lock();
273 LIST_FOR_EACH( ptr, &class_list )
275 CLASS *class = LIST_ENTRY( ptr, CLASS, entry );
276 if (atom)
278 if (class->atomName != atom) continue;
280 else
282 if (!name || strcmpiW( class->name, name )) continue;
284 if (!hinstance || !class->local || class->hInstance == hinstance)
286 TRACE("%s %p -> %p\n", debugstr_w(name), hinstance, class);
287 return class;
290 USER_Unlock();
291 TRACE("%s %p -> not found\n", debugstr_w(name), hinstance);
292 return NULL;
296 /***********************************************************************
297 * CLASS_RegisterClass
299 * The real RegisterClass() functionality.
301 static CLASS *CLASS_RegisterClass( LPCWSTR name, HINSTANCE hInstance, BOOL local,
302 DWORD style, INT classExtra, INT winExtra )
304 CLASS *classPtr;
305 BOOL ret;
307 TRACE("name=%s hinst=%p style=0x%x clExtr=0x%x winExtr=0x%x\n",
308 debugstr_w(name), hInstance, style, classExtra, winExtra );
310 /* Fix the extra bytes value */
312 if (classExtra > 40) /* Extra bytes are limited to 40 in Win32 */
313 WARN("Class extra bytes %d is > 40\n", classExtra);
314 if (winExtra > 40) /* Extra bytes are limited to 40 in Win32 */
315 WARN("Win extra bytes %d is > 40\n", winExtra );
317 classPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLASS) + classExtra );
318 if (!classPtr) return NULL;
320 classPtr->atomName = get_int_atom_value( name );
321 if (!classPtr->atomName && name) strcpyW( classPtr->name, name );
322 else GlobalGetAtomNameW( classPtr->atomName, classPtr->name, sizeof(classPtr->name)/sizeof(WCHAR) );
324 SERVER_START_REQ( create_class )
326 req->local = local;
327 req->style = style;
328 req->instance = wine_server_client_ptr( hInstance );
329 req->extra = classExtra;
330 req->win_extra = winExtra;
331 req->client_ptr = wine_server_client_ptr( classPtr );
332 req->atom = classPtr->atomName;
333 if (!req->atom && name) wine_server_add_data( req, name, strlenW(name) * sizeof(WCHAR) );
334 ret = !wine_server_call_err( req );
335 classPtr->atomName = reply->atom;
337 SERVER_END_REQ;
338 if (!ret)
340 HeapFree( GetProcessHeap(), 0, classPtr );
341 return NULL;
344 classPtr->style = style;
345 classPtr->local = local;
346 classPtr->cbWndExtra = winExtra;
347 classPtr->cbClsExtra = classExtra;
348 classPtr->hInstance = hInstance;
350 /* Other non-null values must be set by caller */
352 USER_Lock();
353 if (local) list_add_head( &class_list, &classPtr->entry );
354 else list_add_tail( &class_list, &classPtr->entry );
355 return classPtr;
359 /***********************************************************************
360 * register_builtin
362 * Register a builtin control class.
363 * This allows having both ASCII and Unicode winprocs for the same class.
365 static void register_builtin( const struct builtin_class_descr *descr )
367 CLASS *classPtr;
369 if (!(classPtr = CLASS_RegisterClass( descr->name, user32_module, FALSE,
370 descr->style, 0, descr->extra ))) return;
372 classPtr->hCursor = LoadCursorA( 0, (LPSTR)descr->cursor );
373 classPtr->hbrBackground = descr->brush;
374 classPtr->winproc = BUILTIN_WINPROC( descr->proc );
375 release_class_ptr( classPtr );
379 /***********************************************************************
380 * register_builtins
382 static BOOL WINAPI register_builtins( INIT_ONCE *once, void *param, void **context )
384 register_builtin( &DESKTOP_builtin_class );
385 register_builtin( &BUTTON_builtin_class );
386 register_builtin( &COMBO_builtin_class );
387 register_builtin( &COMBOLBOX_builtin_class );
388 register_builtin( &DIALOG_builtin_class );
389 register_builtin( &EDIT_builtin_class );
390 register_builtin( &ICONTITLE_builtin_class );
391 register_builtin( &LISTBOX_builtin_class );
392 register_builtin( &MDICLIENT_builtin_class );
393 register_builtin( &MENU_builtin_class );
394 register_builtin( &MESSAGE_builtin_class );
395 register_builtin( &SCROLL_builtin_class );
396 register_builtin( &STATIC_builtin_class );
397 return TRUE;
401 /***********************************************************************
402 * CLASS_RegisterBuiltinClasses
404 void CLASS_RegisterBuiltinClasses(void)
406 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
410 /***********************************************************************
411 * get_class_winproc
413 WNDPROC get_class_winproc( CLASS *class )
415 return class->winproc;
419 /***********************************************************************
420 * get_class_dce
422 struct dce *get_class_dce( CLASS *class )
424 return class->dce;
428 /***********************************************************************
429 * set_class_dce
431 struct dce *set_class_dce( CLASS *class, struct dce *dce )
433 if (class->dce) return class->dce; /* already set, don't change it */
434 class->dce = dce;
435 return dce;
439 /***********************************************************************
440 * RegisterClassA (USER32.@)
442 * Register a window class.
444 * RETURNS
445 * >0: Unique identifier
446 * 0: Failure
448 ATOM WINAPI RegisterClassA( const WNDCLASSA* wc ) /* [in] Address of structure with class data */
450 WNDCLASSEXA wcex;
452 wcex.cbSize = sizeof(wcex);
453 wcex.style = wc->style;
454 wcex.lpfnWndProc = wc->lpfnWndProc;
455 wcex.cbClsExtra = wc->cbClsExtra;
456 wcex.cbWndExtra = wc->cbWndExtra;
457 wcex.hInstance = wc->hInstance;
458 wcex.hIcon = wc->hIcon;
459 wcex.hCursor = wc->hCursor;
460 wcex.hbrBackground = wc->hbrBackground;
461 wcex.lpszMenuName = wc->lpszMenuName;
462 wcex.lpszClassName = wc->lpszClassName;
463 wcex.hIconSm = 0;
464 return RegisterClassExA( &wcex );
468 /***********************************************************************
469 * RegisterClassW (USER32.@)
471 * See RegisterClassA.
473 ATOM WINAPI RegisterClassW( const WNDCLASSW* wc )
475 WNDCLASSEXW wcex;
477 wcex.cbSize = sizeof(wcex);
478 wcex.style = wc->style;
479 wcex.lpfnWndProc = wc->lpfnWndProc;
480 wcex.cbClsExtra = wc->cbClsExtra;
481 wcex.cbWndExtra = wc->cbWndExtra;
482 wcex.hInstance = wc->hInstance;
483 wcex.hIcon = wc->hIcon;
484 wcex.hCursor = wc->hCursor;
485 wcex.hbrBackground = wc->hbrBackground;
486 wcex.lpszMenuName = wc->lpszMenuName;
487 wcex.lpszClassName = wc->lpszClassName;
488 wcex.hIconSm = 0;
489 return RegisterClassExW( &wcex );
493 /***********************************************************************
494 * RegisterClassExA (USER32.@)
496 ATOM WINAPI RegisterClassExA( const WNDCLASSEXA* wc )
498 ATOM atom;
499 CLASS *classPtr;
500 HINSTANCE instance;
502 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
504 if (wc->cbSize != sizeof(*wc) || wc->cbClsExtra < 0 || wc->cbWndExtra < 0 ||
505 wc->hInstance == user32_module) /* we can't register a class for user32 */
507 SetLastError( ERROR_INVALID_PARAMETER );
508 return 0;
510 if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
512 if (!IS_INTRESOURCE(wc->lpszClassName))
514 WCHAR name[MAX_ATOM_LEN + 1];
516 if (!MultiByteToWideChar( CP_ACP, 0, wc->lpszClassName, -1, name, MAX_ATOM_LEN + 1 )) return 0;
517 classPtr = CLASS_RegisterClass( name, instance, !(wc->style & CS_GLOBALCLASS),
518 wc->style, wc->cbClsExtra, wc->cbWndExtra );
520 else
522 classPtr = CLASS_RegisterClass( (LPCWSTR)wc->lpszClassName, instance,
523 !(wc->style & CS_GLOBALCLASS), wc->style,
524 wc->cbClsExtra, wc->cbWndExtra );
526 if (!classPtr) return 0;
527 atom = classPtr->atomName;
529 TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
530 debugstr_a(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
531 wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
533 classPtr->hIcon = wc->hIcon;
534 classPtr->hIconSm = wc->hIconSm;
535 classPtr->hIconSmIntern = wc->hIcon && !wc->hIconSm ?
536 CopyImage( wc->hIcon, IMAGE_ICON,
537 GetSystemMetrics( SM_CXSMICON ),
538 GetSystemMetrics( SM_CYSMICON ), 0 ) : NULL;
539 classPtr->hCursor = wc->hCursor;
540 classPtr->hbrBackground = wc->hbrBackground;
541 classPtr->winproc = WINPROC_AllocProc( wc->lpfnWndProc, FALSE );
542 CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
543 release_class_ptr( classPtr );
544 return atom;
548 /***********************************************************************
549 * RegisterClassExW (USER32.@)
551 ATOM WINAPI RegisterClassExW( const WNDCLASSEXW* wc )
553 ATOM atom;
554 CLASS *classPtr;
555 HINSTANCE instance;
557 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
559 if (wc->cbSize != sizeof(*wc) || wc->cbClsExtra < 0 || wc->cbWndExtra < 0 ||
560 wc->hInstance == user32_module) /* we can't register a class for user32 */
562 SetLastError( ERROR_INVALID_PARAMETER );
563 return 0;
565 if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
567 if (!(classPtr = CLASS_RegisterClass( wc->lpszClassName, instance, !(wc->style & CS_GLOBALCLASS),
568 wc->style, wc->cbClsExtra, wc->cbWndExtra )))
569 return 0;
571 atom = classPtr->atomName;
573 TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
574 debugstr_w(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
575 wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
577 classPtr->hIcon = wc->hIcon;
578 classPtr->hIconSm = wc->hIconSm;
579 classPtr->hIconSmIntern = wc->hIcon && !wc->hIconSm ?
580 CopyImage( wc->hIcon, IMAGE_ICON,
581 GetSystemMetrics( SM_CXSMICON ),
582 GetSystemMetrics( SM_CYSMICON ), 0 ) : NULL;
583 classPtr->hCursor = wc->hCursor;
584 classPtr->hbrBackground = wc->hbrBackground;
585 classPtr->winproc = WINPROC_AllocProc( wc->lpfnWndProc, TRUE );
586 CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
587 release_class_ptr( classPtr );
588 return atom;
592 /***********************************************************************
593 * UnregisterClassA (USER32.@)
595 BOOL WINAPI UnregisterClassA( LPCSTR className, HINSTANCE hInstance )
597 if (!IS_INTRESOURCE(className))
599 WCHAR name[MAX_ATOM_LEN + 1];
601 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, name, MAX_ATOM_LEN + 1 ))
602 return FALSE;
603 return UnregisterClassW( name, hInstance );
605 return UnregisterClassW( (LPCWSTR)className, hInstance );
608 /***********************************************************************
609 * UnregisterClassW (USER32.@)
611 BOOL WINAPI UnregisterClassW( LPCWSTR className, HINSTANCE hInstance )
613 CLASS *classPtr = NULL;
615 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
617 SERVER_START_REQ( destroy_class )
619 req->instance = wine_server_client_ptr( hInstance );
620 if (!(req->atom = get_int_atom_value(className)) && className)
621 wine_server_add_data( req, className, strlenW(className) * sizeof(WCHAR) );
622 if (!wine_server_call_err( req )) classPtr = wine_server_get_ptr( reply->client_ptr );
624 SERVER_END_REQ;
626 if (classPtr) CLASS_FreeClass( classPtr );
627 return (classPtr != NULL);
631 /***********************************************************************
632 * GetClassWord (USER32.@)
634 WORD WINAPI GetClassWord( HWND hwnd, INT offset )
636 CLASS *class;
637 WORD retvalue = 0;
639 if (offset < 0) return GetClassLongA( hwnd, offset );
641 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
643 if (class == CLASS_OTHER_PROCESS)
645 SERVER_START_REQ( set_class_info )
647 req->window = wine_server_user_handle( hwnd );
648 req->flags = 0;
649 req->extra_offset = offset;
650 req->extra_size = sizeof(retvalue);
651 if (!wine_server_call_err( req ))
652 memcpy( &retvalue, &reply->old_extra_value, sizeof(retvalue) );
654 SERVER_END_REQ;
655 return retvalue;
658 if (offset <= class->cbClsExtra - sizeof(WORD))
659 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(retvalue) );
660 else
661 SetLastError( ERROR_INVALID_INDEX );
662 release_class_ptr( class );
663 return retvalue;
667 /***********************************************************************
668 * CLASS_GetClassLong
670 * Implementation of GetClassLong(Ptr)A/W
672 static ULONG_PTR CLASS_GetClassLong( HWND hwnd, INT offset, UINT size,
673 BOOL unicode )
675 CLASS *class;
676 ULONG_PTR retvalue = 0;
678 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
680 if (class == CLASS_OTHER_PROCESS)
682 SERVER_START_REQ( set_class_info )
684 req->window = wine_server_user_handle( hwnd );
685 req->flags = 0;
686 req->extra_offset = (offset >= 0) ? offset : -1;
687 req->extra_size = (offset >= 0) ? size : 0;
688 if (!wine_server_call_err( req ))
690 switch(offset)
692 case GCLP_HBRBACKGROUND:
693 case GCLP_HCURSOR:
694 case GCLP_HICON:
695 case GCLP_HICONSM:
696 case GCLP_WNDPROC:
697 case GCLP_MENUNAME:
698 FIXME( "offset %d (%s) not supported on other process window %p\n",
699 offset, SPY_GetClassLongOffsetName(offset), hwnd );
700 SetLastError( ERROR_INVALID_HANDLE );
701 break;
702 case GCL_STYLE:
703 retvalue = reply->old_style;
704 break;
705 case GCL_CBWNDEXTRA:
706 retvalue = reply->old_win_extra;
707 break;
708 case GCL_CBCLSEXTRA:
709 retvalue = reply->old_extra;
710 break;
711 case GCLP_HMODULE:
712 retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
713 break;
714 case GCW_ATOM:
715 retvalue = reply->old_atom;
716 break;
717 default:
718 if (offset >= 0)
720 if (size == sizeof(DWORD))
722 DWORD retdword;
723 memcpy( &retdword, &reply->old_extra_value, sizeof(DWORD) );
724 retvalue = retdword;
726 else
727 memcpy( &retvalue, &reply->old_extra_value,
728 sizeof(ULONG_PTR) );
730 else SetLastError( ERROR_INVALID_INDEX );
731 break;
735 SERVER_END_REQ;
736 return retvalue;
739 if (offset >= 0)
741 if (offset <= class->cbClsExtra - size)
743 if (size == sizeof(DWORD))
745 DWORD retdword;
746 memcpy( &retdword, (char *)(class + 1) + offset, sizeof(DWORD) );
747 retvalue = retdword;
749 else
750 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(ULONG_PTR) );
752 else
753 SetLastError( ERROR_INVALID_INDEX );
754 release_class_ptr( class );
755 return retvalue;
758 switch(offset)
760 case GCLP_HBRBACKGROUND:
761 retvalue = (ULONG_PTR)class->hbrBackground;
762 break;
763 case GCLP_HCURSOR:
764 retvalue = (ULONG_PTR)class->hCursor;
765 break;
766 case GCLP_HICON:
767 retvalue = (ULONG_PTR)class->hIcon;
768 break;
769 case GCLP_HICONSM:
770 retvalue = (ULONG_PTR)(class->hIconSm ? class->hIconSm : class->hIconSmIntern);
771 break;
772 case GCL_STYLE:
773 retvalue = class->style;
774 break;
775 case GCL_CBWNDEXTRA:
776 retvalue = class->cbWndExtra;
777 break;
778 case GCL_CBCLSEXTRA:
779 retvalue = class->cbClsExtra;
780 break;
781 case GCLP_HMODULE:
782 retvalue = (ULONG_PTR)class->hInstance;
783 break;
784 case GCLP_WNDPROC:
785 retvalue = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
786 break;
787 case GCLP_MENUNAME:
788 retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
789 if (unicode)
790 retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
791 else
792 retvalue = (ULONG_PTR)CLASS_GetMenuNameA( class );
793 break;
794 case GCW_ATOM:
795 retvalue = class->atomName;
796 break;
797 default:
798 SetLastError( ERROR_INVALID_INDEX );
799 break;
801 release_class_ptr( class );
802 return retvalue;
806 /***********************************************************************
807 * GetClassLongW (USER32.@)
809 DWORD WINAPI GetClassLongW( HWND hwnd, INT offset )
811 return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), TRUE );
816 /***********************************************************************
817 * GetClassLongA (USER32.@)
819 DWORD WINAPI GetClassLongA( HWND hwnd, INT offset )
821 return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), FALSE );
825 /***********************************************************************
826 * SetClassWord (USER32.@)
828 WORD WINAPI SetClassWord( HWND hwnd, INT offset, WORD newval )
830 CLASS *class;
831 WORD retval = 0;
833 if (offset < 0) return SetClassLongA( hwnd, offset, (DWORD)newval );
835 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
837 SERVER_START_REQ( set_class_info )
839 req->window = wine_server_user_handle( hwnd );
840 req->flags = SET_CLASS_EXTRA;
841 req->extra_offset = offset;
842 req->extra_size = sizeof(newval);
843 memcpy( &req->extra_value, &newval, sizeof(newval) );
844 if (!wine_server_call_err( req ))
846 void *ptr = (char *)(class + 1) + offset;
847 memcpy( &retval, ptr, sizeof(retval) );
848 memcpy( ptr, &newval, sizeof(newval) );
851 SERVER_END_REQ;
852 release_class_ptr( class );
853 return retval;
857 /***********************************************************************
858 * CLASS_SetClassLong
860 * Implementation of SetClassLong(Ptr)A/W
862 static ULONG_PTR CLASS_SetClassLong( HWND hwnd, INT offset, LONG_PTR newval,
863 UINT size, BOOL unicode )
865 CLASS *class;
866 ULONG_PTR retval = 0;
868 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
870 if (offset >= 0)
872 if (set_server_info( hwnd, offset, newval, size ))
874 void *ptr = (char *)(class + 1) + offset;
875 if ( size == sizeof(LONG) )
877 DWORD retdword;
878 LONG newlong = newval;
879 memcpy( &retdword, ptr, sizeof(DWORD) );
880 memcpy( ptr, &newlong, sizeof(LONG) );
881 retval = retdword;
883 else
885 memcpy( &retval, ptr, sizeof(ULONG_PTR) );
886 memcpy( ptr, &newval, sizeof(LONG_PTR) );
890 else switch(offset)
892 case GCLP_MENUNAME:
893 if ( unicode )
894 CLASS_SetMenuNameW( class, (LPCWSTR)newval );
895 else
896 CLASS_SetMenuNameA( class, (LPCSTR)newval );
897 retval = 0; /* Old value is now meaningless anyway */
898 break;
899 case GCLP_WNDPROC:
900 retval = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
901 class->winproc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
902 break;
903 case GCLP_HBRBACKGROUND:
904 retval = (ULONG_PTR)class->hbrBackground;
905 class->hbrBackground = (HBRUSH)newval;
906 break;
907 case GCLP_HCURSOR:
908 retval = (ULONG_PTR)class->hCursor;
909 class->hCursor = (HCURSOR)newval;
910 break;
911 case GCLP_HICON:
912 retval = (ULONG_PTR)class->hIcon;
913 if (retval && class->hIconSmIntern)
915 DestroyIcon(class->hIconSmIntern);
916 class->hIconSmIntern = NULL;
918 if (newval && !class->hIconSm)
919 class->hIconSmIntern = CopyImage( (HICON)newval, IMAGE_ICON,
920 GetSystemMetrics( SM_CXSMICON ), GetSystemMetrics( SM_CYSMICON ), 0 );
921 class->hIcon = (HICON)newval;
922 break;
923 case GCLP_HICONSM:
924 retval = (ULONG_PTR)class->hIconSm;
925 if (retval && !newval)
926 class->hIconSmIntern = class->hIcon ? CopyImage( class->hIcon, IMAGE_ICON,
927 GetSystemMetrics( SM_CXSMICON ),
928 GetSystemMetrics( SM_CYSMICON ), 0 ) : NULL;
929 else if (!retval && newval && class->hIconSmIntern)
931 DestroyIcon(class->hIconSmIntern);
932 class->hIconSmIntern = NULL;
934 class->hIconSm = (HICON)newval;
935 break;
936 case GCL_STYLE:
937 if (!set_server_info( hwnd, offset, newval, size )) break;
938 retval = class->style;
939 class->style = newval;
940 break;
941 case GCL_CBWNDEXTRA:
942 if (!set_server_info( hwnd, offset, newval, size )) break;
943 retval = class->cbWndExtra;
944 class->cbWndExtra = newval;
945 break;
946 case GCLP_HMODULE:
947 if (!set_server_info( hwnd, offset, newval, size )) break;
948 retval = (ULONG_PTR)class->hInstance;
949 class->hInstance = (HINSTANCE)newval;
950 break;
951 case GCW_ATOM:
952 if (!set_server_info( hwnd, offset, newval, size )) break;
953 retval = class->atomName;
954 class->atomName = newval;
955 GlobalGetAtomNameW( newval, class->name, sizeof(class->name)/sizeof(WCHAR) );
956 break;
957 case GCL_CBCLSEXTRA: /* cannot change this one */
958 SetLastError( ERROR_INVALID_PARAMETER );
959 break;
960 default:
961 SetLastError( ERROR_INVALID_INDEX );
962 break;
964 release_class_ptr( class );
965 return retval;
969 /***********************************************************************
970 * SetClassLongW (USER32.@)
972 DWORD WINAPI SetClassLongW( HWND hwnd, INT offset, LONG newval )
974 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), TRUE );
978 /***********************************************************************
979 * SetClassLongA (USER32.@)
981 DWORD WINAPI SetClassLongA( HWND hwnd, INT offset, LONG newval )
983 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), FALSE );
987 /***********************************************************************
988 * GetClassNameA (USER32.@)
990 INT WINAPI GetClassNameA( HWND hwnd, LPSTR buffer, INT count )
992 WCHAR tmpbuf[MAX_ATOM_LEN + 1];
993 DWORD len;
995 if (count <= 0) return 0;
996 if (!GetClassNameW( hwnd, tmpbuf, sizeof(tmpbuf)/sizeof(WCHAR) )) return 0;
997 RtlUnicodeToMultiByteN( buffer, count - 1, &len, tmpbuf, strlenW(tmpbuf) * sizeof(WCHAR) );
998 buffer[len] = 0;
999 return len;
1003 /***********************************************************************
1004 * GetClassNameW (USER32.@)
1006 INT WINAPI GetClassNameW( HWND hwnd, LPWSTR buffer, INT count )
1008 CLASS *class;
1009 INT ret;
1011 TRACE("%p %p %d\n", hwnd, buffer, count);
1013 if (count <= 0) return 0;
1015 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
1017 if (class == CLASS_OTHER_PROCESS)
1019 WCHAR tmpbuf[MAX_ATOM_LEN + 1];
1021 ret = GlobalGetAtomNameW( GetClassLongW( hwnd, GCW_ATOM ), tmpbuf, MAX_ATOM_LEN + 1 );
1022 if (ret)
1024 ret = min(count - 1, ret);
1025 memcpy(buffer, tmpbuf, ret * sizeof(WCHAR));
1026 buffer[ret] = 0;
1029 else
1031 lstrcpynW( buffer, class->name, count );
1032 release_class_ptr( class );
1033 ret = strlenW( buffer );
1035 return ret;
1039 /***********************************************************************
1040 * RealGetWindowClassA (USER32.@)
1042 UINT WINAPI RealGetWindowClassA( HWND hwnd, LPSTR buffer, UINT count )
1044 return GetClassNameA( hwnd, buffer, count );
1048 /***********************************************************************
1049 * RealGetWindowClassW (USER32.@)
1051 UINT WINAPI RealGetWindowClassW( HWND hwnd, LPWSTR buffer, UINT count )
1053 return GetClassNameW( hwnd, buffer, count );
1057 /***********************************************************************
1058 * GetClassInfoA (USER32.@)
1060 BOOL WINAPI GetClassInfoA( HINSTANCE hInstance, LPCSTR name, WNDCLASSA *wc )
1062 WNDCLASSEXA wcex;
1063 UINT ret = GetClassInfoExA( hInstance, name, &wcex );
1065 if (ret)
1067 wc->style = wcex.style;
1068 wc->lpfnWndProc = wcex.lpfnWndProc;
1069 wc->cbClsExtra = wcex.cbClsExtra;
1070 wc->cbWndExtra = wcex.cbWndExtra;
1071 wc->hInstance = wcex.hInstance;
1072 wc->hIcon = wcex.hIcon;
1073 wc->hCursor = wcex.hCursor;
1074 wc->hbrBackground = wcex.hbrBackground;
1075 wc->lpszMenuName = wcex.lpszMenuName;
1076 wc->lpszClassName = wcex.lpszClassName;
1078 return ret;
1082 /***********************************************************************
1083 * GetClassInfoW (USER32.@)
1085 BOOL WINAPI GetClassInfoW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSW *wc )
1087 WNDCLASSEXW wcex;
1088 UINT ret = GetClassInfoExW( hInstance, name, &wcex );
1090 if (ret)
1092 wc->style = wcex.style;
1093 wc->lpfnWndProc = wcex.lpfnWndProc;
1094 wc->cbClsExtra = wcex.cbClsExtra;
1095 wc->cbWndExtra = wcex.cbWndExtra;
1096 wc->hInstance = wcex.hInstance;
1097 wc->hIcon = wcex.hIcon;
1098 wc->hCursor = wcex.hCursor;
1099 wc->hbrBackground = wcex.hbrBackground;
1100 wc->lpszMenuName = wcex.lpszMenuName;
1101 wc->lpszClassName = wcex.lpszClassName;
1103 return ret;
1107 /***********************************************************************
1108 * GetClassInfoExA (USER32.@)
1110 BOOL WINAPI GetClassInfoExA( HINSTANCE hInstance, LPCSTR name, WNDCLASSEXA *wc )
1112 ATOM atom;
1113 CLASS *classPtr;
1115 TRACE("%p %s %p\n", hInstance, debugstr_a(name), wc);
1117 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
1119 if (!wc)
1121 SetLastError( ERROR_NOACCESS );
1122 return FALSE;
1125 if (!hInstance) hInstance = user32_module;
1127 if (!IS_INTRESOURCE(name))
1129 WCHAR nameW[MAX_ATOM_LEN + 1];
1130 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, sizeof(nameW)/sizeof(WCHAR) ))
1131 return FALSE;
1132 classPtr = CLASS_FindClass( nameW, hInstance );
1134 else classPtr = CLASS_FindClass( (LPCWSTR)name, hInstance );
1136 if (!classPtr)
1138 SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1139 return FALSE;
1141 wc->style = classPtr->style;
1142 wc->lpfnWndProc = WINPROC_GetProc( classPtr->winproc, FALSE );
1143 wc->cbClsExtra = classPtr->cbClsExtra;
1144 wc->cbWndExtra = classPtr->cbWndExtra;
1145 wc->hInstance = (hInstance == user32_module) ? 0 : hInstance;
1146 wc->hIcon = classPtr->hIcon;
1147 wc->hIconSm = classPtr->hIconSm ? classPtr->hIconSm : classPtr->hIconSmIntern;
1148 wc->hCursor = classPtr->hCursor;
1149 wc->hbrBackground = classPtr->hbrBackground;
1150 wc->lpszMenuName = CLASS_GetMenuNameA( classPtr );
1151 wc->lpszClassName = name;
1152 atom = classPtr->atomName;
1153 release_class_ptr( classPtr );
1155 /* We must return the atom of the class here instead of just TRUE. */
1156 return atom;
1160 /***********************************************************************
1161 * GetClassInfoExW (USER32.@)
1163 BOOL WINAPI GetClassInfoExW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSEXW *wc )
1165 ATOM atom;
1166 CLASS *classPtr;
1168 TRACE("%p %s %p\n", hInstance, debugstr_w(name), wc);
1170 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
1172 if (!wc)
1174 SetLastError( ERROR_NOACCESS );
1175 return FALSE;
1178 if (!hInstance) hInstance = user32_module;
1180 if (!(classPtr = CLASS_FindClass( name, hInstance )))
1182 SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1183 return FALSE;
1185 wc->style = classPtr->style;
1186 wc->lpfnWndProc = WINPROC_GetProc( classPtr->winproc, TRUE );
1187 wc->cbClsExtra = classPtr->cbClsExtra;
1188 wc->cbWndExtra = classPtr->cbWndExtra;
1189 wc->hInstance = (hInstance == user32_module) ? 0 : hInstance;
1190 wc->hIcon = classPtr->hIcon;
1191 wc->hIconSm = classPtr->hIconSm ? classPtr->hIconSm : classPtr->hIconSmIntern;
1192 wc->hCursor = classPtr->hCursor;
1193 wc->hbrBackground = classPtr->hbrBackground;
1194 wc->lpszMenuName = CLASS_GetMenuNameW( classPtr );
1195 wc->lpszClassName = name;
1196 atom = classPtr->atomName;
1197 release_class_ptr( classPtr );
1199 /* We must return the atom of the class here instead of just TRUE. */
1200 return atom;
1204 #if 0 /* toolhelp is in kernel, so this cannot work */
1206 /***********************************************************************
1207 * ClassFirst (TOOLHELP.69)
1209 BOOL16 WINAPI ClassFirst16( CLASSENTRY *pClassEntry )
1211 TRACE("%p\n",pClassEntry);
1212 pClassEntry->wNext = 1;
1213 return ClassNext16( pClassEntry );
1217 /***********************************************************************
1218 * ClassNext (TOOLHELP.70)
1220 BOOL16 WINAPI ClassNext16( CLASSENTRY *pClassEntry )
1222 int i;
1223 CLASS *class = firstClass;
1225 TRACE("%p\n",pClassEntry);
1227 if (!pClassEntry->wNext) return FALSE;
1228 for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1229 if (!class)
1231 pClassEntry->wNext = 0;
1232 return FALSE;
1234 pClassEntry->hInst = class->hInstance;
1235 pClassEntry->wNext++;
1236 GlobalGetAtomNameA( class->atomName, pClassEntry->szClassName,
1237 sizeof(pClassEntry->szClassName) );
1238 return TRUE;
1240 #endif
1242 /* 64bit versions */
1244 #ifdef GetClassLongPtrA
1245 #undef GetClassLongPtrA
1246 #endif
1248 #ifdef GetClassLongPtrW
1249 #undef GetClassLongPtrW
1250 #endif
1252 #ifdef SetClassLongPtrA
1253 #undef SetClassLongPtrA
1254 #endif
1256 #ifdef SetClassLongPtrW
1257 #undef SetClassLongPtrW
1258 #endif
1260 /***********************************************************************
1261 * GetClassLongPtrA (USER32.@)
1263 ULONG_PTR WINAPI GetClassLongPtrA( HWND hwnd, INT offset )
1265 return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), FALSE );
1268 /***********************************************************************
1269 * GetClassLongPtrW (USER32.@)
1271 ULONG_PTR WINAPI GetClassLongPtrW( HWND hwnd, INT offset )
1273 return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), TRUE );
1276 /***********************************************************************
1277 * SetClassLongPtrW (USER32.@)
1279 ULONG_PTR WINAPI SetClassLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
1281 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), TRUE );
1284 /***********************************************************************
1285 * SetClassLongPtrA (USER32.@)
1287 ULONG_PTR WINAPI SetClassLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
1289 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), FALSE );