user32: Create the desktop window to trigger builtin class registration before access...
[wine.git] / dlls / user32 / class.c
blobfa340dc0edb160684fe08df44515c3bb2998d764
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 GetDesktopWindow(); /* create the desktop window to trigger builtin class registration */
273 USER_Lock();
275 LIST_FOR_EACH( ptr, &class_list )
277 CLASS *class = LIST_ENTRY( ptr, CLASS, entry );
278 if (atom)
280 if (class->atomName != atom) continue;
282 else
284 if (!name || strcmpiW( class->name, name )) continue;
286 if (!hinstance || !class->local || class->hInstance == hinstance)
288 TRACE("%s %p -> %p\n", debugstr_w(name), hinstance, class);
289 return class;
292 USER_Unlock();
293 TRACE("%s %p -> not found\n", debugstr_w(name), hinstance);
294 return NULL;
298 /***********************************************************************
299 * CLASS_RegisterClass
301 * The real RegisterClass() functionality.
303 static CLASS *CLASS_RegisterClass( LPCWSTR name, HINSTANCE hInstance, BOOL local,
304 DWORD style, INT classExtra, INT winExtra )
306 CLASS *classPtr;
307 BOOL ret;
309 TRACE("name=%s hinst=%p style=0x%x clExtr=0x%x winExtr=0x%x\n",
310 debugstr_w(name), hInstance, style, classExtra, winExtra );
312 /* Fix the extra bytes value */
314 if (classExtra > 40) /* Extra bytes are limited to 40 in Win32 */
315 WARN("Class extra bytes %d is > 40\n", classExtra);
316 if (winExtra > 40) /* Extra bytes are limited to 40 in Win32 */
317 WARN("Win extra bytes %d is > 40\n", winExtra );
319 classPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLASS) + classExtra );
320 if (!classPtr) return NULL;
322 classPtr->atomName = get_int_atom_value( name );
323 if (!classPtr->atomName && name) strcpyW( classPtr->name, name );
324 else GlobalGetAtomNameW( classPtr->atomName, classPtr->name, sizeof(classPtr->name)/sizeof(WCHAR) );
326 SERVER_START_REQ( create_class )
328 req->local = local;
329 req->style = style;
330 req->instance = wine_server_client_ptr( hInstance );
331 req->extra = classExtra;
332 req->win_extra = winExtra;
333 req->client_ptr = wine_server_client_ptr( classPtr );
334 req->atom = classPtr->atomName;
335 if (!req->atom && name) wine_server_add_data( req, name, strlenW(name) * sizeof(WCHAR) );
336 ret = !wine_server_call_err( req );
337 classPtr->atomName = reply->atom;
339 SERVER_END_REQ;
340 if (!ret)
342 HeapFree( GetProcessHeap(), 0, classPtr );
343 return NULL;
346 classPtr->style = style;
347 classPtr->local = local;
348 classPtr->cbWndExtra = winExtra;
349 classPtr->cbClsExtra = classExtra;
350 classPtr->hInstance = hInstance;
352 /* Other non-null values must be set by caller */
354 USER_Lock();
355 if (local) list_add_head( &class_list, &classPtr->entry );
356 else list_add_tail( &class_list, &classPtr->entry );
357 return classPtr;
361 /***********************************************************************
362 * register_builtin
364 * Register a builtin control class.
365 * This allows having both ASCII and Unicode winprocs for the same class.
367 static void register_builtin( const struct builtin_class_descr *descr )
369 CLASS *classPtr;
371 if (!(classPtr = CLASS_RegisterClass( descr->name, user32_module, FALSE,
372 descr->style, 0, descr->extra ))) return;
374 if (descr->cursor) classPtr->hCursor = LoadCursorA( 0, (LPSTR)descr->cursor );
375 classPtr->hbrBackground = descr->brush;
376 classPtr->winproc = BUILTIN_WINPROC( descr->proc );
377 release_class_ptr( classPtr );
381 /***********************************************************************
382 * register_builtins
384 static BOOL WINAPI register_builtins( INIT_ONCE *once, void *param, void **context )
386 register_builtin( &BUTTON_builtin_class );
387 register_builtin( &COMBO_builtin_class );
388 register_builtin( &COMBOLBOX_builtin_class );
389 register_builtin( &DIALOG_builtin_class );
390 register_builtin( &EDIT_builtin_class );
391 register_builtin( &ICONTITLE_builtin_class );
392 register_builtin( &LISTBOX_builtin_class );
393 register_builtin( &MDICLIENT_builtin_class );
394 register_builtin( &MENU_builtin_class );
395 register_builtin( &SCROLL_builtin_class );
396 register_builtin( &STATIC_builtin_class );
397 return TRUE;
401 /***********************************************************************
402 * register_builtin_classes
404 void register_builtin_classes(void)
406 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
410 /***********************************************************************
411 * register_desktop_class
413 void register_desktop_class(void)
415 register_builtin( &DESKTOP_builtin_class );
416 register_builtin( &MESSAGE_builtin_class );
420 /***********************************************************************
421 * get_class_winproc
423 WNDPROC get_class_winproc( CLASS *class )
425 return class->winproc;
429 /***********************************************************************
430 * get_class_dce
432 struct dce *get_class_dce( CLASS *class )
434 return class->dce;
438 /***********************************************************************
439 * set_class_dce
441 struct dce *set_class_dce( CLASS *class, struct dce *dce )
443 if (class->dce) return class->dce; /* already set, don't change it */
444 class->dce = dce;
445 return dce;
449 /***********************************************************************
450 * RegisterClassA (USER32.@)
452 * Register a window class.
454 * RETURNS
455 * >0: Unique identifier
456 * 0: Failure
458 ATOM WINAPI RegisterClassA( const WNDCLASSA* wc ) /* [in] Address of structure with class data */
460 WNDCLASSEXA wcex;
462 wcex.cbSize = sizeof(wcex);
463 wcex.style = wc->style;
464 wcex.lpfnWndProc = wc->lpfnWndProc;
465 wcex.cbClsExtra = wc->cbClsExtra;
466 wcex.cbWndExtra = wc->cbWndExtra;
467 wcex.hInstance = wc->hInstance;
468 wcex.hIcon = wc->hIcon;
469 wcex.hCursor = wc->hCursor;
470 wcex.hbrBackground = wc->hbrBackground;
471 wcex.lpszMenuName = wc->lpszMenuName;
472 wcex.lpszClassName = wc->lpszClassName;
473 wcex.hIconSm = 0;
474 return RegisterClassExA( &wcex );
478 /***********************************************************************
479 * RegisterClassW (USER32.@)
481 * See RegisterClassA.
483 ATOM WINAPI RegisterClassW( const WNDCLASSW* wc )
485 WNDCLASSEXW wcex;
487 wcex.cbSize = sizeof(wcex);
488 wcex.style = wc->style;
489 wcex.lpfnWndProc = wc->lpfnWndProc;
490 wcex.cbClsExtra = wc->cbClsExtra;
491 wcex.cbWndExtra = wc->cbWndExtra;
492 wcex.hInstance = wc->hInstance;
493 wcex.hIcon = wc->hIcon;
494 wcex.hCursor = wc->hCursor;
495 wcex.hbrBackground = wc->hbrBackground;
496 wcex.lpszMenuName = wc->lpszMenuName;
497 wcex.lpszClassName = wc->lpszClassName;
498 wcex.hIconSm = 0;
499 return RegisterClassExW( &wcex );
503 /***********************************************************************
504 * RegisterClassExA (USER32.@)
506 ATOM WINAPI RegisterClassExA( const WNDCLASSEXA* wc )
508 ATOM atom;
509 CLASS *classPtr;
510 HINSTANCE instance;
512 GetDesktopWindow(); /* create the desktop window to trigger builtin class registration */
514 if (wc->cbSize != sizeof(*wc) || wc->cbClsExtra < 0 || wc->cbWndExtra < 0 ||
515 wc->hInstance == user32_module) /* we can't register a class for user32 */
517 SetLastError( ERROR_INVALID_PARAMETER );
518 return 0;
520 if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
522 if (!IS_INTRESOURCE(wc->lpszClassName))
524 WCHAR name[MAX_ATOM_LEN + 1];
526 if (!MultiByteToWideChar( CP_ACP, 0, wc->lpszClassName, -1, name, MAX_ATOM_LEN + 1 )) return 0;
527 classPtr = CLASS_RegisterClass( name, instance, !(wc->style & CS_GLOBALCLASS),
528 wc->style, wc->cbClsExtra, wc->cbWndExtra );
530 else
532 classPtr = CLASS_RegisterClass( (LPCWSTR)wc->lpszClassName, instance,
533 !(wc->style & CS_GLOBALCLASS), wc->style,
534 wc->cbClsExtra, wc->cbWndExtra );
536 if (!classPtr) return 0;
537 atom = classPtr->atomName;
539 TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
540 debugstr_a(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
541 wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
543 classPtr->hIcon = wc->hIcon;
544 classPtr->hIconSm = wc->hIconSm;
545 classPtr->hIconSmIntern = wc->hIcon && !wc->hIconSm ?
546 CopyImage( wc->hIcon, IMAGE_ICON,
547 GetSystemMetrics( SM_CXSMICON ),
548 GetSystemMetrics( SM_CYSMICON ), 0 ) : NULL;
549 classPtr->hCursor = wc->hCursor;
550 classPtr->hbrBackground = wc->hbrBackground;
551 classPtr->winproc = WINPROC_AllocProc( wc->lpfnWndProc, FALSE );
552 CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
553 release_class_ptr( classPtr );
554 return atom;
558 /***********************************************************************
559 * RegisterClassExW (USER32.@)
561 ATOM WINAPI RegisterClassExW( const WNDCLASSEXW* wc )
563 ATOM atom;
564 CLASS *classPtr;
565 HINSTANCE instance;
567 GetDesktopWindow(); /* create the desktop window to trigger builtin class registration */
569 if (wc->cbSize != sizeof(*wc) || wc->cbClsExtra < 0 || wc->cbWndExtra < 0 ||
570 wc->hInstance == user32_module) /* we can't register a class for user32 */
572 SetLastError( ERROR_INVALID_PARAMETER );
573 return 0;
575 if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
577 if (!(classPtr = CLASS_RegisterClass( wc->lpszClassName, instance, !(wc->style & CS_GLOBALCLASS),
578 wc->style, wc->cbClsExtra, wc->cbWndExtra )))
579 return 0;
581 atom = classPtr->atomName;
583 TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
584 debugstr_w(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
585 wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
587 classPtr->hIcon = wc->hIcon;
588 classPtr->hIconSm = wc->hIconSm;
589 classPtr->hIconSmIntern = wc->hIcon && !wc->hIconSm ?
590 CopyImage( wc->hIcon, IMAGE_ICON,
591 GetSystemMetrics( SM_CXSMICON ),
592 GetSystemMetrics( SM_CYSMICON ), 0 ) : NULL;
593 classPtr->hCursor = wc->hCursor;
594 classPtr->hbrBackground = wc->hbrBackground;
595 classPtr->winproc = WINPROC_AllocProc( wc->lpfnWndProc, TRUE );
596 CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
597 release_class_ptr( classPtr );
598 return atom;
602 /***********************************************************************
603 * UnregisterClassA (USER32.@)
605 BOOL WINAPI UnregisterClassA( LPCSTR className, HINSTANCE hInstance )
607 if (!IS_INTRESOURCE(className))
609 WCHAR name[MAX_ATOM_LEN + 1];
611 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, name, MAX_ATOM_LEN + 1 ))
612 return FALSE;
613 return UnregisterClassW( name, hInstance );
615 return UnregisterClassW( (LPCWSTR)className, hInstance );
618 /***********************************************************************
619 * UnregisterClassW (USER32.@)
621 BOOL WINAPI UnregisterClassW( LPCWSTR className, HINSTANCE hInstance )
623 CLASS *classPtr = NULL;
625 GetDesktopWindow(); /* create the desktop window to trigger builtin class registration */
627 SERVER_START_REQ( destroy_class )
629 req->instance = wine_server_client_ptr( hInstance );
630 if (!(req->atom = get_int_atom_value(className)) && className)
631 wine_server_add_data( req, className, strlenW(className) * sizeof(WCHAR) );
632 if (!wine_server_call_err( req )) classPtr = wine_server_get_ptr( reply->client_ptr );
634 SERVER_END_REQ;
636 if (classPtr) CLASS_FreeClass( classPtr );
637 return (classPtr != NULL);
641 /***********************************************************************
642 * GetClassWord (USER32.@)
644 WORD WINAPI GetClassWord( HWND hwnd, INT offset )
646 CLASS *class;
647 WORD retvalue = 0;
649 if (offset < 0) return GetClassLongA( hwnd, offset );
651 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
653 if (class == CLASS_OTHER_PROCESS)
655 SERVER_START_REQ( set_class_info )
657 req->window = wine_server_user_handle( hwnd );
658 req->flags = 0;
659 req->extra_offset = offset;
660 req->extra_size = sizeof(retvalue);
661 if (!wine_server_call_err( req ))
662 memcpy( &retvalue, &reply->old_extra_value, sizeof(retvalue) );
664 SERVER_END_REQ;
665 return retvalue;
668 if (offset <= class->cbClsExtra - sizeof(WORD))
669 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(retvalue) );
670 else
671 SetLastError( ERROR_INVALID_INDEX );
672 release_class_ptr( class );
673 return retvalue;
677 /***********************************************************************
678 * CLASS_GetClassLong
680 * Implementation of GetClassLong(Ptr)A/W
682 static ULONG_PTR CLASS_GetClassLong( HWND hwnd, INT offset, UINT size,
683 BOOL unicode )
685 CLASS *class;
686 ULONG_PTR retvalue = 0;
688 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
690 if (class == CLASS_OTHER_PROCESS)
692 SERVER_START_REQ( set_class_info )
694 req->window = wine_server_user_handle( hwnd );
695 req->flags = 0;
696 req->extra_offset = (offset >= 0) ? offset : -1;
697 req->extra_size = (offset >= 0) ? size : 0;
698 if (!wine_server_call_err( req ))
700 switch(offset)
702 case GCLP_HBRBACKGROUND:
703 case GCLP_HCURSOR:
704 case GCLP_HICON:
705 case GCLP_HICONSM:
706 case GCLP_WNDPROC:
707 case GCLP_MENUNAME:
708 FIXME( "offset %d (%s) not supported on other process window %p\n",
709 offset, SPY_GetClassLongOffsetName(offset), hwnd );
710 SetLastError( ERROR_INVALID_HANDLE );
711 break;
712 case GCL_STYLE:
713 retvalue = reply->old_style;
714 break;
715 case GCL_CBWNDEXTRA:
716 retvalue = reply->old_win_extra;
717 break;
718 case GCL_CBCLSEXTRA:
719 retvalue = reply->old_extra;
720 break;
721 case GCLP_HMODULE:
722 retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
723 break;
724 case GCW_ATOM:
725 retvalue = reply->old_atom;
726 break;
727 default:
728 if (offset >= 0)
730 if (size == sizeof(DWORD))
732 DWORD retdword;
733 memcpy( &retdword, &reply->old_extra_value, sizeof(DWORD) );
734 retvalue = retdword;
736 else
737 memcpy( &retvalue, &reply->old_extra_value,
738 sizeof(ULONG_PTR) );
740 else SetLastError( ERROR_INVALID_INDEX );
741 break;
745 SERVER_END_REQ;
746 return retvalue;
749 if (offset >= 0)
751 if (offset <= class->cbClsExtra - size)
753 if (size == sizeof(DWORD))
755 DWORD retdword;
756 memcpy( &retdword, (char *)(class + 1) + offset, sizeof(DWORD) );
757 retvalue = retdword;
759 else
760 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(ULONG_PTR) );
762 else
763 SetLastError( ERROR_INVALID_INDEX );
764 release_class_ptr( class );
765 return retvalue;
768 switch(offset)
770 case GCLP_HBRBACKGROUND:
771 retvalue = (ULONG_PTR)class->hbrBackground;
772 break;
773 case GCLP_HCURSOR:
774 retvalue = (ULONG_PTR)class->hCursor;
775 break;
776 case GCLP_HICON:
777 retvalue = (ULONG_PTR)class->hIcon;
778 break;
779 case GCLP_HICONSM:
780 retvalue = (ULONG_PTR)(class->hIconSm ? class->hIconSm : class->hIconSmIntern);
781 break;
782 case GCL_STYLE:
783 retvalue = class->style;
784 break;
785 case GCL_CBWNDEXTRA:
786 retvalue = class->cbWndExtra;
787 break;
788 case GCL_CBCLSEXTRA:
789 retvalue = class->cbClsExtra;
790 break;
791 case GCLP_HMODULE:
792 retvalue = (ULONG_PTR)class->hInstance;
793 break;
794 case GCLP_WNDPROC:
795 retvalue = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
796 break;
797 case GCLP_MENUNAME:
798 retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
799 if (unicode)
800 retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
801 else
802 retvalue = (ULONG_PTR)CLASS_GetMenuNameA( class );
803 break;
804 case GCW_ATOM:
805 retvalue = class->atomName;
806 break;
807 default:
808 SetLastError( ERROR_INVALID_INDEX );
809 break;
811 release_class_ptr( class );
812 return retvalue;
816 /***********************************************************************
817 * GetClassLongW (USER32.@)
819 DWORD WINAPI GetClassLongW( HWND hwnd, INT offset )
821 return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), TRUE );
826 /***********************************************************************
827 * GetClassLongA (USER32.@)
829 DWORD WINAPI GetClassLongA( HWND hwnd, INT offset )
831 return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), FALSE );
835 /***********************************************************************
836 * SetClassWord (USER32.@)
838 WORD WINAPI SetClassWord( HWND hwnd, INT offset, WORD newval )
840 CLASS *class;
841 WORD retval = 0;
843 if (offset < 0) return SetClassLongA( hwnd, offset, (DWORD)newval );
845 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
847 SERVER_START_REQ( set_class_info )
849 req->window = wine_server_user_handle( hwnd );
850 req->flags = SET_CLASS_EXTRA;
851 req->extra_offset = offset;
852 req->extra_size = sizeof(newval);
853 memcpy( &req->extra_value, &newval, sizeof(newval) );
854 if (!wine_server_call_err( req ))
856 void *ptr = (char *)(class + 1) + offset;
857 memcpy( &retval, ptr, sizeof(retval) );
858 memcpy( ptr, &newval, sizeof(newval) );
861 SERVER_END_REQ;
862 release_class_ptr( class );
863 return retval;
867 /***********************************************************************
868 * CLASS_SetClassLong
870 * Implementation of SetClassLong(Ptr)A/W
872 static ULONG_PTR CLASS_SetClassLong( HWND hwnd, INT offset, LONG_PTR newval,
873 UINT size, BOOL unicode )
875 CLASS *class;
876 ULONG_PTR retval = 0;
878 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
880 if (offset >= 0)
882 if (set_server_info( hwnd, offset, newval, size ))
884 void *ptr = (char *)(class + 1) + offset;
885 if ( size == sizeof(LONG) )
887 DWORD retdword;
888 LONG newlong = newval;
889 memcpy( &retdword, ptr, sizeof(DWORD) );
890 memcpy( ptr, &newlong, sizeof(LONG) );
891 retval = retdword;
893 else
895 memcpy( &retval, ptr, sizeof(ULONG_PTR) );
896 memcpy( ptr, &newval, sizeof(LONG_PTR) );
900 else switch(offset)
902 case GCLP_MENUNAME:
903 if ( unicode )
904 CLASS_SetMenuNameW( class, (LPCWSTR)newval );
905 else
906 CLASS_SetMenuNameA( class, (LPCSTR)newval );
907 retval = 0; /* Old value is now meaningless anyway */
908 break;
909 case GCLP_WNDPROC:
910 retval = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
911 class->winproc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
912 break;
913 case GCLP_HBRBACKGROUND:
914 retval = (ULONG_PTR)class->hbrBackground;
915 class->hbrBackground = (HBRUSH)newval;
916 break;
917 case GCLP_HCURSOR:
918 retval = (ULONG_PTR)class->hCursor;
919 class->hCursor = (HCURSOR)newval;
920 break;
921 case GCLP_HICON:
922 retval = (ULONG_PTR)class->hIcon;
923 if (retval && class->hIconSmIntern)
925 DestroyIcon(class->hIconSmIntern);
926 class->hIconSmIntern = NULL;
928 if (newval && !class->hIconSm)
929 class->hIconSmIntern = CopyImage( (HICON)newval, IMAGE_ICON,
930 GetSystemMetrics( SM_CXSMICON ), GetSystemMetrics( SM_CYSMICON ), 0 );
931 class->hIcon = (HICON)newval;
932 break;
933 case GCLP_HICONSM:
934 retval = (ULONG_PTR)class->hIconSm;
935 if (retval && !newval)
936 class->hIconSmIntern = class->hIcon ? CopyImage( class->hIcon, IMAGE_ICON,
937 GetSystemMetrics( SM_CXSMICON ),
938 GetSystemMetrics( SM_CYSMICON ), 0 ) : NULL;
939 else if (!retval && newval && class->hIconSmIntern)
941 DestroyIcon(class->hIconSmIntern);
942 class->hIconSmIntern = NULL;
944 class->hIconSm = (HICON)newval;
945 break;
946 case GCL_STYLE:
947 if (!set_server_info( hwnd, offset, newval, size )) break;
948 retval = class->style;
949 class->style = newval;
950 break;
951 case GCL_CBWNDEXTRA:
952 if (!set_server_info( hwnd, offset, newval, size )) break;
953 retval = class->cbWndExtra;
954 class->cbWndExtra = newval;
955 break;
956 case GCLP_HMODULE:
957 if (!set_server_info( hwnd, offset, newval, size )) break;
958 retval = (ULONG_PTR)class->hInstance;
959 class->hInstance = (HINSTANCE)newval;
960 break;
961 case GCW_ATOM:
962 if (!set_server_info( hwnd, offset, newval, size )) break;
963 retval = class->atomName;
964 class->atomName = newval;
965 GlobalGetAtomNameW( newval, class->name, sizeof(class->name)/sizeof(WCHAR) );
966 break;
967 case GCL_CBCLSEXTRA: /* cannot change this one */
968 SetLastError( ERROR_INVALID_PARAMETER );
969 break;
970 default:
971 SetLastError( ERROR_INVALID_INDEX );
972 break;
974 release_class_ptr( class );
975 return retval;
979 /***********************************************************************
980 * SetClassLongW (USER32.@)
982 DWORD WINAPI SetClassLongW( HWND hwnd, INT offset, LONG newval )
984 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), TRUE );
988 /***********************************************************************
989 * SetClassLongA (USER32.@)
991 DWORD WINAPI SetClassLongA( HWND hwnd, INT offset, LONG newval )
993 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), FALSE );
997 /***********************************************************************
998 * GetClassNameA (USER32.@)
1000 INT WINAPI GetClassNameA( HWND hwnd, LPSTR buffer, INT count )
1002 WCHAR tmpbuf[MAX_ATOM_LEN + 1];
1003 DWORD len;
1005 if (count <= 0) return 0;
1006 if (!GetClassNameW( hwnd, tmpbuf, sizeof(tmpbuf)/sizeof(WCHAR) )) return 0;
1007 RtlUnicodeToMultiByteN( buffer, count - 1, &len, tmpbuf, strlenW(tmpbuf) * sizeof(WCHAR) );
1008 buffer[len] = 0;
1009 return len;
1013 /***********************************************************************
1014 * GetClassNameW (USER32.@)
1016 INT WINAPI GetClassNameW( HWND hwnd, LPWSTR buffer, INT count )
1018 CLASS *class;
1019 INT ret;
1021 TRACE("%p %p %d\n", hwnd, buffer, count);
1023 if (count <= 0) return 0;
1025 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
1027 if (class == CLASS_OTHER_PROCESS)
1029 WCHAR tmpbuf[MAX_ATOM_LEN + 1];
1031 ret = GlobalGetAtomNameW( GetClassLongW( hwnd, GCW_ATOM ), tmpbuf, MAX_ATOM_LEN + 1 );
1032 if (ret)
1034 ret = min(count - 1, ret);
1035 memcpy(buffer, tmpbuf, ret * sizeof(WCHAR));
1036 buffer[ret] = 0;
1039 else
1041 lstrcpynW( buffer, class->name, count );
1042 release_class_ptr( class );
1043 ret = strlenW( buffer );
1045 return ret;
1049 /***********************************************************************
1050 * RealGetWindowClassA (USER32.@)
1052 UINT WINAPI RealGetWindowClassA( HWND hwnd, LPSTR buffer, UINT count )
1054 return GetClassNameA( hwnd, buffer, count );
1058 /***********************************************************************
1059 * RealGetWindowClassW (USER32.@)
1061 UINT WINAPI RealGetWindowClassW( HWND hwnd, LPWSTR buffer, UINT count )
1063 return GetClassNameW( hwnd, buffer, count );
1067 /***********************************************************************
1068 * GetClassInfoA (USER32.@)
1070 BOOL WINAPI GetClassInfoA( HINSTANCE hInstance, LPCSTR name, WNDCLASSA *wc )
1072 WNDCLASSEXA wcex;
1073 UINT ret = GetClassInfoExA( hInstance, name, &wcex );
1075 if (ret)
1077 wc->style = wcex.style;
1078 wc->lpfnWndProc = wcex.lpfnWndProc;
1079 wc->cbClsExtra = wcex.cbClsExtra;
1080 wc->cbWndExtra = wcex.cbWndExtra;
1081 wc->hInstance = wcex.hInstance;
1082 wc->hIcon = wcex.hIcon;
1083 wc->hCursor = wcex.hCursor;
1084 wc->hbrBackground = wcex.hbrBackground;
1085 wc->lpszMenuName = wcex.lpszMenuName;
1086 wc->lpszClassName = wcex.lpszClassName;
1088 return ret;
1092 /***********************************************************************
1093 * GetClassInfoW (USER32.@)
1095 BOOL WINAPI GetClassInfoW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSW *wc )
1097 WNDCLASSEXW wcex;
1098 UINT ret = GetClassInfoExW( hInstance, name, &wcex );
1100 if (ret)
1102 wc->style = wcex.style;
1103 wc->lpfnWndProc = wcex.lpfnWndProc;
1104 wc->cbClsExtra = wcex.cbClsExtra;
1105 wc->cbWndExtra = wcex.cbWndExtra;
1106 wc->hInstance = wcex.hInstance;
1107 wc->hIcon = wcex.hIcon;
1108 wc->hCursor = wcex.hCursor;
1109 wc->hbrBackground = wcex.hbrBackground;
1110 wc->lpszMenuName = wcex.lpszMenuName;
1111 wc->lpszClassName = wcex.lpszClassName;
1113 return ret;
1117 /***********************************************************************
1118 * GetClassInfoExA (USER32.@)
1120 BOOL WINAPI GetClassInfoExA( HINSTANCE hInstance, LPCSTR name, WNDCLASSEXA *wc )
1122 ATOM atom;
1123 CLASS *classPtr;
1125 TRACE("%p %s %p\n", hInstance, debugstr_a(name), wc);
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 if (!wc)
1180 SetLastError( ERROR_NOACCESS );
1181 return FALSE;
1184 if (!hInstance) hInstance = user32_module;
1186 if (!(classPtr = CLASS_FindClass( name, hInstance )))
1188 SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1189 return FALSE;
1191 wc->style = classPtr->style;
1192 wc->lpfnWndProc = WINPROC_GetProc( classPtr->winproc, TRUE );
1193 wc->cbClsExtra = classPtr->cbClsExtra;
1194 wc->cbWndExtra = classPtr->cbWndExtra;
1195 wc->hInstance = (hInstance == user32_module) ? 0 : hInstance;
1196 wc->hIcon = classPtr->hIcon;
1197 wc->hIconSm = classPtr->hIconSm ? classPtr->hIconSm : classPtr->hIconSmIntern;
1198 wc->hCursor = classPtr->hCursor;
1199 wc->hbrBackground = classPtr->hbrBackground;
1200 wc->lpszMenuName = CLASS_GetMenuNameW( classPtr );
1201 wc->lpszClassName = name;
1202 atom = classPtr->atomName;
1203 release_class_ptr( classPtr );
1205 /* We must return the atom of the class here instead of just TRUE. */
1206 return atom;
1210 #if 0 /* toolhelp is in kernel, so this cannot work */
1212 /***********************************************************************
1213 * ClassFirst (TOOLHELP.69)
1215 BOOL16 WINAPI ClassFirst16( CLASSENTRY *pClassEntry )
1217 TRACE("%p\n",pClassEntry);
1218 pClassEntry->wNext = 1;
1219 return ClassNext16( pClassEntry );
1223 /***********************************************************************
1224 * ClassNext (TOOLHELP.70)
1226 BOOL16 WINAPI ClassNext16( CLASSENTRY *pClassEntry )
1228 int i;
1229 CLASS *class = firstClass;
1231 TRACE("%p\n",pClassEntry);
1233 if (!pClassEntry->wNext) return FALSE;
1234 for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1235 if (!class)
1237 pClassEntry->wNext = 0;
1238 return FALSE;
1240 pClassEntry->hInst = class->hInstance;
1241 pClassEntry->wNext++;
1242 GlobalGetAtomNameA( class->atomName, pClassEntry->szClassName,
1243 sizeof(pClassEntry->szClassName) );
1244 return TRUE;
1246 #endif
1248 /* 64bit versions */
1250 #ifdef GetClassLongPtrA
1251 #undef GetClassLongPtrA
1252 #endif
1254 #ifdef GetClassLongPtrW
1255 #undef GetClassLongPtrW
1256 #endif
1258 #ifdef SetClassLongPtrA
1259 #undef SetClassLongPtrA
1260 #endif
1262 #ifdef SetClassLongPtrW
1263 #undef SetClassLongPtrW
1264 #endif
1266 /***********************************************************************
1267 * GetClassLongPtrA (USER32.@)
1269 ULONG_PTR WINAPI GetClassLongPtrA( HWND hwnd, INT offset )
1271 return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), FALSE );
1274 /***********************************************************************
1275 * GetClassLongPtrW (USER32.@)
1277 ULONG_PTR WINAPI GetClassLongPtrW( HWND hwnd, INT offset )
1279 return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), TRUE );
1282 /***********************************************************************
1283 * SetClassLongPtrW (USER32.@)
1285 ULONG_PTR WINAPI SetClassLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
1287 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), TRUE );
1290 /***********************************************************************
1291 * SetClassLongPtrA (USER32.@)
1293 ULONG_PTR WINAPI SetClassLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
1295 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), FALSE );