server: Make module handles always 64-bit.
[wine] / dlls / user32 / class.c
1 /*
2  * Window classes functions
3  *
4  * Copyright 1993, 1996, 2003 Alexandre Julliard
5  * Copyright 1998 Juergen Schmied (jsch)
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29
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"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(class);
43
44 #define MAX_ATOM_LEN 255 /* from dlls/kernel32/atom.c */
45
46 typedef struct tagCLASS
47 {
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     HCURSOR          hCursor;       /* Default cursor */
60     HBRUSH           hbrBackground; /* Default background */
61     ATOM             atomName;      /* Name of the class */
62     WCHAR            name[MAX_ATOM_LEN + 1];
63 } CLASS;
64
65 static struct list class_list = LIST_INIT( class_list );
66
67 #define CLASS_OTHER_PROCESS ((CLASS *)1)
68
69 /***********************************************************************
70  *           get_class_ptr
71  */
72 static CLASS *get_class_ptr( HWND hwnd, BOOL write_access )
73 {
74     WND *ptr = WIN_GetPtr( hwnd );
75
76     if (ptr)
77     {
78         if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP) return ptr->class;
79         if (!write_access) return CLASS_OTHER_PROCESS;
80
81         /* modifying classes in other processes is not allowed */
82         if (ptr == WND_DESKTOP || IsWindow( hwnd ))
83         {
84             SetLastError( ERROR_ACCESS_DENIED );
85             return NULL;
86         }
87     }
88     SetLastError( ERROR_INVALID_WINDOW_HANDLE );
89     return NULL;
90 }
91
92
93 /***********************************************************************
94  *           release_class_ptr
95  */
96 static inline void release_class_ptr( CLASS *ptr )
97 {
98     USER_Unlock();
99 }
100
101
102 /***********************************************************************
103  *           get_int_atom_value
104  */
105 ATOM get_int_atom_value( LPCWSTR name )
106 {
107     UINT ret = 0;
108
109     if (IS_INTRESOURCE(name)) return LOWORD(name);
110     if (*name++ != '#') return 0;
111     while (*name)
112     {
113         if (*name < '0' || *name > '9') return 0;
114         ret = ret * 10 + *name++ - '0';
115         if (ret > 0xffff) return 0;
116     }
117     return ret;
118 }
119
120
121 /***********************************************************************
122  *           set_server_info
123  *
124  * Set class info with the wine server.
125  */
126 static BOOL set_server_info( HWND hwnd, INT offset, LONG_PTR newval, UINT size )
127 {
128     BOOL ret;
129
130     SERVER_START_REQ( set_class_info )
131     {
132         req->window = wine_server_user_handle( hwnd );
133         req->extra_offset = -1;
134         switch(offset)
135         {
136         case GCW_ATOM:
137             req->flags = SET_CLASS_ATOM;
138             req->atom = newval;
139         case GCL_STYLE:
140             req->flags = SET_CLASS_STYLE;
141             req->style = newval;
142             break;
143         case GCL_CBWNDEXTRA:
144             req->flags = SET_CLASS_WINEXTRA;
145             req->win_extra = newval;
146             break;
147         case GCLP_HMODULE:
148             req->flags = SET_CLASS_INSTANCE;
149             req->instance = wine_server_client_ptr( (void *)newval );
150             break;
151         default:
152             assert( offset >= 0 );
153             req->flags = SET_CLASS_EXTRA;
154             req->extra_offset = offset;
155             req->extra_size = size;
156             if ( size == sizeof(LONG) )
157             {
158                 LONG newlong = newval;
159                 memcpy( &req->extra_value, &newlong, sizeof(LONG) );
160             }
161             else
162                 memcpy( &req->extra_value, &newval, sizeof(LONG_PTR) );
163             break;
164         }
165         ret = !wine_server_call_err( req );
166     }
167     SERVER_END_REQ;
168     return ret;
169 }
170
171
172 /***********************************************************************
173  *           CLASS_GetMenuNameA
174  *
175  * Get the menu name as a ASCII string.
176  */
177 static inline LPSTR CLASS_GetMenuNameA( CLASS *classPtr )
178 {
179     if (!HIWORD(classPtr->menuName)) return (LPSTR)classPtr->menuName;
180     return (LPSTR)(classPtr->menuName + strlenW(classPtr->menuName) + 1);
181 }
182
183
184 /***********************************************************************
185  *           CLASS_GetMenuNameW
186  *
187  * Get the menu name as a Unicode string.
188  */
189 static inline LPWSTR CLASS_GetMenuNameW( CLASS *classPtr )
190 {
191     return classPtr->menuName;
192 }
193
194
195 /***********************************************************************
196  *           CLASS_SetMenuNameA
197  *
198  * Set the menu name in a class structure by copying the string.
199  */
200 static void CLASS_SetMenuNameA( CLASS *classPtr, LPCSTR name )
201 {
202     if (HIWORD(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
203     if (HIWORD(name))
204     {
205         DWORD lenA = strlen(name) + 1;
206         DWORD lenW = MultiByteToWideChar( CP_ACP, 0, name, lenA, NULL, 0 );
207         classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
208         MultiByteToWideChar( CP_ACP, 0, name, lenA, classPtr->menuName, lenW );
209         memcpy( classPtr->menuName + lenW, name, lenA );
210     }
211     else classPtr->menuName = (LPWSTR)name;
212 }
213
214
215 /***********************************************************************
216  *           CLASS_SetMenuNameW
217  *
218  * Set the menu name in a class structure by copying the string.
219  */
220 static void CLASS_SetMenuNameW( CLASS *classPtr, LPCWSTR name )
221 {
222     if (HIWORD(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
223     if (HIWORD(name))
224     {
225         DWORD lenW = strlenW(name) + 1;
226         DWORD lenA = WideCharToMultiByte( CP_ACP, 0, name, lenW, NULL, 0, NULL, NULL );
227         classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
228         memcpy( classPtr->menuName, name, lenW*sizeof(WCHAR) );
229         WideCharToMultiByte( CP_ACP, 0, name, lenW,
230                              (char *)(classPtr->menuName + lenW), lenA, NULL, NULL );
231     }
232     else classPtr->menuName = (LPWSTR)name;
233 }
234
235
236 /***********************************************************************
237  *           CLASS_FreeClass
238  *
239  * Free a class structure.
240  */
241 static void CLASS_FreeClass( CLASS *classPtr )
242 {
243     TRACE("%p\n", classPtr);
244
245     USER_Lock();
246
247     if (classPtr->dce) free_dce( classPtr->dce, 0 );
248     list_remove( &classPtr->entry );
249     if (classPtr->hbrBackground > (HBRUSH)(COLOR_GRADIENTINACTIVECAPTION + 1))
250         DeleteObject( classPtr->hbrBackground );
251     HeapFree( GetProcessHeap(), 0, classPtr->menuName );
252     HeapFree( GetProcessHeap(), 0, classPtr );
253     USER_Unlock();
254 }
255
256
257 /***********************************************************************
258  *           CLASS_FreeModuleClasses
259  */
260 void CLASS_FreeModuleClasses( HMODULE16 hModule )
261 {
262     struct list *ptr, *next;
263
264     TRACE("0x%08x\n", hModule);
265
266     USER_Lock();
267     for (ptr = list_head( &class_list ); ptr; ptr = next)
268     {
269         CLASS *class = LIST_ENTRY( ptr, CLASS, entry );
270         next = list_next( &class_list, ptr );
271         if (class->hInstance == HINSTANCE_32(hModule))
272         {
273             BOOL ret;
274
275             SERVER_START_REQ( destroy_class )
276             {
277                 req->atom = class->atomName;
278                 req->instance = wine_server_client_ptr( class->hInstance );
279                 ret = !wine_server_call_err( req );
280             }
281             SERVER_END_REQ;
282             if (ret) CLASS_FreeClass( class );
283         }
284     }
285     USER_Unlock();
286 }
287
288
289 /***********************************************************************
290  *           CLASS_FindClass
291  *
292  * Return a pointer to the class.
293  * hinstance has been normalized by the caller.
294  */
295 static CLASS *CLASS_FindClass( LPCWSTR name, HINSTANCE hinstance )
296 {
297     struct list *ptr;
298     ATOM atom = get_int_atom_value( name );
299
300     USER_Lock();
301
302     LIST_FOR_EACH( ptr, &class_list )
303     {
304         CLASS *class = LIST_ENTRY( ptr, CLASS, entry );
305         if (atom)
306         {
307             if (class->atomName != atom) continue;
308         }
309         else
310         {
311             if (!name || strcmpiW( class->name, name )) continue;
312         }
313         if (!hinstance || !class->local || class->hInstance == hinstance)
314         {
315             TRACE("%s %p -> %p\n", debugstr_w(name), hinstance, class);
316             return class;
317         }
318     }
319     USER_Unlock();
320     TRACE("%s %p -> not found\n", debugstr_w(name), hinstance);
321     return NULL;
322 }
323
324
325 /***********************************************************************
326  *           CLASS_RegisterClass
327  *
328  * The real RegisterClass() functionality.
329  */
330 static CLASS *CLASS_RegisterClass( LPCWSTR name, HINSTANCE hInstance, BOOL local,
331                                    DWORD style, INT classExtra, INT winExtra )
332 {
333     CLASS *classPtr;
334     BOOL ret;
335
336     TRACE("name=%s hinst=%p style=0x%x clExtr=0x%x winExtr=0x%x\n",
337           debugstr_w(name), hInstance, style, classExtra, winExtra );
338
339     /* Fix the extra bytes value */
340
341     if (classExtra > 40)  /* Extra bytes are limited to 40 in Win32 */
342         WARN("Class extra bytes %d is > 40\n", classExtra);
343     if (winExtra > 40)    /* Extra bytes are limited to 40 in Win32 */
344         WARN("Win extra bytes %d is > 40\n", winExtra );
345
346     classPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLASS) + classExtra );
347     if (!classPtr) return NULL;
348
349     classPtr->atomName = get_int_atom_value( name );
350     if (!classPtr->atomName && name) strcpyW( classPtr->name, name );
351     else GlobalGetAtomNameW( classPtr->atomName, classPtr->name, sizeof(classPtr->name)/sizeof(WCHAR) );
352
353     SERVER_START_REQ( create_class )
354     {
355         req->local      = local;
356         req->style      = style;
357         req->instance   = wine_server_client_ptr( hInstance );
358         req->extra      = classExtra;
359         req->win_extra  = winExtra;
360         req->client_ptr = classPtr;
361         req->atom       = classPtr->atomName;
362         if (!req->atom && name) wine_server_add_data( req, name, strlenW(name) * sizeof(WCHAR) );
363         ret = !wine_server_call_err( req );
364         classPtr->atomName = reply->atom;
365     }
366     SERVER_END_REQ;
367     if (!ret)
368     {
369         HeapFree( GetProcessHeap(), 0, classPtr );
370         return NULL;
371     }
372
373     classPtr->style       = style;
374     classPtr->local       = local;
375     classPtr->cbWndExtra  = winExtra;
376     classPtr->cbClsExtra  = classExtra;
377     classPtr->hInstance   = hInstance;
378
379     /* Other non-null values must be set by caller */
380
381     USER_Lock();
382     if (local) list_add_head( &class_list, &classPtr->entry );
383     else list_add_tail( &class_list, &classPtr->entry );
384     return classPtr;
385 }
386
387
388 /***********************************************************************
389  *           register_builtin
390  *
391  * Register a builtin control class.
392  * This allows having both ASCII and Unicode winprocs for the same class.
393  */
394 static WNDPROC register_builtin( const struct builtin_class_descr *descr )
395 {
396     CLASS *classPtr;
397
398     if (!(classPtr = CLASS_RegisterClass( descr->name, user32_module, FALSE,
399                                           descr->style, 0, descr->extra ))) return 0;
400
401     classPtr->hCursor       = LoadCursorA( 0, (LPSTR)descr->cursor );
402     classPtr->hbrBackground = descr->brush;
403     classPtr->winproc       = WINPROC_AllocProc( descr->procA, descr->procW );
404     release_class_ptr( classPtr );
405     return classPtr->winproc;
406 }
407
408
409 /***********************************************************************
410  *           CLASS_RegisterBuiltinClasses
411  */
412 void CLASS_RegisterBuiltinClasses(void)
413 {
414     register_builtin( &DESKTOP_builtin_class );
415     register_builtin( &BUTTON_builtin_class );
416     register_builtin( &COMBO_builtin_class );
417     register_builtin( &COMBOLBOX_builtin_class );
418     register_builtin( &DIALOG_builtin_class );
419     EDIT_winproc_handle = register_builtin( &EDIT_builtin_class );
420     register_builtin( &ICONTITLE_builtin_class );
421     register_builtin( &LISTBOX_builtin_class );
422     register_builtin( &MDICLIENT_builtin_class );
423     register_builtin( &MENU_builtin_class );
424     register_builtin( &MESSAGE_builtin_class );
425     register_builtin( &SCROLL_builtin_class );
426     register_builtin( &STATIC_builtin_class );
427
428     /* the DefWindowProc winprocs are magic too */
429     WINPROC_AllocProc( DefWindowProcA, DefWindowProcW );
430 }
431
432
433 /***********************************************************************
434  *           get_class_winproc
435  */
436 WNDPROC get_class_winproc( CLASS *class )
437 {
438     return class->winproc;
439 }
440
441
442 /***********************************************************************
443  *           get_class_dce
444  */
445 struct dce *get_class_dce( CLASS *class )
446 {
447     return class->dce;
448 }
449
450
451 /***********************************************************************
452  *           set_class_dce
453  */
454 struct dce *set_class_dce( CLASS *class, struct dce *dce )
455 {
456     if (class->dce) return class->dce;  /* already set, don't change it */
457     class->dce = dce;
458     return dce;
459 }
460
461
462 /***********************************************************************
463  *              RegisterClassA (USER32.@)
464  *
465  * Register a window class.
466  *
467  * RETURNS
468  *      >0: Unique identifier
469  *      0: Failure
470  */
471 ATOM WINAPI RegisterClassA( const WNDCLASSA* wc ) /* [in] Address of structure with class data */
472 {
473     WNDCLASSEXA wcex;
474
475     wcex.cbSize        = sizeof(wcex);
476     wcex.style         = wc->style;
477     wcex.lpfnWndProc   = wc->lpfnWndProc;
478     wcex.cbClsExtra    = wc->cbClsExtra;
479     wcex.cbWndExtra    = wc->cbWndExtra;
480     wcex.hInstance     = wc->hInstance;
481     wcex.hIcon         = wc->hIcon;
482     wcex.hCursor       = wc->hCursor;
483     wcex.hbrBackground = wc->hbrBackground;
484     wcex.lpszMenuName  = wc->lpszMenuName;
485     wcex.lpszClassName = wc->lpszClassName;
486     wcex.hIconSm       = 0;
487     return RegisterClassExA( &wcex );
488 }
489
490
491 /***********************************************************************
492  *              RegisterClassW (USER32.@)
493  *
494  * See RegisterClassA.
495  */
496 ATOM WINAPI RegisterClassW( const WNDCLASSW* wc )
497 {
498     WNDCLASSEXW wcex;
499
500     wcex.cbSize        = sizeof(wcex);
501     wcex.style         = wc->style;
502     wcex.lpfnWndProc   = wc->lpfnWndProc;
503     wcex.cbClsExtra    = wc->cbClsExtra;
504     wcex.cbWndExtra    = wc->cbWndExtra;
505     wcex.hInstance     = wc->hInstance;
506     wcex.hIcon         = wc->hIcon;
507     wcex.hCursor       = wc->hCursor;
508     wcex.hbrBackground = wc->hbrBackground;
509     wcex.lpszMenuName  = wc->lpszMenuName;
510     wcex.lpszClassName = wc->lpszClassName;
511     wcex.hIconSm       = 0;
512     return RegisterClassExW( &wcex );
513 }
514
515
516 /***********************************************************************
517  *              RegisterClassExA (USER32.@)
518  */
519 ATOM WINAPI RegisterClassExA( const WNDCLASSEXA* wc )
520 {
521     ATOM atom;
522     CLASS *classPtr;
523     HINSTANCE instance;
524
525     if (wc->cbClsExtra < 0 || wc->cbWndExtra < 0 ||
526         wc->hInstance == user32_module)  /* we can't register a class for user32 */
527     {
528          SetLastError( ERROR_INVALID_PARAMETER );
529          return 0;
530     }
531     if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
532
533     if (!IS_INTRESOURCE(wc->lpszClassName))
534     {
535         WCHAR name[MAX_ATOM_LEN + 1];
536
537         if (!MultiByteToWideChar( CP_ACP, 0, wc->lpszClassName, -1, name, MAX_ATOM_LEN + 1 )) return 0;
538         classPtr = CLASS_RegisterClass( name, instance, !(wc->style & CS_GLOBALCLASS),
539                                         wc->style, wc->cbClsExtra, wc->cbWndExtra );
540     }
541     else
542     {
543         classPtr = CLASS_RegisterClass( (LPCWSTR)wc->lpszClassName, instance,
544                                         !(wc->style & CS_GLOBALCLASS), wc->style,
545                                         wc->cbClsExtra, wc->cbWndExtra );
546     }
547     if (!classPtr) return 0;
548     atom = classPtr->atomName;
549
550     TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
551           debugstr_a(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
552           wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
553
554     classPtr->hIcon         = wc->hIcon;
555     classPtr->hIconSm       = wc->hIconSm;
556     classPtr->hCursor       = wc->hCursor;
557     classPtr->hbrBackground = wc->hbrBackground;
558     classPtr->winproc       = WINPROC_AllocProc( wc->lpfnWndProc, NULL );
559     CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
560     release_class_ptr( classPtr );
561     return atom;
562 }
563
564
565 /***********************************************************************
566  *              RegisterClassExW (USER32.@)
567  */
568 ATOM WINAPI RegisterClassExW( const WNDCLASSEXW* wc )
569 {
570     ATOM atom;
571     CLASS *classPtr;
572     HINSTANCE instance;
573
574     if (wc->cbClsExtra < 0 || wc->cbWndExtra < 0 ||
575         wc->hInstance == user32_module)  /* we can't register a class for user32 */
576     {
577          SetLastError( ERROR_INVALID_PARAMETER );
578          return 0;
579     }
580     if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
581
582     if (!(classPtr = CLASS_RegisterClass( wc->lpszClassName, instance, !(wc->style & CS_GLOBALCLASS),
583                                           wc->style, wc->cbClsExtra, wc->cbWndExtra )))
584         return 0;
585
586     atom = classPtr->atomName;
587
588     TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
589           debugstr_w(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
590           wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
591
592     classPtr->hIcon         = wc->hIcon;
593     classPtr->hIconSm       = wc->hIconSm;
594     classPtr->hCursor       = wc->hCursor;
595     classPtr->hbrBackground = wc->hbrBackground;
596     classPtr->winproc       = WINPROC_AllocProc( NULL, wc->lpfnWndProc );
597     CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
598     release_class_ptr( classPtr );
599     return atom;
600 }
601
602
603 /***********************************************************************
604  *              UnregisterClassA (USER32.@)
605  */
606 BOOL WINAPI UnregisterClassA( LPCSTR className, HINSTANCE hInstance )
607 {
608     if (!IS_INTRESOURCE(className))
609     {
610         WCHAR name[MAX_ATOM_LEN + 1];
611
612         if (!MultiByteToWideChar( CP_ACP, 0, className, -1, name, MAX_ATOM_LEN + 1 ))
613             return FALSE;
614         return UnregisterClassW( name, hInstance );
615     }
616     return UnregisterClassW( (LPCWSTR)className, hInstance );
617 }
618
619 /***********************************************************************
620  *              UnregisterClassW (USER32.@)
621  */
622 BOOL WINAPI UnregisterClassW( LPCWSTR className, HINSTANCE hInstance )
623 {
624     CLASS *classPtr = NULL;
625
626     SERVER_START_REQ( destroy_class )
627     {
628         req->instance = wine_server_client_ptr( hInstance );
629         if (!(req->atom = get_int_atom_value(className)) && className)
630             wine_server_add_data( req, className, strlenW(className) * sizeof(WCHAR) );
631         if (!wine_server_call_err( req )) classPtr = reply->client_ptr;
632     }
633     SERVER_END_REQ;
634
635     if (classPtr) CLASS_FreeClass( classPtr );
636     return (classPtr != NULL);
637 }
638
639
640 /***********************************************************************
641  *              GetClassWord (USER32.@)
642  */
643 WORD WINAPI GetClassWord( HWND hwnd, INT offset )
644 {
645     CLASS *class;
646     WORD retvalue = 0;
647
648     if (offset < 0) return GetClassLongA( hwnd, offset );
649
650     if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
651
652     if (class == CLASS_OTHER_PROCESS)
653     {
654         SERVER_START_REQ( set_class_info )
655         {
656             req->window = wine_server_user_handle( hwnd );
657             req->flags = 0;
658             req->extra_offset = offset;
659             req->extra_size = sizeof(retvalue);
660             if (!wine_server_call_err( req ))
661                 memcpy( &retvalue, &reply->old_extra_value, sizeof(retvalue) );
662         }
663         SERVER_END_REQ;
664         return retvalue;
665     }
666
667     if (offset <= class->cbClsExtra - sizeof(WORD))
668         memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(retvalue) );
669     else
670         SetLastError( ERROR_INVALID_INDEX );
671     release_class_ptr( class );
672     return retvalue;
673 }
674
675
676 /***********************************************************************
677  *             CLASS_GetClassLong
678  *
679  * Implementation of GetClassLong(Ptr)A/W
680  */
681 static ULONG_PTR CLASS_GetClassLong( HWND hwnd, INT offset, UINT size,
682                                      BOOL unicode )
683 {
684     CLASS *class;
685     ULONG_PTR retvalue = 0;
686
687     if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
688
689     if (class == CLASS_OTHER_PROCESS)
690     {
691         SERVER_START_REQ( set_class_info )
692         {
693             req->window = wine_server_user_handle( hwnd );
694             req->flags = 0;
695             req->extra_offset = (offset >= 0) ? offset : -1;
696             req->extra_size = (offset >= 0) ? size : 0;
697             if (!wine_server_call_err( req ))
698             {
699                 switch(offset)
700                 {
701                 case GCLP_HBRBACKGROUND:
702                 case GCLP_HCURSOR:
703                 case GCLP_HICON:
704                 case GCLP_HICONSM:
705                 case GCLP_WNDPROC:
706                 case GCLP_MENUNAME:
707                     FIXME( "offset %d (%s) not supported on other process window %p\n",
708                            offset, SPY_GetClassLongOffsetName(offset), hwnd );
709                     SetLastError( ERROR_INVALID_HANDLE );
710                     break;
711                 case GCL_STYLE:
712                     retvalue = reply->old_style;
713                     break;
714                 case GCL_CBWNDEXTRA:
715                     retvalue = reply->old_win_extra;
716                     break;
717                 case GCL_CBCLSEXTRA:
718                     retvalue = reply->old_extra;
719                     break;
720                 case GCLP_HMODULE:
721                     retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
722                     break;
723                 case GCW_ATOM:
724                     retvalue = reply->old_atom;
725                     break;
726                 default:
727                     if (offset >= 0)
728                     {
729                         if (size == sizeof(DWORD))
730                         {
731                             DWORD retdword;
732                             memcpy( &retdword, &reply->old_extra_value, sizeof(DWORD) );
733                             retvalue = retdword;
734                         }
735                         else
736                             memcpy( &retvalue, &reply->old_extra_value,
737                                     sizeof(ULONG_PTR) );
738                     }
739                     else SetLastError( ERROR_INVALID_INDEX );
740                     break;
741                 }
742             }
743         }
744         SERVER_END_REQ;
745         return retvalue;
746     }
747
748     if (offset >= 0)
749     {
750         if (offset <= class->cbClsExtra - size)
751         {
752             if (size == sizeof(DWORD))
753             {
754                 DWORD retdword;
755                 memcpy( &retdword, (char *)(class + 1) + offset, sizeof(DWORD) );
756                 retvalue = retdword;
757             }
758             else
759                 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(ULONG_PTR) );
760         }
761         else
762             SetLastError( ERROR_INVALID_INDEX );
763         release_class_ptr( class );
764         return retvalue;
765     }
766
767     switch(offset)
768     {
769     case GCLP_HBRBACKGROUND:
770         retvalue = (ULONG_PTR)class->hbrBackground;
771         break;
772     case GCLP_HCURSOR:
773         retvalue = (ULONG_PTR)class->hCursor;
774         break;
775     case GCLP_HICON:
776         retvalue = (ULONG_PTR)class->hIcon;
777         break;
778     case GCLP_HICONSM:
779         retvalue = (ULONG_PTR)class->hIconSm;
780         break;
781     case GCL_STYLE:
782         retvalue = class->style;
783         break;
784     case GCL_CBWNDEXTRA:
785         retvalue = class->cbWndExtra;
786         break;
787     case GCL_CBCLSEXTRA:
788         retvalue = class->cbClsExtra;
789         break;
790     case GCLP_HMODULE:
791         retvalue = (ULONG_PTR)class->hInstance;
792         break;
793     case GCLP_WNDPROC:
794         retvalue = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
795         break;
796     case GCLP_MENUNAME:
797         retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
798         if (unicode)
799             retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
800         else
801             retvalue = (ULONG_PTR)CLASS_GetMenuNameA( class );
802         break;
803     case GCW_ATOM:
804         retvalue = class->atomName;
805         break;
806     default:
807         SetLastError( ERROR_INVALID_INDEX );
808         break;
809     }
810     release_class_ptr( class );
811     return retvalue;
812 }
813
814
815 /***********************************************************************
816  *              GetClassLongW (USER32.@)
817  */
818 DWORD WINAPI GetClassLongW( HWND hwnd, INT offset )
819 {
820     return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), TRUE );
821 }
822
823
824
825 /***********************************************************************
826  *              GetClassLongA (USER32.@)
827  */
828 DWORD WINAPI GetClassLongA( HWND hwnd, INT offset )
829 {
830     return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), FALSE );
831 }
832
833
834 /***********************************************************************
835  *              SetClassWord (USER32.@)
836  */
837 WORD WINAPI SetClassWord( HWND hwnd, INT offset, WORD newval )
838 {
839     CLASS *class;
840     WORD retval = 0;
841
842     if (offset < 0) return SetClassLongA( hwnd, offset, (DWORD)newval );
843
844     if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
845
846     SERVER_START_REQ( set_class_info )
847     {
848         req->window = wine_server_user_handle( hwnd );
849         req->flags = SET_CLASS_EXTRA;
850         req->extra_offset = offset;
851         req->extra_size = sizeof(newval);
852         memcpy( &req->extra_value, &newval, sizeof(newval) );
853         if (!wine_server_call_err( req ))
854         {
855             void *ptr = (char *)(class + 1) + offset;
856             memcpy( &retval, ptr, sizeof(retval) );
857             memcpy( ptr, &newval, sizeof(newval) );
858         }
859     }
860     SERVER_END_REQ;
861     release_class_ptr( class );
862     return retval;
863 }
864
865
866 /***********************************************************************
867  *             CLASS_SetClassLong
868  *
869  * Implementation of SetClassLong(Ptr)A/W
870  */
871 static ULONG_PTR CLASS_SetClassLong( HWND hwnd, INT offset, LONG_PTR newval,
872                                      UINT size, BOOL unicode )
873 {
874     CLASS *class;
875     ULONG_PTR retval = 0;
876
877     if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
878
879     if (offset >= 0)
880     {
881         if (set_server_info( hwnd, offset, newval, size ))
882         {
883             void *ptr = (char *)(class + 1) + offset;
884             if ( size == sizeof(LONG) )
885             {
886                 DWORD retdword;
887                 LONG newlong = newval;
888                 memcpy( &retdword, ptr, sizeof(DWORD) );
889                 memcpy( ptr, &newlong, sizeof(LONG) );
890                 retval = retdword;
891             }
892             else
893             {
894                 memcpy( &retval, ptr, sizeof(ULONG_PTR) );
895                 memcpy( ptr, &newval, sizeof(LONG_PTR) );
896             }
897         }
898     }
899     else switch(offset)
900     {
901     case GCLP_MENUNAME:
902         if ( unicode )
903             CLASS_SetMenuNameW( class, (LPCWSTR)newval );
904         else
905             CLASS_SetMenuNameA( class, (LPCSTR)newval );
906         retval = 0;  /* Old value is now meaningless anyway */
907         break;
908     case GCLP_WNDPROC:
909         retval = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
910         class->winproc = WINPROC_AllocProc( unicode ? NULL : (WNDPROC)newval,
911                                             unicode ? (WNDPROC)newval : NULL );
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         class->hIcon = (HICON)newval;
924         break;
925     case GCLP_HICONSM:
926         retval = (ULONG_PTR)class->hIconSm;
927         class->hIconSm = (HICON)newval;
928         break;
929     case GCL_STYLE:
930         if (!set_server_info( hwnd, offset, newval, size )) break;
931         retval = class->style;
932         class->style = newval;
933         break;
934     case GCL_CBWNDEXTRA:
935         if (!set_server_info( hwnd, offset, newval, size )) break;
936         retval = class->cbWndExtra;
937         class->cbWndExtra = newval;
938         break;
939     case GCLP_HMODULE:
940         if (!set_server_info( hwnd, offset, newval, size )) break;
941         retval = (ULONG_PTR)class->hInstance;
942         class->hInstance = (HINSTANCE)newval;
943         break;
944     case GCW_ATOM:
945         if (!set_server_info( hwnd, offset, newval, size )) break;
946         retval = class->atomName;
947         class->atomName = newval;
948         GlobalGetAtomNameW( newval, class->name, sizeof(class->name)/sizeof(WCHAR) );
949         break;
950     case GCL_CBCLSEXTRA:  /* cannot change this one */
951         SetLastError( ERROR_INVALID_PARAMETER );
952         break;
953     default:
954         SetLastError( ERROR_INVALID_INDEX );
955         break;
956     }
957     release_class_ptr( class );
958     return retval;
959 }
960
961
962 /***********************************************************************
963  *              SetClassLongW (USER32.@)
964  */
965 DWORD WINAPI SetClassLongW( HWND hwnd, INT offset, LONG newval )
966 {
967     return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), TRUE );
968 }
969
970
971 /***********************************************************************
972  *              SetClassLongA (USER32.@)
973  */
974 DWORD WINAPI SetClassLongA( HWND hwnd, INT offset, LONG newval )
975 {
976     return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), FALSE );
977 }
978
979
980 /***********************************************************************
981  *              GetClassNameA (USER32.@)
982  */
983 INT WINAPI GetClassNameA( HWND hwnd, LPSTR buffer, INT count )
984 {
985     WCHAR tmpbuf[MAX_ATOM_LEN + 1];
986     DWORD len;
987
988     if (count <= 0) return 0;
989     if (!GetClassNameW( hwnd, tmpbuf, sizeof(tmpbuf)/sizeof(WCHAR) )) return 0;
990     RtlUnicodeToMultiByteN( buffer, count - 1, &len, tmpbuf, strlenW(tmpbuf) * sizeof(WCHAR) );
991     buffer[len] = 0;
992     return len;
993 }
994
995
996 /***********************************************************************
997  *              GetClassNameW (USER32.@)
998  */
999 INT WINAPI GetClassNameW( HWND hwnd, LPWSTR buffer, INT count )
1000 {
1001     CLASS *class;
1002     INT ret;
1003
1004     TRACE("%p %p %d\n", hwnd, buffer, count);
1005
1006     if (count <= 0) return 0;
1007
1008     if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
1009
1010     if (class == CLASS_OTHER_PROCESS)
1011     {
1012         WCHAR tmpbuf[MAX_ATOM_LEN + 1];
1013
1014         ret = GlobalGetAtomNameW( GetClassLongW( hwnd, GCW_ATOM ), tmpbuf, MAX_ATOM_LEN + 1 );
1015         if (ret)
1016         {
1017             ret = min(count - 1, ret);
1018             memcpy(buffer, tmpbuf, ret * sizeof(WCHAR));
1019             buffer[ret] = 0;
1020         }
1021     }
1022     else
1023     {
1024         lstrcpynW( buffer, class->name, count );
1025         release_class_ptr( class );
1026         ret = strlenW( buffer );
1027     }
1028     return ret;
1029 }
1030
1031
1032 /***********************************************************************
1033  *              RealGetWindowClassA (USER32.@)
1034  */
1035 UINT WINAPI RealGetWindowClassA( HWND hwnd, LPSTR buffer, UINT count )
1036 {
1037     return GetClassNameA( hwnd, buffer, count );
1038 }
1039
1040
1041 /***********************************************************************
1042  *              RealGetWindowClassW (USER32.@)
1043  */
1044 UINT WINAPI RealGetWindowClassW( HWND hwnd, LPWSTR buffer, UINT count )
1045 {
1046     return GetClassNameW( hwnd, buffer, count );
1047 }
1048
1049
1050 /***********************************************************************
1051  *              GetClassInfoA (USER32.@)
1052  */
1053 BOOL WINAPI GetClassInfoA( HINSTANCE hInstance, LPCSTR name, WNDCLASSA *wc )
1054 {
1055     WNDCLASSEXA wcex;
1056     UINT ret = GetClassInfoExA( hInstance, name, &wcex );
1057
1058     if (ret)
1059     {
1060         wc->style         = wcex.style;
1061         wc->lpfnWndProc   = wcex.lpfnWndProc;
1062         wc->cbClsExtra    = wcex.cbClsExtra;
1063         wc->cbWndExtra    = wcex.cbWndExtra;
1064         wc->hInstance     = wcex.hInstance;
1065         wc->hIcon         = wcex.hIcon;
1066         wc->hCursor       = wcex.hCursor;
1067         wc->hbrBackground = wcex.hbrBackground;
1068         wc->lpszMenuName  = wcex.lpszMenuName;
1069         wc->lpszClassName = wcex.lpszClassName;
1070     }
1071     return ret;
1072 }
1073
1074
1075 /***********************************************************************
1076  *              GetClassInfoW (USER32.@)
1077  */
1078 BOOL WINAPI GetClassInfoW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSW *wc )
1079 {
1080     WNDCLASSEXW wcex;
1081     UINT ret = GetClassInfoExW( hInstance, name, &wcex );
1082
1083     if (ret)
1084     {
1085         wc->style         = wcex.style;
1086         wc->lpfnWndProc   = wcex.lpfnWndProc;
1087         wc->cbClsExtra    = wcex.cbClsExtra;
1088         wc->cbWndExtra    = wcex.cbWndExtra;
1089         wc->hInstance     = wcex.hInstance;
1090         wc->hIcon         = wcex.hIcon;
1091         wc->hCursor       = wcex.hCursor;
1092         wc->hbrBackground = wcex.hbrBackground;
1093         wc->lpszMenuName  = wcex.lpszMenuName;
1094         wc->lpszClassName = wcex.lpszClassName;
1095     }
1096     return ret;
1097 }
1098
1099
1100 /***********************************************************************
1101  *              GetClassInfoExA (USER32.@)
1102  */
1103 BOOL WINAPI GetClassInfoExA( HINSTANCE hInstance, LPCSTR name, WNDCLASSEXA *wc )
1104 {
1105     ATOM atom;
1106     CLASS *classPtr;
1107
1108     TRACE("%p %s %p\n", hInstance, debugstr_a(name), wc);
1109
1110     if (!hInstance) hInstance = user32_module;
1111
1112     if (!IS_INTRESOURCE(name))
1113     {
1114         WCHAR nameW[MAX_ATOM_LEN + 1];
1115         if (!MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, sizeof(nameW)/sizeof(WCHAR) ))
1116             return FALSE;
1117         classPtr = CLASS_FindClass( nameW, hInstance );
1118     }
1119     else classPtr = CLASS_FindClass( (LPCWSTR)name, hInstance );
1120
1121     if (!classPtr)
1122     {
1123         SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1124         return FALSE;
1125     }
1126     wc->style         = classPtr->style;
1127     wc->lpfnWndProc   = WINPROC_GetProc( classPtr->winproc, FALSE );
1128     wc->cbClsExtra    = classPtr->cbClsExtra;
1129     wc->cbWndExtra    = classPtr->cbWndExtra;
1130     wc->hInstance     = (hInstance == user32_module) ? 0 : hInstance;
1131     wc->hIcon         = classPtr->hIcon;
1132     wc->hIconSm       = classPtr->hIconSm;
1133     wc->hCursor       = classPtr->hCursor;
1134     wc->hbrBackground = classPtr->hbrBackground;
1135     wc->lpszMenuName  = CLASS_GetMenuNameA( classPtr );
1136     wc->lpszClassName = name;
1137     atom = classPtr->atomName;
1138     release_class_ptr( classPtr );
1139
1140     /* We must return the atom of the class here instead of just TRUE. */
1141     return atom;
1142 }
1143
1144
1145 /***********************************************************************
1146  *              GetClassInfoExW (USER32.@)
1147  */
1148 BOOL WINAPI GetClassInfoExW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSEXW *wc )
1149 {
1150     ATOM atom;
1151     CLASS *classPtr;
1152
1153     TRACE("%p %s %p\n", hInstance, debugstr_w(name), wc);
1154
1155     if (!hInstance) hInstance = user32_module;
1156
1157     if (!(classPtr = CLASS_FindClass( name, hInstance )))
1158     {
1159         SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1160         return FALSE;
1161     }
1162     wc->style         = classPtr->style;
1163     wc->lpfnWndProc   = WINPROC_GetProc( classPtr->winproc, TRUE );
1164     wc->cbClsExtra    = classPtr->cbClsExtra;
1165     wc->cbWndExtra    = classPtr->cbWndExtra;
1166     wc->hInstance     = (hInstance == user32_module) ? 0 : hInstance;
1167     wc->hIcon         = (HICON)classPtr->hIcon;
1168     wc->hIconSm       = (HICON)classPtr->hIconSm;
1169     wc->hCursor       = (HCURSOR)classPtr->hCursor;
1170     wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1171     wc->lpszMenuName  = CLASS_GetMenuNameW( classPtr );
1172     wc->lpszClassName = name;
1173     atom = classPtr->atomName;
1174     release_class_ptr( classPtr );
1175
1176     /* We must return the atom of the class here instead of just TRUE. */
1177     return atom;
1178 }
1179
1180
1181 #if 0  /* toolhelp is in kernel, so this cannot work */
1182
1183 /***********************************************************************
1184  *              ClassFirst (TOOLHELP.69)
1185  */
1186 BOOL16 WINAPI ClassFirst16( CLASSENTRY *pClassEntry )
1187 {
1188     TRACE("%p\n",pClassEntry);
1189     pClassEntry->wNext = 1;
1190     return ClassNext16( pClassEntry );
1191 }
1192
1193
1194 /***********************************************************************
1195  *              ClassNext (TOOLHELP.70)
1196  */
1197 BOOL16 WINAPI ClassNext16( CLASSENTRY *pClassEntry )
1198 {
1199     int i;
1200     CLASS *class = firstClass;
1201
1202     TRACE("%p\n",pClassEntry);
1203
1204     if (!pClassEntry->wNext) return FALSE;
1205     for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1206     if (!class)
1207     {
1208         pClassEntry->wNext = 0;
1209         return FALSE;
1210     }
1211     pClassEntry->hInst = class->hInstance;
1212     pClassEntry->wNext++;
1213     GlobalGetAtomNameA( class->atomName, pClassEntry->szClassName,
1214                           sizeof(pClassEntry->szClassName) );
1215     return TRUE;
1216 }
1217 #endif
1218
1219 /* 64bit versions */
1220
1221 #ifdef GetClassLongPtrA
1222 #undef GetClassLongPtrA
1223 #endif
1224
1225 #ifdef GetClassLongPtrW
1226 #undef GetClassLongPtrW
1227 #endif
1228
1229 #ifdef SetClassLongPtrA
1230 #undef SetClassLongPtrA
1231 #endif
1232
1233 #ifdef SetClassLongPtrW
1234 #undef SetClassLongPtrW
1235 #endif
1236
1237 /***********************************************************************
1238  *              GetClassLongPtrA (USER32.@)
1239  */
1240 ULONG_PTR WINAPI GetClassLongPtrA( HWND hwnd, INT offset )
1241 {
1242     return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), FALSE );
1243 }
1244
1245 /***********************************************************************
1246  *              GetClassLongPtrW (USER32.@)
1247  */
1248 ULONG_PTR WINAPI GetClassLongPtrW( HWND hwnd, INT offset )
1249 {
1250     return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), TRUE );
1251 }
1252
1253 /***********************************************************************
1254  *              SetClassLongPtrW (USER32.@)
1255  */
1256 ULONG_PTR WINAPI SetClassLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
1257 {
1258     return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), TRUE );
1259 }
1260
1261 /***********************************************************************
1262  *              SetClassLongPtrA (USER32.@)
1263  */
1264 ULONG_PTR WINAPI SetClassLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
1265 {
1266     return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), FALSE );
1267 }