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