Release 950430
[wine] / windows / class.c
1 /*
2  * Window classes functions
3  *
4  * Copyright 1993 Alexandre Julliard
5  *
6 static char Copyright[] = "Copyright  Alexandre Julliard, 1993";
7 */
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 "toolhelp.h"
17 #include "stddebug.h"
18 /* #define 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, WORD hinstance, CLASS **ptr )
32 {
33     ATOM atom;
34     HCLASS class;
35     CLASS * classPtr;
36
37     if (!(atom = GlobalFindAtom( name ))) return 0;
38
39       /* First search task-specific classes */
40
41     for (class = firstClass; (class); class = classPtr->hNext)
42     {
43         classPtr = (CLASS *) USER_HEAP_LIN_ADDR(class);
44         if (classPtr->wc.style & CS_GLOBALCLASS) continue;
45         if ((classPtr->atomName == atom) && 
46             ((hinstance==0xffff )|| (hinstance == classPtr->wc.hInstance)))
47         {
48             if (ptr) *ptr = classPtr;
49             return class;
50         }
51     }
52     
53       /* Then search global classes */
54
55     for (class = firstClass; (class); class = classPtr->hNext)
56     {
57         classPtr = (CLASS *) USER_HEAP_LIN_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     return 0;
67 }
68
69
70 /***********************************************************************
71  *           CLASS_FindClassPtr
72  *
73  * Return a pointer to the CLASS structure corresponding to a HCLASS.
74  */
75 CLASS * CLASS_FindClassPtr( HCLASS hclass )
76 {
77     CLASS * ptr;
78     
79     if (!hclass) return NULL;
80     ptr = (CLASS *) USER_HEAP_LIN_ADDR( hclass );
81     if (ptr->wMagic != CLASS_MAGIC) return NULL;
82     return ptr;
83 }
84
85
86 /***********************************************************************
87  *           RegisterClass    (USER.57)
88  */
89 ATOM RegisterClass( LPWNDCLASS class )
90 {
91     CLASS * newClass, * prevClassPtr;
92     HCLASS handle, prevClass;
93     int classExtra;
94     char *name = PTR_SEG_TO_LIN( class->lpszClassName );
95
96     dprintf_class(stddeb, "RegisterClass: wndproc=%p hinst=%d name='%s' background %x\n", 
97             class->lpfnWndProc, class->hInstance, name, class->hbrBackground );
98
99       /* Check if a class with this name already exists */
100
101     prevClass = CLASS_FindClassByName( name, class->hInstance, &prevClassPtr );
102     if (prevClass)
103     {
104           /* Class can be created only if it is local and */
105           /* if the class with the same name is global.   */
106
107         if (class->style & CS_GLOBALCLASS) return 0;
108         if (!(prevClassPtr->wc.style & CS_GLOBALCLASS)) return 0;
109     }
110
111       /* Create class */
112
113     classExtra = (class->cbClsExtra < 0) ? 0 : class->cbClsExtra;
114     handle = USER_HEAP_ALLOC( sizeof(CLASS) + classExtra );
115     if (!handle) return 0;
116     newClass = (CLASS *) USER_HEAP_LIN_ADDR( handle );
117     newClass->hNext         = firstClass;
118     newClass->wMagic        = CLASS_MAGIC;
119     newClass->cWindows      = 0;  
120     newClass->wc            = *class;
121     newClass->wc.cbWndExtra = (class->cbWndExtra < 0) ? 0 : class->cbWndExtra;
122     newClass->wc.cbClsExtra = classExtra;
123
124     newClass->atomName = GlobalAddAtom( name );
125     newClass->wc.lpszClassName = NULL; 
126
127     if (newClass->wc.style & CS_CLASSDC)
128         newClass->hdce = DCE_AllocDCE( DCE_CLASS_DC );
129     else newClass->hdce = 0;
130
131       /* Make a copy of the menu name (only if it is a string) */
132
133     if ((int)class->lpszMenuName & 0xffff0000)
134     {
135         char *menuname = PTR_SEG_TO_LIN( class->lpszMenuName );
136         HANDLE hname = USER_HEAP_ALLOC( strlen(menuname)+1 );
137         if (hname)
138         {
139             newClass->wc.lpszMenuName = (char *)USER_HEAP_SEG_ADDR( hname );
140             strcpy( USER_HEAP_LIN_ADDR( hname ), menuname );
141         }
142     }
143
144     if (classExtra) memset( newClass->wExtra, 0, classExtra );
145     firstClass = handle;
146     return newClass->atomName;
147 }
148
149
150 /***********************************************************************
151  *           UnregisterClass    (USER.403)
152  */
153 BOOL UnregisterClass( LPSTR className, HANDLE instance )
154 {
155     HANDLE class, prevClass;
156     CLASS * classPtr, * prevClassPtr;
157     
158       /* Check if we can remove this class */
159     class = CLASS_FindClassByName( className, instance, &classPtr );
160     if (!class) return FALSE;
161     if ((classPtr->wc.hInstance != instance) || (classPtr->cWindows > 0))
162         return FALSE;
163     
164       /* Remove the class from the linked list */
165     if (firstClass == class) firstClass = classPtr->hNext;
166     else
167     {
168         for (prevClass = firstClass; prevClass; prevClass=prevClassPtr->hNext)
169         {
170             prevClassPtr = (CLASS *) USER_HEAP_LIN_ADDR(prevClass);
171             if (prevClassPtr->hNext == class) break;
172         }
173         if (!prevClass)
174         {
175             fprintf(stderr, "ERROR: Class list corrupted\n" );
176             return FALSE;
177         }
178         prevClassPtr->hNext = classPtr->hNext;
179     }
180
181       /* Delete the class */
182     if (classPtr->hdce) DCE_FreeDCE( classPtr->hdce );
183     if (classPtr->wc.hbrBackground) DeleteObject( classPtr->wc.hbrBackground );
184     /*if (classPtr->wc.style & CS_GLOBALCLASS)*/ GlobalDeleteAtom( classPtr->atomName );
185     /*else DeleteAtom( classPtr->atomName );*/
186     if ((int)classPtr->wc.lpszMenuName & 0xffff0000)
187         USER_HEAP_FREE( (int)classPtr->wc.lpszMenuName & 0xffff );
188     USER_HEAP_FREE( class );
189     return TRUE;
190 }
191
192
193 /***********************************************************************
194  *           GetClassWord    (USER.129)
195  */
196 WORD GetClassWord( HWND hwnd, short offset )
197 {
198     return (WORD)GetClassLong( hwnd, offset );
199 }
200
201
202 /***********************************************************************
203  *           SetClassWord    (USER.130)
204  */
205 WORD SetClassWord( HWND hwnd, short offset, WORD newval )
206 {
207     CLASS * classPtr;
208     WND * wndPtr;
209     WORD *ptr, retval = 0;
210     
211     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
212     if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
213     ptr = (WORD *)(((char *)classPtr->wExtra) + offset);
214     retval = *ptr;
215     *ptr = newval;
216     return retval;
217 }
218
219
220 /***********************************************************************
221  *           GetClassLong    (USER.131)
222  */
223 LONG GetClassLong( HWND hwnd, short offset )
224 {
225     CLASS * classPtr;
226     WND * wndPtr;
227     
228     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
229     if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
230     return *(LONG *)(((char *)classPtr->wExtra) + offset);
231 }
232
233
234 /***********************************************************************
235  *           SetClassLong    (USER.132)
236  */
237 LONG SetClassLong( HWND hwnd, short offset, LONG newval )
238 {
239     CLASS * classPtr;
240     WND * wndPtr;
241     LONG *ptr, retval = 0;
242     
243     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
244     if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
245     ptr = (LONG *)(((char *)classPtr->wExtra) + offset);
246     retval = *ptr;
247     *ptr = newval;
248     return retval;
249 }
250
251
252 /***********************************************************************
253  *           GetClassName      (USER.58)
254  */
255 int GetClassName(HWND hwnd, LPSTR lpClassName, short maxCount)
256 {
257     WND *wndPtr;
258     CLASS *classPtr;
259
260     /* FIXME: We have the find the correct hInstance */
261     dprintf_class(stddeb,"GetClassName(%x,%p,%d)\n",hwnd,lpClassName,maxCount);
262     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
263     if (!(classPtr = CLASS_FindClassPtr(wndPtr->hClass))) return 0;
264     
265     return GlobalGetAtomName(classPtr->atomName, lpClassName, maxCount);
266 }
267
268
269 /***********************************************************************
270  *           GetClassInfo      (USER.404)
271  */
272 BOOL GetClassInfo(HANDLE hInstance, SEGPTR ClassName, 
273                                     LPWNDCLASS lpWndClass)
274 {
275     CLASS *classPtr;
276     LPSTR lpClassName = 0;
277     char  temp[10];
278     if (HIWORD(ClassName)) {
279       lpClassName = PTR_SEG_TO_LIN(ClassName);
280     } else  {
281       sprintf(temp,"#%d",(int)LOWORD(ClassName));
282       lpClassName = temp;
283     }
284     dprintf_class(stddeb, "GetClassInfo   hInstance=%04x  lpClassName=%s\n",
285                   hInstance, lpClassName);
286
287
288     /* if (!(CLASS_FindClassByName(lpClassName, &classPtr))) return FALSE; */
289     if (!(CLASS_FindClassByName(lpClassName, hInstance, &classPtr)))
290     {
291 /*        if (!HIWORD(lpClassName))
292         {
293             char temp[10];
294             sprintf(temp, "#%d", (int)lpClassName);
295             if (!(CLASS_FindClassByName(temp, hInstance, &classPtr))) return FALSE;
296
297         }
298         else */return FALSE;
299     }
300
301     if (hInstance && (hInstance != classPtr->wc.hInstance)) return FALSE;
302
303     memcpy(lpWndClass, &(classPtr->wc), sizeof(WNDCLASS));
304     return TRUE;
305 }
306
307
308 /***********************************************************************
309  *           ClassFirst      (TOOLHELP.69)
310  */
311 BOOL ClassFirst( CLASSENTRY *pClassEntry )
312 {
313     pClassEntry->wNext = firstClass;
314     return ClassNext( pClassEntry );
315 }
316
317
318 /***********************************************************************
319  *           ClassNext      (TOOLHELP.70)
320  */
321 BOOL ClassNext( CLASSENTRY *pClassEntry )
322 {
323     CLASS *classPtr = (CLASS *) USER_HEAP_LIN_ADDR( pClassEntry->wNext );
324     if (!classPtr) return FALSE;
325
326     pClassEntry->hInst = classPtr->wc.hInstance;
327     pClassEntry->wNext = classPtr->hNext;
328     GlobalGetAtomName( classPtr->atomName, pClassEntry->szClassName,
329                        sizeof(pClassEntry->szClassName) );
330     return TRUE;
331 }