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

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

listview: ver 4 feature complete

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