user32: Register classes once the graphics driver has been loaded, except for the...
[wine.git] / dlls / user32 / class.c
blobe03272fd616cf189be67aa42f51d3a386f5c909c
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 if (descr->cursor) 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( &BUTTON_builtin_class );
385 register_builtin( &COMBO_builtin_class );
386 register_builtin( &COMBOLBOX_builtin_class );
387 register_builtin( &DIALOG_builtin_class );
388 register_builtin( &EDIT_builtin_class );
389 register_builtin( &ICONTITLE_builtin_class );
390 register_builtin( &LISTBOX_builtin_class );
391 register_builtin( &MDICLIENT_builtin_class );
392 register_builtin( &MENU_builtin_class );
393 register_builtin( &SCROLL_builtin_class );
394 register_builtin( &STATIC_builtin_class );
395 return TRUE;
399 /***********************************************************************
400 * register_builtin_classes
402 void register_builtin_classes(void)
404 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
408 /***********************************************************************
409 * register_desktop_class
411 void register_desktop_class(void)
413 register_builtin( &DESKTOP_builtin_class );
414 register_builtin( &MESSAGE_builtin_class );
418 /***********************************************************************
419 * get_class_winproc
421 WNDPROC get_class_winproc( CLASS *class )
423 return class->winproc;
427 /***********************************************************************
428 * get_class_dce
430 struct dce *get_class_dce( CLASS *class )
432 return class->dce;
436 /***********************************************************************
437 * set_class_dce
439 struct dce *set_class_dce( CLASS *class, struct dce *dce )
441 if (class->dce) return class->dce; /* already set, don't change it */
442 class->dce = dce;
443 return dce;
447 /***********************************************************************
448 * RegisterClassA (USER32.@)
450 * Register a window class.
452 * RETURNS
453 * >0: Unique identifier
454 * 0: Failure
456 ATOM WINAPI RegisterClassA( const WNDCLASSA* wc ) /* [in] Address of structure with class data */
458 WNDCLASSEXA wcex;
460 wcex.cbSize = sizeof(wcex);
461 wcex.style = wc->style;
462 wcex.lpfnWndProc = wc->lpfnWndProc;
463 wcex.cbClsExtra = wc->cbClsExtra;
464 wcex.cbWndExtra = wc->cbWndExtra;
465 wcex.hInstance = wc->hInstance;
466 wcex.hIcon = wc->hIcon;
467 wcex.hCursor = wc->hCursor;
468 wcex.hbrBackground = wc->hbrBackground;
469 wcex.lpszMenuName = wc->lpszMenuName;
470 wcex.lpszClassName = wc->lpszClassName;
471 wcex.hIconSm = 0;
472 return RegisterClassExA( &wcex );
476 /***********************************************************************
477 * RegisterClassW (USER32.@)
479 * See RegisterClassA.
481 ATOM WINAPI RegisterClassW( const WNDCLASSW* wc )
483 WNDCLASSEXW wcex;
485 wcex.cbSize = sizeof(wcex);
486 wcex.style = wc->style;
487 wcex.lpfnWndProc = wc->lpfnWndProc;
488 wcex.cbClsExtra = wc->cbClsExtra;
489 wcex.cbWndExtra = wc->cbWndExtra;
490 wcex.hInstance = wc->hInstance;
491 wcex.hIcon = wc->hIcon;
492 wcex.hCursor = wc->hCursor;
493 wcex.hbrBackground = wc->hbrBackground;
494 wcex.lpszMenuName = wc->lpszMenuName;
495 wcex.lpszClassName = wc->lpszClassName;
496 wcex.hIconSm = 0;
497 return RegisterClassExW( &wcex );
501 /***********************************************************************
502 * RegisterClassExA (USER32.@)
504 ATOM WINAPI RegisterClassExA( const WNDCLASSEXA* wc )
506 ATOM atom;
507 CLASS *classPtr;
508 HINSTANCE instance;
510 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
512 if (wc->cbSize != sizeof(*wc) || wc->cbClsExtra < 0 || wc->cbWndExtra < 0 ||
513 wc->hInstance == user32_module) /* we can't register a class for user32 */
515 SetLastError( ERROR_INVALID_PARAMETER );
516 return 0;
518 if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
520 if (!IS_INTRESOURCE(wc->lpszClassName))
522 WCHAR name[MAX_ATOM_LEN + 1];
524 if (!MultiByteToWideChar( CP_ACP, 0, wc->lpszClassName, -1, name, MAX_ATOM_LEN + 1 )) return 0;
525 classPtr = CLASS_RegisterClass( name, instance, !(wc->style & CS_GLOBALCLASS),
526 wc->style, wc->cbClsExtra, wc->cbWndExtra );
528 else
530 classPtr = CLASS_RegisterClass( (LPCWSTR)wc->lpszClassName, instance,
531 !(wc->style & CS_GLOBALCLASS), wc->style,
532 wc->cbClsExtra, wc->cbWndExtra );
534 if (!classPtr) return 0;
535 atom = classPtr->atomName;
537 TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
538 debugstr_a(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
539 wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
541 classPtr->hIcon = wc->hIcon;
542 classPtr->hIconSm = wc->hIconSm;
543 classPtr->hIconSmIntern = wc->hIcon && !wc->hIconSm ?
544 CopyImage( wc->hIcon, IMAGE_ICON,
545 GetSystemMetrics( SM_CXSMICON ),
546 GetSystemMetrics( SM_CYSMICON ), 0 ) : NULL;
547 classPtr->hCursor = wc->hCursor;
548 classPtr->hbrBackground = wc->hbrBackground;
549 classPtr->winproc = WINPROC_AllocProc( wc->lpfnWndProc, FALSE );
550 CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
551 release_class_ptr( classPtr );
552 return atom;
556 /***********************************************************************
557 * RegisterClassExW (USER32.@)
559 ATOM WINAPI RegisterClassExW( const WNDCLASSEXW* wc )
561 ATOM atom;
562 CLASS *classPtr;
563 HINSTANCE instance;
565 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
567 if (wc->cbSize != sizeof(*wc) || wc->cbClsExtra < 0 || wc->cbWndExtra < 0 ||
568 wc->hInstance == user32_module) /* we can't register a class for user32 */
570 SetLastError( ERROR_INVALID_PARAMETER );
571 return 0;
573 if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
575 if (!(classPtr = CLASS_RegisterClass( wc->lpszClassName, instance, !(wc->style & CS_GLOBALCLASS),
576 wc->style, wc->cbClsExtra, wc->cbWndExtra )))
577 return 0;
579 atom = classPtr->atomName;
581 TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
582 debugstr_w(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
583 wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
585 classPtr->hIcon = wc->hIcon;
586 classPtr->hIconSm = wc->hIconSm;
587 classPtr->hIconSmIntern = wc->hIcon && !wc->hIconSm ?
588 CopyImage( wc->hIcon, IMAGE_ICON,
589 GetSystemMetrics( SM_CXSMICON ),
590 GetSystemMetrics( SM_CYSMICON ), 0 ) : NULL;
591 classPtr->hCursor = wc->hCursor;
592 classPtr->hbrBackground = wc->hbrBackground;
593 classPtr->winproc = WINPROC_AllocProc( wc->lpfnWndProc, TRUE );
594 CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
595 release_class_ptr( classPtr );
596 return atom;
600 /***********************************************************************
601 * UnregisterClassA (USER32.@)
603 BOOL WINAPI UnregisterClassA( LPCSTR className, HINSTANCE hInstance )
605 if (!IS_INTRESOURCE(className))
607 WCHAR name[MAX_ATOM_LEN + 1];
609 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, name, MAX_ATOM_LEN + 1 ))
610 return FALSE;
611 return UnregisterClassW( name, hInstance );
613 return UnregisterClassW( (LPCWSTR)className, hInstance );
616 /***********************************************************************
617 * UnregisterClassW (USER32.@)
619 BOOL WINAPI UnregisterClassW( LPCWSTR className, HINSTANCE hInstance )
621 CLASS *classPtr = NULL;
623 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
625 SERVER_START_REQ( destroy_class )
627 req->instance = wine_server_client_ptr( hInstance );
628 if (!(req->atom = get_int_atom_value(className)) && className)
629 wine_server_add_data( req, className, strlenW(className) * sizeof(WCHAR) );
630 if (!wine_server_call_err( req )) classPtr = wine_server_get_ptr( reply->client_ptr );
632 SERVER_END_REQ;
634 if (classPtr) CLASS_FreeClass( classPtr );
635 return (classPtr != NULL);
639 /***********************************************************************
640 * GetClassWord (USER32.@)
642 WORD WINAPI GetClassWord( HWND hwnd, INT offset )
644 CLASS *class;
645 WORD retvalue = 0;
647 if (offset < 0) return GetClassLongA( hwnd, offset );
649 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
651 if (class == CLASS_OTHER_PROCESS)
653 SERVER_START_REQ( set_class_info )
655 req->window = wine_server_user_handle( hwnd );
656 req->flags = 0;
657 req->extra_offset = offset;
658 req->extra_size = sizeof(retvalue);
659 if (!wine_server_call_err( req ))
660 memcpy( &retvalue, &reply->old_extra_value, sizeof(retvalue) );
662 SERVER_END_REQ;
663 return retvalue;
666 if (offset <= class->cbClsExtra - sizeof(WORD))
667 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(retvalue) );
668 else
669 SetLastError( ERROR_INVALID_INDEX );
670 release_class_ptr( class );
671 return retvalue;
675 /***********************************************************************
676 * CLASS_GetClassLong
678 * Implementation of GetClassLong(Ptr)A/W
680 static ULONG_PTR CLASS_GetClassLong( HWND hwnd, INT offset, UINT size,
681 BOOL unicode )
683 CLASS *class;
684 ULONG_PTR retvalue = 0;
686 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
688 if (class == CLASS_OTHER_PROCESS)
690 SERVER_START_REQ( set_class_info )
692 req->window = wine_server_user_handle( hwnd );
693 req->flags = 0;
694 req->extra_offset = (offset >= 0) ? offset : -1;
695 req->extra_size = (offset >= 0) ? size : 0;
696 if (!wine_server_call_err( req ))
698 switch(offset)
700 case GCLP_HBRBACKGROUND:
701 case GCLP_HCURSOR:
702 case GCLP_HICON:
703 case GCLP_HICONSM:
704 case GCLP_WNDPROC:
705 case GCLP_MENUNAME:
706 FIXME( "offset %d (%s) not supported on other process window %p\n",
707 offset, SPY_GetClassLongOffsetName(offset), hwnd );
708 SetLastError( ERROR_INVALID_HANDLE );
709 break;
710 case GCL_STYLE:
711 retvalue = reply->old_style;
712 break;
713 case GCL_CBWNDEXTRA:
714 retvalue = reply->old_win_extra;
715 break;
716 case GCL_CBCLSEXTRA:
717 retvalue = reply->old_extra;
718 break;
719 case GCLP_HMODULE:
720 retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
721 break;
722 case GCW_ATOM:
723 retvalue = reply->old_atom;
724 break;
725 default:
726 if (offset >= 0)
728 if (size == sizeof(DWORD))
730 DWORD retdword;
731 memcpy( &retdword, &reply->old_extra_value, sizeof(DWORD) );
732 retvalue = retdword;
734 else
735 memcpy( &retvalue, &reply->old_extra_value,
736 sizeof(ULONG_PTR) );
738 else SetLastError( ERROR_INVALID_INDEX );
739 break;
743 SERVER_END_REQ;
744 return retvalue;
747 if (offset >= 0)
749 if (offset <= class->cbClsExtra - size)
751 if (size == sizeof(DWORD))
753 DWORD retdword;
754 memcpy( &retdword, (char *)(class + 1) + offset, sizeof(DWORD) );
755 retvalue = retdword;
757 else
758 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(ULONG_PTR) );
760 else
761 SetLastError( ERROR_INVALID_INDEX );
762 release_class_ptr( class );
763 return retvalue;
766 switch(offset)
768 case GCLP_HBRBACKGROUND:
769 retvalue = (ULONG_PTR)class->hbrBackground;
770 break;
771 case GCLP_HCURSOR:
772 retvalue = (ULONG_PTR)class->hCursor;
773 break;
774 case GCLP_HICON:
775 retvalue = (ULONG_PTR)class->hIcon;
776 break;
777 case GCLP_HICONSM:
778 retvalue = (ULONG_PTR)(class->hIconSm ? class->hIconSm : class->hIconSmIntern);
779 break;
780 case GCL_STYLE:
781 retvalue = class->style;
782 break;
783 case GCL_CBWNDEXTRA:
784 retvalue = class->cbWndExtra;
785 break;
786 case GCL_CBCLSEXTRA:
787 retvalue = class->cbClsExtra;
788 break;
789 case GCLP_HMODULE:
790 retvalue = (ULONG_PTR)class->hInstance;
791 break;
792 case GCLP_WNDPROC:
793 retvalue = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
794 break;
795 case GCLP_MENUNAME:
796 retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
797 if (unicode)
798 retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
799 else
800 retvalue = (ULONG_PTR)CLASS_GetMenuNameA( class );
801 break;
802 case GCW_ATOM:
803 retvalue = class->atomName;
804 break;
805 default:
806 SetLastError( ERROR_INVALID_INDEX );
807 break;
809 release_class_ptr( class );
810 return retvalue;
814 /***********************************************************************
815 * GetClassLongW (USER32.@)
817 DWORD WINAPI GetClassLongW( HWND hwnd, INT offset )
819 return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), TRUE );
824 /***********************************************************************
825 * GetClassLongA (USER32.@)
827 DWORD WINAPI GetClassLongA( HWND hwnd, INT offset )
829 return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), FALSE );
833 /***********************************************************************
834 * SetClassWord (USER32.@)
836 WORD WINAPI SetClassWord( HWND hwnd, INT offset, WORD newval )
838 CLASS *class;
839 WORD retval = 0;
841 if (offset < 0) return SetClassLongA( hwnd, offset, (DWORD)newval );
843 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
845 SERVER_START_REQ( set_class_info )
847 req->window = wine_server_user_handle( hwnd );
848 req->flags = SET_CLASS_EXTRA;
849 req->extra_offset = offset;
850 req->extra_size = sizeof(newval);
851 memcpy( &req->extra_value, &newval, sizeof(newval) );
852 if (!wine_server_call_err( req ))
854 void *ptr = (char *)(class + 1) + offset;
855 memcpy( &retval, ptr, sizeof(retval) );
856 memcpy( ptr, &newval, sizeof(newval) );
859 SERVER_END_REQ;
860 release_class_ptr( class );
861 return retval;
865 /***********************************************************************
866 * CLASS_SetClassLong
868 * Implementation of SetClassLong(Ptr)A/W
870 static ULONG_PTR CLASS_SetClassLong( HWND hwnd, INT offset, LONG_PTR newval,
871 UINT size, BOOL unicode )
873 CLASS *class;
874 ULONG_PTR retval = 0;
876 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
878 if (offset >= 0)
880 if (set_server_info( hwnd, offset, newval, size ))
882 void *ptr = (char *)(class + 1) + offset;
883 if ( size == sizeof(LONG) )
885 DWORD retdword;
886 LONG newlong = newval;
887 memcpy( &retdword, ptr, sizeof(DWORD) );
888 memcpy( ptr, &newlong, sizeof(LONG) );
889 retval = retdword;
891 else
893 memcpy( &retval, ptr, sizeof(ULONG_PTR) );
894 memcpy( ptr, &newval, sizeof(LONG_PTR) );
898 else switch(offset)
900 case GCLP_MENUNAME:
901 if ( unicode )
902 CLASS_SetMenuNameW( class, (LPCWSTR)newval );
903 else
904 CLASS_SetMenuNameA( class, (LPCSTR)newval );
905 retval = 0; /* Old value is now meaningless anyway */
906 break;
907 case GCLP_WNDPROC:
908 retval = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
909 class->winproc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
910 break;
911 case GCLP_HBRBACKGROUND:
912 retval = (ULONG_PTR)class->hbrBackground;
913 class->hbrBackground = (HBRUSH)newval;
914 break;
915 case GCLP_HCURSOR:
916 retval = (ULONG_PTR)class->hCursor;
917 class->hCursor = (HCURSOR)newval;
918 break;
919 case GCLP_HICON:
920 retval = (ULONG_PTR)class->hIcon;
921 if (retval && class->hIconSmIntern)
923 DestroyIcon(class->hIconSmIntern);
924 class->hIconSmIntern = NULL;
926 if (newval && !class->hIconSm)
927 class->hIconSmIntern = CopyImage( (HICON)newval, IMAGE_ICON,
928 GetSystemMetrics( SM_CXSMICON ), GetSystemMetrics( SM_CYSMICON ), 0 );
929 class->hIcon = (HICON)newval;
930 break;
931 case GCLP_HICONSM:
932 retval = (ULONG_PTR)class->hIconSm;
933 if (retval && !newval)
934 class->hIconSmIntern = class->hIcon ? CopyImage( class->hIcon, IMAGE_ICON,
935 GetSystemMetrics( SM_CXSMICON ),
936 GetSystemMetrics( SM_CYSMICON ), 0 ) : NULL;
937 else if (!retval && newval && class->hIconSmIntern)
939 DestroyIcon(class->hIconSmIntern);
940 class->hIconSmIntern = NULL;
942 class->hIconSm = (HICON)newval;
943 break;
944 case GCL_STYLE:
945 if (!set_server_info( hwnd, offset, newval, size )) break;
946 retval = class->style;
947 class->style = newval;
948 break;
949 case GCL_CBWNDEXTRA:
950 if (!set_server_info( hwnd, offset, newval, size )) break;
951 retval = class->cbWndExtra;
952 class->cbWndExtra = newval;
953 break;
954 case GCLP_HMODULE:
955 if (!set_server_info( hwnd, offset, newval, size )) break;
956 retval = (ULONG_PTR)class->hInstance;
957 class->hInstance = (HINSTANCE)newval;
958 break;
959 case GCW_ATOM:
960 if (!set_server_info( hwnd, offset, newval, size )) break;
961 retval = class->atomName;
962 class->atomName = newval;
963 GlobalGetAtomNameW( newval, class->name, sizeof(class->name)/sizeof(WCHAR) );
964 break;
965 case GCL_CBCLSEXTRA: /* cannot change this one */
966 SetLastError( ERROR_INVALID_PARAMETER );
967 break;
968 default:
969 SetLastError( ERROR_INVALID_INDEX );
970 break;
972 release_class_ptr( class );
973 return retval;
977 /***********************************************************************
978 * SetClassLongW (USER32.@)
980 DWORD WINAPI SetClassLongW( HWND hwnd, INT offset, LONG newval )
982 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), TRUE );
986 /***********************************************************************
987 * SetClassLongA (USER32.@)
989 DWORD WINAPI SetClassLongA( HWND hwnd, INT offset, LONG newval )
991 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), FALSE );
995 /***********************************************************************
996 * GetClassNameA (USER32.@)
998 INT WINAPI GetClassNameA( HWND hwnd, LPSTR buffer, INT count )
1000 WCHAR tmpbuf[MAX_ATOM_LEN + 1];
1001 DWORD len;
1003 if (count <= 0) return 0;
1004 if (!GetClassNameW( hwnd, tmpbuf, sizeof(tmpbuf)/sizeof(WCHAR) )) return 0;
1005 RtlUnicodeToMultiByteN( buffer, count - 1, &len, tmpbuf, strlenW(tmpbuf) * sizeof(WCHAR) );
1006 buffer[len] = 0;
1007 return len;
1011 /***********************************************************************
1012 * GetClassNameW (USER32.@)
1014 INT WINAPI GetClassNameW( HWND hwnd, LPWSTR buffer, INT count )
1016 CLASS *class;
1017 INT ret;
1019 TRACE("%p %p %d\n", hwnd, buffer, count);
1021 if (count <= 0) return 0;
1023 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
1025 if (class == CLASS_OTHER_PROCESS)
1027 WCHAR tmpbuf[MAX_ATOM_LEN + 1];
1029 ret = GlobalGetAtomNameW( GetClassLongW( hwnd, GCW_ATOM ), tmpbuf, MAX_ATOM_LEN + 1 );
1030 if (ret)
1032 ret = min(count - 1, ret);
1033 memcpy(buffer, tmpbuf, ret * sizeof(WCHAR));
1034 buffer[ret] = 0;
1037 else
1039 lstrcpynW( buffer, class->name, count );
1040 release_class_ptr( class );
1041 ret = strlenW( buffer );
1043 return ret;
1047 /***********************************************************************
1048 * RealGetWindowClassA (USER32.@)
1050 UINT WINAPI RealGetWindowClassA( HWND hwnd, LPSTR buffer, UINT count )
1052 return GetClassNameA( hwnd, buffer, count );
1056 /***********************************************************************
1057 * RealGetWindowClassW (USER32.@)
1059 UINT WINAPI RealGetWindowClassW( HWND hwnd, LPWSTR buffer, UINT count )
1061 return GetClassNameW( hwnd, buffer, count );
1065 /***********************************************************************
1066 * GetClassInfoA (USER32.@)
1068 BOOL WINAPI GetClassInfoA( HINSTANCE hInstance, LPCSTR name, WNDCLASSA *wc )
1070 WNDCLASSEXA wcex;
1071 UINT ret = GetClassInfoExA( hInstance, name, &wcex );
1073 if (ret)
1075 wc->style = wcex.style;
1076 wc->lpfnWndProc = wcex.lpfnWndProc;
1077 wc->cbClsExtra = wcex.cbClsExtra;
1078 wc->cbWndExtra = wcex.cbWndExtra;
1079 wc->hInstance = wcex.hInstance;
1080 wc->hIcon = wcex.hIcon;
1081 wc->hCursor = wcex.hCursor;
1082 wc->hbrBackground = wcex.hbrBackground;
1083 wc->lpszMenuName = wcex.lpszMenuName;
1084 wc->lpszClassName = wcex.lpszClassName;
1086 return ret;
1090 /***********************************************************************
1091 * GetClassInfoW (USER32.@)
1093 BOOL WINAPI GetClassInfoW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSW *wc )
1095 WNDCLASSEXW wcex;
1096 UINT ret = GetClassInfoExW( hInstance, name, &wcex );
1098 if (ret)
1100 wc->style = wcex.style;
1101 wc->lpfnWndProc = wcex.lpfnWndProc;
1102 wc->cbClsExtra = wcex.cbClsExtra;
1103 wc->cbWndExtra = wcex.cbWndExtra;
1104 wc->hInstance = wcex.hInstance;
1105 wc->hIcon = wcex.hIcon;
1106 wc->hCursor = wcex.hCursor;
1107 wc->hbrBackground = wcex.hbrBackground;
1108 wc->lpszMenuName = wcex.lpszMenuName;
1109 wc->lpszClassName = wcex.lpszClassName;
1111 return ret;
1115 /***********************************************************************
1116 * GetClassInfoExA (USER32.@)
1118 BOOL WINAPI GetClassInfoExA( HINSTANCE hInstance, LPCSTR name, WNDCLASSEXA *wc )
1120 ATOM atom;
1121 CLASS *classPtr;
1123 TRACE("%p %s %p\n", hInstance, debugstr_a(name), wc);
1125 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
1127 if (!wc)
1129 SetLastError( ERROR_NOACCESS );
1130 return FALSE;
1133 if (!hInstance) hInstance = user32_module;
1135 if (!IS_INTRESOURCE(name))
1137 WCHAR nameW[MAX_ATOM_LEN + 1];
1138 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, sizeof(nameW)/sizeof(WCHAR) ))
1139 return FALSE;
1140 classPtr = CLASS_FindClass( nameW, hInstance );
1142 else classPtr = CLASS_FindClass( (LPCWSTR)name, hInstance );
1144 if (!classPtr)
1146 SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1147 return FALSE;
1149 wc->style = classPtr->style;
1150 wc->lpfnWndProc = WINPROC_GetProc( classPtr->winproc, FALSE );
1151 wc->cbClsExtra = classPtr->cbClsExtra;
1152 wc->cbWndExtra = classPtr->cbWndExtra;
1153 wc->hInstance = (hInstance == user32_module) ? 0 : hInstance;
1154 wc->hIcon = classPtr->hIcon;
1155 wc->hIconSm = classPtr->hIconSm ? classPtr->hIconSm : classPtr->hIconSmIntern;
1156 wc->hCursor = classPtr->hCursor;
1157 wc->hbrBackground = classPtr->hbrBackground;
1158 wc->lpszMenuName = CLASS_GetMenuNameA( classPtr );
1159 wc->lpszClassName = name;
1160 atom = classPtr->atomName;
1161 release_class_ptr( classPtr );
1163 /* We must return the atom of the class here instead of just TRUE. */
1164 return atom;
1168 /***********************************************************************
1169 * GetClassInfoExW (USER32.@)
1171 BOOL WINAPI GetClassInfoExW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSEXW *wc )
1173 ATOM atom;
1174 CLASS *classPtr;
1176 TRACE("%p %s %p\n", hInstance, debugstr_w(name), wc);
1178 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
1180 if (!wc)
1182 SetLastError( ERROR_NOACCESS );
1183 return FALSE;
1186 if (!hInstance) hInstance = user32_module;
1188 if (!(classPtr = CLASS_FindClass( name, hInstance )))
1190 SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1191 return FALSE;
1193 wc->style = classPtr->style;
1194 wc->lpfnWndProc = WINPROC_GetProc( classPtr->winproc, TRUE );
1195 wc->cbClsExtra = classPtr->cbClsExtra;
1196 wc->cbWndExtra = classPtr->cbWndExtra;
1197 wc->hInstance = (hInstance == user32_module) ? 0 : hInstance;
1198 wc->hIcon = classPtr->hIcon;
1199 wc->hIconSm = classPtr->hIconSm ? classPtr->hIconSm : classPtr->hIconSmIntern;
1200 wc->hCursor = classPtr->hCursor;
1201 wc->hbrBackground = classPtr->hbrBackground;
1202 wc->lpszMenuName = CLASS_GetMenuNameW( classPtr );
1203 wc->lpszClassName = name;
1204 atom = classPtr->atomName;
1205 release_class_ptr( classPtr );
1207 /* We must return the atom of the class here instead of just TRUE. */
1208 return atom;
1212 #if 0 /* toolhelp is in kernel, so this cannot work */
1214 /***********************************************************************
1215 * ClassFirst (TOOLHELP.69)
1217 BOOL16 WINAPI ClassFirst16( CLASSENTRY *pClassEntry )
1219 TRACE("%p\n",pClassEntry);
1220 pClassEntry->wNext = 1;
1221 return ClassNext16( pClassEntry );
1225 /***********************************************************************
1226 * ClassNext (TOOLHELP.70)
1228 BOOL16 WINAPI ClassNext16( CLASSENTRY *pClassEntry )
1230 int i;
1231 CLASS *class = firstClass;
1233 TRACE("%p\n",pClassEntry);
1235 if (!pClassEntry->wNext) return FALSE;
1236 for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1237 if (!class)
1239 pClassEntry->wNext = 0;
1240 return FALSE;
1242 pClassEntry->hInst = class->hInstance;
1243 pClassEntry->wNext++;
1244 GlobalGetAtomNameA( class->atomName, pClassEntry->szClassName,
1245 sizeof(pClassEntry->szClassName) );
1246 return TRUE;
1248 #endif
1250 /* 64bit versions */
1252 #ifdef GetClassLongPtrA
1253 #undef GetClassLongPtrA
1254 #endif
1256 #ifdef GetClassLongPtrW
1257 #undef GetClassLongPtrW
1258 #endif
1260 #ifdef SetClassLongPtrA
1261 #undef SetClassLongPtrA
1262 #endif
1264 #ifdef SetClassLongPtrW
1265 #undef SetClassLongPtrW
1266 #endif
1268 /***********************************************************************
1269 * GetClassLongPtrA (USER32.@)
1271 ULONG_PTR WINAPI GetClassLongPtrA( HWND hwnd, INT offset )
1273 return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), FALSE );
1276 /***********************************************************************
1277 * GetClassLongPtrW (USER32.@)
1279 ULONG_PTR WINAPI GetClassLongPtrW( HWND hwnd, INT offset )
1281 return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), TRUE );
1284 /***********************************************************************
1285 * SetClassLongPtrW (USER32.@)
1287 ULONG_PTR WINAPI SetClassLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
1289 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), TRUE );
1292 /***********************************************************************
1293 * SetClassLongPtrA (USER32.@)
1295 ULONG_PTR WINAPI SetClassLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
1297 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), FALSE );