Release 940706
[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     int classExtra;
95
96 #ifdef DEBUG_CLASS
97     printf( "RegisterClass: wndproc=%08x hinst=%d name='%s' background %x\n", 
98             class->lpfnWndProc, class->hInstance, class->lpszClassName,
99             class->hbrBackground );
100 #endif
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             printf( "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 }