source: trunk/src/comctl32/CCBase.cpp@ 3830

Last change on this file since 3830 was 3410, checked in by cbratschi, 25 years ago

* empty log message *

File size: 10.0 KB
Line 
1/* $Id: CCBase.cpp,v 1.9 2000-04-16 18:52:38 cbratschi Exp $ */
2/*
3 * COMCTL32 Base Functions and Macros for all Controls
4 *
5 * Copyright 2000 Christoph Bratschi (cbratschi@datacomm.ch)
6 *
7 * parts from WINE code
8 */
9#include <string.h>
10#include <wcstr.h>
11#include "winbase.h"
12#include "comctl32.h"
13#include "ccbase.h"
14
15BOOL checkVersion(INT iVersion)
16{
17 return iVersion <= COMCTL32_VERSION;
18}
19
20//init-done
21
22PVOID initControl(HWND hwnd,DWORD dwSize)
23{
24 COMCTL32_HEADER *infoPtr;
25
26 if (dwSize < sizeof(COMCTL32_HEADER)) return NULL;
27
28 infoPtr = (COMCTL32_HEADER*)COMCTL32_Alloc(dwSize);
29
30 if (!infoPtr) return NULL;
31
32 setInfoPtr(hwnd,infoPtr);
33 ZeroMemory(infoPtr,dwSize);
34 infoPtr->dwSize = dwSize;
35 infoPtr->iVersion = 0;
36 infoPtr->fUnicode = IsWindowUnicode(hwnd);
37 infoPtr->uNotifyFormat = sendNotifyFormat(GetParent(hwnd),hwnd,NF_QUERY);
38 infoPtr->hwndNotify = GetParent(hwnd);
39
40 return infoPtr;
41}
42
43VOID doneControl(HWND hwnd)
44{
45 COMCTL32_HEADER *infoPtr = getInfoPtr(hwnd);
46
47 if (infoPtr)
48 {
49 COMCTL32_Free(infoPtr);
50 setInfoPtr(hwnd,NULL);
51 }
52}
53
54//Default message handler
55
56LRESULT defComCtl32Proc(HWND hwnd,UINT Msg,WPARAM wParam,LPARAM lParam,BOOL unicode)
57{
58 COMCTL32_HEADER *infoPtr;
59
60 switch (Msg)
61 {
62 case CCM_GETVERSION:
63 infoPtr = getInfoPtr(hwnd);
64 return infoPtr ? infoPtr->iVersion:0;
65
66 case CCM_SETVERSION:
67 infoPtr = getInfoPtr(hwnd);
68 if (infoPtr)
69 {
70 if (checkVersion((INT)wParam))
71 {
72 INT oldVersion;
73
74 oldVersion = infoPtr->iVersion;
75 infoPtr->iVersion = (INT)wParam;
76 return oldVersion;
77 } else return -1;
78 } else return 0;
79
80 case CCM_GETUNICODEFORMAT:
81 infoPtr = getInfoPtr(hwnd);
82 return infoPtr ? infoPtr->fUnicode:IsWindowUnicode(hwnd);
83
84 case CCM_SETUNICODEFORMAT:
85 infoPtr = getInfoPtr(hwnd);
86 if (infoPtr)
87 {
88 BOOL oldFormat;
89
90 oldFormat = infoPtr->fUnicode;
91 infoPtr->fUnicode = (INT)wParam;
92 return oldFormat;
93 } else return IsWindowUnicode(hwnd);
94
95 case WM_NOTIFYFORMAT:
96 {
97 infoPtr = getInfoPtr(hwnd);
98
99 if (!infoPtr) break;
100
101 if (lParam == NF_REQUERY)
102 {
103 infoPtr->uNotifyFormat = sendNotifyFormat(GetParent(hwnd),hwnd,NF_QUERY);
104 if ((infoPtr->uNotifyFormat != NFR_ANSI) && (infoPtr->uNotifyFormat != NFR_UNICODE))
105 infoPtr->uNotifyFormat = IsWindowUnicode(GetParent(hwnd)) ? NFR_UNICODE:NFR_ANSI;
106 return infoPtr->uNotifyFormat;
107 } else if (lParam == NF_QUERY)
108 {
109 return infoPtr->uNotifyFormat;
110 }
111 break;
112 }
113
114 case CCM_SETNOTIFYWINDOW:
115 {
116 infoPtr = getInfoPtr(hwnd);
117
118 if (!infoPtr) break;
119
120 infoPtr->hwndNotify = (HWND)wParam;
121
122 break;
123 }
124 }
125
126 if (unicode)
127 return DefWindowProcW(hwnd,Msg,wParam,lParam);
128 else
129 return DefWindowProcA(hwnd,Msg,wParam,lParam);
130}
131
132LRESULT defComCtl32ProcA(HWND hwnd,UINT Msg,WPARAM wParam,LPARAM lParam)
133{
134 return defComCtl32Proc(hwnd,Msg,wParam,lParam,FALSE);
135}
136
137LRESULT defComCtl32ProcW(HWND hwnd,UINT Msg,WPARAM wParam,LPARAM lParam)
138{
139 return defComCtl32Proc(hwnd,Msg,wParam,lParam,TRUE);
140}
141
142//Notifications
143
144BOOL isUnicodeNotify(COMCTL32_HEADER *infoPtr)
145{
146 if (!infoPtr) return FALSE;
147
148 return infoPtr->uNotifyFormat == NFR_UNICODE;
149}
150
151BOOL isUnicodeNotify(HWND hwnd)
152{
153 COMCTL32_HEADER *infoPtr = getInfoPtr(hwnd);
154
155 return isUnicodeNotify(infoPtr);
156}
157
158HWND getNotifyWindow(COMCTL32_HEADER *infoPtr)
159{
160 if (!infoPtr) return 0;
161
162 return infoPtr->hwndNotify;
163}
164
165HWND getNotifyWindow(HWND hwnd)
166{
167 COMCTL32_HEADER *infoPtr = getInfoPtr(hwnd);
168
169 return getNotifyWindow(infoPtr);
170}
171
172LRESULT sendNotify(HWND hwnd,UINT code)
173{
174 NMHDR nmhdr;
175
176 nmhdr.hwndFrom = hwnd;
177 nmhdr.idFrom = GetWindowLongA(hwnd,GWL_ID);
178 nmhdr.code = code;
179
180 return SendMessageA(getNotifyWindow(hwnd),WM_NOTIFY,nmhdr.idFrom,(LPARAM)&nmhdr);
181}
182
183LRESULT sendNotify(HWND hwndFrom,HWND hwndTo,UINT code)
184{
185 NMHDR nmhdr;
186
187 nmhdr.hwndFrom = hwndFrom;
188 nmhdr.idFrom = GetWindowLongA(hwndFrom,GWL_ID);
189 nmhdr.code = code;
190
191 return SendMessageA(hwndTo,WM_NOTIFY,nmhdr.idFrom,(LPARAM)&nmhdr);
192}
193
194LRESULT sendNotify(HWND hwnd,UINT code,LPNMHDR nmhdr)
195{
196 if (!nmhdr) return 0;
197
198 nmhdr->hwndFrom = hwnd;
199 nmhdr->idFrom = GetWindowLongA(hwnd,GWL_ID);
200 nmhdr->code = code;
201
202 return SendMessageA(getNotifyWindow(hwnd),WM_NOTIFY,nmhdr->idFrom,(LPARAM)nmhdr);
203}
204
205LRESULT sendNotify(HWND hwndFrom,HWND hwndTo,UINT code,LPNMHDR nmhdr)
206{
207 if (!nmhdr) return 0;
208
209 nmhdr->hwndFrom = hwndFrom;
210 nmhdr->idFrom = GetWindowLongA(hwndFrom,GWL_ID);
211 nmhdr->code = code;
212
213 return SendMessageA(hwndTo,WM_NOTIFY,nmhdr->idFrom,(LPARAM)nmhdr);
214}
215
216LRESULT sendNotifyFormat(HWND hwnd,HWND hwndFrom,LPARAM command)
217{
218 return SendMessageA(hwnd,WM_NOTIFYFORMAT,hwndFrom,command);
219}
220
221LRESULT sendCommand(HWND hwnd,UINT wNotifyCode)
222{
223 return SendMessageA(getNotifyWindow(hwnd),WM_COMMAND,MAKEWPARAM(GetWindowLongA(hwnd,GWL_ID),wNotifyCode),(LPARAM)hwnd);
224}
225
226LRESULT sendHScroll(HWND hwnd,UINT wNotifyCode)
227{
228 return SendMessageA(getNotifyWindow(hwnd),WM_HSCROLL,(WPARAM)wNotifyCode,(LPARAM)hwnd);
229}
230
231LRESULT sendVScroll(HWND hwnd,UINT wNotifyCode)
232{
233 return SendMessageA(getNotifyWindow(hwnd),WM_VSCROLL,(WPARAM)wNotifyCode,(LPARAM)hwnd);
234}
235
236//Tooltips
237
238HWND createToolTip(HWND hwnd,UINT flags,BOOL addtool)
239{
240 HWND hwndToolTip;
241 NMTOOLTIPSCREATED nmttc;
242 TTTOOLINFOA ti;
243
244 hwndToolTip =
245 CreateWindowExA (0, TOOLTIPS_CLASSA, NULL, 0,
246 CW_USEDEFAULT, CW_USEDEFAULT,
247 CW_USEDEFAULT, CW_USEDEFAULT,
248 hwnd, 0, 0, 0);
249
250 if (!hwndToolTip) return 0;
251
252 /* Send NM_TOOLTIPSCREATED notification */
253 nmttc.hwndToolTips = hwndToolTip;
254 sendNotify(hwnd,NM_TOOLTIPSCREATED,&nmttc.hdr);
255
256 if (addtool)
257 {
258 ZeroMemory(&ti,sizeof(TTTOOLINFOA));
259 ti.cbSize = sizeof(TTTOOLINFOA);
260 ti.uFlags = flags;
261 ti.hwnd = hwnd;
262 ti.uId = 0;
263 ti.lpszText = "";
264 SetRectEmpty (&ti.rect);
265
266 SendMessageA(hwndToolTip,TTM_ADDTOOLA,0,(LPARAM)&ti);
267 }
268
269 return hwndToolTip;
270}
271
272VOID destroyToolTip(HWND hwndToolTip)
273{
274 if (hwndToolTip) DestroyWindow(hwndToolTip);
275}
276
277//stub control
278
279VOID drawStubControl(HWND hwnd,HDC hdc)
280{
281 RECT rect;
282 HBRUSH brush = CreateSolidBrush(RGB(0,0,255));
283 HPEN pen = CreatePen(PS_SOLID,0,RGB(255,0,0)),oldPen;
284 COLORREF oldColor;
285
286 GetClientRect(hwnd,&rect);
287 FillRect(hdc,&rect,brush);
288 oldPen = SelectObject(hdc,pen);
289 MoveToEx(hdc,0,0,NULL);
290 LineTo(hdc,rect.right,rect.bottom);
291 MoveToEx(hdc,rect.right,0,NULL);
292 LineTo(hdc,0,rect.bottom);
293 SelectObject(hdc,oldPen);
294 oldColor = SetTextColor(hdc,RGB(255,255,255));
295 DrawTextA(hdc,"Unimplemented Control!",-1,&rect,DT_CENTER | DT_SINGLELINE | DT_VCENTER);
296 SetTextColor(hdc,oldColor);
297
298 DeleteObject(brush);
299 DeleteObject(pen);
300}
301
302//string functions
303
304//compare ANSI with UNICODE string
305INT lstrcmpAtoW(CHAR* textA,WCHAR* textW)
306{
307 INT len,res;
308 WCHAR* tmp;
309
310 len = lstrlenA(textA);
311 if (len > 0)
312 {
313 len++;
314 tmp = (WCHAR*)COMCTL32_Alloc(len*sizeof(WCHAR));
315 lstrcpyAtoW(tmp,textA);
316 } else tmp = NULL;
317
318 res = lstrcmpW(tmp,textW);
319
320 if (tmp) COMCTL32_Free(tmp);
321 return res;
322}
323
324INT lstrcmpAW(WCHAR *textA,BOOL textaunicode,WCHAR *textB,BOOL textbunicode)
325{
326 if (textaunicode)
327 {
328 if (textbunicode)
329 {
330 return lstrcmpW(textA,textB);
331 } else
332 {
333 return lstrcmpAtoW((LPSTR)textB,textA);
334 }
335 } else
336 {
337 if (textbunicode)
338 {
339 return lstrcmpAtoW((LPSTR)textA,textB);
340 } else
341 {
342 return lstrcmpA((LPSTR)textA,(LPSTR)textB);
343 }
344 }
345}
346
347//NOTE for lstrcmpni*: both buffers must be at least shortLen+1 characters long!
348
349INT lstrcmpniA(CHAR *textA,CHAR *textB,INT len)
350{
351 CHAR tmp1,tmp2;
352 INT res;
353
354 tmp1 = textA[len];
355 textA[len] = 0;
356 tmp2 = textB[len];
357 textB[len] = 0;
358
359 res = lstrcmpiA(textA,textB);
360
361 textA[len] = tmp1;
362 textB[len] = tmp2;
363 return res;
364}
365
366INT lstrcmpniAtoW(CHAR *textA,WCHAR* textB,INT len)
367{
368 WCHAR *tmp;
369 INT res;
370
371 tmp = (WCHAR*)COMCTL32_Alloc((len+1)*sizeof(WCHAR));
372 lstrcpynAtoW(tmp,textA,len+1);
373
374 res = lstrcmpniW(tmp,textB,len);
375
376 COMCTL32_Free(tmp);
377 return res;
378}
379
380
381INT lstrcmpniW(WCHAR *textA,WCHAR *textB,INT len)
382{
383 WCHAR tmp1,tmp2;
384 INT res;
385
386 tmp1 = textA[len];
387 textA[len] = 0;
388 tmp2 = textB[len];
389 textB[len] = 0;
390
391 res = lstrcmpiW(textA,textB);
392
393 textA[len] = tmp1;
394 textB[len] = tmp2;
395 return res;
396}
397
398INT lstrcmpniAW(WCHAR *textA,BOOL unicodeA,WCHAR *textB,BOOL unicodeB,INT len)
399{
400 if (unicodeA)
401 {
402 if (unicodeB)
403 {
404 return lstrcmpniW(textA,textB,len);
405 } else
406 {
407 return lstrcmpniAtoW((LPSTR)textB,textA,len);
408 }
409 } else
410 {
411 if (unicodeB)
412 {
413 return lstrcmpniAtoW((LPSTR)textA,textB,len);
414 } else
415 {
416 return lstrcmpniA((LPSTR)textA,(LPSTR)textB,len);
417 }
418 }
419}
420
421CHAR* lstrstrA(CHAR *text,CHAR *subtext)
422{
423 return strstr(text,subtext);
424}
425
426WCHAR* lstrstrW(WCHAR *text,WCHAR *subtext)
427{
428 return (WCHAR*)wcswcs((const wchar_t*)text,(wchar_t*)subtext);
429}
430
431//NOTE: less information in ASCII subtext
432CHAR* lstrstrAtoW(CHAR *text,WCHAR *subtext)
433{
434 INT len;
435 CHAR *tmp,*res;
436
437 len = lstrlenW(subtext);
438 if (len > 0)
439 {
440 len++;
441 tmp = (CHAR*)COMCTL32_Alloc(len);
442 lstrcpyWtoA(tmp,subtext);
443 } else tmp = NULL;
444
445 res = strstr(text,tmp);
446
447 if (tmp) COMCTL32_Free(tmp);
448 return res;
449}
450
451WCHAR* lstrstrWtoA(WCHAR *text,CHAR *subtext)
452{
453 INT len;
454 WCHAR *tmp,*res;
455
456 len = lstrlenA(subtext);
457 if (len > 0)
458 {
459 len++;
460 tmp = (WCHAR*)COMCTL32_Alloc(len*sizeof(WCHAR));
461 lstrcpyAtoW(tmp,subtext);
462 } else tmp = NULL;
463
464 res = (WCHAR*)wcswcs((const wchar_t*)text,(wchar_t*)tmp);
465
466 if (tmp) COMCTL32_Free(tmp);
467 return res;
468}
469
470WCHAR* lstrstrAW(WCHAR *text,BOOL textunicode,WCHAR *subtext,BOOL subtextunicode)
471{
472 if (textunicode)
473 {
474 if (subtextunicode)
475 {
476 return lstrstrW(text,subtext);
477 } else
478 {
479 return lstrstrWtoA(text,(LPSTR)subtext);
480 }
481 } else
482 {
483 if (subtextunicode)
484 {
485 return (WCHAR*)lstrstrAtoW((LPSTR)text,subtext);
486 } else
487 {
488 return (WCHAR*)lstrstrA((LPSTR)text,(LPSTR)subtext);
489 }
490 }
491}
Note: See TracBrowser for help on using the repository browser.