2 * Window classes functions
4 * Copyright 1993 Alexandre Julliard
22 static CLASS *firstClass = NULL;
25 /***********************************************************************
28 * Dump the content of a class structure to stderr.
30 void CLASS_DumpClass( CLASS *ptr )
35 if (ptr->magic != CLASS_MAGIC)
37 fprintf( stderr, "%p is not a class\n", ptr );
41 GlobalGetAtomName( ptr->atomName, className, sizeof(className) );
43 fprintf( stderr, "Class %p:\n", ptr );
45 "next=%p name=%04x '%s' style=%08x wndProc=%08lx\n"
46 "inst=%04x hdce=%04x icon=%04x cursor=%04x bkgnd=%04x\n"
47 "clsExtra=%d winExtra=%d #windows=%d\n",
48 ptr->next, ptr->atomName, className, ptr->style,
49 (DWORD)ptr->lpfnWndProc, ptr->hInstance, ptr->hdce,
50 ptr->hIcon, ptr->hCursor, ptr->hbrBackground,
51 ptr->cbClsExtra, ptr->cbWndExtra, ptr->cWindows );
54 fprintf( stderr, "extra bytes:" );
55 for (i = 0; i < ptr->cbClsExtra; i++)
56 fprintf( stderr, " %02x", *((BYTE *)ptr->wExtra+i) );
57 fprintf( stderr, "\n" );
59 fprintf( stderr, "\n" );
63 /***********************************************************************
66 * Walk the class list and print each class on stderr.
68 void CLASS_WalkClasses(void)
73 fprintf( stderr, " Class Name Style WndProc\n" );
74 for (ptr = firstClass; ptr; ptr = ptr->next)
76 GlobalGetAtomName( ptr->atomName, className, sizeof(className) );
77 fprintf( stderr, "%08lx %-20.20s %08x %08lx\n", (DWORD)ptr, className,
78 ptr->style, (DWORD)ptr->lpfnWndProc );
80 fprintf( stderr, "\n" );
84 /***********************************************************************
87 * Free a class structure.
89 static void CLASS_FreeClass( CLASS *classPtr )
93 /* Remove the class from the linked list */
95 for (ppClass = &firstClass; *ppClass; ppClass = &(*ppClass)->next)
96 if (*ppClass == classPtr) break;
99 fprintf(stderr, "ERROR: Class list corrupted\n" );
102 *ppClass = classPtr->next;
104 /* Delete the class */
106 if (classPtr->hdce) DCE_FreeDCE( classPtr->hdce );
107 if (classPtr->hbrBackground) DeleteObject( classPtr->hbrBackground );
108 GlobalDeleteAtom( classPtr->atomName );
109 if (HIWORD(classPtr->lpszMenuName))
110 USER_HEAP_FREE( (HANDLE)classPtr->lpszMenuName );
111 HeapFree( SystemHeap, 0, classPtr );
115 /***********************************************************************
116 * CLASS_FreeModuleClasses
118 void CLASS_FreeModuleClasses( HMODULE hModule )
122 for (ptr = firstClass; ptr; ptr = next)
125 if (ptr->hInstance == hModule) CLASS_FreeClass( ptr );
130 /***********************************************************************
131 * CLASS_FindClassByAtom
133 * Return a pointer to the class.
135 CLASS *CLASS_FindClassByAtom( ATOM atom, HINSTANCE16 hinstance )
139 /* First search task-specific classes */
141 for (class = firstClass; (class); class = class->next)
143 if (class->style & CS_GLOBALCLASS) continue;
144 if ((class->atomName == atom) &&
145 ((hinstance==(HINSTANCE16)0xffff) ||
146 (hinstance == class->hInstance))) return class;
149 /* Then search global classes */
151 for (class = firstClass; (class); class = class->next)
153 if (!(class->style & CS_GLOBALCLASS)) continue;
154 if (class->atomName == atom) return class;
161 /***********************************************************************
162 * CLASS_FindClassByName
164 * Return a pointer to the class.
166 CLASS *CLASS_FindClassByName( SEGPTR name, HINSTANCE hinstance )
170 if (!(atom = GlobalFindAtom( name ))) return 0;
171 return CLASS_FindClassByAtom( atom, hinstance );
175 /***********************************************************************
176 * RegisterClass16 (USER.57)
178 ATOM RegisterClass16( const WNDCLASS16 *wc )
180 CLASS * newClass, * prevClass;
184 dprintf_class( stddeb, "RegisterClass: wndproc=%08lx hinst=%04x name='%s' background %04x\n",
185 (DWORD)wc->lpfnWndProc, wc->hInstance,
186 HIWORD(wc->lpszClassName) ?
187 (char *)PTR_SEG_TO_LIN(wc->lpszClassName) : "(int)",
189 dprintf_class(stddeb," style=%04x clsExtra=%d winExtra=%d\n",
190 wc->style, wc->cbClsExtra, wc->cbWndExtra );
192 /* Window classes are owned by modules, not instances */
193 hInstance = GetExePtr( wc->hInstance );
195 /* Check if a class with this name already exists */
196 prevClass = CLASS_FindClassByName( wc->lpszClassName, hInstance );
199 /* Class can be created only if it is local and */
200 /* if the class with the same name is global. */
202 if (wc->style & CS_GLOBALCLASS) return 0;
203 if (!(prevClass->style & CS_GLOBALCLASS)) return 0;
208 classExtra = (wc->cbClsExtra < 0) ? 0 : wc->cbClsExtra;
209 newClass = (CLASS *)HeapAlloc( SystemHeap, 0, sizeof(CLASS) + classExtra );
210 if (!newClass) return 0;
211 newClass->next = firstClass;
212 newClass->magic = CLASS_MAGIC;
213 newClass->cWindows = 0;
214 newClass->style = wc->style;
215 newClass->lpfnWndProc = wc->lpfnWndProc;
216 newClass->cbWndExtra = (wc->cbWndExtra < 0) ? 0 : wc->cbWndExtra;
217 newClass->cbClsExtra = classExtra;
218 newClass->lpszMenuName = wc->lpszMenuName;
219 newClass->hInstance = hInstance;
220 newClass->hIcon = wc->hIcon;
221 newClass->hCursor = wc->hCursor;
222 newClass->hbrBackground = wc->hbrBackground;
224 newClass->atomName = GlobalAddAtom( wc->lpszClassName );
226 if (newClass->style & CS_CLASSDC)
227 newClass->hdce = DCE_AllocDCE( DCE_CLASS_DC );
228 else newClass->hdce = 0;
230 /* Make a copy of the menu name (only if it is a string) */
232 if (HIWORD(wc->lpszMenuName))
234 char *menuname = PTR_SEG_TO_LIN( wc->lpszMenuName );
235 HANDLE hname = USER_HEAP_ALLOC( strlen(menuname)+1 );
238 newClass->lpszMenuName = (SEGPTR)USER_HEAP_SEG_ADDR( hname );
239 strcpy( USER_HEAP_LIN_ADDR( hname ), menuname );
243 if (classExtra) memset( newClass->wExtra, 0, classExtra );
244 firstClass = newClass;
245 return newClass->atomName;
249 /***********************************************************************
250 * RegisterClass32A (USER32.426)
252 ATOM RegisterClass32A( const WNDCLASS32A* wc )
255 HANDLE classh = 0, menuh = 0;
256 SEGPTR classsegp, menusegp;
257 char *classbuf, *menubuf;
260 copy.style=wc->style;
261 ALIAS_RegisterAlias(0,0,(DWORD)wc->lpfnWndProc);
262 copy.lpfnWndProc=wc->lpfnWndProc;
263 copy.cbClsExtra=wc->cbClsExtra;
264 copy.cbWndExtra=wc->cbWndExtra;
265 copy.hInstance=(HINSTANCE)wc->hInstance;
266 copy.hIcon=(HICON)wc->hIcon;
267 copy.hCursor=(HCURSOR)wc->hCursor;
268 copy.hbrBackground=(HBRUSH)wc->hbrBackground;
270 /* FIXME: There has to be a better way of doing this - but neither
271 malloc nor alloca will work */
275 menuh = GlobalAlloc16(0, strlen(wc->lpszMenuName)+1);
276 menusegp = WIN16_GlobalLock16(menuh);
277 menubuf = PTR_SEG_TO_LIN(menusegp);
278 strcpy( menubuf, wc->lpszMenuName);
279 copy.lpszMenuName=menusegp;
282 if(wc->lpszClassName)
284 classh = GlobalAlloc16(0, strlen(wc->lpszClassName)+1);
285 classsegp = WIN16_GlobalLock16(classh);
286 classbuf = PTR_SEG_TO_LIN(classsegp);
287 strcpy( classbuf, wc->lpszClassName);
288 copy.lpszClassName=classsegp;
290 retval = RegisterClass16(©);
292 GlobalFree16(classh);
297 /***********************************************************************
298 * UnregisterClass16 (USER.403)
300 BOOL UnregisterClass16( SEGPTR className, HINSTANCE16 hinstance )
304 hinstance = GetExePtr( hinstance );
306 /* Check if we can remove this class */
307 if (!(classPtr = CLASS_FindClassByName( className, hinstance )))
309 if ((classPtr->hInstance != hinstance) || (classPtr->cWindows > 0))
311 CLASS_FreeClass( classPtr );
316 /***********************************************************************
317 * GetClassWord (USER.129)
319 WORD GetClassWord( HWND hwnd, short offset )
323 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
325 return *(WORD *)(((char *)wndPtr->class->wExtra) + offset);
328 case GCW_HBRBACKGROUND: return wndPtr->class->hbrBackground;
329 case GCW_HCURSOR: return wndPtr->class->hCursor;
330 case GCW_HICON: return wndPtr->class->hIcon;
331 case GCW_HMODULE: return wndPtr->class->hInstance;
332 case GCW_ATOM: return wndPtr->class->atomName;
336 return (WORD)GetClassLong( hwnd, offset );
338 fprintf(stderr, "Warning: invalid offset %d for GetClassWord()\n", offset);
343 /***********************************************************************
344 * SetClassWord (USER.130)
346 WORD SetClassWord( HWND hwnd, short offset, WORD newval )
349 WORD *ptr, retval = 0;
351 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
352 if (offset >= 0) ptr = (WORD *)(((char *)wndPtr->class->wExtra) + offset);
355 case GCW_HBRBACKGROUND: ptr = &wndPtr->class->hbrBackground; break;
356 case GCW_HCURSOR: ptr = &wndPtr->class->hCursor; break;
357 case GCW_HICON: ptr = &wndPtr->class->hIcon; break;
358 case GCW_HMODULE: ptr = &wndPtr->class->hInstance; break;
359 case GCW_ATOM: ptr = &wndPtr->class->atomName; break;
363 return (WORD)SetClassLong( hwnd, offset, (LONG)newval );
365 fprintf( stderr, "Warning: invalid offset %d for SetClassWord()\n",
375 /***********************************************************************
376 * GetClassLong (USER.131)
378 LONG GetClassLong( HWND hwnd, short offset )
382 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
384 return *(WORD *)(((char *)wndPtr->class->wExtra) + offset);
387 case GCL_STYLE: return (LONG)wndPtr->class->style;
388 case GCL_CBWNDEXTRA: return (LONG)wndPtr->class->cbWndExtra;
389 case GCL_CBCLSEXTRA: return (LONG)wndPtr->class->cbClsExtra;
390 case GCL_MENUNAME: return (LONG)wndPtr->class->lpszMenuName;
391 case GCL_WNDPROC: return (LONG)wndPtr->class->lpfnWndProc;
393 fprintf(stderr, "Warning: invalid offset %d for GetClassLong()\n", offset);
398 /***********************************************************************
399 * SetClassLong (USER.132)
401 LONG SetClassLong( HWND hwnd, short offset, LONG newval )
404 LONG *ptr, retval = 0;
406 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
407 if (offset >= 0) ptr = (LONG *)(((char *)wndPtr->class->wExtra) + offset);
410 case GCL_STYLE: ptr = (LONG*)&wndPtr->class->style; break;
411 case GCL_CBWNDEXTRA: ptr = (LONG*)&wndPtr->class->cbWndExtra; break;
412 case GCL_CBCLSEXTRA: ptr = (LONG*)&wndPtr->class->cbClsExtra; break;
413 case GCL_MENUNAME: ptr = (LONG*)&wndPtr->class->lpszMenuName; break;
414 case GCL_WNDPROC: ptr = (LONG*)&wndPtr->class->lpfnWndProc; break;
416 fprintf( stderr, "Warning: invalid offset %d for SetClassLong()\n",
426 /***********************************************************************
427 * GetClassName (USER.58)
429 int GetClassName(HWND hwnd, LPSTR lpClassName, short maxCount)
433 /* FIXME: We have the find the correct hInstance */
434 dprintf_class(stddeb,"GetClassName(%04x,%p,%d)\n",hwnd,lpClassName,maxCount);
435 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
437 return GlobalGetAtomName( wndPtr->class->atomName, lpClassName, maxCount );
441 /***********************************************************************
442 * GetClassInfo (USER.404)
444 BOOL GetClassInfo( HANDLE hInstance, SEGPTR name, WNDCLASS16 *lpWndClass )
448 dprintf_class( stddeb, "GetClassInfo: hInstance=%04x className=%s\n",
450 HIWORD(name) ? (char *)PTR_SEG_TO_LIN(name) : "(int)" );
452 hInstance = GetExePtr( hInstance );
454 if (!(classPtr = CLASS_FindClassByName( name, hInstance ))) return FALSE;
455 if (hInstance && (hInstance != classPtr->hInstance)) return FALSE;
457 lpWndClass->style = (UINT16)classPtr->style;
458 lpWndClass->lpfnWndProc = classPtr->lpfnWndProc;
459 lpWndClass->cbClsExtra = (INT16)classPtr->cbClsExtra;
460 lpWndClass->cbWndExtra = (INT16)classPtr->cbWndExtra;
461 lpWndClass->hInstance = classPtr->hInstance;
462 lpWndClass->hIcon = classPtr->hIcon;
463 lpWndClass->hCursor = classPtr->hCursor;
464 lpWndClass->hbrBackground = classPtr->hbrBackground;
465 lpWndClass->lpszMenuName = classPtr->lpszMenuName;
466 lpWndClass->lpszClassName = 0;
472 /***********************************************************************
473 * ClassFirst (TOOLHELP.69)
475 BOOL ClassFirst( CLASSENTRY *pClassEntry )
477 pClassEntry->wNext = 1;
478 return ClassNext( pClassEntry );
482 /***********************************************************************
483 * ClassNext (TOOLHELP.70)
485 BOOL ClassNext( CLASSENTRY *pClassEntry )
488 CLASS *class = firstClass;
490 if (!pClassEntry->wNext) return FALSE;
491 for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
494 pClassEntry->wNext = 0;
497 pClassEntry->hInst = class->hInstance;
498 pClassEntry->wNext++;
499 GlobalGetAtomName( class->atomName, pClassEntry->szClassName,
500 sizeof(pClassEntry->szClassName) );