2 * Window classes functions
4 * Copyright 1993 Alexandre Julliard
6 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
18 /* #define DEBUG_CLASS */
22 static HCLASS firstClass = 0;
25 /***********************************************************************
26 * CLASS_FindClassByName
28 * Return a handle and a pointer to the class.
29 * 'ptr' can be NULL if the pointer is not needed.
31 HCLASS CLASS_FindClassByName( char * name, WORD hinstance, CLASS **ptr )
37 if (!(atom = GlobalFindAtom( name ))) return 0;
39 /* First search task-specific classes */
41 for (class = firstClass; (class); class = classPtr->hNext)
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)))
48 if (ptr) *ptr = classPtr;
53 /* Then search global classes */
55 for (class = firstClass; (class); class = classPtr->hNext)
57 classPtr = (CLASS *) USER_HEAP_LIN_ADDR(class);
59 /* This breaks Borland's Resource Workshop: A DLL creates a class,
60 * and the main program tries to use it, but passes a different
62 if (!(classPtr->wc.style & CS_GLOBALCLASS)) continue;
64 if (classPtr->atomName == atom)
66 if (ptr) *ptr = classPtr;
75 /***********************************************************************
78 * Return a pointer to the CLASS structure corresponding to a HCLASS.
80 CLASS * CLASS_FindClassPtr( HCLASS hclass )
84 if (!hclass) return NULL;
85 ptr = (CLASS *) USER_HEAP_LIN_ADDR( hclass );
86 if (ptr->wMagic != CLASS_MAGIC) return NULL;
91 /***********************************************************************
92 * RegisterClass (USER.57)
94 ATOM RegisterClass( LPWNDCLASS class )
96 CLASS * newClass, * prevClassPtr;
97 HCLASS handle, prevClass;
99 char *name = PTR_SEG_TO_LIN( class->lpszClassName );
101 dprintf_class(stddeb, "RegisterClass: wndproc=%08lx hinst=%d name='%s' background %04x\n",
102 (DWORD)class->lpfnWndProc, class->hInstance, name, class->hbrBackground );
103 dprintf_class(stddeb, " style %04x\n",class->style);
104 /* Check if a class with this name already exists */
106 prevClass = CLASS_FindClassByName( name, class->hInstance, &prevClassPtr );
109 /* Class can be created only if it is local and */
110 /* if the class with the same name is global. */
112 if (class->style & CS_GLOBALCLASS) return 0;
113 if (!(prevClassPtr->wc.style & CS_GLOBALCLASS)) return 0;
118 classExtra = (class->cbClsExtra < 0) ? 0 : class->cbClsExtra;
119 handle = USER_HEAP_ALLOC( sizeof(CLASS) + classExtra );
120 if (!handle) return 0;
121 newClass = (CLASS *) USER_HEAP_LIN_ADDR( handle );
122 newClass->hNext = firstClass;
123 newClass->wMagic = CLASS_MAGIC;
124 newClass->cWindows = 0;
125 newClass->wc = *class;
126 newClass->wc.cbWndExtra = (class->cbWndExtra < 0) ? 0 : class->cbWndExtra;
127 newClass->wc.cbClsExtra = classExtra;
129 newClass->atomName = GlobalAddAtom( name );
130 newClass->wc.lpszClassName = NULL;
132 if (newClass->wc.style & CS_CLASSDC)
133 newClass->hdce = DCE_AllocDCE( DCE_CLASS_DC );
134 else newClass->hdce = 0;
136 /* Make a copy of the menu name (only if it is a string) */
138 if ((int)class->lpszMenuName & 0xffff0000)
140 char *menuname = PTR_SEG_TO_LIN( class->lpszMenuName );
141 HANDLE hname = USER_HEAP_ALLOC( strlen(menuname)+1 );
144 newClass->wc.lpszMenuName = (char *)USER_HEAP_SEG_ADDR( hname );
145 strcpy( USER_HEAP_LIN_ADDR( hname ), menuname );
149 if (classExtra) memset( newClass->wExtra, 0, classExtra );
151 return newClass->atomName;
155 /***********************************************************************
156 * UnregisterClass (USER.403)
158 BOOL UnregisterClass( LPSTR className, HANDLE instance )
160 HANDLE class, prevClass;
161 CLASS * classPtr, * prevClassPtr;
163 /* Check if we can remove this class */
164 class = CLASS_FindClassByName( className, instance, &classPtr );
165 if (!class) return FALSE;
166 if ((classPtr->wc.hInstance != instance) || (classPtr->cWindows > 0))
169 /* Remove the class from the linked list */
170 if (firstClass == class) firstClass = classPtr->hNext;
173 for (prevClass = firstClass; prevClass; prevClass=prevClassPtr->hNext)
175 prevClassPtr = (CLASS *) USER_HEAP_LIN_ADDR(prevClass);
176 if (prevClassPtr->hNext == class) break;
180 fprintf(stderr, "ERROR: Class list corrupted\n" );
183 prevClassPtr->hNext = classPtr->hNext;
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 );
198 /***********************************************************************
199 * GetClassWord (USER.129)
201 WORD GetClassWord( HWND hwnd, short offset )
203 return (WORD)GetClassLong( hwnd, offset );
207 /***********************************************************************
208 * SetClassWord (USER.130)
210 WORD SetClassWord( HWND hwnd, short offset, WORD newval )
214 WORD *ptr, retval = 0;
216 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
217 if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
218 ptr = (WORD *)(((char *)classPtr->wExtra) + offset);
225 /***********************************************************************
226 * GetClassLong (USER.131)
228 LONG GetClassLong( HWND hwnd, short offset )
233 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
234 if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
235 return *(LONG *)(((char *)classPtr->wExtra) + offset);
239 /***********************************************************************
240 * SetClassLong (USER.132)
242 LONG SetClassLong( HWND hwnd, short offset, LONG newval )
246 LONG *ptr, retval = 0;
248 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
249 if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
250 ptr = (LONG *)(((char *)classPtr->wExtra) + offset);
257 /***********************************************************************
258 * GetClassName (USER.58)
260 int GetClassName(HWND hwnd, LPSTR lpClassName, short maxCount)
265 /* FIXME: We have the find the correct hInstance */
266 dprintf_class(stddeb,"GetClassName(%x,%p,%d)\n",hwnd,lpClassName,maxCount);
267 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
268 if (!(classPtr = CLASS_FindClassPtr(wndPtr->hClass))) return 0;
270 return GlobalGetAtomName(classPtr->atomName, lpClassName, maxCount);
274 /***********************************************************************
275 * GetClassInfo (USER.404)
277 BOOL GetClassInfo(HANDLE hInstance, SEGPTR ClassName,
278 LPWNDCLASS lpWndClass)
281 LPSTR lpClassName = 0;
283 if (HIWORD(ClassName)) {
284 lpClassName = PTR_SEG_TO_LIN(ClassName);
286 sprintf(temp,"#%d",(int)LOWORD(ClassName));
289 dprintf_class(stddeb, "GetClassInfo hInstance=%04x lpClassName=%s\n",
290 hInstance, lpClassName);
293 /* if (!(CLASS_FindClassByName(lpClassName, &classPtr))) return FALSE; */
294 if (!(CLASS_FindClassByName(lpClassName, hInstance, &classPtr)))
296 /* if (!HIWORD(lpClassName))
299 sprintf(temp, "#%d", (int)lpClassName);
300 if (!(CLASS_FindClassByName(temp, hInstance, &classPtr))) return FALSE;
306 if (hInstance && (hInstance != classPtr->wc.hInstance)) return FALSE;
308 memcpy(lpWndClass, &(classPtr->wc), sizeof(WNDCLASS));
313 /***********************************************************************
314 * ClassFirst (TOOLHELP.69)
316 BOOL ClassFirst( CLASSENTRY *pClassEntry )
318 pClassEntry->wNext = firstClass;
319 return ClassNext( pClassEntry );
323 /***********************************************************************
324 * ClassNext (TOOLHELP.70)
326 BOOL ClassNext( CLASSENTRY *pClassEntry )
328 CLASS *classPtr = (CLASS *) USER_HEAP_LIN_ADDR( pClassEntry->wNext );
329 if (!classPtr) return FALSE;
331 pClassEntry->hInst = classPtr->wc.hInstance;
332 pClassEntry->wNext = classPtr->hNext;
333 GlobalGetAtomName( classPtr->atomName, pClassEntry->szClassName,
334 sizeof(pClassEntry->szClassName) );