4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1998 Alex Priem <alexp@sci.kun.nl>
11 * -Check ranges when changing field-focus.
12 * -Check all notifications/behavior.
13 * -Optimization: include lpipsi in IPADDRESS_INFO.
14 * -CurrentFocus: field that has focus at moment of processing.
15 * -connect Rect32 rcClient.
16 * -handle right and left arrows correctly. Boring.
17 * -split GotoNextField in CheckField and GotoNextField.
18 * -check ipaddress.cpp for missing features.
19 * -refresh: draw '.' instead of setpixel.
28 #include "ipaddress.h"
32 DEFAULT_DEBUG_CHANNEL(ipaddress)
35 #define IPADDRESS_GetInfoPtr(hwnd) ((IPADDRESS_INFO *)GetWindowLongA (hwnd, 0))
39 IPADDRESS_SendNotify (HWND hwnd, UINT command);
41 IPADDRESS_SendIPAddressNotify (HWND hwnd, UINT field, BYTE newValue);
44 /* property name of tooltip window handle */
45 #define IP_SUBCLASS_PROP "CCIP32SubclassInfo"
48 static LRESULT CALLBACK
49 IPADDRESS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
55 IPADDRESS_Refresh (HWND hwnd, HDC hdc)
59 COLORREF clr=GetSysColor (COLOR_3DDKSHADOW);
62 GetClientRect (hwnd, &rcClient);
63 hbr = CreateSolidBrush (RGB(255,255,255));
64 DrawEdge (hdc, &rcClient, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
65 FillRect (hdc, &rcClient, hbr);
69 fieldsize=(rcClient.right-rcClient.left) /4;
71 for (i=0; i<3; i++) { /* Should draw text "." here */
73 SetPixel (hdc, x, 13, clr);
74 SetPixel (hdc, x, 14, clr);
75 SetPixel (hdc, x+1, 13, clr);
76 SetPixel (hdc, x+1, 14, clr);
87 IPADDRESS_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
89 IPADDRESS_INFO *infoPtr;
92 LPIP_SUBCLASS_INFO lpipsi;
95 infoPtr = (IPADDRESS_INFO *)COMCTL32_Alloc (sizeof(IPADDRESS_INFO));
96 SetWindowLongA (hwnd, 0, (DWORD)infoPtr);
98 GetClientRect (hwnd, &rcClient);
100 fieldsize=(rcClient.right-rcClient.left) /4;
102 edit.top =rcClient.top+2;
103 edit.bottom=rcClient.bottom-2;
105 lpipsi=(LPIP_SUBCLASS_INFO)
106 GetPropA ((HWND)hwnd, IP_SUBCLASS_PROP);
107 if (lpipsi == NULL) {
108 lpipsi= (LPIP_SUBCLASS_INFO) COMCTL32_Alloc (sizeof(IP_SUBCLASS_INFO));
111 SetPropA ((HWND)hwnd, IP_SUBCLASS_PROP,
113 /* infoPtr->lpipsi= lpipsi; */
115 WARN (ipaddress,"IP-create called twice\n");
117 for (i=0; i<=3; i++) {
118 infoPtr->LowerLimit[i]=0;
119 infoPtr->UpperLimit[i]=255;
120 edit.left=rcClient.left+i*fieldsize+3;
121 edit.right=rcClient.left+(i+1)*fieldsize-2;
122 lpipsi->hwndIP[i]= CreateWindowA ("edit", NULL,
123 WS_CHILD | WS_VISIBLE | ES_LEFT,
124 edit.left, edit.top, edit.right-edit.left, edit.bottom-edit.top,
125 hwnd, (HMENU) 1, GetWindowLongA (hwnd, GWL_HINSTANCE), NULL);
126 lpipsi->wpOrigProc[i]= (WNDPROC)
127 SetWindowLongA (lpipsi->hwndIP[i],GWL_WNDPROC, (LONG)
128 IPADDRESS_SubclassProc);
129 SetPropA ((HWND)lpipsi->hwndIP[i], IP_SUBCLASS_PROP,
133 lpipsi->infoPtr= infoPtr;
140 IPADDRESS_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
143 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd);
144 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
145 GetPropA ((HWND)hwnd, IP_SUBCLASS_PROP);
147 for (i=0; i<=3; i++) {
148 SetWindowLongA ((HWND)lpipsi->hwndIP[i], GWL_WNDPROC,
149 (LONG)lpipsi->wpOrigProc[i]);
152 COMCTL32_Free (infoPtr);
158 IPADDRESS_KillFocus (HWND hwnd, WPARAM wParam, LPARAM lParam)
162 TRACE (ipaddress,"\n");
164 IPADDRESS_Refresh (hwnd, hdc);
165 ReleaseDC (hwnd, hdc);
167 IPADDRESS_SendIPAddressNotify (hwnd, 0, 0); /* FIXME: should use -1 */
168 IPADDRESS_SendNotify (hwnd, EN_KILLFOCUS);
169 InvalidateRect (hwnd, NULL, TRUE);
176 IPADDRESS_Paint (HWND hwnd, WPARAM wParam)
181 hdc = wParam==0 ? BeginPaint (hwnd, &ps) : (HDC)wParam;
182 IPADDRESS_Refresh (hwnd, hdc);
184 EndPaint (hwnd, &ps);
190 IPADDRESS_SetFocus (HWND hwnd, WPARAM wParam, LPARAM lParam)
194 TRACE (ipaddress,"\n");
197 IPADDRESS_Refresh (hwnd, hdc);
198 ReleaseDC (hwnd, hdc);
205 IPADDRESS_Size (HWND hwnd, WPARAM wParam, LPARAM lParam)
207 /* IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd); */
208 TRACE (ipaddress,"\n");
214 IPADDRESS_SendNotify (HWND hwnd, UINT command)
217 TRACE (ipaddress, "%x\n",command);
218 return (BOOL)SendMessageA (GetParent (hwnd), WM_COMMAND,
219 MAKEWPARAM (GetWindowLongA (hwnd, GWL_ID),command), (LPARAM)hwnd);
224 IPADDRESS_SendIPAddressNotify (HWND hwnd, UINT field, BYTE newValue)
228 TRACE (ipaddress, "%x %x\n",field,newValue);
229 nmip.hdr.hwndFrom = hwnd;
230 nmip.hdr.idFrom = GetWindowLongA (hwnd, GWL_ID);
231 nmip.hdr.code = IPN_FIELDCHANGED;
234 nmip.iValue=newValue;
236 return (BOOL)SendMessageA (GetParent (hwnd), WM_NOTIFY,
237 (WPARAM)nmip.hdr.idFrom, (LPARAM)&nmip);
244 IPADDRESS_ClearAddress (HWND hwnd, WPARAM wParam, LPARAM lParam)
249 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
250 GetPropA ((HWND)hwnd,IP_SUBCLASS_PROP);
252 TRACE (ipaddress,"\n");
256 SetWindowTextA (lpipsi->hwndIP[i],buf);
259 IPADDRESS_Refresh (hwnd, hdc);
260 ReleaseDC (hwnd, hdc);
266 IPADDRESS_IsBlank (HWND hwnd, WPARAM wParam, LPARAM lParam)
270 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
271 GetPropA ((HWND)hwnd, IP_SUBCLASS_PROP);
273 TRACE (ipaddress,"\n");
275 for (i=0; i<=3; i++) {
276 GetWindowTextA (lpipsi->hwndIP[i],buf,5);
285 IPADDRESS_GetAddress (HWND hwnd, WPARAM wParam, LPARAM lParam)
288 int i,valid,fieldvalue;
290 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd);
291 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
292 GetPropA ((HWND)hwnd, IP_SUBCLASS_PROP);
294 TRACE (ipaddress,"\n");
298 for (i=0; i<=3; i++) {
299 GetWindowTextA (lpipsi->hwndIP[i],field,4);
303 fieldvalue=atoi(field);
304 if (fieldvalue<infoPtr->LowerLimit[i])
305 fieldvalue=infoPtr->LowerLimit[i];
306 if (fieldvalue>infoPtr->UpperLimit[i])
307 fieldvalue=infoPtr->UpperLimit[i];
313 *(LPDWORD) lParam=ip_addr;
320 IPADDRESS_SetRange (HWND hwnd, WPARAM wParam, LPARAM lParam)
322 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd);
325 TRACE (ipaddress,"\n");
328 if ((index<0) || (index>3)) return 0;
330 infoPtr->LowerLimit[index]=lParam & 0xff;
331 infoPtr->UpperLimit[index]=(lParam >>8) & 0xff;
336 IPADDRESS_SetAddress (HWND hwnd, WPARAM wParam, LPARAM lParam)
338 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd);
340 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
341 GetPropA ((HWND)hwnd, IP_SUBCLASS_PROP);
342 int i,ip_address,value;
345 TRACE (ipaddress,"\n");
346 ip_address=(DWORD) lParam;
348 for (i=3; i>=0; i--) {
349 value=ip_address & 0xff;
350 if ((value>=infoPtr->LowerLimit[i]) && (value<=infoPtr->UpperLimit[i]))
352 sprintf (buf,"%d",value);
353 SetWindowTextA (lpipsi->hwndIP[i],buf);
354 IPADDRESS_SendNotify (hwnd, EN_CHANGE);
359 hdc = GetDC (hwnd); /* & send notifications */
360 IPADDRESS_Refresh (hwnd, hdc);
361 ReleaseDC (hwnd, hdc);
370 IPADDRESS_SetFocusToField (HWND hwnd, WPARAM wParam, LPARAM lParam)
372 /* IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd); */
373 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
374 GetPropA ((HWND)hwnd, IP_SUBCLASS_PROP);
378 TRACE (ipaddress," %d\n", index);
379 if ((index<0) || (index>3)) return 0;
381 SetFocus (lpipsi->hwndIP[index]);
388 IPADDRESS_LButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
390 TRACE (ipaddress, "\n");
393 IPADDRESS_SendNotify (hwnd, EN_SETFOCUS);
394 IPADDRESS_SetFocusToField (hwnd, 0, 0);
401 /* tab/shift-tab: IPN_FIELDCHANGED, lose focus.
402 dot, space,right arrow: set focus to next child edit.
403 numerics (0..9), control characters: forward to default edit control
404 other characters: dropped
411 IPADDRESS_GotoNextField (LPIP_SUBCLASS_INFO lpipsi, int currentfield)
413 int newField,fieldvalue;
415 IPADDRESS_INFO *infoPtr=lpipsi->infoPtr;
417 TRACE (ipaddress,"\n");
418 GetWindowTextA (lpipsi->hwndIP[currentfield],field,4);
422 fieldvalue=atoi(field);
423 if (fieldvalue<infoPtr->LowerLimit[currentfield])
424 newField=infoPtr->LowerLimit[currentfield];
425 if (fieldvalue>infoPtr->UpperLimit[currentfield])
426 newField=infoPtr->UpperLimit[currentfield];
428 sprintf (field,"%d",newField);
429 SetWindowTextA (lpipsi->hwndIP[currentfield], field);
435 SetFocus (lpipsi->hwndIP[currentfield+1]);
441 IPADDRESS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
444 IPADDRESS_INFO *infoPtr;
445 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
446 GetPropA ((HWND)hwnd,IP_SUBCLASS_PROP);
448 infoPtr = lpipsi->infoPtr;
451 if (lpipsi->hwndIP[i]==hwnd) index=i;
456 char c=(char) wParam;
460 shift = GetKeyState(VK_SHIFT) & 0x8000;
462 pwnd=GetNextDlgTabItem (GetParent (hwnd), 0, TRUE);
464 pwnd=GetNextDlgTabItem (GetParent (hwnd), 0, FALSE);
465 if (pwnd) SetFocus (pwnd);
469 if ((c==' ') || (c=='.') || (c==VK_RIGHT)) {
470 IPADDRESS_GotoNextField (lpipsi,index);
480 if (((c>='0') && (c<='9')) || (iscntrl(c))) {
481 l=GetWindowTextLengthA (lpipsi->hwndIP[index]);
483 if (IPADDRESS_GotoNextField (lpipsi,index)) {
496 return CallWindowProcA (lpipsi->wpOrigProc[index], hwnd, uMsg, wParam, lParam);
500 IPADDRESS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
504 case IPM_CLEARADDRESS:
505 return IPADDRESS_ClearAddress (hwnd, wParam, lParam);
508 return IPADDRESS_SetAddress (hwnd, wParam, lParam);
511 return IPADDRESS_GetAddress (hwnd, wParam, lParam);
514 return IPADDRESS_SetRange (hwnd, wParam, lParam);
517 return IPADDRESS_SetFocusToField (hwnd, wParam, lParam);
520 return IPADDRESS_IsBlank (hwnd, wParam, lParam);
523 return IPADDRESS_Create (hwnd, wParam, lParam);
526 return IPADDRESS_Destroy (hwnd, wParam, lParam);
529 return DLGC_WANTARROWS | DLGC_WANTCHARS;
532 return IPADDRESS_KillFocus (hwnd, wParam, lParam);
535 return IPADDRESS_LButtonDown (hwnd, wParam, lParam);
538 return IPADDRESS_Paint (hwnd, wParam);
541 return IPADDRESS_SetFocus (hwnd, wParam, lParam);
544 return IPADDRESS_Size (hwnd, wParam, lParam);
548 ERR (ipaddress, "unknown msg %04x wp=%08x lp=%08lx\n",
549 uMsg, wParam, lParam);
550 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
557 IPADDRESS_Register (void)
561 if (GlobalFindAtomA (WC_IPADDRESSA)) return;
563 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
564 wndClass.style = CS_GLOBALCLASS;
565 wndClass.lpfnWndProc = (WNDPROC)IPADDRESS_WindowProc;
566 wndClass.cbClsExtra = 0;
567 wndClass.cbWndExtra = sizeof(IPADDRESS_INFO *);
568 wndClass.hCursor = LoadCursorA (0, IDC_ARROWA);
569 wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
570 wndClass.lpszClassName = WC_IPADDRESSA;
572 RegisterClassA (&wndClass);
576 IPADDRESS_Unregister (VOID)
578 if (GlobalFindAtomA (WC_IPADDRESSA))
579 UnregisterClassA (WC_IPADDRESSA, (HINSTANCE)NULL);