xref: /reactos/dll/win32/comctl32/ipaddress.c (revision 0707475f)
1 /*
2  * IP Address control
3  *
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>
9  *
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.
14  *
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.
19  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winnls.h"
36 #include "commctrl.h"
37 #include "comctl32.h"
38 #include "uxtheme.h"
39 #include "vsstyle.h"
40 #include "vssym32.h"
41 #include "wine/debug.h"
42 #include "wine/heap.h"
43 
44 WINE_DEFAULT_DEBUG_CHANNEL(ipaddress);
45 
46 typedef struct
47 {
48     HWND     EditHwnd;
49     INT      LowerLimit;
50     INT      UpperLimit;
51     WNDPROC  OrigProc;
52 } IPPART_INFO;
53 
54 typedef struct
55 {
56     HWND	Self;
57     HWND	Notify;
58     BOOL	Enabled;
59     IPPART_INFO	Part[4];
60 } IPADDRESS_INFO;
61 
62 static const WCHAR IP_SUBCLASS_PROP[] =
63     { 'C', 'C', 'I', 'P', '3', '2', 'S', 'u', 'b', 'c', 'l', 'a', 's', 's', 'I', 'n', 'f', 'o', 0 };
64 
65 #define POS_DEFAULT	0
66 #define POS_LEFT	1
67 #define POS_RIGHT	2
68 #define POS_SELALL	3
69 
70 static LRESULT CALLBACK
71 IPADDRESS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
72 
IPADDRESS_UpdateText(const IPADDRESS_INFO * infoPtr)73 static void IPADDRESS_UpdateText (const IPADDRESS_INFO *infoPtr)
74 {
75     static const WCHAR zero[] = {'0', 0};
76     static const WCHAR dot[]  = {'.', 0};
77     WCHAR field[4];
78     WCHAR ip[16];
79     INT i;
80 
81     ip[0] = 0;
82 
83     for (i = 0; i < 4; i++) {
84         if (GetWindowTextW (infoPtr->Part[i].EditHwnd, field, 4))
85             lstrcatW(ip, field);
86         else
87             /* empty edit treated as zero */
88             lstrcatW(ip, zero);
89         if (i != 3)
90             lstrcatW(ip, dot);
91     }
92 
93     SetWindowTextW(infoPtr->Self, ip);
94 }
95 
IPADDRESS_Notify(const IPADDRESS_INFO * infoPtr,UINT command)96 static LRESULT IPADDRESS_Notify (const IPADDRESS_INFO *infoPtr, UINT command)
97 {
98     HWND hwnd = infoPtr->Self;
99 
100     TRACE("(command=%x)\n", command);
101 
102     return SendMessageW (infoPtr->Notify, WM_COMMAND,
103              MAKEWPARAM (GetWindowLongPtrW (hwnd, GWLP_ID), command), (LPARAM)hwnd);
104 }
105 
IPADDRESS_IPNotify(const IPADDRESS_INFO * infoPtr,INT field,INT value)106 static INT IPADDRESS_IPNotify (const IPADDRESS_INFO *infoPtr, INT field, INT value)
107 {
108     NMIPADDRESS nmip;
109 
110     TRACE("(field=%x, value=%d)\n", field, value);
111 
112     nmip.hdr.hwndFrom = infoPtr->Self;
113     nmip.hdr.idFrom   = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
114     nmip.hdr.code     = IPN_FIELDCHANGED;
115 
116     nmip.iField = field;
117     nmip.iValue = value;
118 
119     SendMessageW (infoPtr->Notify, WM_NOTIFY, nmip.hdr.idFrom, (LPARAM)&nmip);
120 
121     TRACE("<-- %d\n", nmip.iValue);
122 
123     return nmip.iValue;
124 }
125 
126 
IPADDRESS_GetPartIndex(const IPADDRESS_INFO * infoPtr,HWND hwnd)127 static int IPADDRESS_GetPartIndex(const IPADDRESS_INFO *infoPtr, HWND hwnd)
128 {
129     int i;
130 
131     TRACE("(hwnd=%p)\n", hwnd);
132 
133     for (i = 0; i < 4; i++)
134         if (infoPtr->Part[i].EditHwnd == hwnd) return i;
135 
136     ERR("We subclassed the wrong window! (hwnd=%p)\n", hwnd);
137     return -1;
138 }
139 
140 
IPADDRESS_Draw(const IPADDRESS_INFO * infoPtr,HDC hdc)141 static LRESULT IPADDRESS_Draw (const IPADDRESS_INFO *infoPtr, HDC hdc)
142 {
143     static const WCHAR dotW[] = { '.', 0 };
144     RECT rect, rcPart;
145     COLORREF bgCol, fgCol;
146     HTHEME theme;
147     int i, state = ETS_NORMAL;
148 
149     TRACE("\n");
150 
151     GetClientRect (infoPtr->Self, &rect);
152 
153     theme = OpenThemeData(infoPtr->Self, WC_EDITW);
154 
155     if (theme) {
156         DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
157 
158         if (!infoPtr->Enabled)
159             state = ETS_DISABLED;
160         else if (dwStyle & ES_READONLY)
161             state = ETS_READONLY;
162         else if (GetFocus() == infoPtr->Self)
163             state = ETS_FOCUSED;
164 
165         GetThemeColor(theme, EP_EDITTEXT, state, TMT_FILLCOLOR, &bgCol);
166         GetThemeColor(theme, EP_EDITTEXT, state, TMT_TEXTCOLOR, &fgCol);
167 
168         if (IsThemeBackgroundPartiallyTransparent (theme, EP_EDITTEXT, state))
169             DrawThemeParentBackground(infoPtr->Self, hdc, &rect);
170         DrawThemeBackground (theme, hdc, EP_EDITTEXT, state, &rect, 0);
171     } else {
172         if (infoPtr->Enabled) {
173             bgCol = comctl32_color.clrWindow;
174             fgCol = comctl32_color.clrWindowText;
175         } else {
176             bgCol = comctl32_color.clr3dFace;
177             fgCol = comctl32_color.clrGrayText;
178         }
179 
180 #ifdef __REACTOS__
181         {
182             HBRUSH brush = CreateSolidBrush(bgCol);
183             FillRect(hdc, &rect, brush);
184             DeleteObject(brush);
185         }
186 #else
187         FillRect (hdc, &rect, (HBRUSH)(DWORD_PTR)(bgCol+1));
188 #endif
189         DrawEdge (hdc, &rect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
190     }
191 
192     SetBkColor  (hdc, bgCol);
193     SetTextColor(hdc, fgCol);
194 
195     for (i = 0; i < 3; i++) {
196         GetWindowRect (infoPtr->Part[i].EditHwnd, &rcPart);
197         MapWindowPoints( 0, infoPtr->Self, (POINT *)&rcPart, 2 );
198         rect.left = rcPart.right;
199         GetWindowRect (infoPtr->Part[i+1].EditHwnd, &rcPart);
200         MapWindowPoints( 0, infoPtr->Self, (POINT *)&rcPart, 2 );
201         rect.right = rcPart.left;
202 
203         if (theme)
204             DrawThemeText(theme, hdc, EP_EDITTEXT, state, dotW, 1, DT_SINGLELINE | DT_CENTER | DT_BOTTOM, 0, &rect);
205         else
206             DrawTextW(hdc, dotW, 1, &rect, DT_SINGLELINE | DT_CENTER | DT_BOTTOM);
207     }
208 
209     if (theme)
210         CloseThemeData(theme);
211 
212     return 0;
213 }
214 
215 
IPADDRESS_Create(HWND hwnd,const CREATESTRUCTA * lpCreate)216 static LRESULT IPADDRESS_Create (HWND hwnd, const CREATESTRUCTA *lpCreate)
217 {
218     IPADDRESS_INFO *infoPtr;
219     RECT rcClient, edit;
220     int i, fieldsize;
221     HFONT hFont, hSysFont;
222     LOGFONTW logFont, logSysFont;
223 
224     TRACE("\n");
225 
226     SetWindowLongW (hwnd, GWL_STYLE,
227 		    GetWindowLongW(hwnd, GWL_STYLE) & ~WS_BORDER);
228 
229     infoPtr = heap_alloc_zero (sizeof(*infoPtr));
230     if (!infoPtr) return -1;
231     SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
232 
233     GetClientRect (hwnd, &rcClient);
234 
235     fieldsize = (rcClient.right - rcClient.left) / 4;
236 
237     edit.top    = rcClient.top + 2;
238     edit.bottom = rcClient.bottom - 2;
239 
240     infoPtr->Self = hwnd;
241     infoPtr->Enabled = TRUE;
242     infoPtr->Notify = lpCreate->hwndParent;
243 
244     hSysFont = GetStockObject(ANSI_VAR_FONT);
245     GetObjectW(hSysFont, sizeof(LOGFONTW), &logSysFont);
246     SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
247     lstrcpyW(logFont.lfFaceName, logSysFont.lfFaceName);
248     hFont = CreateFontIndirectW(&logFont);
249 
250     for (i = 0; i < 4; i++) {
251 	IPPART_INFO* part = &infoPtr->Part[i];
252 
253 	part->LowerLimit = 0;
254 	part->UpperLimit = 255;
255         edit.left = rcClient.left + i*fieldsize + 6;
256         edit.right = rcClient.left + (i+1)*fieldsize - 2;
257         part->EditHwnd =
258 		CreateWindowW (WC_EDITW, NULL, WS_CHILD | WS_VISIBLE | ES_CENTER,
259                                edit.left, edit.top, edit.right - edit.left,
260 			       edit.bottom - edit.top, hwnd, (HMENU) 1,
261 			       (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE), NULL);
262         SendMessageW(part->EditHwnd, WM_SETFONT, (WPARAM) hFont, FALSE);
263 	SetPropW(part->EditHwnd, IP_SUBCLASS_PROP, hwnd);
264         part->OrigProc = (WNDPROC)
265 		SetWindowLongPtrW (part->EditHwnd, GWLP_WNDPROC,
266 				(DWORD_PTR)IPADDRESS_SubclassProc);
267         EnableWindow(part->EditHwnd, infoPtr->Enabled);
268     }
269 
270     IPADDRESS_UpdateText (infoPtr);
271 
272     return 0;
273 }
274 
275 
IPADDRESS_Destroy(IPADDRESS_INFO * infoPtr)276 static LRESULT IPADDRESS_Destroy (IPADDRESS_INFO *infoPtr)
277 {
278     int i;
279 
280     TRACE("\n");
281 
282     for (i = 0; i < 4; i++) {
283 	IPPART_INFO* part = &infoPtr->Part[i];
284         SetWindowLongPtrW (part->EditHwnd, GWLP_WNDPROC, (DWORD_PTR)part->OrigProc);
285     }
286 
287     SetWindowLongPtrW (infoPtr->Self, 0, 0);
288     heap_free (infoPtr);
289     return 0;
290 }
291 
292 
IPADDRESS_Enable(IPADDRESS_INFO * infoPtr,BOOL enabled)293 static LRESULT IPADDRESS_Enable (IPADDRESS_INFO *infoPtr, BOOL enabled)
294 {
295     int i;
296 
297     infoPtr->Enabled = enabled;
298 
299     for (i = 0; i < 4; i++)
300         EnableWindow(infoPtr->Part[i].EditHwnd, enabled);
301 
302     InvalidateRgn(infoPtr->Self, NULL, FALSE);
303     return 0;
304 }
305 
306 
IPADDRESS_Paint(const IPADDRESS_INFO * infoPtr,HDC hdc)307 static LRESULT IPADDRESS_Paint (const IPADDRESS_INFO *infoPtr, HDC hdc)
308 {
309     PAINTSTRUCT ps;
310 
311     TRACE("\n");
312 
313     if (hdc) return IPADDRESS_Draw (infoPtr, hdc);
314 
315     hdc = BeginPaint (infoPtr->Self, &ps);
316     IPADDRESS_Draw (infoPtr, hdc);
317     EndPaint (infoPtr->Self, &ps);
318     return 0;
319 }
320 
321 
IPADDRESS_IsBlank(const IPADDRESS_INFO * infoPtr)322 static BOOL IPADDRESS_IsBlank (const IPADDRESS_INFO *infoPtr)
323 {
324     int i;
325 
326     TRACE("\n");
327 
328     for (i = 0; i < 4; i++)
329         if (GetWindowTextLengthW (infoPtr->Part[i].EditHwnd)) return FALSE;
330 
331     return TRUE;
332 }
333 
334 
IPADDRESS_GetAddress(const IPADDRESS_INFO * infoPtr,LPDWORD ip_address)335 static int IPADDRESS_GetAddress (const IPADDRESS_INFO *infoPtr, LPDWORD ip_address)
336 {
337     WCHAR field[5];
338     int i, invalid = 0;
339     DWORD ip_addr = 0;
340 
341     TRACE("\n");
342 
343     for (i = 0; i < 4; i++) {
344         ip_addr *= 256;
345         if (GetWindowTextW (infoPtr->Part[i].EditHwnd, field, 4))
346 	    ip_addr += wcstol(field, NULL, 10);
347 	else
348 	    invalid++;
349     }
350     *ip_address = ip_addr;
351 
352     return 4 - invalid;
353 }
354 
355 
IPADDRESS_SetRange(IPADDRESS_INFO * infoPtr,int index,WORD range)356 static BOOL IPADDRESS_SetRange (IPADDRESS_INFO *infoPtr, int index, WORD range)
357 {
358     TRACE("\n");
359 
360     if ( (index < 0) || (index > 3) ) return FALSE;
361 
362     infoPtr->Part[index].LowerLimit = range & 0xFF;
363     infoPtr->Part[index].UpperLimit = (range >> 8)  & 0xFF;
364 
365     return TRUE;
366 }
367 
368 
IPADDRESS_ClearAddress(const IPADDRESS_INFO * infoPtr)369 static void IPADDRESS_ClearAddress (const IPADDRESS_INFO *infoPtr)
370 {
371     static const WCHAR nil[] = { 0 };
372     int i;
373 
374     TRACE("\n");
375 
376     for (i = 0; i < 4; i++)
377         SetWindowTextW (infoPtr->Part[i].EditHwnd, nil);
378 }
379 
380 
IPADDRESS_SetAddress(const IPADDRESS_INFO * infoPtr,DWORD ip_address)381 static LRESULT IPADDRESS_SetAddress (const IPADDRESS_INFO *infoPtr, DWORD ip_address)
382 {
383     WCHAR buf[20];
384     static const WCHAR fmt[] = { '%', 'd', 0 };
385     int i;
386 
387     TRACE("\n");
388 
389     for (i = 3; i >= 0; i--) {
390 	const IPPART_INFO* part = &infoPtr->Part[i];
391         int value = ip_address & 0xff;
392 	if ( (value >= part->LowerLimit) && (value <= part->UpperLimit) ) {
393 	    wsprintfW (buf, fmt, value);
394 	    SetWindowTextW (part->EditHwnd, buf);
395 	    IPADDRESS_Notify (infoPtr, EN_CHANGE);
396         }
397         ip_address >>= 8;
398     }
399 
400     return TRUE;
401 }
402 
403 
IPADDRESS_SetFocusToField(const IPADDRESS_INFO * infoPtr,INT index)404 static void IPADDRESS_SetFocusToField (const IPADDRESS_INFO *infoPtr, INT index)
405 {
406     TRACE("(index=%d)\n", index);
407 
408     if (index > 3 || index < 0) index=0;
409 
410     SetFocus (infoPtr->Part[index].EditHwnd);
411 }
412 
413 
IPADDRESS_ConstrainField(const IPADDRESS_INFO * infoPtr,int currentfield)414 static BOOL IPADDRESS_ConstrainField (const IPADDRESS_INFO *infoPtr, int currentfield)
415 {
416     static const WCHAR fmt[] = { '%', 'd', 0 };
417     const IPPART_INFO *part;
418     int curValue, newValue;
419     WCHAR field[10];
420 
421     TRACE("(currentfield=%d)\n", currentfield);
422 
423     if (currentfield < 0 || currentfield > 3) return FALSE;
424 
425     part = &infoPtr->Part[currentfield];
426     if (!GetWindowTextW (part->EditHwnd, field, 4)) return FALSE;
427 
428     curValue = wcstol(field, NULL, 10);
429     TRACE("  curValue=%d\n", curValue);
430 
431     newValue = IPADDRESS_IPNotify(infoPtr, currentfield, curValue);
432     TRACE("  newValue=%d\n", newValue);
433 
434     if (newValue < part->LowerLimit) newValue = part->LowerLimit;
435     if (newValue > part->UpperLimit) newValue = part->UpperLimit;
436 
437     if (newValue == curValue) return FALSE;
438 
439     wsprintfW (field, fmt, newValue);
440     TRACE("  field=%s\n", debugstr_w(field));
441     return SetWindowTextW (part->EditHwnd, field);
442 }
443 
444 
IPADDRESS_GotoNextField(const IPADDRESS_INFO * infoPtr,int cur,int sel)445 static BOOL IPADDRESS_GotoNextField (const IPADDRESS_INFO *infoPtr, int cur, int sel)
446 {
447     TRACE("\n");
448 
449     if(cur >= -1 && cur < 4) {
450 	IPADDRESS_ConstrainField(infoPtr, cur);
451 
452 	if(cur < 3) {
453 	    const IPPART_INFO *next = &infoPtr->Part[cur + 1];
454 	    int start = 0, end = 0;
455             SetFocus (next->EditHwnd);
456 	    if (sel != POS_DEFAULT) {
457 		if (sel == POS_RIGHT)
458 		    start = end = GetWindowTextLengthW(next->EditHwnd);
459 		else if (sel == POS_SELALL)
460 		    end = -1;
461 	        SendMessageW(next->EditHwnd, EM_SETSEL, start, end);
462 	    }
463 	    return TRUE;
464 	}
465 
466     }
467     return FALSE;
468 }
469 
470 
471 /*
472  * period: move and select the text in the next field to the right if
473  *         the current field is not empty(l!=0), we are not in the
474  *         left most position, and nothing is selected(startsel==endsel)
475  *
476  * spacebar: same behavior as period
477  *
478  * alpha characters: completely ignored
479  *
480  * digits: accepted when field text length < 2 ignored otherwise.
481  *         when 3 numbers have been entered into the field the value
482  *         of the field is checked, if the field value exceeds the
483  *         maximum value and is changed the field remains the current
484  *         field, otherwise focus moves to the field to the right
485  *
486  * tab: change focus from the current ipaddress control to the next
487  *      control in the tab order
488  *
489  * right arrow: move to the field on the right to the left most
490  *              position in that field if no text is selected,
491  *              we are in the right most position in the field,
492  *              we are not in the right most field
493  *
494  * left arrow: move to the field on the left to the right most
495  *             position in that field if no text is selected,
496  *             we are in the left most position in the current field
497  *             and we are not in the left most field
498  *
499  * backspace: delete the character to the left of the cursor position,
500  *            if none are present move to the field on the left if
501  *            we are not in the left most field and delete the right
502  *            most digit in that field while keeping the cursor
503  *            on the right side of the field
504  */
505 LRESULT CALLBACK
IPADDRESS_SubclassProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)506 IPADDRESS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
507 {
508     HWND Self = GetPropW (hwnd, IP_SUBCLASS_PROP);
509     IPADDRESS_INFO *infoPtr = (IPADDRESS_INFO *)GetWindowLongPtrW (Self, 0);
510     CHAR c = (CHAR)wParam;
511     INT index, len = 0, startsel, endsel;
512     IPPART_INFO *part;
513 
514     TRACE("(hwnd=%p msg=0x%x wparam=0x%lx lparam=0x%lx)\n", hwnd, uMsg, wParam, lParam);
515 
516     if ( (index = IPADDRESS_GetPartIndex(infoPtr, hwnd)) < 0) return 0;
517     part = &infoPtr->Part[index];
518 
519     if (uMsg == WM_CHAR || uMsg == WM_KEYDOWN) {
520 	len = GetWindowTextLengthW (hwnd);
521 	SendMessageW(hwnd, EM_GETSEL, (WPARAM)&startsel, (LPARAM)&endsel);
522     }
523     switch (uMsg) {
524  	case WM_CHAR:
525  	    if(isdigit(c)) {
526 		if(len == 2 && startsel==endsel && endsel==len) {
527 		    /* process the digit press before we check the field */
528 		    int return_val = CallWindowProcW (part->OrigProc, hwnd, uMsg, wParam, lParam);
529 
530 		    /* if the field value was changed stay at the current field */
531 		    if(!IPADDRESS_ConstrainField(infoPtr, index))
532 			IPADDRESS_GotoNextField (infoPtr, index, POS_DEFAULT);
533 
534 		    return return_val;
535 		} else if (len == 3 && startsel==endsel && endsel==len)
536 		    IPADDRESS_GotoNextField (infoPtr, index, POS_SELALL);
537 		else if (len < 3 || startsel != endsel) break;
538 	    } else if(c == '.' || c == ' ') {
539 		if(len && startsel==endsel && startsel != 0) {
540 		    IPADDRESS_GotoNextField(infoPtr, index, POS_SELALL);
541 		}
542  	    } else if (c == VK_BACK) break;
543 	    return 0;
544 
545 	case WM_KEYDOWN:
546 	    switch(c) {
547 		case VK_RIGHT:
548 		    if(startsel==endsel && startsel==len) {
549 			IPADDRESS_GotoNextField(infoPtr, index, POS_LEFT);
550 			return 0;
551 		    }
552 		    break;
553 		case VK_LEFT:
554 		    if(startsel==0 && startsel==endsel && index > 0) {
555 			IPADDRESS_GotoNextField(infoPtr, index - 2, POS_RIGHT);
556 			return 0;
557 		    }
558 		    break;
559 		case VK_BACK:
560 		    if(startsel==endsel && startsel==0 && index > 0) {
561 			IPPART_INFO *prev = &infoPtr->Part[index-1];
562 			WCHAR val[10];
563 
564 			if(GetWindowTextW(prev->EditHwnd, val, 5)) {
565 			    val[lstrlenW(val) - 1] = 0;
566 			    SetWindowTextW(prev->EditHwnd, val);
567 			}
568 
569 			IPADDRESS_GotoNextField(infoPtr, index - 2, POS_RIGHT);
570 			return 0;
571 		    }
572 		    break;
573 	    }
574 	    break;
575 	case WM_KILLFOCUS:
576 	    if (IPADDRESS_GetPartIndex(infoPtr, (HWND)wParam) < 0)
577 		IPADDRESS_Notify(infoPtr, EN_KILLFOCUS);
578 	    break;
579 	case WM_SETFOCUS:
580 	    if (IPADDRESS_GetPartIndex(infoPtr, (HWND)wParam) < 0)
581 		IPADDRESS_Notify(infoPtr, EN_SETFOCUS);
582 	    break;
583     }
584     return CallWindowProcW (part->OrigProc, hwnd, uMsg, wParam, lParam);
585 }
586 
587 
588 static LRESULT WINAPI
IPADDRESS_WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)589 IPADDRESS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
590 {
591     IPADDRESS_INFO *infoPtr = (IPADDRESS_INFO *)GetWindowLongPtrW (hwnd, 0);
592 
593     TRACE("(hwnd=%p msg=0x%x wparam=0x%lx lparam=0x%lx)\n", hwnd, uMsg, wParam, lParam);
594 
595     if (!infoPtr && (uMsg != WM_CREATE))
596         return DefWindowProcW (hwnd, uMsg, wParam, lParam);
597 
598     switch (uMsg)
599     {
600 	case WM_CREATE:
601 	    return IPADDRESS_Create (hwnd, (LPCREATESTRUCTA)lParam);
602 
603 	case WM_DESTROY:
604 	    return IPADDRESS_Destroy (infoPtr);
605 
606 	case WM_ENABLE:
607 	    return IPADDRESS_Enable (infoPtr, (BOOL)wParam);
608 
609 	case WM_PAINT:
610 	    return IPADDRESS_Paint (infoPtr, (HDC)wParam);
611 
612 #ifdef __REACTOS__
613 	case WM_SETFOCUS:
614 	    IPADDRESS_GotoNextField(infoPtr, -1, POS_SELALL);
615 	    return 0;
616 #endif
617 	case WM_COMMAND:
618 	    switch(wParam >> 16) {
619 		case EN_CHANGE:
620 		    IPADDRESS_UpdateText(infoPtr);
621 		    IPADDRESS_Notify(infoPtr, EN_CHANGE);
622 		    break;
623 		case EN_KILLFOCUS:
624 		    IPADDRESS_ConstrainField(infoPtr, IPADDRESS_GetPartIndex(infoPtr, (HWND)lParam));
625 		    break;
626 	    }
627 	    break;
628 
629         case WM_SYSCOLORCHANGE:
630             COMCTL32_RefreshSysColors();
631             return 0;
632 
633         case IPM_CLEARADDRESS:
634             IPADDRESS_ClearAddress (infoPtr);
635 	    break;
636 
637         case IPM_SETADDRESS:
638             return IPADDRESS_SetAddress (infoPtr, (DWORD)lParam);
639 
640         case IPM_GETADDRESS:
641  	    return IPADDRESS_GetAddress (infoPtr, (LPDWORD)lParam);
642 
643 	case IPM_SETRANGE:
644 	    return IPADDRESS_SetRange (infoPtr, (int)wParam, (WORD)lParam);
645 
646 	case IPM_SETFOCUS:
647 	    IPADDRESS_SetFocusToField (infoPtr, (int)wParam);
648 	    break;
649 
650 	case IPM_ISBLANK:
651 	    return IPADDRESS_IsBlank (infoPtr);
652 
653 	default:
654 	    if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
655 		ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
656 	    return DefWindowProcW (hwnd, uMsg, wParam, lParam);
657     }
658     return 0;
659 }
660 
661 
IPADDRESS_Register(void)662 void IPADDRESS_Register (void)
663 {
664     WNDCLASSW wndClass;
665 
666     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
667     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
668     wndClass.lpfnWndProc   = IPADDRESS_WindowProc;
669     wndClass.cbClsExtra    = 0;
670     wndClass.cbWndExtra    = sizeof(IPADDRESS_INFO *);
671     wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_IBEAM);
672     wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
673     wndClass.lpszClassName = WC_IPADDRESSW;
674 
675     RegisterClassW (&wndClass);
676 }
677 
678 
IPADDRESS_Unregister(void)679 void IPADDRESS_Unregister (void)
680 {
681     UnregisterClassW (WC_IPADDRESSW, NULL);
682 }
683