user32/tests: Add an additional test for GetMenuItemInfo with NULL pointer as TypeData.
[wine/multimedia.git] / dlls / user32 / class.c
blobd40d22e1cd2fd6c0e67328ffd4829eb39425c8ea
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 * is_comctl32_class
126 static BOOL is_comctl32_class( const WCHAR *name )
128 static const WCHAR classesW[][20] =
130 {'C','o','m','b','o','B','o','x','E','x','3','2',0},
131 {'m','s','c','t','l','s','_','h','o','t','k','e','y','3','2',0},
132 {'m','s','c','t','l','s','_','p','r','o','g','r','e','s','s','3','2',0},
133 {'m','s','c','t','l','s','_','s','t','a','t','u','s','b','a','r','3','2',0},
134 {'m','s','c','t','l','s','_','t','r','a','c','k','b','a','r','3','2',0},
135 {'m','s','c','t','l','s','_','u','p','d','o','w','n','3','2',0},
136 {'N','a','t','i','v','e','F','o','n','t','C','t','l',0},
137 {'R','e','B','a','r','W','i','n','d','o','w','3','2',0},
138 {'S','y','s','A','n','i','m','a','t','e','3','2',0},
139 {'S','y','s','D','a','t','e','T','i','m','e','P','i','c','k','3','2',0},
140 {'S','y','s','H','e','a','d','e','r','3','2',0},
141 {'S','y','s','I','P','A','d','d','r','e','s','s','3','2',0},
142 {'S','y','s','L','i','s','t','V','i','e','w','3','2',0},
143 {'S','y','s','M','o','n','t','h','C','a','l','3','2',0},
144 {'S','y','s','P','a','g','e','r',0},
145 {'S','y','s','T','a','b','C','o','n','t','r','o','l','3','2',0},
146 {'S','y','s','T','r','e','e','V','i','e','w','3','2',0},
147 {'T','o','o','l','b','a','r','W','i','n','d','o','w','3','2',0},
148 {'t','o','o','l','t','i','p','s','_','c','l','a','s','s','3','2',0},
151 int min = 0, max = (sizeof(classesW) / sizeof(classesW[0])) - 1;
153 while (min <= max)
155 int res, pos = (min + max) / 2;
156 if (!(res = strcmpiW( name, classesW[pos] ))) return TRUE;
157 if (res < 0) max = pos - 1;
158 else min = pos + 1;
160 return FALSE;
164 /***********************************************************************
165 * set_server_info
167 * Set class info with the wine server.
169 static BOOL set_server_info( HWND hwnd, INT offset, LONG_PTR newval, UINT size )
171 BOOL ret;
173 SERVER_START_REQ( set_class_info )
175 req->window = wine_server_user_handle( hwnd );
176 req->extra_offset = -1;
177 switch(offset)
179 case GCW_ATOM:
180 req->flags = SET_CLASS_ATOM;
181 req->atom = LOWORD(newval);
182 break;
183 case GCL_STYLE:
184 req->flags = SET_CLASS_STYLE;
185 req->style = newval;
186 break;
187 case GCL_CBWNDEXTRA:
188 req->flags = SET_CLASS_WINEXTRA;
189 req->win_extra = newval;
190 break;
191 case GCLP_HMODULE:
192 req->flags = SET_CLASS_INSTANCE;
193 req->instance = wine_server_client_ptr( (void *)newval );
194 break;
195 default:
196 assert( offset >= 0 );
197 req->flags = SET_CLASS_EXTRA;
198 req->extra_offset = offset;
199 req->extra_size = size;
200 if ( size == sizeof(LONG) )
202 LONG newlong = newval;
203 memcpy( &req->extra_value, &newlong, sizeof(LONG) );
205 else
206 memcpy( &req->extra_value, &newval, sizeof(LONG_PTR) );
207 break;
209 ret = !wine_server_call_err( req );
211 SERVER_END_REQ;
212 return ret;
216 /***********************************************************************
217 * CLASS_GetMenuNameA
219 * Get the menu name as a ASCII string.
221 static inline LPSTR CLASS_GetMenuNameA( CLASS *classPtr )
223 if (IS_INTRESOURCE(classPtr->menuName)) return (LPSTR)classPtr->menuName;
224 return (LPSTR)(classPtr->menuName + strlenW(classPtr->menuName) + 1);
228 /***********************************************************************
229 * CLASS_GetMenuNameW
231 * Get the menu name as a Unicode string.
233 static inline LPWSTR CLASS_GetMenuNameW( CLASS *classPtr )
235 return classPtr->menuName;
239 /***********************************************************************
240 * CLASS_SetMenuNameA
242 * Set the menu name in a class structure by copying the string.
244 static void CLASS_SetMenuNameA( CLASS *classPtr, LPCSTR name )
246 if (!IS_INTRESOURCE(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
247 if (!IS_INTRESOURCE(name))
249 DWORD lenA = strlen(name) + 1;
250 DWORD lenW = MultiByteToWideChar( CP_ACP, 0, name, lenA, NULL, 0 );
251 classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
252 MultiByteToWideChar( CP_ACP, 0, name, lenA, classPtr->menuName, lenW );
253 memcpy( classPtr->menuName + lenW, name, lenA );
255 else classPtr->menuName = (LPWSTR)name;
259 /***********************************************************************
260 * CLASS_SetMenuNameW
262 * Set the menu name in a class structure by copying the string.
264 static void CLASS_SetMenuNameW( CLASS *classPtr, LPCWSTR name )
266 if (!IS_INTRESOURCE(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
267 if (!IS_INTRESOURCE(name))
269 DWORD lenW = strlenW(name) + 1;
270 DWORD lenA = WideCharToMultiByte( CP_ACP, 0, name, lenW, NULL, 0, NULL, NULL );
271 classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
272 memcpy( classPtr->menuName, name, lenW*sizeof(WCHAR) );
273 WideCharToMultiByte( CP_ACP, 0, name, lenW,
274 (char *)(classPtr->menuName + lenW), lenA, NULL, NULL );
276 else classPtr->menuName = (LPWSTR)name;
280 /***********************************************************************
281 * CLASS_FreeClass
283 * Free a class structure.
285 static void CLASS_FreeClass( CLASS *classPtr )
287 TRACE("%p\n", classPtr);
289 USER_Lock();
291 if (classPtr->dce) free_dce( classPtr->dce, 0 );
292 list_remove( &classPtr->entry );
293 if (classPtr->hbrBackground > (HBRUSH)(COLOR_GRADIENTINACTIVECAPTION + 1))
294 DeleteObject( classPtr->hbrBackground );
295 HeapFree( GetProcessHeap(), 0, classPtr->menuName );
296 HeapFree( GetProcessHeap(), 0, classPtr );
297 USER_Unlock();
301 /***********************************************************************
302 * CLASS_FindClass
304 * Return a pointer to the class.
306 static CLASS *CLASS_FindClass( LPCWSTR name, HINSTANCE hinstance )
308 static const WCHAR comctl32W[] = {'c','o','m','c','t','l','3','2','.','d','l','l',0};
309 struct list *ptr;
310 ATOM atom = get_int_atom_value( name );
312 GetDesktopWindow(); /* create the desktop window to trigger builtin class registration */
314 if (!name) return NULL;
316 for (;;)
318 USER_Lock();
320 LIST_FOR_EACH( ptr, &class_list )
322 CLASS *class = LIST_ENTRY( ptr, CLASS, entry );
323 if (atom)
325 if (class->atomName != atom) continue;
327 else
329 if (strcmpiW( class->name, name )) continue;
331 if (!class->local || class->hInstance == hinstance)
333 TRACE("%s %p -> %p\n", debugstr_w(name), hinstance, class);
334 return class;
337 USER_Unlock();
339 if (atom) break;
340 if (!is_comctl32_class( name )) break;
341 if (GetModuleHandleW( comctl32W )) break;
342 if (!LoadLibraryW( comctl32W )) break;
343 TRACE( "%s retrying after loading comctl32\n", debugstr_w(name) );
346 TRACE("%s %p -> not found\n", debugstr_w(name), hinstance);
347 return NULL;
351 /***********************************************************************
352 * CLASS_RegisterClass
354 * The real RegisterClass() functionality.
356 static CLASS *CLASS_RegisterClass( LPCWSTR name, HINSTANCE hInstance, BOOL local,
357 DWORD style, INT classExtra, INT winExtra )
359 CLASS *classPtr;
360 BOOL ret;
362 TRACE("name=%s hinst=%p style=0x%x clExtr=0x%x winExtr=0x%x\n",
363 debugstr_w(name), hInstance, style, classExtra, winExtra );
365 /* Fix the extra bytes value */
367 if (classExtra > 40) /* Extra bytes are limited to 40 in Win32 */
368 WARN("Class extra bytes %d is > 40\n", classExtra);
369 if (winExtra > 40) /* Extra bytes are limited to 40 in Win32 */
370 WARN("Win extra bytes %d is > 40\n", winExtra );
372 classPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLASS) + classExtra );
373 if (!classPtr) return NULL;
375 classPtr->atomName = get_int_atom_value( name );
376 if (!classPtr->atomName && name) strcpyW( classPtr->name, name );
377 else GlobalGetAtomNameW( classPtr->atomName, classPtr->name, sizeof(classPtr->name)/sizeof(WCHAR) );
379 SERVER_START_REQ( create_class )
381 req->local = local;
382 req->style = style;
383 req->instance = wine_server_client_ptr( hInstance );
384 req->extra = classExtra;
385 req->win_extra = winExtra;
386 req->client_ptr = wine_server_client_ptr( classPtr );
387 req->atom = classPtr->atomName;
388 if (!req->atom && name) wine_server_add_data( req, name, strlenW(name) * sizeof(WCHAR) );
389 ret = !wine_server_call_err( req );
390 classPtr->atomName = reply->atom;
392 SERVER_END_REQ;
393 if (!ret)
395 HeapFree( GetProcessHeap(), 0, classPtr );
396 return NULL;
399 classPtr->style = style;
400 classPtr->local = local;
401 classPtr->cbWndExtra = winExtra;
402 classPtr->cbClsExtra = classExtra;
403 classPtr->hInstance = hInstance;
405 /* Other non-null values must be set by caller */
407 USER_Lock();
408 if (local) list_add_head( &class_list, &classPtr->entry );
409 else list_add_tail( &class_list, &classPtr->entry );
410 return classPtr;
414 /***********************************************************************
415 * register_builtin
417 * Register a builtin control class.
418 * This allows having both ASCII and Unicode winprocs for the same class.
420 static void register_builtin( const struct builtin_class_descr *descr )
422 CLASS *classPtr;
424 if (!(classPtr = CLASS_RegisterClass( descr->name, user32_module, FALSE,
425 descr->style, 0, descr->extra ))) return;
427 if (descr->cursor) classPtr->hCursor = LoadCursorA( 0, (LPSTR)descr->cursor );
428 classPtr->hbrBackground = descr->brush;
429 classPtr->winproc = BUILTIN_WINPROC( descr->proc );
430 release_class_ptr( classPtr );
434 /***********************************************************************
435 * register_builtins
437 static BOOL WINAPI register_builtins( INIT_ONCE *once, void *param, void **context )
439 register_builtin( &BUTTON_builtin_class );
440 register_builtin( &COMBO_builtin_class );
441 register_builtin( &COMBOLBOX_builtin_class );
442 register_builtin( &DIALOG_builtin_class );
443 register_builtin( &EDIT_builtin_class );
444 register_builtin( &ICONTITLE_builtin_class );
445 register_builtin( &LISTBOX_builtin_class );
446 register_builtin( &MDICLIENT_builtin_class );
447 register_builtin( &MENU_builtin_class );
448 register_builtin( &SCROLL_builtin_class );
449 register_builtin( &STATIC_builtin_class );
450 return TRUE;
454 /***********************************************************************
455 * register_builtin_classes
457 void register_builtin_classes(void)
459 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
463 /***********************************************************************
464 * register_desktop_class
466 void register_desktop_class(void)
468 register_builtin( &DESKTOP_builtin_class );
469 register_builtin( &MESSAGE_builtin_class );
473 /***********************************************************************
474 * get_class_winproc
476 WNDPROC get_class_winproc( CLASS *class )
478 return class->winproc;
482 /***********************************************************************
483 * get_class_dce
485 struct dce *get_class_dce( CLASS *class )
487 return class->dce;
491 /***********************************************************************
492 * set_class_dce
494 struct dce *set_class_dce( CLASS *class, struct dce *dce )
496 if (class->dce) return class->dce; /* already set, don't change it */
497 class->dce = dce;
498 return dce;
502 /***********************************************************************
503 * RegisterClassA (USER32.@)
505 * Register a window class.
507 * RETURNS
508 * >0: Unique identifier
509 * 0: Failure
511 ATOM WINAPI RegisterClassA( const WNDCLASSA* wc ) /* [in] Address of structure with class data */
513 WNDCLASSEXA wcex;
515 wcex.cbSize = sizeof(wcex);
516 wcex.style = wc->style;
517 wcex.lpfnWndProc = wc->lpfnWndProc;
518 wcex.cbClsExtra = wc->cbClsExtra;
519 wcex.cbWndExtra = wc->cbWndExtra;
520 wcex.hInstance = wc->hInstance;
521 wcex.hIcon = wc->hIcon;
522 wcex.hCursor = wc->hCursor;
523 wcex.hbrBackground = wc->hbrBackground;
524 wcex.lpszMenuName = wc->lpszMenuName;
525 wcex.lpszClassName = wc->lpszClassName;
526 wcex.hIconSm = 0;
527 return RegisterClassExA( &wcex );
531 /***********************************************************************
532 * RegisterClassW (USER32.@)
534 * See RegisterClassA.
536 ATOM WINAPI RegisterClassW( const WNDCLASSW* wc )
538 WNDCLASSEXW wcex;
540 wcex.cbSize = sizeof(wcex);
541 wcex.style = wc->style;
542 wcex.lpfnWndProc = wc->lpfnWndProc;
543 wcex.cbClsExtra = wc->cbClsExtra;
544 wcex.cbWndExtra = wc->cbWndExtra;
545 wcex.hInstance = wc->hInstance;
546 wcex.hIcon = wc->hIcon;
547 wcex.hCursor = wc->hCursor;
548 wcex.hbrBackground = wc->hbrBackground;
549 wcex.lpszMenuName = wc->lpszMenuName;
550 wcex.lpszClassName = wc->lpszClassName;
551 wcex.hIconSm = 0;
552 return RegisterClassExW( &wcex );
556 /***********************************************************************
557 * RegisterClassExA (USER32.@)
559 ATOM WINAPI RegisterClassExA( const WNDCLASSEXA* wc )
561 ATOM atom;
562 CLASS *classPtr;
563 HINSTANCE instance;
565 GetDesktopWindow(); /* create the desktop window to trigger builtin class registration */
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 (!IS_INTRESOURCE(wc->lpszClassName))
577 WCHAR name[MAX_ATOM_LEN + 1];
579 if (!MultiByteToWideChar( CP_ACP, 0, wc->lpszClassName, -1, name, MAX_ATOM_LEN + 1 )) return 0;
580 classPtr = CLASS_RegisterClass( name, instance, !(wc->style & CS_GLOBALCLASS),
581 wc->style, wc->cbClsExtra, wc->cbWndExtra );
583 else
585 classPtr = CLASS_RegisterClass( (LPCWSTR)wc->lpszClassName, instance,
586 !(wc->style & CS_GLOBALCLASS), wc->style,
587 wc->cbClsExtra, wc->cbWndExtra );
589 if (!classPtr) return 0;
590 atom = classPtr->atomName;
592 TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
593 debugstr_a(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
594 wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
596 classPtr->hIcon = wc->hIcon;
597 classPtr->hIconSm = wc->hIconSm;
598 classPtr->hIconSmIntern = wc->hIcon && !wc->hIconSm ?
599 CopyImage( wc->hIcon, IMAGE_ICON,
600 GetSystemMetrics( SM_CXSMICON ),
601 GetSystemMetrics( SM_CYSMICON ), 0 ) : NULL;
602 classPtr->hCursor = wc->hCursor;
603 classPtr->hbrBackground = wc->hbrBackground;
604 classPtr->winproc = WINPROC_AllocProc( wc->lpfnWndProc, FALSE );
605 CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
606 release_class_ptr( classPtr );
607 return atom;
611 /***********************************************************************
612 * RegisterClassExW (USER32.@)
614 ATOM WINAPI RegisterClassExW( const WNDCLASSEXW* wc )
616 ATOM atom;
617 CLASS *classPtr;
618 HINSTANCE instance;
620 GetDesktopWindow(); /* create the desktop window to trigger builtin class registration */
622 if (wc->cbSize != sizeof(*wc) || wc->cbClsExtra < 0 || wc->cbWndExtra < 0 ||
623 wc->hInstance == user32_module) /* we can't register a class for user32 */
625 SetLastError( ERROR_INVALID_PARAMETER );
626 return 0;
628 if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
630 if (!(classPtr = CLASS_RegisterClass( wc->lpszClassName, instance, !(wc->style & CS_GLOBALCLASS),
631 wc->style, wc->cbClsExtra, wc->cbWndExtra )))
632 return 0;
634 atom = classPtr->atomName;
636 TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
637 debugstr_w(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
638 wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
640 classPtr->hIcon = wc->hIcon;
641 classPtr->hIconSm = wc->hIconSm;
642 classPtr->hIconSmIntern = wc->hIcon && !wc->hIconSm ?
643 CopyImage( wc->hIcon, IMAGE_ICON,
644 GetSystemMetrics( SM_CXSMICON ),
645 GetSystemMetrics( SM_CYSMICON ), 0 ) : NULL;
646 classPtr->hCursor = wc->hCursor;
647 classPtr->hbrBackground = wc->hbrBackground;
648 classPtr->winproc = WINPROC_AllocProc( wc->lpfnWndProc, TRUE );
649 CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
650 release_class_ptr( classPtr );
651 return atom;
655 /***********************************************************************
656 * UnregisterClassA (USER32.@)
658 BOOL WINAPI UnregisterClassA( LPCSTR className, HINSTANCE hInstance )
660 if (!IS_INTRESOURCE(className))
662 WCHAR name[MAX_ATOM_LEN + 1];
664 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, name, MAX_ATOM_LEN + 1 ))
665 return FALSE;
666 return UnregisterClassW( name, hInstance );
668 return UnregisterClassW( (LPCWSTR)className, hInstance );
671 /***********************************************************************
672 * UnregisterClassW (USER32.@)
674 BOOL WINAPI UnregisterClassW( LPCWSTR className, HINSTANCE hInstance )
676 CLASS *classPtr = NULL;
678 GetDesktopWindow(); /* create the desktop window to trigger builtin class registration */
680 SERVER_START_REQ( destroy_class )
682 req->instance = wine_server_client_ptr( hInstance );
683 if (!(req->atom = get_int_atom_value(className)) && className)
684 wine_server_add_data( req, className, strlenW(className) * sizeof(WCHAR) );
685 if (!wine_server_call_err( req )) classPtr = wine_server_get_ptr( reply->client_ptr );
687 SERVER_END_REQ;
689 if (classPtr) CLASS_FreeClass( classPtr );
690 return (classPtr != NULL);
694 /***********************************************************************
695 * GetClassWord (USER32.@)
697 WORD WINAPI GetClassWord( HWND hwnd, INT offset )
699 CLASS *class;
700 WORD retvalue = 0;
702 if (offset < 0) return GetClassLongA( hwnd, offset );
704 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
706 if (class == CLASS_OTHER_PROCESS)
708 SERVER_START_REQ( set_class_info )
710 req->window = wine_server_user_handle( hwnd );
711 req->flags = 0;
712 req->extra_offset = offset;
713 req->extra_size = sizeof(retvalue);
714 if (!wine_server_call_err( req ))
715 memcpy( &retvalue, &reply->old_extra_value, sizeof(retvalue) );
717 SERVER_END_REQ;
718 return retvalue;
721 if (offset <= class->cbClsExtra - sizeof(WORD))
722 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(retvalue) );
723 else
724 SetLastError( ERROR_INVALID_INDEX );
725 release_class_ptr( class );
726 return retvalue;
730 /***********************************************************************
731 * CLASS_GetClassLong
733 * Implementation of GetClassLong(Ptr)A/W
735 static ULONG_PTR CLASS_GetClassLong( HWND hwnd, INT offset, UINT size,
736 BOOL unicode )
738 CLASS *class;
739 ULONG_PTR retvalue = 0;
741 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
743 if (class == CLASS_OTHER_PROCESS)
745 SERVER_START_REQ( set_class_info )
747 req->window = wine_server_user_handle( hwnd );
748 req->flags = 0;
749 req->extra_offset = (offset >= 0) ? offset : -1;
750 req->extra_size = (offset >= 0) ? size : 0;
751 if (!wine_server_call_err( req ))
753 switch(offset)
755 case GCLP_HBRBACKGROUND:
756 case GCLP_HCURSOR:
757 case GCLP_HICON:
758 case GCLP_HICONSM:
759 case GCLP_WNDPROC:
760 case GCLP_MENUNAME:
761 FIXME( "offset %d (%s) not supported on other process window %p\n",
762 offset, SPY_GetClassLongOffsetName(offset), hwnd );
763 SetLastError( ERROR_INVALID_HANDLE );
764 break;
765 case GCL_STYLE:
766 retvalue = reply->old_style;
767 break;
768 case GCL_CBWNDEXTRA:
769 retvalue = reply->old_win_extra;
770 break;
771 case GCL_CBCLSEXTRA:
772 retvalue = reply->old_extra;
773 break;
774 case GCLP_HMODULE:
775 retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
776 break;
777 case GCW_ATOM:
778 retvalue = reply->old_atom;
779 break;
780 default:
781 if (offset >= 0)
783 if (size == sizeof(DWORD))
785 DWORD retdword;
786 memcpy( &retdword, &reply->old_extra_value, sizeof(DWORD) );
787 retvalue = retdword;
789 else
790 memcpy( &retvalue, &reply->old_extra_value,
791 sizeof(ULONG_PTR) );
793 else SetLastError( ERROR_INVALID_INDEX );
794 break;
798 SERVER_END_REQ;
799 return retvalue;
802 if (offset >= 0)
804 if (offset <= class->cbClsExtra - size)
806 if (size == sizeof(DWORD))
808 DWORD retdword;
809 memcpy( &retdword, (char *)(class + 1) + offset, sizeof(DWORD) );
810 retvalue = retdword;
812 else
813 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(ULONG_PTR) );
815 else
816 SetLastError( ERROR_INVALID_INDEX );
817 release_class_ptr( class );
818 return retvalue;
821 switch(offset)
823 case GCLP_HBRBACKGROUND:
824 retvalue = (ULONG_PTR)class->hbrBackground;
825 break;
826 case GCLP_HCURSOR:
827 retvalue = (ULONG_PTR)class->hCursor;
828 break;
829 case GCLP_HICON:
830 retvalue = (ULONG_PTR)class->hIcon;
831 break;
832 case GCLP_HICONSM:
833 retvalue = (ULONG_PTR)(class->hIconSm ? class->hIconSm : class->hIconSmIntern);
834 break;
835 case GCL_STYLE:
836 retvalue = class->style;
837 break;
838 case GCL_CBWNDEXTRA:
839 retvalue = class->cbWndExtra;
840 break;
841 case GCL_CBCLSEXTRA:
842 retvalue = class->cbClsExtra;
843 break;
844 case GCLP_HMODULE:
845 retvalue = (ULONG_PTR)class->hInstance;
846 break;
847 case GCLP_WNDPROC:
848 retvalue = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
849 break;
850 case GCLP_MENUNAME:
851 retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
852 if (unicode)
853 retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
854 else
855 retvalue = (ULONG_PTR)CLASS_GetMenuNameA( class );
856 break;
857 case GCW_ATOM:
858 retvalue = class->atomName;
859 break;
860 default:
861 SetLastError( ERROR_INVALID_INDEX );
862 break;
864 release_class_ptr( class );
865 return retvalue;
869 /***********************************************************************
870 * GetClassLongW (USER32.@)
872 DWORD WINAPI GetClassLongW( HWND hwnd, INT offset )
874 return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), TRUE );
879 /***********************************************************************
880 * GetClassLongA (USER32.@)
882 DWORD WINAPI GetClassLongA( HWND hwnd, INT offset )
884 return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), FALSE );
888 /***********************************************************************
889 * SetClassWord (USER32.@)
891 WORD WINAPI SetClassWord( HWND hwnd, INT offset, WORD newval )
893 CLASS *class;
894 WORD retval = 0;
896 if (offset < 0) return SetClassLongA( hwnd, offset, (DWORD)newval );
898 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
900 SERVER_START_REQ( set_class_info )
902 req->window = wine_server_user_handle( hwnd );
903 req->flags = SET_CLASS_EXTRA;
904 req->extra_offset = offset;
905 req->extra_size = sizeof(newval);
906 memcpy( &req->extra_value, &newval, sizeof(newval) );
907 if (!wine_server_call_err( req ))
909 void *ptr = (char *)(class + 1) + offset;
910 memcpy( &retval, ptr, sizeof(retval) );
911 memcpy( ptr, &newval, sizeof(newval) );
914 SERVER_END_REQ;
915 release_class_ptr( class );
916 return retval;
920 /***********************************************************************
921 * CLASS_SetClassLong
923 * Implementation of SetClassLong(Ptr)A/W
925 static ULONG_PTR CLASS_SetClassLong( HWND hwnd, INT offset, LONG_PTR newval,
926 UINT size, BOOL unicode )
928 CLASS *class;
929 ULONG_PTR retval = 0;
931 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
933 if (offset >= 0)
935 if (set_server_info( hwnd, offset, newval, size ))
937 void *ptr = (char *)(class + 1) + offset;
938 if ( size == sizeof(LONG) )
940 DWORD retdword;
941 LONG newlong = newval;
942 memcpy( &retdword, ptr, sizeof(DWORD) );
943 memcpy( ptr, &newlong, sizeof(LONG) );
944 retval = retdword;
946 else
948 memcpy( &retval, ptr, sizeof(ULONG_PTR) );
949 memcpy( ptr, &newval, sizeof(LONG_PTR) );
953 else switch(offset)
955 case GCLP_MENUNAME:
956 if ( unicode )
957 CLASS_SetMenuNameW( class, (LPCWSTR)newval );
958 else
959 CLASS_SetMenuNameA( class, (LPCSTR)newval );
960 retval = 0; /* Old value is now meaningless anyway */
961 break;
962 case GCLP_WNDPROC:
963 retval = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
964 class->winproc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
965 break;
966 case GCLP_HBRBACKGROUND:
967 retval = (ULONG_PTR)class->hbrBackground;
968 class->hbrBackground = (HBRUSH)newval;
969 break;
970 case GCLP_HCURSOR:
971 retval = (ULONG_PTR)class->hCursor;
972 class->hCursor = (HCURSOR)newval;
973 break;
974 case GCLP_HICON:
975 retval = (ULONG_PTR)class->hIcon;
976 if (retval && class->hIconSmIntern)
978 DestroyIcon(class->hIconSmIntern);
979 class->hIconSmIntern = NULL;
981 if (newval && !class->hIconSm)
982 class->hIconSmIntern = CopyImage( (HICON)newval, IMAGE_ICON,
983 GetSystemMetrics( SM_CXSMICON ), GetSystemMetrics( SM_CYSMICON ), 0 );
984 class->hIcon = (HICON)newval;
985 break;
986 case GCLP_HICONSM:
987 retval = (ULONG_PTR)class->hIconSm;
988 if (retval && !newval)
989 class->hIconSmIntern = class->hIcon ? CopyImage( class->hIcon, IMAGE_ICON,
990 GetSystemMetrics( SM_CXSMICON ),
991 GetSystemMetrics( SM_CYSMICON ), 0 ) : NULL;
992 else if (!retval && newval && class->hIconSmIntern)
994 DestroyIcon(class->hIconSmIntern);
995 class->hIconSmIntern = NULL;
997 class->hIconSm = (HICON)newval;
998 break;
999 case GCL_STYLE:
1000 if (!set_server_info( hwnd, offset, newval, size )) break;
1001 retval = class->style;
1002 class->style = newval;
1003 break;
1004 case GCL_CBWNDEXTRA:
1005 if (!set_server_info( hwnd, offset, newval, size )) break;
1006 retval = class->cbWndExtra;
1007 class->cbWndExtra = newval;
1008 break;
1009 case GCLP_HMODULE:
1010 if (!set_server_info( hwnd, offset, newval, size )) break;
1011 retval = (ULONG_PTR)class->hInstance;
1012 class->hInstance = (HINSTANCE)newval;
1013 break;
1014 case GCW_ATOM:
1015 if (!set_server_info( hwnd, offset, newval, size )) break;
1016 retval = class->atomName;
1017 class->atomName = newval;
1018 GlobalGetAtomNameW( newval, class->name, sizeof(class->name)/sizeof(WCHAR) );
1019 break;
1020 case GCL_CBCLSEXTRA: /* cannot change this one */
1021 SetLastError( ERROR_INVALID_PARAMETER );
1022 break;
1023 default:
1024 SetLastError( ERROR_INVALID_INDEX );
1025 break;
1027 release_class_ptr( class );
1028 return retval;
1032 /***********************************************************************
1033 * SetClassLongW (USER32.@)
1035 DWORD WINAPI SetClassLongW( HWND hwnd, INT offset, LONG newval )
1037 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), TRUE );
1041 /***********************************************************************
1042 * SetClassLongA (USER32.@)
1044 DWORD WINAPI SetClassLongA( HWND hwnd, INT offset, LONG newval )
1046 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), FALSE );
1050 /***********************************************************************
1051 * GetClassNameA (USER32.@)
1053 INT WINAPI GetClassNameA( HWND hwnd, LPSTR buffer, INT count )
1055 WCHAR tmpbuf[MAX_ATOM_LEN + 1];
1056 DWORD len;
1058 if (count <= 0) return 0;
1059 if (!GetClassNameW( hwnd, tmpbuf, sizeof(tmpbuf)/sizeof(WCHAR) )) return 0;
1060 RtlUnicodeToMultiByteN( buffer, count - 1, &len, tmpbuf, strlenW(tmpbuf) * sizeof(WCHAR) );
1061 buffer[len] = 0;
1062 return len;
1066 /***********************************************************************
1067 * GetClassNameW (USER32.@)
1069 INT WINAPI GetClassNameW( HWND hwnd, LPWSTR buffer, INT count )
1071 CLASS *class;
1072 INT ret;
1074 TRACE("%p %p %d\n", hwnd, buffer, count);
1076 if (count <= 0) return 0;
1078 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
1080 if (class == CLASS_OTHER_PROCESS)
1082 WCHAR tmpbuf[MAX_ATOM_LEN + 1];
1084 ret = GlobalGetAtomNameW( GetClassLongW( hwnd, GCW_ATOM ), tmpbuf, MAX_ATOM_LEN + 1 );
1085 if (ret)
1087 ret = min(count - 1, ret);
1088 memcpy(buffer, tmpbuf, ret * sizeof(WCHAR));
1089 buffer[ret] = 0;
1092 else
1094 lstrcpynW( buffer, class->name, count );
1095 release_class_ptr( class );
1096 ret = strlenW( buffer );
1098 return ret;
1102 /***********************************************************************
1103 * RealGetWindowClassA (USER32.@)
1105 UINT WINAPI RealGetWindowClassA( HWND hwnd, LPSTR buffer, UINT count )
1107 return GetClassNameA( hwnd, buffer, count );
1111 /***********************************************************************
1112 * RealGetWindowClassW (USER32.@)
1114 UINT WINAPI RealGetWindowClassW( HWND hwnd, LPWSTR buffer, UINT count )
1116 return GetClassNameW( hwnd, buffer, count );
1120 /***********************************************************************
1121 * GetClassInfoA (USER32.@)
1123 BOOL WINAPI GetClassInfoA( HINSTANCE hInstance, LPCSTR name, WNDCLASSA *wc )
1125 WNDCLASSEXA wcex;
1126 UINT ret = GetClassInfoExA( hInstance, name, &wcex );
1128 if (ret)
1130 wc->style = wcex.style;
1131 wc->lpfnWndProc = wcex.lpfnWndProc;
1132 wc->cbClsExtra = wcex.cbClsExtra;
1133 wc->cbWndExtra = wcex.cbWndExtra;
1134 wc->hInstance = wcex.hInstance;
1135 wc->hIcon = wcex.hIcon;
1136 wc->hCursor = wcex.hCursor;
1137 wc->hbrBackground = wcex.hbrBackground;
1138 wc->lpszMenuName = wcex.lpszMenuName;
1139 wc->lpszClassName = wcex.lpszClassName;
1141 return ret;
1145 /***********************************************************************
1146 * GetClassInfoW (USER32.@)
1148 BOOL WINAPI GetClassInfoW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSW *wc )
1150 WNDCLASSEXW wcex;
1151 UINT ret = GetClassInfoExW( hInstance, name, &wcex );
1153 if (ret)
1155 wc->style = wcex.style;
1156 wc->lpfnWndProc = wcex.lpfnWndProc;
1157 wc->cbClsExtra = wcex.cbClsExtra;
1158 wc->cbWndExtra = wcex.cbWndExtra;
1159 wc->hInstance = wcex.hInstance;
1160 wc->hIcon = wcex.hIcon;
1161 wc->hCursor = wcex.hCursor;
1162 wc->hbrBackground = wcex.hbrBackground;
1163 wc->lpszMenuName = wcex.lpszMenuName;
1164 wc->lpszClassName = wcex.lpszClassName;
1166 return ret;
1170 /***********************************************************************
1171 * GetClassInfoExA (USER32.@)
1173 BOOL WINAPI GetClassInfoExA( HINSTANCE hInstance, LPCSTR name, WNDCLASSEXA *wc )
1175 ATOM atom;
1176 CLASS *classPtr;
1178 TRACE("%p %s %p\n", hInstance, debugstr_a(name), wc);
1180 if (!wc)
1182 SetLastError( ERROR_NOACCESS );
1183 return FALSE;
1186 if (!hInstance) hInstance = user32_module;
1188 if (!IS_INTRESOURCE(name))
1190 WCHAR nameW[MAX_ATOM_LEN + 1];
1191 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, sizeof(nameW)/sizeof(WCHAR) ))
1192 return FALSE;
1193 classPtr = CLASS_FindClass( nameW, hInstance );
1195 else classPtr = CLASS_FindClass( (LPCWSTR)name, hInstance );
1197 if (!classPtr)
1199 SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1200 return FALSE;
1202 wc->style = classPtr->style;
1203 wc->lpfnWndProc = WINPROC_GetProc( classPtr->winproc, FALSE );
1204 wc->cbClsExtra = classPtr->cbClsExtra;
1205 wc->cbWndExtra = classPtr->cbWndExtra;
1206 wc->hInstance = (hInstance == user32_module) ? 0 : hInstance;
1207 wc->hIcon = classPtr->hIcon;
1208 wc->hIconSm = classPtr->hIconSm ? classPtr->hIconSm : classPtr->hIconSmIntern;
1209 wc->hCursor = classPtr->hCursor;
1210 wc->hbrBackground = classPtr->hbrBackground;
1211 wc->lpszMenuName = CLASS_GetMenuNameA( classPtr );
1212 wc->lpszClassName = name;
1213 atom = classPtr->atomName;
1214 release_class_ptr( classPtr );
1216 /* We must return the atom of the class here instead of just TRUE. */
1217 return atom;
1221 /***********************************************************************
1222 * GetClassInfoExW (USER32.@)
1224 BOOL WINAPI GetClassInfoExW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSEXW *wc )
1226 ATOM atom;
1227 CLASS *classPtr;
1229 TRACE("%p %s %p\n", hInstance, debugstr_w(name), wc);
1231 if (!wc)
1233 SetLastError( ERROR_NOACCESS );
1234 return FALSE;
1237 if (!hInstance) hInstance = user32_module;
1239 if (!(classPtr = CLASS_FindClass( name, hInstance )))
1241 SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1242 return FALSE;
1244 wc->style = classPtr->style;
1245 wc->lpfnWndProc = WINPROC_GetProc( classPtr->winproc, TRUE );
1246 wc->cbClsExtra = classPtr->cbClsExtra;
1247 wc->cbWndExtra = classPtr->cbWndExtra;
1248 wc->hInstance = (hInstance == user32_module) ? 0 : hInstance;
1249 wc->hIcon = classPtr->hIcon;
1250 wc->hIconSm = classPtr->hIconSm ? classPtr->hIconSm : classPtr->hIconSmIntern;
1251 wc->hCursor = classPtr->hCursor;
1252 wc->hbrBackground = classPtr->hbrBackground;
1253 wc->lpszMenuName = CLASS_GetMenuNameW( classPtr );
1254 wc->lpszClassName = name;
1255 atom = classPtr->atomName;
1256 release_class_ptr( classPtr );
1258 /* We must return the atom of the class here instead of just TRUE. */
1259 return atom;
1263 #if 0 /* toolhelp is in kernel, so this cannot work */
1265 /***********************************************************************
1266 * ClassFirst (TOOLHELP.69)
1268 BOOL16 WINAPI ClassFirst16( CLASSENTRY *pClassEntry )
1270 TRACE("%p\n",pClassEntry);
1271 pClassEntry->wNext = 1;
1272 return ClassNext16( pClassEntry );
1276 /***********************************************************************
1277 * ClassNext (TOOLHELP.70)
1279 BOOL16 WINAPI ClassNext16( CLASSENTRY *pClassEntry )
1281 int i;
1282 CLASS *class = firstClass;
1284 TRACE("%p\n",pClassEntry);
1286 if (!pClassEntry->wNext) return FALSE;
1287 for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1288 if (!class)
1290 pClassEntry->wNext = 0;
1291 return FALSE;
1293 pClassEntry->hInst = class->hInstance;
1294 pClassEntry->wNext++;
1295 GlobalGetAtomNameA( class->atomName, pClassEntry->szClassName,
1296 sizeof(pClassEntry->szClassName) );
1297 return TRUE;
1299 #endif
1301 /* 64bit versions */
1303 #ifdef GetClassLongPtrA
1304 #undef GetClassLongPtrA
1305 #endif
1307 #ifdef GetClassLongPtrW
1308 #undef GetClassLongPtrW
1309 #endif
1311 #ifdef SetClassLongPtrA
1312 #undef SetClassLongPtrA
1313 #endif
1315 #ifdef SetClassLongPtrW
1316 #undef SetClassLongPtrW
1317 #endif
1319 /***********************************************************************
1320 * GetClassLongPtrA (USER32.@)
1322 ULONG_PTR WINAPI GetClassLongPtrA( HWND hwnd, INT offset )
1324 return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), FALSE );
1327 /***********************************************************************
1328 * GetClassLongPtrW (USER32.@)
1330 ULONG_PTR WINAPI GetClassLongPtrW( HWND hwnd, INT offset )
1332 return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), TRUE );
1335 /***********************************************************************
1336 * SetClassLongPtrW (USER32.@)
1338 ULONG_PTR WINAPI SetClassLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
1340 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), TRUE );
1343 /***********************************************************************
1344 * SetClassLongPtrA (USER32.@)
1346 ULONG_PTR WINAPI SetClassLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
1348 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), FALSE );