4 * Copyright 1999 Chris Morgan<cmorgan@wpi.edu> and
5 * James Abbatiello<abbeyj@wpi.edu>
6 * Copyright 1998, 1999 Eric Kohl
7 * Copyright 1998 Alex Priem <alexp@sci.kun.nl>
12 * -Edit control doesn't support the ES_CENTER style which prevents
13 * this ipaddress control from having centered text look like the
14 * windows ipaddress control
15 * -Check all notifications/behavior.
17 * -include lpipsi in IPADDRESS_INFO.
18 * -CurrentFocus: field that has focus at moment of processing.
19 * -connect Rect32 rcClient.
20 * -check ipaddress.cpp for missing features.
21 * -refresh: draw '.' instead of setpixel.
32 #include "debugtools.h"
34 DEFAULT_DEBUG_CHANNEL(ipaddress);
47 WNDPROC wpOrigProc[4];
49 IPADDRESS_INFO *infoPtr;
52 } IP_SUBCLASS_INFO, *LPIP_SUBCLASS_INFO;
54 #define IPADDRESS_GetInfoPtr(hwnd) ((IPADDRESS_INFO *)GetWindowLongA (hwnd, 0))
57 IPADDRESS_SendNotify (HWND hwnd, UINT command);
59 IPADDRESS_SendIPAddressNotify (HWND hwnd, UINT field, BYTE newValue);
61 /* property name of tooltip window handle */
62 #define IP_SUBCLASS_PROP "CCIP32SubclassInfo"
65 static LRESULT CALLBACK
66 IPADDRESS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
70 IPADDRESS_Refresh (HWND hwnd, HDC hdc)
74 COLORREF clr=GetSysColor (COLOR_3DDKSHADOW);
77 GetClientRect (hwnd, &rcClient);
78 hbr = CreateSolidBrush (RGB(255,255,255));
79 DrawEdge (hdc, &rcClient, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
80 FillRect (hdc, &rcClient, hbr);
84 fieldsize=(rcClient.right-rcClient.left) / 4;
86 for (i=0; i<3; i++) { /* Should draw text "." here */
88 SetPixel (hdc, x, 13, clr);
89 SetPixel (hdc, x, 14, clr);
90 SetPixel (hdc, x+1, 13, clr);
91 SetPixel (hdc, x+1, 14, clr);
97 IPADDRESS_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
99 IPADDRESS_INFO *infoPtr;
102 LPIP_SUBCLASS_INFO lpipsi;
105 infoPtr = (IPADDRESS_INFO *)COMCTL32_Alloc (sizeof(IPADDRESS_INFO));
106 SetWindowLongA (hwnd, 0, (DWORD)infoPtr);
108 GetClientRect (hwnd, &rcClient);
110 fieldsize=(rcClient.right-rcClient.left) /4;
112 edit.top =rcClient.top+2;
113 edit.bottom=rcClient.bottom-2;
115 lpipsi=(LPIP_SUBCLASS_INFO) GetPropA ((HWND)hwnd, IP_SUBCLASS_PROP);
116 if (lpipsi == NULL) {
117 lpipsi = (LPIP_SUBCLASS_INFO) COMCTL32_Alloc (sizeof(IP_SUBCLASS_INFO));
120 SetPropA ((HWND)hwnd, IP_SUBCLASS_PROP, (HANDLE)lpipsi);
121 /* infoPtr->lpipsi= lpipsi; */
123 WARN("IP-create called twice\n");
127 infoPtr->LowerLimit[i]=0;
128 infoPtr->UpperLimit[i]=255;
129 edit.left=rcClient.left+i*fieldsize+6;
130 edit.right=rcClient.left+(i+1)*fieldsize-2;
131 lpipsi->hwndIP[i]= CreateWindowA ("edit", NULL,
132 WS_CHILD | WS_VISIBLE | ES_CENTER,
133 edit.left, edit.top, edit.right-edit.left, edit.bottom-edit.top,
134 hwnd, (HMENU) 1, GetWindowLongA (hwnd, GWL_HINSTANCE), NULL);
135 lpipsi->wpOrigProc[i]= (WNDPROC)
136 SetWindowLongA (lpipsi->hwndIP[i],GWL_WNDPROC, (LONG)
137 IPADDRESS_SubclassProc);
138 SetPropA ((HWND)lpipsi->hwndIP[i], IP_SUBCLASS_PROP, (HANDLE)lpipsi);
141 lpipsi->infoPtr= infoPtr;
148 IPADDRESS_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
151 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd);
152 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
153 GetPropA ((HWND)hwnd, IP_SUBCLASS_PROP);
155 for (i=0; i<=3; i++) {
156 SetWindowLongA ((HWND)lpipsi->hwndIP[i], GWL_WNDPROC,
157 (LONG)lpipsi->wpOrigProc[i]);
160 COMCTL32_Free (infoPtr);
161 SetWindowLongA (hwnd, 0, 0);
167 IPADDRESS_KillFocus (HWND hwnd, WPARAM wParam, LPARAM lParam)
173 IPADDRESS_Refresh (hwnd, hdc);
174 ReleaseDC (hwnd, hdc);
176 IPADDRESS_SendIPAddressNotify (hwnd, 0, 0); /* FIXME: should use -1 */
177 IPADDRESS_SendNotify (hwnd, EN_KILLFOCUS);
178 InvalidateRect (hwnd, NULL, TRUE);
185 IPADDRESS_Paint (HWND hwnd, WPARAM wParam)
190 hdc = wParam==0 ? BeginPaint (hwnd, &ps) : (HDC)wParam;
191 IPADDRESS_Refresh (hwnd, hdc);
193 EndPaint (hwnd, &ps);
199 IPADDRESS_SetFocus (HWND hwnd, WPARAM wParam, LPARAM lParam)
206 IPADDRESS_Refresh (hwnd, hdc);
207 ReleaseDC (hwnd, hdc);
214 IPADDRESS_Size (HWND hwnd, WPARAM wParam, LPARAM lParam)
216 /* IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd); */
223 IPADDRESS_SendNotify (HWND hwnd, UINT command)
225 TRACE("%x\n",command);
226 return (BOOL)SendMessageA (GetParent (hwnd), WM_COMMAND,
227 MAKEWPARAM (GetWindowLongA (hwnd, GWL_ID),command), (LPARAM)hwnd);
232 IPADDRESS_SendIPAddressNotify (HWND hwnd, UINT field, BYTE newValue)
236 TRACE("%x %x\n",field,newValue);
237 nmip.hdr.hwndFrom = hwnd;
238 nmip.hdr.idFrom = GetWindowLongA (hwnd, GWL_ID);
239 nmip.hdr.code = IPN_FIELDCHANGED;
242 nmip.iValue=newValue;
244 return (BOOL)SendMessageA (GetParent (hwnd), WM_NOTIFY,
245 (WPARAM)nmip.hdr.idFrom, (LPARAM)&nmip);
250 IPADDRESS_ClearAddress (HWND hwnd, WPARAM wParam, LPARAM lParam)
255 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
256 GetPropA ((HWND)hwnd,IP_SUBCLASS_PROP);
262 SetWindowTextA (lpipsi->hwndIP[i],buf);
265 IPADDRESS_Refresh (hwnd, hdc);
266 ReleaseDC (hwnd, hdc);
273 IPADDRESS_IsBlank (HWND hwnd, WPARAM wParam, LPARAM lParam)
277 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
278 GetPropA ((HWND)hwnd, IP_SUBCLASS_PROP);
282 for (i=0; i<=3; i++) {
283 GetWindowTextA (lpipsi->hwndIP[i],buf,5);
293 IPADDRESS_GetAddress (HWND hwnd, WPARAM wParam, LPARAM lParam)
296 int i,valid,fieldvalue;
298 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd);
299 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
300 GetPropA ((HWND)hwnd, IP_SUBCLASS_PROP);
306 for (i=0; i<=3; i++) {
307 GetWindowTextA (lpipsi->hwndIP[i],field,4);
311 fieldvalue=atoi(field);
312 if (fieldvalue<infoPtr->LowerLimit[i])
313 fieldvalue=infoPtr->LowerLimit[i];
314 if (fieldvalue>infoPtr->UpperLimit[i])
315 fieldvalue=infoPtr->UpperLimit[i];
321 *(LPDWORD) lParam=ip_addr;
328 IPADDRESS_SetRange (HWND hwnd, WPARAM wParam, LPARAM lParam)
330 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd);
336 if ((index<0) || (index>3)) return 0;
338 infoPtr->LowerLimit[index]=lParam & 0xff;
339 infoPtr->UpperLimit[index]=(lParam >>8) & 0xff;
345 IPADDRESS_SetAddress (HWND hwnd, WPARAM wParam, LPARAM lParam)
347 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd);
349 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
350 GetPropA ((HWND)hwnd, IP_SUBCLASS_PROP);
351 int i,ip_address,value;
355 ip_address=(DWORD) lParam;
357 for (i=3; i>=0; i--) {
358 value=ip_address & 0xff;
359 if ((value>=infoPtr->LowerLimit[i]) && (value<=infoPtr->UpperLimit[i]))
361 sprintf (buf,"%d",value);
362 SetWindowTextA (lpipsi->hwndIP[i],buf);
363 IPADDRESS_SendNotify (hwnd, EN_CHANGE);
368 hdc = GetDC (hwnd); /* & send notifications */
369 IPADDRESS_Refresh (hwnd, hdc);
370 ReleaseDC (hwnd, hdc);
377 IPADDRESS_SetFocusToField (HWND hwnd, WPARAM wParam, LPARAM lParam)
379 /* IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd); */
380 LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO) GetPropA ((HWND)hwnd,
385 TRACE(" %d\n", index);
386 if ((index<0) || (index>3)) return 0;
388 SetFocus (lpipsi->hwndIP[index]);
395 IPADDRESS_LButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
400 IPADDRESS_SendNotify (hwnd, EN_SETFOCUS);
401 IPADDRESS_SetFocusToField (hwnd, 0, 0);
408 IPADDRESS_CheckField (LPIP_SUBCLASS_INFO lpipsi, int currentfield)
410 int newField,fieldvalue;
412 IPADDRESS_INFO *infoPtr=lpipsi->infoPtr;
414 if(currentfield >= 0 && currentfield < 4)
417 GetWindowTextA (lpipsi->hwndIP[currentfield],field,4);
422 fieldvalue=atoi(field);
424 if (fieldvalue < infoPtr->LowerLimit[currentfield])
425 newField=infoPtr->LowerLimit[currentfield];
427 if (fieldvalue > infoPtr->UpperLimit[currentfield])
428 newField=infoPtr->UpperLimit[currentfield];
432 sprintf (field,"%d",newField);
433 SetWindowTextA (lpipsi->hwndIP[currentfield], field);
443 IPADDRESS_GotoNextField (LPIP_SUBCLASS_INFO lpipsi, int currentfield)
445 if(currentfield >= -1 && currentfield < 4)
447 IPADDRESS_CheckField(lpipsi, currentfield); /* check the fields value */
451 SetFocus (lpipsi->hwndIP[currentfield+1]);
459 /* period: move and select the text in the next field to the right if */
460 /* the current field is not empty(l!=0), we are not in the */
461 /* left most position, and nothing is selected(startsel==endsel) */
463 /* spacebar: same behavior as period */
465 /* alpha characters: completely ignored */
467 /* digits: accepted when field text length < 2 ignored otherwise. */
468 /* when 3 numbers have been entered into the field the value */
469 /* of the field is checked, if the field value exceeds the */
470 /* maximum value and is changed the field remains the current */
471 /* field, otherwise focus moves to the field to the right */
473 /* tab: change focus from the current ipaddress control to the next */
474 /* control in the tab order */
476 /* right arrow: move to the field on the right to the left most */
477 /* position in that field if no text is selected, */
478 /* we are in the right most position in the field, */
479 /* we are not in the right most field */
481 /* left arrow: move to the field on the left to the right most */
482 /* position in that field if no text is selected, */
483 /* we are in the left most position in the current field */
484 /* and we are not in the left most field */
486 /* backspace: delete the character to the left of the cursor position, */
487 /* if none are present move to the field on the left if */
488 /* we are not in the left most field and delete the right */
489 /* most digit in that field while keeping the cursor */
490 /* on the right side of the field */
494 IPADDRESS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
496 IPADDRESS_INFO *infoPtr;
497 LPIP_SUBCLASS_INFO lpipsi = (LPIP_SUBCLASS_INFO) GetPropA
498 ((HWND)hwnd,IP_SUBCLASS_PROP);
499 CHAR c = (CHAR)wParam;
500 INT i, l, index, startsel, endsel;
502 infoPtr = lpipsi->infoPtr;
505 if (lpipsi->hwndIP[i]==hwnd) index=i;
509 if(isdigit(c)) /* process all digits */
512 SendMessageA(lpipsi->hwndIP[index], EM_GETSEL, (WPARAM)&startsel, (LPARAM)&endsel);
513 l = GetWindowTextLengthA (lpipsi->hwndIP[index]);
514 if(l==2 && startsel==endsel) /* field is full after this key is processed */
516 /* process the digit press before we check the field */
517 return_val = CallWindowProcA (lpipsi->wpOrigProc[index], hwnd, uMsg, wParam, lParam);
519 /* if the field value was changed stay at the current field */
520 if(!IPADDRESS_CheckField(lpipsi, index))
521 IPADDRESS_GotoNextField (lpipsi,index);
526 if(l > 2) /* field is full, stop key messages */
534 if(c == '.') /* VK_PERIOD */
536 l = GetWindowTextLengthA(lpipsi->hwndIP[index]);
537 SendMessageA(lpipsi->hwndIP[index], EM_GETSEL, (WPARAM)&startsel, (LPARAM)&endsel);
538 if(l != 0 && startsel==endsel && startsel != 0)
540 IPADDRESS_GotoNextField(lpipsi, index);
541 SendMessageA(lpipsi->hwndIP[index+1], EM_SETSEL, (WPARAM)0, (LPARAM)3);
545 /* stop all other characters */
553 l = GetWindowTextLengthA(lpipsi->hwndIP[index]);
554 SendMessageA(lpipsi->hwndIP[index], EM_GETSEL, (WPARAM)&startsel, (LPARAM)&endsel);
555 if(l != 0 && startsel==endsel && startsel != 0)
557 IPADDRESS_GotoNextField(lpipsi, index);
558 SendMessageA(lpipsi->hwndIP[index+1], EM_SETSEL, (WPARAM)0, (LPARAM)3);
564 SendMessageA(lpipsi->hwndIP[index], EM_GETSEL, (WPARAM)&startsel, (LPARAM)&endsel);
565 l = GetWindowTextLengthA (lpipsi->hwndIP[index]);
567 if(startsel==endsel && startsel==l)
569 IPADDRESS_GotoNextField(lpipsi, index);
570 SendMessageA(lpipsi->hwndIP[index+1], EM_SETSEL, (WPARAM)0,(LPARAM)0);
576 SendMessageA(lpipsi->hwndIP[index], EM_GETSEL, (WPARAM)&startsel, (LPARAM)&endsel);
577 if(startsel==0 && startsel==endsel && index > 0)
579 IPADDRESS_GotoNextField(lpipsi, index - 2);
580 l = GetWindowTextLengthA(lpipsi->hwndIP[index-1]);
581 SendMessageA(lpipsi->hwndIP[index-1], EM_SETSEL, (WPARAM)l,(LPARAM)l);
585 if(c == VK_BACK) /* VK_BACKSPACE */
589 SendMessageA(lpipsi->hwndIP[index], EM_GETSEL, (WPARAM)&startsel, (LPARAM)&endsel);
590 l = GetWindowTextLengthA (lpipsi->hwndIP[index]);
591 if(startsel==endsel && startsel==0 && index > 0)
593 l = GetWindowTextLengthA(lpipsi->hwndIP[index-1]);
596 GetWindowTextA (lpipsi->hwndIP[index-1],buf,4);
598 SetWindowTextA(lpipsi->hwndIP[index-1], buf);
599 SendMessageA(lpipsi->hwndIP[index-1], EM_SETSEL, (WPARAM)l,(LPARAM)l);
601 IPADDRESS_GotoNextField(lpipsi, index - 2);
609 return CallWindowProcA (lpipsi->wpOrigProc[index], hwnd, uMsg, wParam, lParam);
613 static LRESULT WINAPI
614 IPADDRESS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
616 TRACE("hwnd=%x msg=%x wparam=%x lparam=%lx\n", hwnd, uMsg, wParam, lParam);
617 if (!IPADDRESS_GetInfoPtr (hwnd) && (uMsg != WM_CREATE))
618 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
621 case IPM_CLEARADDRESS:
622 return IPADDRESS_ClearAddress (hwnd, wParam, lParam);
625 return IPADDRESS_SetAddress (hwnd, wParam, lParam);
628 return IPADDRESS_GetAddress (hwnd, wParam, lParam);
631 return IPADDRESS_SetRange (hwnd, wParam, lParam);
634 return IPADDRESS_SetFocusToField (hwnd, wParam, lParam);
637 return IPADDRESS_IsBlank (hwnd, wParam, lParam);
640 return IPADDRESS_Create (hwnd, wParam, lParam);
643 return IPADDRESS_Destroy (hwnd, wParam, lParam);
646 return DLGC_WANTARROWS | DLGC_WANTCHARS;
649 return IPADDRESS_KillFocus (hwnd, wParam, lParam);
652 return IPADDRESS_LButtonDown (hwnd, wParam, lParam);
655 return IPADDRESS_Paint (hwnd, wParam);
658 return IPADDRESS_SetFocus (hwnd, wParam, lParam);
661 return IPADDRESS_Size (hwnd, wParam, lParam);
665 ERR("unknown msg %04x wp=%08x lp=%08lx\n",
666 uMsg, wParam, lParam);
667 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
673 void IPADDRESS_Register (void)
677 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
678 wndClass.style = CS_GLOBALCLASS;
679 wndClass.lpfnWndProc = (WNDPROC)IPADDRESS_WindowProc;
680 wndClass.cbClsExtra = 0;
681 wndClass.cbWndExtra = sizeof(IPADDRESS_INFO *);
682 wndClass.hCursor = LoadCursorA (0, IDC_ARROWA);
683 wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
684 wndClass.lpszClassName = WC_IPADDRESSA;
686 RegisterClassA (&wndClass);
690 VOID IPADDRESS_Unregister (void)
692 UnregisterClassA (WC_IPADDRESSA, (HINSTANCE)NULL);