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);
58 if (!(classPtr->wc.style & CS_GLOBALCLASS)) continue;
59 if (classPtr->atomName == atom)
61 if (ptr) *ptr = classPtr;
70 /***********************************************************************
73 * Return a pointer to the CLASS structure corresponding to a HCLASS.
75 CLASS * CLASS_FindClassPtr( HCLASS hclass )
79 if (!hclass) return NULL;
80 ptr = (CLASS *) USER_HEAP_LIN_ADDR( hclass );
81 if (ptr->wMagic != CLASS_MAGIC) return NULL;
86 /***********************************************************************
87 * RegisterClass (USER.57)
89 ATOM RegisterClass( LPWNDCLASS class )
91 CLASS * newClass, * prevClassPtr;
92 HCLASS handle, prevClass;
94 char *name = PTR_SEG_TO_LIN( class->lpszClassName );
96 dprintf_class(stddeb, "RegisterClass: wndproc=%p hinst=%d name='%s' background %x\n",
97 class->lpfnWndProc, class->hInstance, name, class->hbrBackground );
99 /* Check if a class with this name already exists */
101 prevClass = CLASS_FindClassByName( name, class->hInstance, &prevClassPtr );
104 /* Class can be created only if it is local and */
105 /* if the class with the same name is global. */
107 if (class->style & CS_GLOBALCLASS) return 0;
108 if (!(prevClassPtr->wc.style & CS_GLOBALCLASS)) return 0;
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;
124 newClass->atomName = GlobalAddAtom( name );
125 newClass->wc.lpszClassName = NULL;
127 if (newClass->wc.style & CS_CLASSDC)
128 newClass->hdce = DCE_AllocDCE( DCE_CLASS_DC );
129 else newClass->hdce = 0;
131 /* Make a copy of the menu name (only if it is a string) */
133 if ((int)class->lpszMenuName & 0xffff0000)
135 char *menuname = PTR_SEG_TO_LIN( class->lpszMenuName );
136 HANDLE hname = USER_HEAP_ALLOC( strlen(menuname)+1 );
139 newClass->wc.lpszMenuName = (char *)USER_HEAP_SEG_ADDR( hname );
140 strcpy( USER_HEAP_LIN_ADDR( hname ), menuname );
144 if (classExtra) memset( newClass->wExtra, 0, classExtra );
146 return newClass->atomName;
150 /***********************************************************************
151 * UnregisterClass (USER.403)
153 BOOL UnregisterClass( LPSTR className, HANDLE instance )
155 HANDLE class, prevClass;
156 CLASS * classPtr, * prevClassPtr;
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))
164 /* Remove the class from the linked list */
165 if (firstClass == class) firstClass = classPtr->hNext;
168 for (prevClass = firstClass; prevClass; prevClass=prevClassPtr->hNext)
170 prevClassPtr = (CLASS *) USER_HEAP_LIN_ADDR(prevClass);
171 if (prevClassPtr->hNext == class) break;
175 fprintf(stderr, "ERROR: Class list corrupted\n" );
178 prevClassPtr->hNext = classPtr->hNext;
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 );
193 /***********************************************************************
194 * GetClassWord (USER.129)
196 WORD GetClassWord( HWND hwnd, short offset )
198 return (WORD)GetClassLong( hwnd, offset );
202 /***********************************************************************
203 * SetClassWord (USER.130)
205 WORD SetClassWord( HWND hwnd, short offset, WORD newval )
209 WORD *ptr, retval = 0;
211 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
212 if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
213 ptr = (WORD *)(((char *)classPtr->wExtra) + offset);
220 /***********************************************************************
221 * GetClassLong (USER.131)
223 LONG GetClassLong( HWND hwnd, short offset )
228 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
229 if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
230 return *(LONG *)(((char *)classPtr->wExtra) + offset);
234 /***********************************************************************
235 * SetClassLong (USER.132)
237 LONG SetClassLong( HWND hwnd, short offset, LONG newval )
241 LONG *ptr, retval = 0;
243 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
244 if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
245 ptr = (LONG *)(((char *)classPtr->wExtra) + offset);
252 /***********************************************************************
253 * GetClassName (USER.58)
255 int GetClassName(HWND hwnd, LPSTR lpClassName, short maxCount)
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;
265 return (GetAtomName(classPtr->atomName, lpClassName, maxCount));
269 /***********************************************************************
270 * GetClassInfo (USER.404)
272 BOOL GetClassInfo(HANDLE hInstance, LPSTR lpClassName,
273 LPWNDCLASS lpWndClass)
277 if (HIWORD(lpClassName))
279 dprintf_class(stddeb, "GetClassInfo hInstance=%04x lpClassName=%s\n",
280 hInstance, lpClassName);
283 dprintf_class(stddeb, "GetClassInfo hInstance=%04x lpClassName=#%d\n",
284 hInstance, (int)lpClassName);
287 /* if (!(CLASS_FindClassByName(lpClassName, &classPtr))) return FALSE; */
288 if (!(CLASS_FindClassByName(lpClassName, hInstance, &classPtr)))
290 if (!HIWORD(lpClassName))
293 sprintf(temp, "#%d", (int)lpClassName);
294 if (!(CLASS_FindClassByName(temp, hInstance, &classPtr))) return FALSE;
300 if (hInstance && (hInstance != classPtr->wc.hInstance)) return FALSE;
302 memcpy(lpWndClass, &(classPtr->wc), sizeof(WNDCLASS));
307 /***********************************************************************
308 * ClassFirst (TOOLHELP.69)
310 BOOL ClassFirst( CLASSENTRY *pClassEntry )
312 pClassEntry->wNext = firstClass;
313 return ClassNext( pClassEntry );
317 /***********************************************************************
318 * ClassNext (TOOLHELP.70)
320 BOOL ClassNext( CLASSENTRY *pClassEntry )
322 CLASS *classPtr = (CLASS *) USER_HEAP_LIN_ADDR( pClassEntry->wNext );
323 if (!classPtr) return FALSE;
325 pClassEntry->hInst = classPtr->wc.hInstance;
326 pClassEntry->wNext = classPtr->hNext;
327 GlobalGetAtomName( classPtr->atomName, pClassEntry->szClassName,
328 sizeof(pClassEntry->szClassName) );