4 * Copyright 2002 Dimitrie O. Paun
5 * Copyright 1999 Chris Morgan<cmorgan@wpi.edu>
6 * Copyright 1999 James Abbatiello<abbeyj@wpi.edu>
7 * Copyright 1998, 1999 Eric Kohl
8 * Copyright 1998 Alex Priem <alexp@sci.kun.nl>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * This code was audited for completeness against the documented features
27 * of Comctl32.dll version 6.0 on Sep. 9, 2002, by Dimitrie O. Paun.
29 * Unless otherwise noted, we belive this code to be complete, as per
30 * the specification mentioned above.
31 * If you discover missing features, or bugs, please note them below.
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(ipaddress);
72 #define IP_SUBCLASS_PROP "CCIP32SubclassInfo"
73 #define IPADDRESS_GetInfoPtr(hwnd) ((IPADDRESS_INFO *)GetWindowLongW (hwnd, 0))
76 static LRESULT CALLBACK
77 IPADDRESS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
79 static LRESULT IPADDRESS_Notify (IPADDRESS_INFO *infoPtr, UINT command)
81 HWND hwnd = infoPtr->Self;
83 TRACE("(command=%x)\n", command);
85 return SendMessageW (GetParent (hwnd), WM_COMMAND,
86 MAKEWPARAM (GetWindowLongW (hwnd, GWL_ID), command), (LPARAM)hwnd);
89 static INT IPADDRESS_IPNotify (IPADDRESS_INFO *infoPtr, INT field, INT value)
93 TRACE("(field=%x, value=%d)\n", field, value);
95 nmip.hdr.hwndFrom = infoPtr->Self;
96 nmip.hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
97 nmip.hdr.code = IPN_FIELDCHANGED;
102 SendMessageW (GetParent (infoPtr->Self), WM_NOTIFY,
103 (WPARAM)nmip.hdr.idFrom, (LPARAM)&nmip);
105 TRACE("<-- %d\n", nmip.iValue);
111 static int IPADDRESS_GetPartIndex(IPADDRESS_INFO *infoPtr, HWND hwnd)
115 TRACE("(hwnd=%p)\n", hwnd);
117 for (i = 0; i < 4; i++)
118 if (infoPtr->Part[i].EditHwnd == hwnd) return i;
120 ERR("We subclassed the wrong window! (hwnd=%p)\n", hwnd);
125 static LRESULT IPADDRESS_Draw (IPADDRESS_INFO *infoPtr, HDC hdc)
133 GetClientRect (infoPtr->Self, &rect);
134 DrawEdge (hdc, &rect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
136 for (i = 0; i < 3; i++) {
137 GetWindowRect (infoPtr->Part[i].EditHwnd, &rcPart);
139 ScreenToClient(infoPtr->Self, &pt);
141 GetWindowRect (infoPtr->Part[i+1].EditHwnd, &rcPart);
143 ScreenToClient(infoPtr->Self, &pt);
145 DrawTextA(hdc, ".", 1, &rect, DT_SINGLELINE | DT_CENTER | DT_BOTTOM);
152 static LRESULT IPADDRESS_Create (HWND hwnd)
154 IPADDRESS_INFO *infoPtr;
157 WCHAR EDIT[] = { 'E', 'd', 'i', 't', 0 };
161 SetWindowLongW (hwnd, GWL_STYLE,
162 GetWindowLongW(hwnd, GWL_STYLE) & ~WS_BORDER);
164 infoPtr = (IPADDRESS_INFO *)Alloc (sizeof(IPADDRESS_INFO));
165 if (!infoPtr) return -1;
166 SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
168 GetClientRect (hwnd, &rcClient);
170 fieldsize = (rcClient.right - rcClient.left) / 4;
172 edit.top = rcClient.top + 2;
173 edit.bottom = rcClient.bottom - 2;
175 infoPtr->Self = hwnd;
177 for (i = 0; i < 4; i++) {
178 IPPART_INFO* part = &infoPtr->Part[i];
180 part->LowerLimit = 0;
181 part->UpperLimit = 255;
182 edit.left = rcClient.left + i*fieldsize + 6;
183 edit.right = rcClient.left + (i+1)*fieldsize - 2;
185 CreateWindowW (EDIT, NULL, WS_CHILD | WS_VISIBLE | ES_CENTER,
186 edit.left, edit.top, edit.right - edit.left,
187 edit.bottom - edit.top, hwnd, (HMENU) 1,
188 (HINSTANCE)GetWindowLongW(hwnd, GWL_HINSTANCE), NULL);
189 SetPropA(part->EditHwnd, IP_SUBCLASS_PROP, hwnd);
190 part->OrigProc = (WNDPROC)
191 SetWindowLongW (part->EditHwnd, GWL_WNDPROC,
192 (LONG)IPADDRESS_SubclassProc);
199 static LRESULT IPADDRESS_Destroy (IPADDRESS_INFO *infoPtr)
205 for (i = 0; i < 4; i++) {
206 IPPART_INFO* part = &infoPtr->Part[i];
207 SetWindowLongW (part->EditHwnd, GWL_WNDPROC, (LONG)part->OrigProc);
210 SetWindowLongW (infoPtr->Self, 0, 0);
216 static LRESULT IPADDRESS_Paint (IPADDRESS_INFO *infoPtr, HDC hdc)
222 if (hdc) return IPADDRESS_Draw (infoPtr, hdc);
224 hdc = BeginPaint (infoPtr->Self, &ps);
225 IPADDRESS_Draw (infoPtr, hdc);
226 EndPaint (infoPtr->Self, &ps);
231 static BOOL IPADDRESS_IsBlank (IPADDRESS_INFO *infoPtr)
237 for (i = 0; i < 4; i++)
238 if (GetWindowTextLengthW (infoPtr->Part[i].EditHwnd)) return FALSE;
244 static int IPADDRESS_GetAddress (IPADDRESS_INFO *infoPtr, LPDWORD ip_address)
252 for (i = 0; i < 4; i++) {
254 if (GetWindowTextW (infoPtr->Part[i].EditHwnd, field, 4))
255 ip_addr += atolW(field);
259 *ip_address = ip_addr;
265 static BOOL IPADDRESS_SetRange (IPADDRESS_INFO *infoPtr, int index, WORD range)
269 if ( (index < 0) || (index > 3) ) return FALSE;
271 infoPtr->Part[index].LowerLimit = range & 0xFF;
272 infoPtr->Part[index].UpperLimit = (range >> 8) & 0xFF;
278 static void IPADDRESS_ClearAddress (IPADDRESS_INFO *infoPtr)
280 WCHAR nil[1] = { 0 };
285 for (i = 0; i < 4; i++)
286 SetWindowTextW (infoPtr->Part[i].EditHwnd, nil);
290 static LRESULT IPADDRESS_SetAddress (IPADDRESS_INFO *infoPtr, DWORD ip_address)
292 WCHAR buf[20], fmt[] = { '%', 'd', 0 };
297 for (i = 3; i >= 0; i--) {
298 IPPART_INFO* part = &infoPtr->Part[i];
299 int value = ip_address & 0xff;
300 if ( (value >= part->LowerLimit) && (value <= part->UpperLimit) ) {
301 wsprintfW (buf, fmt, value);
302 SetWindowTextW (part->EditHwnd, buf);
303 IPADDRESS_Notify (infoPtr, EN_CHANGE);
312 static void IPADDRESS_SetFocusToField (IPADDRESS_INFO *infoPtr, INT index)
314 TRACE("(index=%d)\n", index);
317 for (index = 0; index < 4; index++)
318 if (!GetWindowTextLengthW(infoPtr->Part[index].EditHwnd)) break;
320 if (index < 9 || index > 3) index = 0;
322 SetFocus (infoPtr->Part[index].EditHwnd);
326 static BOOL IPADDRESS_ConstrainField (IPADDRESS_INFO *infoPtr, int currentfield)
328 IPPART_INFO *part = &infoPtr->Part[currentfield];
329 WCHAR field[10], fmt[] = { '%', 'd', 0 };
330 int curValue, newValue;
332 TRACE("(currentfield=%d)\n", currentfield);
334 if (currentfield < 0 || currentfield > 3) return FALSE;
336 if (!GetWindowTextW (part->EditHwnd, field, 4)) return FALSE;
338 curValue = atoiW(field);
339 TRACE(" curValue=%d\n", curValue);
341 newValue = IPADDRESS_IPNotify(infoPtr, currentfield, curValue);
342 TRACE(" newValue=%d\n", newValue);
344 if (newValue < part->LowerLimit) newValue = part->LowerLimit;
345 if (newValue > part->UpperLimit) newValue = part->UpperLimit;
347 if (newValue == curValue) return FALSE;
349 wsprintfW (field, fmt, newValue);
350 TRACE(" field='%s'\n", debugstr_w(field));
351 return SetWindowTextW (part->EditHwnd, field);
355 static BOOL IPADDRESS_GotoNextField (IPADDRESS_INFO *infoPtr, int cur, int sel)
359 if(cur >= -1 && cur < 4) {
360 IPADDRESS_ConstrainField(infoPtr, cur);
363 IPPART_INFO *next = &infoPtr->Part[cur + 1];
364 int start = 0, end = 0;
365 SetFocus (next->EditHwnd);
366 if (sel != POS_DEFAULT) {
367 if (sel == POS_RIGHT)
368 start = end = GetWindowTextLengthW(next->EditHwnd);
369 else if (sel == POS_SELALL)
371 SendMessageW(next->EditHwnd, EM_SETSEL, start, end);
382 * period: move and select the text in the next field to the right if
383 * the current field is not empty(l!=0), we are not in the
384 * left most position, and nothing is selected(startsel==endsel)
386 * spacebar: same behavior as period
388 * alpha characters: completely ignored
390 * digits: accepted when field text length < 2 ignored otherwise.
391 * when 3 numbers have been entered into the field the value
392 * of the field is checked, if the field value exceeds the
393 * maximum value and is changed the field remains the current
394 * field, otherwise focus moves to the field to the right
396 * tab: change focus from the current ipaddress control to the next
397 * control in the tab order
399 * right arrow: move to the field on the right to the left most
400 * position in that field if no text is selected,
401 * we are in the right most position in the field,
402 * we are not in the right most field
404 * left arrow: move to the field on the left to the right most
405 * position in that field if no text is selected,
406 * we are in the left most position in the current field
407 * and we are not in the left most field
409 * backspace: delete the character to the left of the cursor position,
410 * if none are present move to the field on the left if
411 * we are not in the left most field and delete the right
412 * most digit in that field while keeping the cursor
413 * on the right side of the field
416 IPADDRESS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
418 HWND Self = (HWND)GetPropA (hwnd, IP_SUBCLASS_PROP);
419 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (Self);
420 CHAR c = (CHAR)wParam;
421 INT index, len = 0, startsel, endsel;
424 TRACE("(hwnd=%p msg=0x%x wparam=0x%x lparam=0x%lx)\n", hwnd, uMsg, wParam, lParam);
426 if ( (index = IPADDRESS_GetPartIndex(infoPtr, hwnd)) < 0) return 0;
427 part = &infoPtr->Part[index];
429 if (uMsg == WM_CHAR || uMsg == WM_KEYDOWN) {
430 len = GetWindowTextLengthW (hwnd);
431 SendMessageW(hwnd, EM_GETSEL, (WPARAM)&startsel, (LPARAM)&endsel);
436 if(len == 2 && startsel==endsel && endsel==len) {
437 /* process the digit press before we check the field */
438 int return_val = CallWindowProcW (part->OrigProc, hwnd, uMsg, wParam, lParam);
440 /* if the field value was changed stay at the current field */
441 if(!IPADDRESS_ConstrainField(infoPtr, index))
442 IPADDRESS_GotoNextField (infoPtr, index, POS_DEFAULT);
445 } else if (len == 3 && startsel==endsel && endsel==len)
446 IPADDRESS_GotoNextField (infoPtr, index, POS_SELALL);
447 else if (len < 3) break;
448 } else if(c == '.' || c == ' ') {
449 if(len && startsel==endsel && startsel != 0) {
450 IPADDRESS_GotoNextField(infoPtr, index, POS_SELALL);
452 } else if (c == VK_BACK) break;
458 if(startsel==endsel && startsel==len) {
459 IPADDRESS_GotoNextField(infoPtr, index, POS_LEFT);
464 if(startsel==0 && startsel==endsel && index > 0) {
465 IPADDRESS_GotoNextField(infoPtr, index - 2, POS_RIGHT);
470 if(startsel==endsel && startsel==0 && index > 0) {
471 IPPART_INFO *prev = &infoPtr->Part[index-1];
474 if(GetWindowTextW(prev->EditHwnd, val, 5)) {
475 val[lstrlenW(val) - 1] = 0;
476 SetWindowTextW(prev->EditHwnd, val);
479 IPADDRESS_GotoNextField(infoPtr, index - 2, POS_RIGHT);
486 if (IPADDRESS_GetPartIndex(infoPtr, (HWND)wParam) < 0)
487 IPADDRESS_Notify(infoPtr, EN_KILLFOCUS);
490 if (IPADDRESS_GetPartIndex(infoPtr, (HWND)wParam) < 0)
491 IPADDRESS_Notify(infoPtr, EN_SETFOCUS);
494 return CallWindowProcW (part->OrigProc, hwnd, uMsg, wParam, lParam);
498 static LRESULT WINAPI
499 IPADDRESS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
501 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd);
503 TRACE("(hwnd=%p msg=0x%x wparam=0x%x lparam=0x%lx)\n", hwnd, uMsg, wParam, lParam);
505 if (!infoPtr && (uMsg != WM_CREATE))
506 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
511 return IPADDRESS_Create (hwnd);
514 return IPADDRESS_Destroy (infoPtr);
517 return IPADDRESS_Paint (infoPtr, (HDC)wParam);
520 switch(wParam >> 16) {
522 IPADDRESS_Notify(infoPtr, EN_CHANGE);
525 IPADDRESS_ConstrainField(infoPtr, IPADDRESS_GetPartIndex(infoPtr, (HWND)lParam));
530 case IPM_CLEARADDRESS:
531 IPADDRESS_ClearAddress (infoPtr);
535 return IPADDRESS_SetAddress (infoPtr, (DWORD)lParam);
538 return IPADDRESS_GetAddress (infoPtr, (LPDWORD)lParam);
541 return IPADDRESS_SetRange (infoPtr, (int)wParam, (WORD)lParam);
544 IPADDRESS_SetFocusToField (infoPtr, (int)wParam);
548 return IPADDRESS_IsBlank (infoPtr);
551 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
552 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
553 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
559 void IPADDRESS_Register (void)
563 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
564 wndClass.style = CS_GLOBALCLASS;
565 wndClass.lpfnWndProc = (WNDPROC)IPADDRESS_WindowProc;
566 wndClass.cbClsExtra = 0;
567 wndClass.cbWndExtra = sizeof(IPADDRESS_INFO *);
568 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_IBEAM);
569 wndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
570 wndClass.lpszClassName = WC_IPADDRESSW;
572 RegisterClassW (&wndClass);
576 void IPADDRESS_Unregister (void)
578 UnregisterClassW (WC_IPADDRESSW, NULL);