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