3 * Copyright 1998 Eric Kohl
4 * Copyright 1998 Alex Priem <alexp@sci.kun.nl>
10 * -Check ranges when changing field-focus.
11 * -Check all notifications/behavior.
12 * -Optimization: include lpipsi in IPADDRESS_INFO.
13 * -CurrentFocus: field that has focus at moment of processing.
14 * -connect Rect32 rcClient.
15 * -handle right and left arrows correctly. Boring.
16 * -split GotoNextField in CheckField and GotoNextField.
17 * -check ipaddress.cpp for missing features.
18 * -refresh: draw '.' instead of setpixel.
28 #include "ipaddress.h"
33 #define IPADDRESS_GetInfoPtr(wndPtr) ((IPADDRESS_INFO *)wndPtr->wExtra[0])
37 IPADDRESS_SendNotify (WND *wndPtr, UINT32 command);
39 IPADDRESS_SendIPAddressNotify (WND *wndPtr, UINT32 field, BYTE newValue);
42 /* property name of tooltip window handle */
43 #define IP_SUBCLASS_PROP "CCIP32SubclassInfo"
46 static LRESULT CALLBACK
47 IPADDRESS_SubclassProc (HWND32 hwnd, UINT32 uMsg, WPARAM32 wParam, LPARAM lParam);
53 IPADDRESS_Refresh (WND *wndPtr, HDC32 hdc)
57 COLORREF clr=GetSysColor32 (COLOR_3DDKSHADOW);
60 GetClientRect32 (wndPtr->hwndSelf, &rcClient);
61 hbr = CreateSolidBrush32 (RGB(255,255,255));
62 DrawEdge32 (hdc, &rcClient, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
63 FillRect32 (hdc, &rcClient, hbr);
67 fieldsize=(rcClient.right-rcClient.left) /4;
69 for (i=0; i<3; i++) { /* Should draw text "." here */
71 SetPixel32 (hdc, x, 13, clr);
72 SetPixel32 (hdc, x, 14, clr);
73 SetPixel32 (hdc, x+1, 13, clr);
74 SetPixel32 (hdc, x+1, 14, clr);
85 IPADDRESS_Create (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
87 IPADDRESS_INFO *infoPtr;
88 RECT32 rcClient, edit;
90 LPIP_SUBCLASS_INFO lpipsi;
93 infoPtr = (IPADDRESS_INFO *)COMCTL32_Alloc (sizeof(IPADDRESS_INFO));
94 wndPtr->wExtra[0] = (DWORD)infoPtr;
96 if (infoPtr == NULL) {
97 ERR (ipaddress, "could not allocate info memory!\n");
101 GetClientRect32 (wndPtr->hwndSelf, &rcClient);
103 fieldsize=(rcClient.right-rcClient.left) /4;
105 edit.top =rcClient.top+2;
106 edit.bottom=rcClient.bottom-2;
108 lpipsi=(LPIP_SUBCLASS_INFO)
109 GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
110 if (lpipsi == NULL) {
111 lpipsi= (LPIP_SUBCLASS_INFO) COMCTL32_Alloc (sizeof(IP_SUBCLASS_INFO));
112 lpipsi->wndPtr=wndPtr;
114 SetProp32A ((HWND32)wndPtr->hwndSelf, IP_SUBCLASS_PROP,
116 /* infoPtr->lpipsi= lpipsi; */
118 WARN (ipaddress,"IP-create called twice\n");
120 for (i=0; i<=3; i++) {
121 infoPtr->LowerLimit[i]=0;
122 infoPtr->UpperLimit[i]=255;
123 edit.left=rcClient.left+i*fieldsize+3;
124 edit.right=rcClient.left+(i+1)*fieldsize-2;
125 lpipsi->hwndIP[i]= CreateWindow32A ("edit", NULL,
126 WS_CHILD | WS_VISIBLE | ES_LEFT,
127 edit.left, edit.top, edit.right-edit.left, edit.bottom-edit.top,
128 wndPtr->hwndSelf, (HMENU32) 1, wndPtr->hInstance, NULL);
129 lpipsi->wpOrigProc[i]= (WNDPROC32)
130 SetWindowLong32A (lpipsi->hwndIP[i],GWL_WNDPROC, (LONG)
131 IPADDRESS_SubclassProc);
132 SetProp32A ((HWND32)lpipsi->hwndIP[i], IP_SUBCLASS_PROP,
136 lpipsi->infoPtr= infoPtr;
143 IPADDRESS_Destroy (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
146 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr(wndPtr);
147 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
148 GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
150 for (i=0; i<=3; i++) {
151 SetWindowLong32A ((HWND32)lpipsi->hwndIP[i], GWL_WNDPROC,
152 (LONG)lpipsi->wpOrigProc[i]);
155 COMCTL32_Free (infoPtr);
161 IPADDRESS_KillFocus (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
165 TRACE (ipaddress,"\n");
166 hdc = GetDC32 (wndPtr->hwndSelf);
167 IPADDRESS_Refresh (wndPtr, hdc);
168 ReleaseDC32 (wndPtr->hwndSelf, hdc);
170 IPADDRESS_SendIPAddressNotify (wndPtr, 0, 0); /* FIXME: should use -1 */
171 IPADDRESS_SendNotify (wndPtr, EN_KILLFOCUS);
172 InvalidateRect32 (wndPtr->hwndSelf, NULL, TRUE);
179 IPADDRESS_Paint (WND *wndPtr, WPARAM32 wParam)
184 hdc = wParam==0 ? BeginPaint32 (wndPtr->hwndSelf, &ps) : (HDC32)wParam;
185 IPADDRESS_Refresh (wndPtr, hdc);
187 EndPaint32 (wndPtr->hwndSelf, &ps);
193 IPADDRESS_SetFocus (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
197 TRACE (ipaddress,"\n");
199 hdc = GetDC32 (wndPtr->hwndSelf);
200 IPADDRESS_Refresh (wndPtr, hdc);
201 ReleaseDC32 (wndPtr->hwndSelf, hdc);
208 IPADDRESS_Size (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
210 /* IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr(wndPtr); */
211 TRACE (ipaddress,"\n");
217 IPADDRESS_SendNotify (WND *wndPtr, UINT32 command)
220 TRACE (ipaddress, "%x\n",command);
221 return (BOOL32)SendMessage32A (GetParent32 (wndPtr->hwndSelf), WM_COMMAND,
222 MAKEWPARAM (wndPtr->wIDmenu,command), (LPARAM) wndPtr->hwndSelf);
227 IPADDRESS_SendIPAddressNotify (WND *wndPtr, UINT32 field, BYTE newValue)
232 TRACE (ipaddress, "%x %x\n",field,newValue);
233 nmip.hdr.hwndFrom = wndPtr->hwndSelf;
234 nmip.hdr.idFrom = wndPtr->wIDmenu;
235 nmip.hdr.code = IPN_FIELDCHANGED;
238 nmip.iValue=newValue;
240 return (BOOL32)SendMessage32A (GetParent32 (wndPtr->hwndSelf), WM_NOTIFY,
241 (WPARAM32)wndPtr->wIDmenu, (LPARAM)&nmip);
248 IPADDRESS_ClearAddress (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
253 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
254 GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
256 TRACE (ipaddress,"\n");
260 SetWindowText32A (lpipsi->hwndIP[i],buf);
262 hdc = GetDC32 (wndPtr->hwndSelf);
263 IPADDRESS_Refresh (wndPtr, hdc);
264 ReleaseDC32 (wndPtr->hwndSelf, hdc);
269 IPADDRESS_IsBlank (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
273 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
274 GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
276 TRACE (ipaddress,"\n");
278 for (i=0; i<=3; i++) {
279 GetWindowText32A (lpipsi->hwndIP[i],buf,5);
280 if (buf[0]) return 0;
287 IPADDRESS_GetAddress (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
290 int i,valid,fieldvalue;
292 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr(wndPtr);
293 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
294 GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
296 TRACE (ipaddress,"\n");
300 for (i=0; i<=3; i++) {
301 GetWindowText32A (lpipsi->hwndIP[i],field,4);
305 fieldvalue=atoi(field);
306 if (fieldvalue<infoPtr->LowerLimit[i])
307 fieldvalue=infoPtr->LowerLimit[i];
308 if (fieldvalue>infoPtr->UpperLimit[i])
309 fieldvalue=infoPtr->UpperLimit[i];
315 *(LPDWORD) lParam=ip_addr;
321 IPADDRESS_SetRange (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
324 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr(wndPtr);
327 TRACE (ipaddress,"\n");
329 index=(INT32) wParam;
330 if ((index<0) || (index>3)) return 0;
332 infoPtr->LowerLimit[index]=lParam & 0xff;
333 infoPtr->UpperLimit[index]=(lParam >>8) & 0xff;
338 IPADDRESS_SetAddress (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
341 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr(wndPtr);
342 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
343 GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
344 int i,ip_address,value;
347 TRACE (ipaddress,"\n");
348 ip_address=(DWORD) lParam;
350 for (i=3; i>=0; i--) {
351 value=ip_address & 0xff;
352 if ((value>=infoPtr->LowerLimit[i]) && (value<=infoPtr->UpperLimit[i]))
354 sprintf (buf,"%d",value);
355 SetWindowText32A (lpipsi->hwndIP[i],buf);
356 IPADDRESS_SendNotify (wndPtr, EN_CHANGE);
361 hdc = GetDC32 (wndPtr->hwndSelf); /* & send notifications */
362 IPADDRESS_Refresh (wndPtr, hdc);
363 ReleaseDC32 (wndPtr->hwndSelf, hdc);
372 IPADDRESS_SetFocusToField (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
374 /* IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr(wndPtr); */
375 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
376 GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
379 index=(INT32) wParam;
380 TRACE (ipaddress," %d\n", index);
381 if ((index<0) || (index>3)) return 0;
383 SetFocus32 (lpipsi->hwndIP[index]);
390 IPADDRESS_LButtonDown (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
392 TRACE (ipaddress, "\n");
394 SetFocus32 (wndPtr->hwndSelf);
395 IPADDRESS_SendNotify (wndPtr, EN_SETFOCUS);
396 IPADDRESS_SetFocusToField (wndPtr, 0, 0);
403 /* tab/shift-tab: IPN_FIELDCHANGED, lose focus.
404 dot, space,right arrow: set focus to next child edit.
405 numerics (0..9), control characters: forward to default edit control
406 other characters: dropped
413 IPADDRESS_GotoNextField (LPIP_SUBCLASS_INFO lpipsi, int currentfield)
415 int newField,fieldvalue;
417 IPADDRESS_INFO *infoPtr=lpipsi->infoPtr;
419 TRACE (ipaddress,"\n");
420 GetWindowText32A (lpipsi->hwndIP[currentfield],field,4);
424 fieldvalue=atoi(field);
425 if (fieldvalue<infoPtr->LowerLimit[currentfield])
426 newField=infoPtr->LowerLimit[currentfield];
427 if (fieldvalue>infoPtr->UpperLimit[currentfield])
428 newField=infoPtr->UpperLimit[currentfield];
430 sprintf (field,"%d",newField);
431 SetWindowText32A (lpipsi->hwndIP[currentfield], field);
437 SetFocus32 (lpipsi->hwndIP[currentfield+1]);
443 IPADDRESS_SubclassProc (HWND32 hwnd, UINT32 uMsg, WPARAM32 wParam, LPARAM lParam)
446 IPADDRESS_INFO *infoPtr;
447 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
448 GetProp32A ((HWND32)hwnd,IP_SUBCLASS_PROP);
450 infoPtr = lpipsi->infoPtr;
453 if (lpipsi->hwndIP[i]==hwnd) index=i;
458 char c=(char) wParam;
462 shift = GetKeyState32(VK_SHIFT) & 0x8000;
464 pwnd=GetNextDlgTabItem32 (GetParent32 (hwnd), 0, TRUE);
466 pwnd=GetNextDlgTabItem32 (GetParent32 (hwnd), 0, FALSE);
467 if (pwnd) SetFocus32 (pwnd);
471 if ((c==' ') || (c=='.') || (c==VK_RIGHT)) {
472 IPADDRESS_GotoNextField (lpipsi,index);
482 if (((c>='0') && (c<='9')) || (iscntrl(c))) {
483 l=GetWindowTextLength32A (lpipsi->hwndIP[index]);
485 if (IPADDRESS_GotoNextField (lpipsi,index)) {
498 return CallWindowProc32A (lpipsi->wpOrigProc[index], hwnd, uMsg, wParam, lParam);
502 IPADDRESS_WindowProc (HWND32 hwnd, UINT32 uMsg, WPARAM32 wParam, LPARAM lParam)
504 WND *wndPtr = WIN_FindWndPtr(hwnd);
508 case IPM_CLEARADDRESS:
509 return IPADDRESS_ClearAddress (wndPtr, wParam, lParam);
512 return IPADDRESS_SetAddress (wndPtr, wParam, lParam);
515 return IPADDRESS_GetAddress (wndPtr, wParam, lParam);
518 return IPADDRESS_SetRange (wndPtr, wParam, lParam);
521 return IPADDRESS_SetFocusToField (wndPtr, wParam, lParam);
524 return IPADDRESS_IsBlank (wndPtr, wParam, lParam);
527 return IPADDRESS_Create (wndPtr, wParam, lParam);
530 return IPADDRESS_Destroy (wndPtr, wParam, lParam);
533 return DLGC_WANTARROWS | DLGC_WANTCHARS;
536 return IPADDRESS_KillFocus (wndPtr, wParam, lParam);
539 return IPADDRESS_LButtonDown (wndPtr, wParam, lParam);
542 return IPADDRESS_Paint (wndPtr, wParam);
545 return IPADDRESS_SetFocus (wndPtr, wParam, lParam);
548 return IPADDRESS_Size (wndPtr, wParam, lParam);
552 ERR (ipaddress, "unknown msg %04x wp=%08x lp=%08lx\n",
553 uMsg, wParam, lParam);
554 return DefWindowProc32A (hwnd, uMsg, wParam, lParam);
561 IPADDRESS_Register (void)
563 WNDCLASS32A wndClass;
565 if (GlobalFindAtom32A (WC_IPADDRESS32A)) return;
567 ZeroMemory (&wndClass, sizeof(WNDCLASS32A));
568 wndClass.style = CS_GLOBALCLASS;
569 wndClass.lpfnWndProc = (WNDPROC32)IPADDRESS_WindowProc;
570 wndClass.cbClsExtra = 0;
571 wndClass.cbWndExtra = sizeof(IPADDRESS_INFO *);
572 wndClass.hCursor = LoadCursor32A (0, IDC_ARROW32A);
573 wndClass.hbrBackground = (HBRUSH32)(COLOR_3DFACE + 1);
574 wndClass.lpszClassName = WC_IPADDRESS32A;
576 RegisterClass32A (&wndClass);
580 IPADDRESS_Unregister (VOID)
582 if (GlobalFindAtom32A (WC_IPADDRESS32A))
583 UnregisterClass32A (WC_IPADDRESS32A, (HINSTANCE32)NULL);