Avoid excessive heap memory reallocation when generating EMF
[wine] / dlls / comctl32 / ipaddress.c
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  * NOTE
25  * 
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.
28  * 
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.
32  * 
33  */
34
35 #include <ctype.h>
36 #include <stdlib.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 #include "windef.h"
42 #include "winbase.h"
43 #include "wingdi.h"
44 #include "winuser.h"
45 #include "winnls.h"
46 #include "commctrl.h"
47 #include "comctl32.h"
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(ipaddress);
52
53 typedef struct
54 {
55     HWND     EditHwnd;
56     INT      LowerLimit;
57     INT      UpperLimit;
58     WNDPROC  OrigProc;
59 } IPPART_INFO;
60
61 typedef struct
62 {
63     HWND        Self;
64     IPPART_INFO Part[4];
65 } IPADDRESS_INFO;
66
67 #define POS_DEFAULT     0
68 #define POS_LEFT        1
69 #define POS_RIGHT       2
70 #define POS_SELALL      3
71
72 #define IP_SUBCLASS_PROP "CCIP32SubclassInfo"
73 #define IPADDRESS_GetInfoPtr(hwnd) ((IPADDRESS_INFO *)GetWindowLongW (hwnd, 0))
74
75
76 static LRESULT CALLBACK
77 IPADDRESS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
78
79 static LRESULT IPADDRESS_Notify (IPADDRESS_INFO *infoPtr, UINT command)
80 {
81     HWND hwnd = infoPtr->Self;
82
83     TRACE("(command=%x)\n", command);
84
85     return SendMessageW (GetParent (hwnd), WM_COMMAND,
86              MAKEWPARAM (GetWindowLongW (hwnd, GWL_ID), command), (LPARAM)hwnd);
87 }
88
89 static INT IPADDRESS_IPNotify (IPADDRESS_INFO *infoPtr, INT field, INT value)
90 {
91     NMIPADDRESS nmip;
92
93     TRACE("(field=%x, value=%d)\n", field, value);
94
95     nmip.hdr.hwndFrom = infoPtr->Self;
96     nmip.hdr.idFrom   = GetWindowLongW (infoPtr->Self, GWL_ID);
97     nmip.hdr.code     = IPN_FIELDCHANGED;
98
99     nmip.iField = field;
100     nmip.iValue = value;
101
102     SendMessageW (GetParent (infoPtr->Self), WM_NOTIFY,
103                   (WPARAM)nmip.hdr.idFrom, (LPARAM)&nmip);
104
105     TRACE("<-- %d\n", nmip.iValue);
106
107     return nmip.iValue;
108 }
109
110
111 static int IPADDRESS_GetPartIndex(IPADDRESS_INFO *infoPtr, HWND hwnd)
112 {
113     int i;
114
115     TRACE("(hwnd=%p)\n", hwnd);
116
117     for (i = 0; i < 4; i++)
118         if (infoPtr->Part[i].EditHwnd == hwnd) return i;
119
120     ERR("We subclassed the wrong window! (hwnd=%p)\n", hwnd);
121     return -1;
122 }
123
124
125 static LRESULT IPADDRESS_Draw (IPADDRESS_INFO *infoPtr, HDC hdc)
126 {
127     RECT rect, rcPart;
128     POINT pt;
129     int i;
130
131     TRACE("\n");
132
133     GetClientRect (infoPtr->Self, &rect);
134     DrawEdge (hdc, &rect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
135
136     for (i = 0; i < 3; i++) {
137         GetWindowRect (infoPtr->Part[i].EditHwnd, &rcPart);
138         pt.x = rcPart.right;
139         ScreenToClient(infoPtr->Self, &pt);
140         rect.left = pt.x;
141         GetWindowRect (infoPtr->Part[i+1].EditHwnd, &rcPart);
142         pt.x = rcPart.left;
143         ScreenToClient(infoPtr->Self, &pt);
144         rect.right = pt.x;
145         DrawTextA(hdc, ".", 1, &rect, DT_SINGLELINE | DT_CENTER | DT_BOTTOM);
146     }
147
148     return 0;
149 }
150
151
152 static LRESULT IPADDRESS_Create (HWND hwnd)
153 {
154     IPADDRESS_INFO *infoPtr;
155     RECT rcClient, edit;
156     int i, fieldsize;
157     WCHAR EDIT[] = { 'E', 'd', 'i', 't', 0 };
158
159     TRACE("\n");
160
161     SetWindowLongW (hwnd, GWL_STYLE,
162                     GetWindowLongW(hwnd, GWL_STYLE) & ~WS_BORDER);
163
164     infoPtr = (IPADDRESS_INFO *)Alloc (sizeof(IPADDRESS_INFO));
165     if (!infoPtr) return -1;
166     SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
167
168     GetClientRect (hwnd, &rcClient);
169
170     fieldsize = (rcClient.right - rcClient.left) / 4;
171
172     edit.top    = rcClient.top + 2;
173     edit.bottom = rcClient.bottom - 2;
174
175     infoPtr->Self = hwnd;
176
177     for (i = 0; i < 4; i++) {
178         IPPART_INFO* part = &infoPtr->Part[i];
179
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;
184         part->EditHwnd =
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);
193     }
194
195     return 0;
196 }
197
198
199 static LRESULT IPADDRESS_Destroy (IPADDRESS_INFO *infoPtr)
200 {
201     int i;
202
203     TRACE("\n");
204
205     for (i = 0; i < 4; i++) {
206         IPPART_INFO* part = &infoPtr->Part[i];
207         SetWindowLongW (part->EditHwnd, GWL_WNDPROC, (LONG)part->OrigProc);
208     }
209
210     SetWindowLongW (infoPtr->Self, 0, 0);
211     Free (infoPtr);
212     return 0;
213 }
214
215
216 static LRESULT IPADDRESS_Paint (IPADDRESS_INFO *infoPtr, HDC hdc)
217 {
218     PAINTSTRUCT ps;
219
220     TRACE("\n");
221
222     if (hdc) return IPADDRESS_Draw (infoPtr, hdc);
223
224     hdc = BeginPaint (infoPtr->Self, &ps);
225     IPADDRESS_Draw (infoPtr, hdc);
226     EndPaint (infoPtr->Self, &ps);
227     return 0;
228 }
229
230
231 static BOOL IPADDRESS_IsBlank (IPADDRESS_INFO *infoPtr)
232 {
233     int i;
234
235     TRACE("\n");
236
237     for (i = 0; i < 4; i++)
238         if (GetWindowTextLengthW (infoPtr->Part[i].EditHwnd)) return FALSE;
239
240     return TRUE;
241 }
242
243
244 static int IPADDRESS_GetAddress (IPADDRESS_INFO *infoPtr, LPDWORD ip_address)
245 {
246     WCHAR field[5];
247     int i, invalid = 0;
248     DWORD ip_addr = 0;
249
250     TRACE("\n");
251
252     for (i = 0; i < 4; i++) {
253         ip_addr *= 256;
254         if (GetWindowTextW (infoPtr->Part[i].EditHwnd, field, 4))
255             ip_addr += atolW(field);
256         else
257             invalid++;
258     }
259     *ip_address = ip_addr;
260
261     return 4 - invalid;
262 }
263
264
265 static BOOL IPADDRESS_SetRange (IPADDRESS_INFO *infoPtr, int index, WORD range)
266 {
267     TRACE("\n");
268
269     if ( (index < 0) || (index > 3) ) return FALSE;
270
271     infoPtr->Part[index].LowerLimit = range & 0xFF;
272     infoPtr->Part[index].UpperLimit = (range >> 8)  & 0xFF;
273
274     return TRUE;
275 }
276
277
278 static void IPADDRESS_ClearAddress (IPADDRESS_INFO *infoPtr)
279 {
280     WCHAR nil[1] = { 0 };
281     int i;
282
283     TRACE("\n");
284
285     for (i = 0; i < 4; i++)
286         SetWindowTextW (infoPtr->Part[i].EditHwnd, nil);
287 }
288
289
290 static LRESULT IPADDRESS_SetAddress (IPADDRESS_INFO *infoPtr, DWORD ip_address)
291 {
292     WCHAR buf[20], fmt[] = { '%', 'd', 0 };
293     int i;
294
295     TRACE("\n");
296
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);
304         }
305         ip_address >>= 8;
306     }
307
308     return TRUE;
309 }
310
311
312 static void IPADDRESS_SetFocusToField (IPADDRESS_INFO *infoPtr, INT index)
313 {
314     TRACE("(index=%d)\n", index);
315
316     if (index > 3) {
317         for (index = 0; index < 4; index++)
318             if (!GetWindowTextLengthW(infoPtr->Part[index].EditHwnd)) break;
319     }
320     if (index < 9 || index > 3) index = 0;
321
322     SetFocus (infoPtr->Part[index].EditHwnd);
323 }
324
325
326 static BOOL IPADDRESS_ConstrainField (IPADDRESS_INFO *infoPtr, int currentfield)
327 {
328     IPPART_INFO *part = &infoPtr->Part[currentfield];
329     WCHAR field[10], fmt[] = { '%', 'd', 0 };
330     int curValue, newValue;
331
332     TRACE("(currentfield=%d)\n", currentfield);
333
334     if (currentfield < 0 || currentfield > 3) return FALSE;
335
336     if (!GetWindowTextW (part->EditHwnd, field, 4)) return FALSE;
337
338     curValue = atoiW(field);
339     TRACE("  curValue=%d\n", curValue);
340
341     newValue = IPADDRESS_IPNotify(infoPtr, currentfield, curValue);
342     TRACE("  newValue=%d\n", newValue);
343
344     if (newValue < part->LowerLimit) newValue = part->LowerLimit;
345     if (newValue > part->UpperLimit) newValue = part->UpperLimit;
346
347     if (newValue == curValue) return FALSE;
348
349     wsprintfW (field, fmt, newValue);
350     TRACE("  field='%s'\n", debugstr_w(field));
351     return SetWindowTextW (part->EditHwnd, field);
352 }
353
354
355 static BOOL IPADDRESS_GotoNextField (IPADDRESS_INFO *infoPtr, int cur, int sel)
356 {
357     TRACE("\n");
358
359     if(cur >= -1 && cur < 4) {
360         IPADDRESS_ConstrainField(infoPtr, cur);
361
362         if(cur < 3) {
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)
370                     end = -1;
371                 SendMessageW(next->EditHwnd, EM_SETSEL, start, end);
372             }
373             return TRUE;
374         }
375
376     }
377     return FALSE;
378 }
379
380
381 /*
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)
385  *
386  * spacebar: same behavior as period
387  *
388  * alpha characters: completely ignored
389  *
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
395  *
396  * tab: change focus from the current ipaddress control to the next
397  *      control in the tab order
398  *
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
403  *
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
408  *
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
414  */
415 LRESULT CALLBACK
416 IPADDRESS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
417 {
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;
422     IPPART_INFO *part;
423
424     TRACE("(hwnd=%p msg=0x%x wparam=0x%x lparam=0x%lx)\n", hwnd, uMsg, wParam, lParam);
425
426     if ( (index = IPADDRESS_GetPartIndex(infoPtr, hwnd)) < 0) return 0;
427     part = &infoPtr->Part[index];
428
429     if (uMsg == WM_CHAR || uMsg == WM_KEYDOWN) {
430         len = GetWindowTextLengthW (hwnd);
431         SendMessageW(hwnd, EM_GETSEL, (WPARAM)&startsel, (LPARAM)&endsel);
432     }
433     switch (uMsg) {
434         case WM_CHAR:
435             if(isdigit(c)) {
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);
439
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);
443
444                     return return_val;
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);
451                 }
452             } else if (c == VK_BACK) break;
453             return 0;
454
455         case WM_KEYDOWN:
456             switch(c) {
457                 case VK_RIGHT:
458                     if(startsel==endsel && startsel==len) {
459                         IPADDRESS_GotoNextField(infoPtr, index, POS_LEFT);
460                         return 0;
461                     }
462                     break;
463                 case VK_LEFT:
464                     if(startsel==0 && startsel==endsel && index > 0) {
465                         IPADDRESS_GotoNextField(infoPtr, index - 2, POS_RIGHT);
466                         return 0;
467                     }
468                     break;
469                 case VK_BACK:
470                     if(startsel==endsel && startsel==0 && index > 0) {
471                         IPPART_INFO *prev = &infoPtr->Part[index-1];
472                         WCHAR val[10];
473
474                         if(GetWindowTextW(prev->EditHwnd, val, 5)) {
475                             val[lstrlenW(val) - 1] = 0;
476                             SetWindowTextW(prev->EditHwnd, val);
477                         }
478
479                         IPADDRESS_GotoNextField(infoPtr, index - 2, POS_RIGHT);
480                         return 0;
481                     }
482                     break;
483             }
484             break;
485         case WM_KILLFOCUS:
486             if (IPADDRESS_GetPartIndex(infoPtr, (HWND)wParam) < 0)
487                 IPADDRESS_Notify(infoPtr, EN_KILLFOCUS);
488             break;
489         case WM_SETFOCUS:
490             if (IPADDRESS_GetPartIndex(infoPtr, (HWND)wParam) < 0)
491                 IPADDRESS_Notify(infoPtr, EN_SETFOCUS);
492             break;
493     }
494     return CallWindowProcW (part->OrigProc, hwnd, uMsg, wParam, lParam);
495 }
496
497
498 static LRESULT WINAPI
499 IPADDRESS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
500 {
501     IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr (hwnd);
502
503     TRACE("(hwnd=%p msg=0x%x wparam=0x%x lparam=0x%lx)\n", hwnd, uMsg, wParam, lParam);
504
505     if (!infoPtr && (uMsg != WM_CREATE))
506         return DefWindowProcW (hwnd, uMsg, wParam, lParam);
507
508     switch (uMsg)
509     {
510         case WM_CREATE:
511             return IPADDRESS_Create (hwnd);
512
513         case WM_DESTROY:
514             return IPADDRESS_Destroy (infoPtr);
515
516         case WM_PAINT:
517             return IPADDRESS_Paint (infoPtr, (HDC)wParam);
518
519         case WM_COMMAND:
520             switch(wParam >> 16) {
521                 case EN_CHANGE:
522                     IPADDRESS_Notify(infoPtr, EN_CHANGE);
523                     break;
524                 case EN_KILLFOCUS:
525                     IPADDRESS_ConstrainField(infoPtr, IPADDRESS_GetPartIndex(infoPtr, (HWND)lParam));
526                     break;
527             }
528             break;
529
530         case IPM_CLEARADDRESS:
531             IPADDRESS_ClearAddress (infoPtr);
532             break;
533
534         case IPM_SETADDRESS:
535             return IPADDRESS_SetAddress (infoPtr, (DWORD)lParam);
536
537         case IPM_GETADDRESS:
538             return IPADDRESS_GetAddress (infoPtr, (LPDWORD)lParam);
539
540         case IPM_SETRANGE:
541             return IPADDRESS_SetRange (infoPtr, (int)wParam, (WORD)lParam);
542
543         case IPM_SETFOCUS:
544             IPADDRESS_SetFocusToField (infoPtr, (int)wParam);
545             break;
546
547         case IPM_ISBLANK:
548             return IPADDRESS_IsBlank (infoPtr);
549
550         default:
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);
554     }
555     return 0;
556 }
557
558
559 void IPADDRESS_Register (void)
560 {
561     WNDCLASSW wndClass;
562
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;
571
572     RegisterClassW (&wndClass);
573 }
574
575
576 void IPADDRESS_Unregister (void)
577 {
578     UnregisterClassW (WC_IPADDRESSW, NULL);
579 }