source: trunk/src/comctl32/ipaddress.c@ 8663

Last change on this file since 8663 was 8382, checked in by sandervl, 23 years ago

merge with latest Wine

File size: 14.9 KB
Line 
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26#include <ctype.h>
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30
31#include "ntddk.h"
32#include "winbase.h"
33#include "commctrl.h"
34#include "wine/debug.h"
35#ifdef __WIN32OS2__
36#include "comctl32.h"
37#endif
38
39WINE_DEFAULT_DEBUG_CHANNEL(ipaddress);
40
41typedef struct
42{
43 HWND EditHwnd;
44 INT LowerLimit;
45 INT UpperLimit;
46 WNDPROC OrigProc;
47} IPPART_INFO;
48
49typedef struct
50{
51 HWND Self;
52 IPPART_INFO Part[4];
53} IPADDRESS_INFO;
54
55#define POS_DEFAULT 0
56#define POS_LEFT 1
57#define POS_RIGHT 2
58#define POS_SELALL 3
59
60#define IP_SUBCLASS_PROP "CCIP32SubclassInfo"
61#define IPADDRESS_GetInfoPtr(hwnd) ((IPADDRESS_INFO *)GetWindowLongW (hwnd, 0))
62
63
64static LRESULT CALLBACK
65IPADDRESS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
66
67static LRESULT IPADDRESS_Notify (IPADDRESS_INFO *infoPtr, UINT command)
68{
69 HWND hwnd = infoPtr->Self;
70
71 TRACE("(command=%x)\n", command);
72
73 return SendMessageW (GetParent (hwnd), WM_COMMAND,
74 MAKEWPARAM (GetWindowLongW (hwnd, GWL_ID), command), (LPARAM)hwnd);
75}
76
77static INT IPADDRESS_IPNotify (IPADDRESS_INFO *infoPtr, INT field, INT value)
78{
79 NMIPADDRESS nmip;
80
81 TRACE("(field=%x, value=%d)\n", field, value);
82
83 nmip.hdr.hwndFrom = infoPtr->Self;
84 nmip.hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
85 nmip.hdr.code = IPN_FIELDCHANGED;
86
87 nmip.iField = field;
88 nmip.iValue = value;
89
90 SendMessageW (GetParent (infoPtr->Self), WM_NOTIFY,
91 (WPARAM)nmip.hdr.idFrom, (LPARAM)&nmip);
92
93 TRACE("<-- %d\n", nmip.iValue);
94
95 return nmip.iValue;
96}
97
98
99static int IPADDRESS_GetPartIndex(IPADDRESS_INFO *infoPtr, HWND hwnd)
100{
101 int i;
102
103 TRACE("(hwnd=%x)\n", hwnd);
104
105 for (i = 0; i < 4; i++)
106 if (infoPtr->Part[i].EditHwnd == hwnd) return i;
107
108 ERR("We subclassed the wrong window! (hwnd=%x)\n", hwnd);
109 return -1;
110}
111
112
113static LRESULT IPADDRESS_Draw (IPADDRESS_INFO *infoPtr, HDC hdc)
114{
115 RECT rect, rcPart;
116 POINT pt;
117 int i;
118
119 TRACE("\n");
120
121 GetClientRect (infoPtr->Self, &rect);
122 DrawEdge (hdc, &rect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
123
124 for (i = 0; i < 3; i++) {
125 GetWindowRect (infoPtr->Part[i].EditHwnd, &rcPart);
126 pt.x = rcPart.right;
127 ScreenToClient(infoPtr->Self, &pt);
128 rect.left = pt.x;
129 GetWindowRect (infoPtr->Part[i+1].EditHwnd, &rcPart);
130 pt.x = rcPart.left;
131 ScreenToClient(infoPtr->Self, &pt);
132 rect.right = pt.x;
133 DrawTextA(hdc, ".", 1, &rect, DT_SINGLELINE | DT_CENTER | DT_BOTTOM);
134 }
135
136 return 0;
137}
138
139
140static LRESULT IPADDRESS_Create (HWND hwnd)
141{
142 IPADDRESS_INFO *infoPtr;
143 RECT rcClient, edit;
144 int i, fieldsize;
145 WCHAR EDIT[] = { 'E', 'd', 'i', 't', 0 };
146
147 TRACE("\n");
148
149 SetWindowLongW (hwnd, GWL_STYLE,
150 GetWindowLongW(hwnd, GWL_STYLE) & ~WS_BORDER);
151
152 infoPtr = (IPADDRESS_INFO *)COMCTL32_Alloc (sizeof(IPADDRESS_INFO));
153 if (!infoPtr) return -1;
154 SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
155
156 GetClientRect (hwnd, &rcClient);
157
158 fieldsize = (rcClient.right - rcClient.left) / 4;
159
160 edit.top = rcClient.top + 2;
161 edit.bottom = rcClient.bottom - 2;
162
163 infoPtr->Self = hwnd;
164
165 for (i = 0; i < 4; i++) {
166 IPPART_INFO* part = &infoPtr->Part[i];
167
168 part->LowerLimit = 0;
169 part->UpperLimit = 255;
170 edit.left = rcClient.left + i*fieldsize + 6;
171 edit.right = rcClient.left + (i+1)*fieldsize - 2;
172 part->EditHwnd =
173 CreateWindowW (EDIT, NULL, WS_CHILD | WS_VISIBLE | ES_CENTER,
174 edit.left, edit.top, edit.right - edit.left,
175 edit.bottom - edit.top, hwnd, (HMENU) 1,
176 GetWindowLongW (hwnd, GWL_HINSTANCE), NULL);
177 SetPropA(part->EditHwnd, IP_SUBCLASS_PROP, hwnd);
178 part->OrigProc = (WNDPROC)
179 SetWindowLongW (part->EditHwnd, GWL_WNDPROC,
180 (LONG)IPADDRESS_SubclassProc);
181 }
182
183 return 0;
184}
185
186
187static LRESULT IPADDRESS_Destroy (IPADDRESS_INFO *infoPtr)
188{
189 int i;
190
191 TRACE("\n");
192
193 for (i = 0; i < 4; i++) {
194 IPPART_INFO* part = &infoPtr->Part[i];
195 SetWindowLongW (part->EditHwnd, GWL_WNDPROC, (LONG)part->OrigProc);
196 }
197
198 SetWindowLongW (infoPtr->Self, 0, 0);
199 COMCTL32_Free (infoPtr);
200 return 0;
201}
202
203
204static LRESULT IPADDRESS_Paint (IPADDRESS_INFO *infoPtr, HDC hdc)
205{
206 PAINTSTRUCT ps;
207
208 TRACE("\n");
209
210 if (hdc) return IPADDRESS_Draw (infoPtr, hdc);
211
212 hdc = BeginPaint (infoPtr->Self, &ps);
213 IPADDRESS_Draw (infoPtr, hdc);
214 EndPaint (infoPtr->Self, &ps);
215 return 0;
216}
217
218
219static BOOL IPADDRESS_IsBlank (IPADDRESS_INFO *infoPtr)
220{
221 int i;
222
223 TRACE("\n");
224
225 for (i = 0; i < 4; i++)
226 if (GetWindowTextLengthW (infoPtr->Part[i].EditHwnd)) return FALSE;
227
228 return TRUE;
229}
230
231
232static int IPADDRESS_GetAddress (IPADDRESS_INFO *infoPtr, LPDWORD ip_address)
233{
234 WCHAR field[5];
235 int i, invalid = 0;
236 DWORD ip_addr = 0;
237
238 TRACE("\n");
239
240 for (i = 0; i < 4; i++) {
241 ip_addr *= 256;
242 if (GetWindowTextW (infoPtr->Part[i].EditHwnd, field, 4))
243 ip_addr += wcstol(field, 0, 10);
244 else
245 invalid++;
246 }
247 *ip_address = ip_addr;
248
249 return 4 - invalid;
250}
251
252
253static BOOL IPADDRESS_SetRange (IPADDRESS_INFO *infoPtr, int index, WORD range)
254{
255 TRACE("\n");
256
257 if ( (index < 0) || (index > 3) ) return FALSE;
258
259 infoPtr->Part[index].LowerLimit = range & 0xFF;
260 infoPtr->Part[index].UpperLimit = (range >> 8) & 0xFF;
261
262 return TRUE;
263}
264
265
266static void IPADDRESS_ClearAddress (IPADDRESS_INFO *infoPtr)
267{
268 WCHAR nil[1] = { 0 };
269 int i;
270
271 TRACE("\n");
272
273 for (i = 0; i < 4; i++)
274 SetWindowTextW (infoPtr->Part[i].EditHwnd, nil);
275}
276
277
278static LRESULT IPADDRESS_SetAddress (IPADDRESS_INFO *infoPtr, DWORD ip_address)
279{
280 WCHAR buf[20], fmt[] = { '%', 'd', 0 };
281 int i;
282
283 TRACE("\n");
284
285 for (i = 3; i >= 0; i--) {
286 IPPART_INFO* part = &infoPtr->Part[i];
287 int value = ip_address & 0xff;
288 if ( (value >= part->LowerLimit) && (value <= part->UpperLimit) ) {
289 swprintf (buf, fmt, value);
290 SetWindowTextW (part->EditHwnd, buf);
291 IPADDRESS_Notify (infoPtr, EN_CHANGE);
292 }
293 ip_address >>= 8;
294 }
295
296 return TRUE;
297}
298
299
300static void IPADDRESS_SetFocusToField (IPADDRESS_INFO *infoPtr, INT index)
301{
302 TRACE("(index=%d)\n", index);
303
304 if (index > 3) {
305 for (index = 0; index < 4; index++)
306 if (!GetWindowTextLengthW(infoPtr->Part[index].EditHwnd)) break;
307 }
308 if (index < 9 || index > 3) index = 0;
309
310 SetFocus (infoPtr->Part[index].EditHwnd);
311}
312
313
314static BOOL IPADDRESS_ConstrainField (IPADDRESS_INFO *infoPtr, int currentfield)
315{
316 IPPART_INFO *part = &infoPtr->Part[currentfield];
317 WCHAR field[10], fmt[] = { '%', 'd', 0 };
318 int curValue, newValue;
319
320 TRACE("(currentfield=%d)\n", currentfield);
321
322 if (currentfield < 0 || currentfield > 3) return FALSE;
323
324 if (!GetWindowTextW (part->EditHwnd, field, 4)) return FALSE;
325
326 curValue = wcstol(field, 0, 10);
327 TRACE(" curValue=%d\n", curValue);
328
329 newValue = IPADDRESS_IPNotify(infoPtr, currentfield, curValue);
330 TRACE(" newValue=%d\n", newValue);
331
332 if (newValue < part->LowerLimit) newValue = part->LowerLimit;
333 if (newValue > part->UpperLimit) newValue = part->UpperLimit;
334
335 if (newValue == curValue) return FALSE;
336
337 swprintf (field, fmt, newValue);
338 TRACE(" field='%s'\n", debugstr_w(field));
339 return SetWindowTextW (part->EditHwnd, field);
340}
341
342
343static BOOL IPADDRESS_GotoNextField (IPADDRESS_INFO *infoPtr, int cur, int sel)
344{
345 TRACE("\n");
346
347 if(cur >= -1 && cur < 4) {
348 IPADDRESS_ConstrainField(infoPtr, cur);
349
350 if(cur < 3) {
351 IPPART_INFO *next = &infoPtr->Part[cur + 1];
352 int start = 0, end = 0;
353 SetFocus (next->EditHwnd);
354 if (sel != POS_DEFAULT) {
355 if (sel == POS_RIGHT)
356 start = end = GetWindowTextLengthW(next->EditHwnd);
357 else if (sel == POS_SELALL)
358 end = -1;
359 SendMessageW(next->EditHwnd, EM_SETSEL, start, end);
360 }
361 return TRUE;
362 }
363
364 }
365 return FALSE;
366}
367
368
369/*
370 * period: move and select the text in the next field to the right if
371 * the current field is not empty(l!=0), we are not in the
372 * left most position, and nothing is selected(startsel==endsel)
373 *
374 * spacebar: same behavior as period
375 *
376 * alpha characters: completely ignored
377 *
378 * digits: accepted when field text length < 2 ignored otherwise.
379 * when 3 numbers have been entered into the field the value
380 * of the field is checked, if the field value exceeds the
381 * maximum value and is changed the field remains the current
382 * field, otherwise focus moves to the field to the right
383 *
384 * tab: change focus from the current ipaddress control to the next
385 * control in the tab order
386 *
387 * right arrow: move to the field on the right to the left most
388 * position in that field if no text is selected,
389 * we are in the right most position in the field,
390 * we are not in the right most field
391 *
392 * left arrow: move to the field on the left to the right most
393 * position in that field if no text is selected,
394 * we are in the left most position in the current field
395 * and we are not in the left most field
396 *
397 * backspace: delete the character to the left of the cursor position,
398 * if none are present move to the field on the left if
399 * we are not in the left most field and delete the right
400 * most digit in that field while keeping the cursor
401 * on the right side of the field
402 */
403LRESULT CALLBACK
404IPADDRESS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
405{
406 HWND Self = (HWND)GetPropA (hwnd, IP_SUBCLASS_PROP);
407 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (Self);
408 CHAR c = (CHAR)wParam;
409 INT index, len = 0, startsel, endsel;
410 IPPART_INFO *part;
411
412 TRACE("(hwnd=0x%x msg=0x%x wparam=0x%x lparam=0x%lx)\n", hwnd, uMsg, wParam, lParam);
413
414 if ( (index = IPADDRESS_GetPartIndex(infoPtr, hwnd)) < 0) return 0;
415 part = &infoPtr->Part[index];
416
417 if (uMsg == WM_CHAR || uMsg == WM_KEYDOWN) {
418 len = GetWindowTextLengthW (hwnd);
419 SendMessageW(hwnd, EM_GETSEL, (WPARAM)&startsel, (LPARAM)&endsel);
420 }
421 switch (uMsg) {
422 case WM_CHAR:
423 if(isdigit(c)) {
424 if(len == 2 && startsel==endsel && endsel==len) {
425 /* process the digit press before we check the field */
426 int return_val = CallWindowProcW (part->OrigProc, hwnd, uMsg, wParam, lParam);
427
428 /* if the field value was changed stay at the current field */
429 if(!IPADDRESS_ConstrainField(infoPtr, index))
430 IPADDRESS_GotoNextField (infoPtr, index, POS_DEFAULT);
431
432 return return_val;
433 } else if (len == 3 && startsel==endsel && endsel==len)
434 IPADDRESS_GotoNextField (infoPtr, index, POS_SELALL);
435 else if (len < 3) break;
436 } else if(c == '.' || c == ' ') {
437 if(len && startsel==endsel && startsel != 0) {
438 IPADDRESS_GotoNextField(infoPtr, index, POS_SELALL);
439 }
440 } else if (c == VK_BACK) break;
441 return 0;
442
443 case WM_KEYDOWN:
444 switch(c) {
445 case VK_RIGHT:
446 if(startsel==endsel && startsel==len) {
447 IPADDRESS_GotoNextField(infoPtr, index, POS_LEFT);
448 return 0;
449 }
450 break;
451 case VK_LEFT:
452 if(startsel==0 && startsel==endsel && index > 0) {
453 IPADDRESS_GotoNextField(infoPtr, index - 2, POS_RIGHT);
454 return 0;
455 }
456 break;
457 case VK_BACK:
458 if(startsel==endsel && startsel==0 && index > 0) {
459 IPPART_INFO *prev = &infoPtr->Part[index-1];
460 WCHAR val[10];
461
462 if(GetWindowTextW(prev->EditHwnd, val, 5)) {
463 val[lstrlenW(val) - 1] = 0;
464 SetWindowTextW(prev->EditHwnd, val);
465 }
466
467 IPADDRESS_GotoNextField(infoPtr, index - 2, POS_RIGHT);
468 return 0;
469 }
470 break;
471 }
472 break;
473 case WM_KILLFOCUS:
474 if (IPADDRESS_GetPartIndex(infoPtr, (HWND)wParam) < 0)
475 IPADDRESS_Notify(infoPtr, EN_KILLFOCUS);
476 break;
477 case WM_SETFOCUS:
478 if (IPADDRESS_GetPartIndex(infoPtr, (HWND)wParam) < 0)
479 IPADDRESS_Notify(infoPtr, EN_SETFOCUS);
480 break;
481 }
482 return CallWindowProcW (part->OrigProc, hwnd, uMsg, wParam, lParam);
483}
484
485
486static LRESULT WINAPI
487IPADDRESS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
488{
489 IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd);
490
491 TRACE("(hwnd=0x%x msg=0x%x wparam=0x%x lparam=0x%lx)\n", hwnd, uMsg, wParam, lParam);
492
493 if (!infoPtr && (uMsg != WM_CREATE))
494 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
495
496 switch (uMsg)
497 {
498 case WM_CREATE:
499 return IPADDRESS_Create (hwnd);
500
501 case WM_DESTROY:
502 return IPADDRESS_Destroy (infoPtr);
503
504 case WM_PAINT:
505 return IPADDRESS_Paint (infoPtr, (HDC)wParam);
506
507 case WM_COMMAND:
508 switch(wParam >> 16) {
509 case EN_CHANGE:
510 IPADDRESS_Notify(infoPtr, EN_CHANGE);
511 break;
512 case EN_KILLFOCUS:
513 IPADDRESS_ConstrainField(infoPtr, IPADDRESS_GetPartIndex(infoPtr, (HWND)lParam));
514 break;
515 }
516 break;
517
518 case IPM_CLEARADDRESS:
519 IPADDRESS_ClearAddress (infoPtr);
520 break;
521
522 case IPM_SETADDRESS:
523 return IPADDRESS_SetAddress (infoPtr, (DWORD)lParam);
524
525 case IPM_GETADDRESS:
526 return IPADDRESS_GetAddress (infoPtr, (LPDWORD)lParam);
527
528 case IPM_SETRANGE:
529 return IPADDRESS_SetRange (infoPtr, (int)wParam, (WORD)lParam);
530
531 case IPM_SETFOCUS:
532 IPADDRESS_SetFocusToField (infoPtr, (int)wParam);
533 break;
534
535 case IPM_ISBLANK:
536 return IPADDRESS_IsBlank (infoPtr);
537
538 default:
539 if (uMsg >= WM_USER)
540 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
541 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
542 }
543 return 0;
544}
545
546
547void IPADDRESS_Register (void)
548{
549 WNDCLASSW wndClass;
550
551 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
552 wndClass.style = CS_GLOBALCLASS;
553 wndClass.lpfnWndProc = (WNDPROC)IPADDRESS_WindowProc;
554 wndClass.cbClsExtra = 0;
555 wndClass.cbWndExtra = sizeof(IPADDRESS_INFO *);
556 wndClass.hCursor = LoadCursorW (0, IDC_IBEAMW);
557 wndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
558 wndClass.lpszClassName = WC_IPADDRESSW;
559
560 RegisterClassW (&wndClass);
561}
562
563
564void IPADDRESS_Unregister (void)
565{
566 UnregisterClassW (WC_IPADDRESSW, (HINSTANCE)NULL);
567}
Note: See TracBrowser for help on using the repository browser.