-Fixed MESSAGE functions that were thunking down to 16 bits implementation.
[wine] / dlls / comctl32 / ipaddress.c
1 /* IP Address control
2  *
3  * Copyright 1998 Eric Kohl
4  * Copyright 1998 Alex Priem <alexp@sci.kun.nl>
5  *
6  * NOTES
7
8  *
9  * TODO:
10  *    -Check ranges when changing field-focus.
11  *        -Check all notifications/behavior.
12  *    -Optimization: include lpipsi in IPADDRESS_INFO.
13  *        -CurrentFocus: field that has focus at moment of processing.
14  *        -connect Rect32 rcClient.
15  *        -handle right and left arrows correctly. Boring.
16  *        -split GotoNextField in CheckField and GotoNextField.
17  *        -check ipaddress.cpp for missing features.
18  *    -refresh: draw '.' instead of setpixel.
19  *        -handle VK_ESCAPE.
20  */
21
22 #include <ctype.h>
23 #include <stdlib.h>
24
25 #include "windows.h"
26 #include "win.h"
27 #include "commctrl.h"
28 #include "ipaddress.h"
29 #include "heap.h"
30 #include "debug.h"
31
32
33 #define IPADDRESS_GetInfoPtr(wndPtr) ((IPADDRESS_INFO *)wndPtr->wExtra[0])
34
35
36 static BOOL32 
37 IPADDRESS_SendNotify (WND *wndPtr, UINT32 command);
38 static BOOL32 
39 IPADDRESS_SendIPAddressNotify (WND *wndPtr, UINT32 field, BYTE newValue);
40
41
42 /* property name of tooltip window handle */
43 #define IP_SUBCLASS_PROP "CCIP32SubclassInfo"
44
45
46 static LRESULT CALLBACK
47 IPADDRESS_SubclassProc (HWND32 hwnd, UINT32 uMsg, WPARAM32 wParam, LPARAM lParam);
48
49
50
51
52 static VOID
53 IPADDRESS_Refresh (WND *wndPtr, HDC32 hdc)
54 {
55         RECT32 rcClient;
56         HBRUSH32 hbr;
57         COLORREF clr=GetSysColor32 (COLOR_3DDKSHADOW);
58     int i,x,fieldsize;
59
60     GetClientRect32 (wndPtr->hwndSelf, &rcClient);
61         hbr =  CreateSolidBrush32 (RGB(255,255,255));
62     DrawEdge32 (hdc, &rcClient, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
63         FillRect32 (hdc, &rcClient, hbr);
64     DeleteObject32 (hbr);
65
66         x=rcClient.left;
67         fieldsize=(rcClient.right-rcClient.left) /4;
68
69         for (i=0; i<3; i++) {           /* Should draw text "." here */
70                 x+=fieldsize;
71                 SetPixel32 (hdc, x,   13, clr);
72                 SetPixel32 (hdc, x,   14, clr);
73                 SetPixel32 (hdc, x+1, 13, clr);
74                 SetPixel32 (hdc, x+1, 14, clr);
75
76         }
77
78 }
79
80
81
82
83
84 static LRESULT
85 IPADDRESS_Create (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
86 {
87     IPADDRESS_INFO *infoPtr;
88         RECT32 rcClient, edit;
89         int i,fieldsize;
90         LPIP_SUBCLASS_INFO lpipsi;
91         
92
93     infoPtr = (IPADDRESS_INFO *)COMCTL32_Alloc (sizeof(IPADDRESS_INFO));
94     wndPtr->wExtra[0] = (DWORD)infoPtr;
95
96         if (infoPtr == NULL) {
97         ERR (ipaddress, "could not allocate info memory!\n");
98         return 0;
99     }
100
101     GetClientRect32 (wndPtr->hwndSelf, &rcClient);
102
103         fieldsize=(rcClient.right-rcClient.left) /4;
104
105         edit.top   =rcClient.top+2;
106         edit.bottom=rcClient.bottom-2;
107
108         lpipsi=(LPIP_SUBCLASS_INFO)
109                         GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
110         if (lpipsi == NULL)  {
111                 lpipsi= (LPIP_SUBCLASS_INFO) COMCTL32_Alloc (sizeof(IP_SUBCLASS_INFO));
112                 lpipsi->wndPtr=wndPtr;
113                 lpipsi->uRefCount++;
114                 SetProp32A ((HWND32)wndPtr->hwndSelf, IP_SUBCLASS_PROP,
115                                         (HANDLE32)lpipsi);
116 /*              infoPtr->lpipsi= lpipsi; */
117         } else 
118                 WARN (ipaddress,"IP-create called twice\n");
119         
120         for (i=0; i<=3; i++) {
121                 infoPtr->LowerLimit[i]=0;
122                 infoPtr->UpperLimit[i]=255;
123                 edit.left=rcClient.left+i*fieldsize+3;
124                 edit.right=rcClient.left+(i+1)*fieldsize-2;
125                 lpipsi->hwndIP[i]= CreateWindow32A ("edit", NULL, 
126                                 WS_CHILD | WS_VISIBLE | ES_LEFT,
127                                 edit.left, edit.top, edit.right-edit.left, edit.bottom-edit.top,
128                                 wndPtr->hwndSelf, (HMENU32) 1, wndPtr->hInstance, NULL);
129                 lpipsi->wpOrigProc[i]= (WNDPROC32)
130                                         SetWindowLong32A (lpipsi->hwndIP[i],GWL_WNDPROC, (LONG)
131                                         IPADDRESS_SubclassProc);
132                 SetProp32A ((HWND32)lpipsi->hwndIP[i], IP_SUBCLASS_PROP,
133                                         (HANDLE32)lpipsi);
134         }
135
136         lpipsi->infoPtr= infoPtr;
137
138     return 0;
139 }
140
141
142 static LRESULT
143 IPADDRESS_Destroy (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
144 {
145         int i;
146     IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr(wndPtr);
147         LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
148             GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
149
150         for (i=0; i<=3; i++) {
151                 SetWindowLong32A ((HWND32)lpipsi->hwndIP[i], GWL_WNDPROC,
152                   (LONG)lpipsi->wpOrigProc[i]);
153         }
154
155     COMCTL32_Free (infoPtr);
156     return 0;
157 }
158
159
160 static LRESULT
161 IPADDRESS_KillFocus (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
162 {
163     HDC32 hdc;
164
165         TRACE (ipaddress,"\n");
166     hdc = GetDC32 (wndPtr->hwndSelf);
167     IPADDRESS_Refresh (wndPtr, hdc);
168     ReleaseDC32 (wndPtr->hwndSelf, hdc);
169
170         IPADDRESS_SendIPAddressNotify (wndPtr, 0, 0);  /* FIXME: should use -1 */
171         IPADDRESS_SendNotify (wndPtr, EN_KILLFOCUS);       
172     InvalidateRect32 (wndPtr->hwndSelf, NULL, TRUE);
173
174     return 0;
175 }
176
177
178 static LRESULT
179 IPADDRESS_Paint (WND *wndPtr, WPARAM32 wParam)
180 {
181     HDC32 hdc;
182     PAINTSTRUCT32 ps;
183
184     hdc = wParam==0 ? BeginPaint32 (wndPtr->hwndSelf, &ps) : (HDC32)wParam;
185     IPADDRESS_Refresh (wndPtr, hdc);
186     if(!wParam)
187         EndPaint32 (wndPtr->hwndSelf, &ps);
188     return 0;
189 }
190
191
192 static LRESULT
193 IPADDRESS_SetFocus (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
194 {
195     HDC32 hdc;
196
197         TRACE (ipaddress,"\n");
198
199     hdc = GetDC32 (wndPtr->hwndSelf);
200     IPADDRESS_Refresh (wndPtr, hdc);
201     ReleaseDC32 (wndPtr->hwndSelf, hdc);
202
203     return 0;
204 }
205
206
207 static LRESULT
208 IPADDRESS_Size (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
209 {
210     /* IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr(wndPtr); */
211         TRACE (ipaddress,"\n");
212     return 0;
213 }
214
215
216 static BOOL32
217 IPADDRESS_SendNotify (WND *wndPtr, UINT32 command)
218
219 {
220     TRACE (ipaddress, "%x\n",command);
221     return (BOOL32)SendMessage32A (GetParent32 (wndPtr->hwndSelf), WM_COMMAND,
222               MAKEWPARAM (wndPtr->wIDmenu,command), (LPARAM) wndPtr->hwndSelf);
223 }
224
225
226 static BOOL32
227 IPADDRESS_SendIPAddressNotify (WND *wndPtr, UINT32 field, BYTE newValue)
228
229 {
230         NMIPADDRESS nmip;
231
232     TRACE (ipaddress, "%x %x\n",field,newValue);
233     nmip.hdr.hwndFrom = wndPtr->hwndSelf;
234     nmip.hdr.idFrom   = wndPtr->wIDmenu;
235     nmip.hdr.code     = IPN_FIELDCHANGED;
236
237         nmip.iField=field;
238         nmip.iValue=newValue;
239
240     return (BOOL32)SendMessage32A (GetParent32 (wndPtr->hwndSelf), WM_NOTIFY,
241                                    (WPARAM32)wndPtr->wIDmenu, (LPARAM)&nmip);
242 }
243
244
245
246
247 static LRESULT
248 IPADDRESS_ClearAddress (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
249 {
250         int i;
251         HDC32 hdc;
252         char buf[1];
253         LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
254             GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
255
256         TRACE (ipaddress,"\n");
257
258         buf[0]=0;
259         for (i=0; i<=3; i++) 
260                 SetWindowText32A (lpipsi->hwndIP[i],buf);
261         
262         hdc = GetDC32 (wndPtr->hwndSelf);
263     IPADDRESS_Refresh (wndPtr, hdc);
264     ReleaseDC32 (wndPtr->hwndSelf, hdc);
265         return 0;
266 }
267
268 static LRESULT
269 IPADDRESS_IsBlank (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
270 {
271  int i;
272  char buf[20];
273  LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
274             GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
275
276  TRACE (ipaddress,"\n");
277
278  for (i=0; i<=3; i++) {
279                 GetWindowText32A (lpipsi->hwndIP[i],buf,5);
280                 if (buf[0]) return 0;
281         }
282
283  return 1;
284 }
285
286 static LRESULT
287 IPADDRESS_GetAddress (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
288 {
289  char field[20];
290  int i,valid,fieldvalue;
291  DWORD ip_addr;
292  IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr(wndPtr);
293  LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
294             GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
295
296  TRACE (ipaddress,"\n");
297
298  valid=0;
299  ip_addr=0;
300  for (i=0; i<=3; i++) {
301                 GetWindowText32A (lpipsi->hwndIP[i],field,4);
302                 ip_addr*=256;
303                 if (field[0]) {
304                         field[3]=0;
305                         fieldvalue=atoi(field);
306                         if (fieldvalue<infoPtr->LowerLimit[i]) 
307                                 fieldvalue=infoPtr->LowerLimit[i];
308                         if (fieldvalue>infoPtr->UpperLimit[i]) 
309                                 fieldvalue=infoPtr->UpperLimit[i];
310                         ip_addr+=fieldvalue;
311                         valid++;
312                 }
313  }
314
315  *(LPDWORD) lParam=ip_addr;
316
317  return valid;
318 }
319
320 static LRESULT
321 IPADDRESS_SetRange (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
322
323 {
324     IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr(wndPtr);
325         INT32 index;
326         
327         TRACE (ipaddress,"\n");
328
329         index=(INT32) wParam;
330         if ((index<0) || (index>3)) return 0;
331
332         infoPtr->LowerLimit[index]=lParam & 0xff;
333         infoPtr->UpperLimit[index]=(lParam >>8)  & 0xff;
334         return 1;
335 }
336
337 static LRESULT
338 IPADDRESS_SetAddress (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
339 {
340         HDC32 hdc;
341     IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr(wndPtr);
342         LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
343             GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
344         int i,ip_address,value;
345     char buf[20];
346
347         TRACE (ipaddress,"\n");
348         ip_address=(DWORD) lParam;
349
350         for (i=3; i>=0; i--) {
351                 value=ip_address & 0xff;
352                 if ((value>=infoPtr->LowerLimit[i]) && (value<=infoPtr->UpperLimit[i])) 
353                         {
354                          sprintf (buf,"%d",value);
355                          SetWindowText32A (lpipsi->hwndIP[i],buf);
356                          IPADDRESS_SendNotify (wndPtr, EN_CHANGE);
357                 }
358                 ip_address/=256;
359         }
360
361         hdc = GetDC32 (wndPtr->hwndSelf);               /* & send notifications */
362     IPADDRESS_Refresh (wndPtr, hdc);
363     ReleaseDC32 (wndPtr->hwndSelf, hdc);
364
365  return TRUE;
366 }
367
368
369
370
371 static LRESULT
372 IPADDRESS_SetFocusToField (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
373 {
374     /* IPADDRESS_INFO *infoPtr = IPADDRESS_GetInfoPtr(wndPtr); */
375         LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
376             GetProp32A ((HWND32)wndPtr->hwndSelf,IP_SUBCLASS_PROP);
377         INT32 index;
378
379         index=(INT32) wParam;
380         TRACE (ipaddress," %d\n", index);
381         if ((index<0) || (index>3)) return 0;
382         
383         SetFocus32 (lpipsi->hwndIP[index]);
384         
385     return 1;
386 }
387
388
389 static LRESULT
390 IPADDRESS_LButtonDown (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
391 {
392     TRACE (ipaddress, "\n");
393
394         SetFocus32 (wndPtr->hwndSelf);
395         IPADDRESS_SendNotify (wndPtr, EN_SETFOCUS);
396         IPADDRESS_SetFocusToField (wndPtr, 0, 0);
397
398         return TRUE;
399 }
400
401
402
403 /* tab/shift-tab: IPN_FIELDCHANGED, lose focus.
404    dot, space,right arrow:      set focus to next child edit.
405    numerics (0..9), control characters: forward to default edit control 
406    other characters: dropped
407 */
408    
409
410
411
412 static int
413 IPADDRESS_GotoNextField (LPIP_SUBCLASS_INFO lpipsi, int currentfield)
414 {
415  int newField,fieldvalue;
416  char field[20];
417  IPADDRESS_INFO *infoPtr=lpipsi->infoPtr;
418
419  TRACE (ipaddress,"\n");
420  GetWindowText32A (lpipsi->hwndIP[currentfield],field,4);
421  if (field[0]) {
422         field[3]=0;     
423         newField=-1;
424         fieldvalue=atoi(field);
425         if (fieldvalue<infoPtr->LowerLimit[currentfield]) 
426                 newField=infoPtr->LowerLimit[currentfield];
427         if (fieldvalue>infoPtr->UpperLimit[currentfield])
428                 newField=infoPtr->UpperLimit[currentfield];
429         if (newField>=0) {
430                 sprintf (field,"%d",newField);
431                 SetWindowText32A (lpipsi->hwndIP[currentfield], field);
432                 return 1;
433         }
434  }
435
436  if (currentfield<3) 
437                 SetFocus32 (lpipsi->hwndIP[currentfield+1]);
438  return 0;
439 }
440
441
442 LRESULT CALLBACK
443 IPADDRESS_SubclassProc (HWND32 hwnd, UINT32 uMsg, WPARAM32 wParam, LPARAM lParam)
444 {
445  int i,l,index;
446  IPADDRESS_INFO *infoPtr;
447  LPIP_SUBCLASS_INFO lpipsi=(LPIP_SUBCLASS_INFO)
448             GetProp32A ((HWND32)hwnd,IP_SUBCLASS_PROP); 
449
450  infoPtr = lpipsi->infoPtr;
451  index=0;             /* FIXME */
452  for (i=0; i<=3; i++) 
453                 if (lpipsi->hwndIP[i]==hwnd) index=i;
454
455  switch (uMsg) {
456         case WM_CHAR: break;
457         case WM_KEYDOWN: {
458                         char c=(char) wParam;
459                         if (c==VK_TAB) {
460                                 HWND32 pwnd;
461                                 int shift;
462                                 shift = GetKeyState32(VK_SHIFT) & 0x8000;
463                                 if (shift)
464                                         pwnd=GetNextDlgTabItem32 (GetParent32 (hwnd), 0, TRUE);
465                                 else
466                                         pwnd=GetNextDlgTabItem32 (GetParent32 (hwnd), 0, FALSE);
467                                 if (pwnd) SetFocus32 (pwnd);
468                                 break;
469                         }
470                         
471                         if ((c==' ') || (c=='.') || (c==VK_RIGHT)) {
472                                 IPADDRESS_GotoNextField (lpipsi,index);
473                                 wParam=0;
474                                 lParam=0;
475                                 break;
476                         }
477                         if (c==VK_LEFT) {
478                                 
479                         }
480                         if (c==VK_RETURN) {
481                         }
482                         if (((c>='0') && (c<='9')) || (iscntrl(c))) {
483                                 l=GetWindowTextLength32A (lpipsi->hwndIP[index]);
484                                 if (l==3) 
485                                         if (IPADDRESS_GotoNextField (lpipsi,index)) {
486                                                 wParam=0;
487                                                 lParam=0;
488                                         }
489                                 break;
490                         }
491         
492                         wParam=0;
493                         lParam=0;
494                         break;
495                 }
496  }
497
498  return CallWindowProc32A (lpipsi->wpOrigProc[index], hwnd, uMsg, wParam, lParam);
499 }
500
501 LRESULT WINAPI
502 IPADDRESS_WindowProc (HWND32 hwnd, UINT32 uMsg, WPARAM32 wParam, LPARAM lParam)
503 {
504     WND *wndPtr = WIN_FindWndPtr(hwnd);
505
506     switch (uMsg)
507     {
508         case IPM_CLEARADDRESS:
509                 return IPADDRESS_ClearAddress (wndPtr, wParam, lParam);
510
511         case IPM_SETADDRESS:
512             return IPADDRESS_SetAddress (wndPtr, wParam, lParam);
513
514         case IPM_GETADDRESS:
515             return IPADDRESS_GetAddress (wndPtr, wParam, lParam);
516
517         case IPM_SETRANGE:
518             return IPADDRESS_SetRange (wndPtr, wParam, lParam);
519
520         case IPM_SETFOCUS:
521             return IPADDRESS_SetFocusToField (wndPtr, wParam, lParam);
522
523         case IPM_ISBLANK:
524                 return IPADDRESS_IsBlank (wndPtr, wParam, lParam);
525
526         case WM_CREATE:
527             return IPADDRESS_Create (wndPtr, wParam, lParam);
528
529         case WM_DESTROY:
530             return IPADDRESS_Destroy (wndPtr, wParam, lParam);
531
532         case WM_GETDLGCODE:
533             return DLGC_WANTARROWS | DLGC_WANTCHARS;
534
535         case WM_KILLFOCUS:
536             return IPADDRESS_KillFocus (wndPtr, wParam, lParam);
537
538         case WM_LBUTTONDOWN:
539         return IPADDRESS_LButtonDown (wndPtr, wParam, lParam);
540
541         case WM_PAINT:
542             return IPADDRESS_Paint (wndPtr, wParam);
543
544         case WM_SETFOCUS:
545             return IPADDRESS_SetFocus (wndPtr, wParam, lParam);
546
547         case WM_SIZE:
548             return IPADDRESS_Size (wndPtr, wParam, lParam);
549
550         default:
551             if (uMsg >= WM_USER)
552                 ERR (ipaddress, "unknown msg %04x wp=%08x lp=%08lx\n",
553                      uMsg, wParam, lParam);
554             return DefWindowProc32A (hwnd, uMsg, wParam, lParam);
555     }
556     return 0;
557 }
558
559
560 void
561 IPADDRESS_Register (void)
562 {
563     WNDCLASS32A wndClass;
564
565     if (GlobalFindAtom32A (WC_IPADDRESS32A)) return;
566
567     ZeroMemory (&wndClass, sizeof(WNDCLASS32A));
568     wndClass.style         = CS_GLOBALCLASS;
569     wndClass.lpfnWndProc   = (WNDPROC32)IPADDRESS_WindowProc;
570     wndClass.cbClsExtra    = 0;
571     wndClass.cbWndExtra    = sizeof(IPADDRESS_INFO *);
572     wndClass.hCursor       = LoadCursor32A (0, IDC_ARROW32A);
573     wndClass.hbrBackground = (HBRUSH32)(COLOR_3DFACE + 1);
574     wndClass.lpszClassName = WC_IPADDRESS32A;
575  
576     RegisterClass32A (&wndClass);
577 }
578
579 VOID
580 IPADDRESS_Unregister (VOID)
581 {
582     if (GlobalFindAtom32A (WC_IPADDRESS32A))
583     UnregisterClass32A (WC_IPADDRESS32A, (HINSTANCE32)NULL);
584 }
585