Release 940518
[wine] / windows / class.c
1 /*
2  * Window classes functions
3  *
4  * Copyright 1993 Alexandre Julliard
5  */
6
7 static char Copyright[] = "Copyright  Alexandre Julliard, 1993";
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include "class.h"
13 #include "user.h"
14 #include "win.h"
15 #include "dce.h"
16
17 /* #define DEBUG_CLASS /* */
18
19
20 static HCLASS firstClass = 0;
21
22
23 /***********************************************************************
24  *           CLASS_FindClassByName
25  *
26  * Return a handle and a pointer to the class.
27  * 'ptr' can be NULL if the pointer is not needed.
28  */
29 HCLASS CLASS_FindClassByName( char * name, CLASS **ptr )
30 {
31     ATOM atom;
32     HCLASS class;
33     CLASS * classPtr;
34
35       /* First search task-specific classes */
36
37     if ((atom = FindAtom( name )) != 0)
38     {
39         for (class = firstClass; (class); class = classPtr->hNext)
40         {
41             classPtr = (CLASS *) USER_HEAP_ADDR(class);
42             if (classPtr->wc.style & CS_GLOBALCLASS) continue;
43             if (classPtr->atomName == atom)
44             {
45                 if (ptr) *ptr = classPtr;
46                 return class;
47             }
48         }
49     }
50     
51       /* Then search global classes */
52
53     if ((atom = GlobalFindAtom( name )) != 0)
54     {
55         for (class = firstClass; (class); class = classPtr->hNext)
56         {
57             classPtr = (CLASS *) USER_HEAP_ADDR(class);
58             if (!(classPtr->wc.style & CS_GLOBALCLASS)) continue;
59             if (classPtr->atomName == atom)
60             {
61                 if (ptr) *ptr = classPtr;
62                 return class;
63             }
64         }
65     }
66
67     return 0;
68 }
69
70
71 /***********************************************************************
72  *           CLASS_FindClassPtr
73  *
74  * Return a pointer to the CLASS structure corresponding to a HCLASS.
75  */
76 CLASS * CLASS_FindClassPtr( HCLASS hclass )
77 {
78     CLASS * ptr;
79     
80     if (!hclass) return NULL;
81     ptr = (CLASS *) USER_HEAP_ADDR( hclass );
82     if (ptr->wMagic != CLASS_MAGIC) return NULL;
83     return ptr;
84 }
85
86
87 /***********************************************************************
88  *           RegisterClass    (USER.57)
89  */
90 ATOM RegisterClass( LPWNDCLASS class )
91 {
92     CLASS * newClass, * prevClassPtr;
93     HCLASS handle, prevClass;
94     
95 #ifdef DEBUG_CLASS
96     printf( "RegisterClass: wndproc=%08x hinst=%d name='%s' background %x\n", 
97             class->lpfnWndProc, class->hInstance, class->lpszClassName,
98             class->hbrBackground );
99 #endif
100
101       /* Check if a class with this name already exists */
102
103     prevClass = CLASS_FindClassByName( class->lpszClassName, &prevClassPtr );
104     if (prevClass)
105     {
106           /* Class can be created only if it is local and */
107           /* if the class with the same name is global.   */
108
109         if (class->style & CS_GLOBALCLASS) return 0;
110         if (!(prevClassPtr->wc.style & CS_GLOBALCLASS)) return 0;
111     }
112
113       /* bug for bug compatible */
114
115     if (class->cbClsExtra < 0) class->cbClsExtra = 0;
116     if (class->cbWndExtra < 0) class->cbWndExtra = 0;
117
118       /* Create class */
119
120     handle = USER_HEAP_ALLOC( GMEM_MOVEABLE, sizeof(CLASS)+class->cbClsExtra );
121     if (!handle) return 0;
122     newClass = (CLASS *) USER_HEAP_ADDR( handle );
123     newClass->hNext      = firstClass;
124     newClass->wMagic     = CLASS_MAGIC;
125     newClass->cWindows   = 0;  
126     newClass->wc         = *class;
127
128     if (newClass->wc.style & CS_GLOBALCLASS)
129         newClass->atomName = GlobalAddAtom( class->lpszClassName );
130     else newClass->atomName = AddAtom( class->lpszClassName );
131     newClass->wc.lpszClassName = NULL; 
132
133     if (newClass->wc.style & CS_CLASSDC)
134         newClass->hdce = DCE_AllocDCE( DCE_CLASS_DC );
135     else newClass->hdce = 0;
136
137       /* Make a copy of the menu name (only if it is a string) */
138
139     if ((int)class->lpszMenuName & 0xffff0000)
140     {
141         HANDLE hname;
142         hname = USER_HEAP_ALLOC( GMEM_MOVEABLE, strlen(class->lpszMenuName)+1);
143         if (hname)
144         {
145             newClass->wc.lpszMenuName = (char *)USER_HEAP_ADDR( hname );
146             strcpy( newClass->wc.lpszMenuName, class->lpszMenuName );
147         }
148     }
149
150     if (class->cbClsExtra) memset( newClass->wExtra, 0, class->cbClsExtra );
151     firstClass = handle;
152     return newClass->atomName;
153 }
154
155
156 /***********************************************************************
157  *           UnregisterClass    (USER.403)
158  */
159 BOOL UnregisterClass( LPSTR className, HANDLE instance )
160 {
161     HANDLE class, prevClass;
162     CLASS * classPtr, * prevClassPtr;
163     
164       /* Check if we can remove this class */
165     class = CLASS_FindClassByName( className, &classPtr );
166     if (!class) return FALSE;
167     if ((classPtr->wc.hInstance != instance) || (classPtr->cWindows > 0))
168         return FALSE;
169     
170       /* Remove the class from the linked list */
171     if (firstClass == class) firstClass = classPtr->hNext;
172     else
173     {
174         for (prevClass = firstClass; prevClass; prevClass=prevClassPtr->hNext)
175         {
176             prevClassPtr = (CLASS *) USER_HEAP_ADDR(prevClass);
177             if (prevClassPtr->hNext == class) break;
178         }
179         if (!prevClass)
180         {
181             printf( "ERROR: Class list corrupted\n" );
182             return FALSE;
183         }
184         prevClassPtr->hNext = classPtr->hNext;
185     }
186
187       /* Delete the class */
188     if (classPtr->hdce) DCE_FreeDCE( classPtr->hdce );
189     if (classPtr->wc.hbrBackground) DeleteObject( classPtr->wc.hbrBackground );
190     if (classPtr->wc.style & CS_GLOBALCLASS) GlobalDeleteAtom( classPtr->atomName );
191     else DeleteAtom( classPtr->atomName );
192     if ((int)classPtr->wc.lpszMenuName & 0xffff0000)
193         USER_HEAP_FREE( (int)classPtr->wc.lpszMenuName & 0xffff );
194     USER_HEAP_FREE( class );
195     return TRUE;
196 }
197
198
199 /***********************************************************************
200  *           GetClassWord    (USER.129)
201  */
202 WORD GetClassWord( HWND hwnd, short offset )
203 {
204     return (WORD)GetClassLong( hwnd, offset );
205 }
206
207
208 /***********************************************************************
209  *           SetClassWord    (USER.130)
210  */
211 WORD SetClassWord( HWND hwnd, short offset, WORD newval )
212 {
213     CLASS * classPtr;
214     WND * wndPtr;
215     WORD *ptr, retval = 0;
216     
217     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
218     if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
219     ptr = (WORD *)(((char *)classPtr->wExtra) + offset);
220     retval = *ptr;
221     *ptr = newval;
222     return retval;
223 }
224
225
226 /***********************************************************************
227  *           GetClassLong    (USER.131)
228  */
229 LONG GetClassLong( HWND hwnd, short offset )
230 {
231     CLASS * classPtr;
232     WND * wndPtr;
233     
234     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
235     if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
236     return *(LONG *)(((char *)classPtr->wExtra) + offset);
237 }
238
239
240 /***********************************************************************
241  *           SetClassLong    (USER.132)
242  */
243 LONG SetClassLong( HWND hwnd, short offset, LONG newval )
244 {
245     CLASS * classPtr;
246     WND * wndPtr;
247     LONG *ptr, retval = 0;
248     
249     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
250     if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
251     ptr = (LONG *)(((char *)classPtr->wExtra) + offset);
252     retval = *ptr;
253     *ptr = newval;
254     return retval;
255 }
256
257
258 /***********************************************************************
259  *           GetClassName      (USER.58)
260  */
261 int GetClassName(HWND hwnd, LPSTR lpClassName, short maxCount)
262 {
263     WND *wndPtr;
264     CLASS *classPtr;
265
266     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
267     if (!(classPtr = CLASS_FindClassPtr(wndPtr->hClass))) return 0;
268
269     return (GetAtomName(classPtr->atomName, lpClassName, maxCount));
270 }
271
272
273 /***********************************************************************
274  *           GetClassInfo      (USER.404)
275  */
276 BOOL GetClassInfo(HANDLE hInstance, LPSTR lpClassName, 
277                                     LPWNDCLASS lpWndClass)
278 {
279     CLASS *classPtr;
280
281     if (!(CLASS_FindClassByName(lpClassName, &classPtr))) return FALSE;
282     if (hInstance && (hInstance != classPtr->wc.hInstance)) return FALSE;
283
284     memcpy(lpWndClass, &(classPtr->wc), sizeof(WNDCLASS));
285     return TRUE;
286 }