source: trunk/src/user32/new/win32class.cpp@ 304

Last change on this file since 304 was 304, checked in by cbratschi, 26 years ago

several bugs fixed, RegisterClass works, CreateWindow on the way

File size: 14.4 KB
Line 
1/* $Id: win32class.cpp,v 1.3 1999-07-14 21:05:58 cbratschi Exp $ */
2/*
3 * Win32 Window Class Managment Code for OS/2
4 *
5 *
6 * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 */
9#include <os2win.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <stdarg.h>
14#include <assert.h>
15#include <misc.h>
16#include <win32class.h>
17#include <win32wnd.h>
18
19//******************************************************************************
20//Win32WndClass methods:
21//******************************************************************************
22Win32WndClass::Win32WndClass(WNDCLASSEXA *wndclass, BOOL isUnicode) : GenericObject(&wndclasses, OBJTYPE_CLASS)
23{
24 if(HIWORD(wndclass->lpszClassName)) {
25 if(isUnicode) {
26 INT len = lstrlenW((LPWSTR)wndclass->lpszClassName)+1;
27
28 classNameA = (PCHAR)malloc(len);
29 classNameW = (WCHAR *)malloc(len*sizeof(WCHAR));
30 }
31 else {
32 INT len = strlen(wndclass->lpszClassName)+1;
33
34 classNameA = (PCHAR)malloc(len);
35 classNameW = (WCHAR *)malloc(len*sizeof(WCHAR));
36 }
37 if(classNameA == NULL || classNameW == NULL) {
38 dprintf(("Win32Class ctr; classNameA/classNameW == NULL"));
39 exit(1);
40 }
41 if(isUnicode) {
42 lstrcpyW(classNameW, (LPWSTR)wndclass->lpszClassName);
43 UnicodeToAscii(classNameW, classNameA);
44 }
45 else {
46 strcpy((char *)classNameA, wndclass->lpszClassName);
47 AsciiToUnicode(classNameA, classNameW);
48 }
49 classAtom = GlobalAddAtomA(classNameA);
50 }
51 else {
52 classNameA = NULL;
53 classNameW = NULL;
54 classAtom = (DWORD)wndclass->lpszClassName;
55 }
56 this->isUnicode = isUnicode;
57
58 dprintf(("USER32: Win32Class ctor\n"));
59 dprintf(("USER32: wndclass->style %X\n", wndclass->style));
60 dprintf(("USER32: wndclass->lpfnWndProc %X\n", wndclass->lpfnWndProc));
61 dprintf(("USER32: wndclass->cbClsExtra %X\n", wndclass->cbClsExtra));
62 dprintf(("USER32: wndclass->cbWndExtra %X\n", wndclass->cbWndExtra));
63 dprintf(("USER32: wndclass->hInstance %X\n", wndclass->hInstance));
64 dprintf(("USER32: wndclass->hIcon %X\n", wndclass->hIcon));
65 dprintf(("USER32: wndclass->hCursor %X\n", wndclass->hCursor));
66 dprintf(("USER32: wndclass->hbrBackground %X\n", wndclass->hbrBackground));
67 if(HIWORD(wndclass->lpszClassName))
68 dprintf(("USER32: wndclass->lpszClassName %s\n", wndclass->lpszClassName));
69 else dprintf(("USER32: wndclass->lpszClassName %X\n", wndclass->lpszClassName));
70
71 if(HIWORD(wndclass->lpszMenuName)) {//convert string name identifier to numeric id
72 dprintf(("USER32: lpszMenuName %s\n", wndclass->lpszMenuName));
73 }
74 else dprintf(("USER32: wndclass->lpszMenuName %X\n", wndclass->lpszMenuName));
75
76 nrExtraClassWords = wndclass->cbClsExtra;
77 nrExtraWindowWords = wndclass->cbWndExtra;
78 backgroundBrush = wndclass->hbrBackground; //TODO: fErase of PAINSTRUCT in WM_PAINT if == NULL
79 hCursor = wndclass->hCursor;
80 hIcon = wndclass->hIcon;
81 hInstance = wndclass->hInstance;
82
83 menuNameA = 0;
84 menuNameW = 0;
85 setMenuName((LPSTR)wndclass->lpszMenuName);
86
87 windowStyle = wndclass->style;
88 windowProc = wndclass->lpfnWndProc;
89
90 //User data class words/longs
91 if(nrExtraClassWords) {
92 userClassLong = (ULONG *)malloc(nrExtraClassWords);
93 if(userClassLong == NULL) {
94 dprintf(("Win32Class ctr: userClassLong == NULL!"));
95 exit(1);
96 }
97 memset(userClassLong, 0, nrExtraClassWords);
98 }
99 else userClassLong = NULL;
100
101 cWindows = 0;
102 hIconSm = wndclass->hIconSm;
103}
104//******************************************************************************
105//******************************************************************************
106Win32WndClass::~Win32WndClass()
107{
108 if(userClassLong) free(userClassLong);
109 if(classNameA) free(classNameA);
110 if(classNameW) free(classNameW);
111 if(menuNameA && HIWORD(menuNameA)) {
112 free(menuNameA);
113 assert(menuNameW);
114 free(menuNameW);
115 }
116}
117//******************************************************************************
118//******************************************************************************
119Win32WndClass *Win32WndClass::FindClass(HINSTANCE hInstance, LPSTR id)
120{
121 Win32WndClass *wndclass = (Win32WndClass *)wndclasses;
122
123 if(wndclass == NULL) return(NULL);
124
125 if(HIWORD(id) != 0) {
126 if(stricmp(wndclass->classNameA, id) == 0 && wndclass->hInstance == hInstance) {
127 return(wndclass);
128 }
129 else {
130 wndclass = (Win32WndClass *)wndclass->GetNext();
131 while(wndclass != NULL) {
132 if(stricmp(wndclass->classNameA, id) == 0 && wndclass->hInstance == hInstance) {
133 return(wndclass);
134 }
135 wndclass = (Win32WndClass *)wndclass->GetNext();
136 }
137 }
138 }
139 else {
140 if(wndclass->classAtom == (DWORD)id && wndclass->hInstance == hInstance) {
141 return(wndclass);
142 }
143 else {
144 wndclass = (Win32WndClass *)wndclass->GetNext();
145 while(wndclass != NULL) {
146 if(wndclass->classAtom == (DWORD)id && wndclass->hInstance == hInstance) {
147 return(wndclass);
148 }
149 wndclass = (Win32WndClass *)wndclass->GetNext();
150 }
151 }
152 }
153 dprintf(("Class %X (inst %X) not found!", id, hInstance));
154 return(NULL);
155}
156//******************************************************************************
157//******************************************************************************
158BOOL Win32WndClass::getClassInfo(WNDCLASSEXA *wndclass)
159{
160 wndclass->cbClsExtra = nrExtraClassWords;
161 wndclass->cbWndExtra = nrExtraWindowWords;
162 wndclass->hbrBackground = backgroundBrush;
163 wndclass->hCursor = hCursor;
164 wndclass->hIcon = hIcon;
165 wndclass->hInstance = hInstance;
166 wndclass->lpszMenuName = (LPCTSTR)menuNameA;
167 wndclass->lpszClassName = (classNameA) ? (LPCTSTR)classNameA : (LPCTSTR)classAtom;
168 wndclass->style = windowStyle;
169 wndclass->lpfnWndProc = windowProc;
170 wndclass->hIconSm = hIconSm;
171 return(TRUE);
172}
173//******************************************************************************
174//******************************************************************************
175BOOL Win32WndClass::getClassInfo(WNDCLASSEXW *wndclass)
176{
177 wndclass->cbClsExtra = nrExtraClassWords;
178 wndclass->cbWndExtra = nrExtraWindowWords;
179 wndclass->hbrBackground = backgroundBrush;
180 wndclass->hCursor = hCursor;
181 wndclass->hIcon = hIcon;
182 wndclass->hInstance = hInstance;
183 wndclass->lpszMenuName = (LPCWSTR)menuNameW;
184 wndclass->lpszClassName = (classNameW) ? (LPCWSTR)classNameW : (LPCWSTR)classAtom;
185 wndclass->style = windowStyle;
186 wndclass->lpfnWndProc = windowProc;
187 wndclass->hIconSm = hIconSm;
188 return(TRUE);
189}
190//******************************************************************************
191//******************************************************************************
192ULONG Win32WndClass::getClassName(LPSTR lpszClassName, ULONG cchClassName)
193{
194 if(HIWORD(classNameA)) {
195 strncpy(lpszClassName, classNameA, cchClassName-1);
196 return strlen(lpszClassName);
197 }
198 *(ULONG *)lpszClassName = classAtom;
199 return(sizeof(ULONG));
200}
201//******************************************************************************
202//******************************************************************************
203ULONG Win32WndClass::getClassName(LPWSTR lpszClassName, ULONG cchClassName)
204{
205 ULONG len;
206
207 if(HIWORD(classNameW)) {
208 lstrcpyW(lpszClassName, classNameW);
209 return lstrlenW(lpszClassName)*sizeof(WCHAR);
210 }
211 *(ULONG *)lpszClassName = classAtom;
212 return(sizeof(ULONG));
213}
214//******************************************************************************
215//******************************************************************************
216void Win32WndClass::setMenuName(LPSTR newMenuName)
217{
218 if(HIWORD(menuNameA)) {
219 free(menuNameA);
220 free(menuNameW);
221 menuNameA = 0;
222 menuNameW = 0;
223 }
224 if(HIWORD(newMenuName)) {
225 if(isUnicode) {
226 menuNameA = (PCHAR)malloc(lstrlenW((LPWSTR)newMenuName)+1);
227 menuNameW = (WCHAR *)malloc((lstrlenW((LPWSTR)newMenuName)+1)*sizeof(WCHAR));
228 }
229 else {
230 menuNameA = (PCHAR)malloc(strlen(newMenuName)+1);
231 menuNameW = (WCHAR *)malloc((strlen(newMenuName)+1)*sizeof(WCHAR));
232 }
233 if(menuNameA == NULL || menuNameW == NULL) {
234 dprintf(("Win32Class ctr; menuName/menuNameW == NULL"));
235 exit(1);
236 }
237 if(isUnicode) {
238 lstrcpyW(menuNameW, (LPWSTR)newMenuName);
239 UnicodeToAscii(menuNameW, menuNameA);
240 }
241 else {
242 strcpy((char *)menuNameA, newMenuName);
243 AsciiToUnicode(menuNameA, menuNameW);
244 }
245 }
246 else {//id
247 menuNameA = (PCHAR)newMenuName;
248 menuNameW = (WCHAR *)newMenuName;
249 }
250}
251//******************************************************************************
252//******************************************************************************
253ULONG Win32WndClass::getClassLongA(int index, BOOL isUnicode)
254{
255 switch(index) {
256 case GCL_CBCLSEXTRA:
257 return nrExtraClassWords;
258 case GCL_CBWNDEXTRA:
259 return nrExtraWindowWords;
260 case GCL_HBRBACKGROUND:
261 return backgroundBrush;
262 case GCL_HCURSOR:
263 return hCursor;
264 case GCL_HICON:
265 return hIcon;
266 case GCL_HMODULE:
267 return hInstance;
268 case GCL_MENUNAME:
269 return (isUnicode) ? (ULONG)menuNameW : (ULONG)menuNameA;
270 case GCL_STYLE:
271 return windowStyle;
272 case GCL_WNDPROC:
273 return (ULONG)windowProc;
274 case GCW_ATOM: //TODO: does this really happen in windows?
275 SetLastError(ERROR_INVALID_PARAMETER);
276 return 0;
277 default:
278 if(index > 0 && index < nrExtraClassWords - sizeof(ULONG)) {
279 return userClassLong[index];
280 }
281 SetLastError(ERROR_INVALID_PARAMETER);
282 return 0;
283 }
284}
285//******************************************************************************
286//******************************************************************************
287WORD Win32WndClass::getClassWord(int index)
288{
289 switch(index) {
290 case GCW_ATOM:
291 return (WORD)classAtom;
292 default:
293 if(index > 0 && index < nrExtraClassWords - sizeof(WORD)) {
294 return ((WORD *)userClassLong)[index];
295 }
296 SetLastError(ERROR_INVALID_PARAMETER);
297 return 0;
298 }
299}
300//******************************************************************************
301//TODO: What effects what immediately?
302//******************************************************************************
303ULONG Win32WndClass::setClassLongA(int index, LONG lNewVal, BOOL isUnicode)
304{
305 ULONG rc;
306
307 switch(index) {
308 case GCL_CBCLSEXTRA: //TODO (doesn't affect allocated classes, so what does it do?)
309 rc = nrExtraClassWords;
310// nrExtraClassWords = lNewVal;
311 break;
312 case GCL_CBWNDEXTRA:
313 rc = nrExtraWindowWords;
314 nrExtraWindowWords = lNewVal;
315 break;
316 case GCL_HBRBACKGROUND:
317 rc = backgroundBrush;
318 backgroundBrush = lNewVal;
319 break;
320 case GCL_HCURSOR:
321 rc = hCursor;
322 hCursor = lNewVal;
323 break;
324 case GCL_HICON:
325 rc = hIcon;
326 hIcon = lNewVal;
327 break;
328 case GCL_HMODULE:
329 rc = hInstance;
330 hInstance = lNewVal;
331 break;
332 case GCL_MENUNAME:
333 rc = 0; //old value is meaningless (according to Wine)
334 setMenuName((LPSTR)lNewVal);
335 break;
336 case GCL_STYLE:
337 rc = windowStyle;
338 windowStyle = lNewVal;
339 break;
340 case GCL_WNDPROC:
341 rc = (ULONG)windowProc;
342 windowProc = (WNDPROC)lNewVal;
343 break;
344 case GCW_ATOM: //TODO: does this really happen in windows?
345 SetLastError(ERROR_INVALID_PARAMETER);
346 return 0;
347 default:
348 if(index > 0 && index < nrExtraClassWords - sizeof(ULONG)) {
349 rc = userClassLong[index];
350 userClassLong[index] = lNewVal;
351 return(rc);
352 }
353 SetLastError(ERROR_INVALID_PARAMETER);
354 return 0;
355 }
356 return(rc);
357}
358//******************************************************************************
359//******************************************************************************
360WORD Win32WndClass::setClassWord(int index, WORD wNewVal)
361{
362 WORD rc;
363
364 switch(index) {
365 case GCW_ATOM:
366 rc = (WORD)classAtom;
367 classAtom = wNewVal;
368 return(rc);
369 default:
370 if(index > 0 && index < nrExtraClassWords - sizeof(WORD)) {
371 rc = ((WORD *)userClassLong)[index];
372 ((WORD *)userClassLong)[index] = wNewVal;
373 return(rc);
374 }
375 SetLastError(ERROR_INVALID_PARAMETER);
376 return 0;
377 }
378}
379//******************************************************************************
380//FIXME: Windows that still exists with this class
381//******************************************************************************
382void Win32WndClass::UnregisterClassA(HINSTANCE hinst, LPSTR id)
383{
384 Win32WndClass *wndclass;
385
386 dprintf(("::UnregisterClass, destroy class %X!!\n", id));
387 wndclass = FindClass(hinst, id);
388 if(wndclass) {
389 delete wndclass;
390 return;
391 }
392 dprintf(("::UnregisterClass, couldn't find class %X!!\n", id));
393}
394//******************************************************************************
395//******************************************************************************
396GenericObject *Win32WndClass::wndclasses = NULL;
397
Note: See TracBrowser for help on using the repository browser.