Added check whether unaligned memory access is allowed.
[wine] / windows / defwnd.c
1 /*
2  * Default window procedure
3  *
4  * Copyright 1993, 1996 Alexandre Julliard
5  *           1995 Alex Korobka
6  */
7
8 #include <string.h>
9
10 #include "win.h"
11 #include "user.h"
12 #include "heap.h"
13 #include "nonclient.h"
14 #include "winpos.h"
15 #include "dce.h"
16 #include "debugtools.h"
17 #include "spy.h"
18 #include "tweak.h"
19 #include "cache.h"
20 #include "windef.h"
21 #include "wingdi.h"
22 #include "winnls.h"
23 #include "wine/unicode.h"
24 #include "wine/winuser16.h"
25 #include "imm.h"
26
27 DEFAULT_DEBUG_CHANNEL(win);
28
29   /* bits in the dwKeyData */
30 #define KEYDATA_ALT             0x2000
31 #define KEYDATA_PREVSTATE       0x4000
32
33 static short iF10Key = 0;
34 static short iMenuSysKey = 0;
35
36 /***********************************************************************
37  *           DEFWND_HandleWindowPosChanged
38  *
39  * Handle the WM_WINDOWPOSCHANGED message.
40  */
41 static void DEFWND_HandleWindowPosChanged( WND *wndPtr, UINT flags )
42 {
43     WPARAM16 wp = SIZE_RESTORED;
44
45     if (!(flags & SWP_NOCLIENTMOVE))
46         SendMessage16( wndPtr->hwndSelf, WM_MOVE, 0,
47                     MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top));
48     if (!(flags & SWP_NOCLIENTSIZE))
49     {
50         if (wndPtr->dwStyle & WS_MAXIMIZE) wp = SIZE_MAXIMIZED;
51         else if (wndPtr->dwStyle & WS_MINIMIZE) wp = SIZE_MINIMIZED;
52
53         SendMessage16( wndPtr->hwndSelf, WM_SIZE, wp, 
54                      MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
55                             wndPtr->rectClient.bottom-wndPtr->rectClient.top));
56     }
57 }
58
59
60 /***********************************************************************
61  *           DEFWND_SetTextA
62  *
63  * Set the window text.
64  */
65 void DEFWND_SetTextA( WND *wndPtr, LPCSTR text )
66 {
67     int count;
68
69     if (!text) text = "";
70     count = MultiByteToWideChar( CP_ACP, 0, text, -1, NULL, 0 );
71
72     if (wndPtr->text) HeapFree(SystemHeap, 0, wndPtr->text);
73     if ((wndPtr->text = HeapAlloc(SystemHeap, 0, count * sizeof(WCHAR))))
74         MultiByteToWideChar( CP_ACP, 0, text, -1, wndPtr->text, count );
75     else
76         ERR("Not enough memory for window text");
77
78     wndPtr->pDriver->pSetText(wndPtr, wndPtr->text);
79 }
80
81 /***********************************************************************
82  *           DEFWND_SetTextW
83  *
84  * Set the window text.
85  */
86 void DEFWND_SetTextW( WND *wndPtr, LPCWSTR text )
87 {
88     static const WCHAR empty_string[] = {0};
89     int count;
90
91     if (!text) text = empty_string;
92     count = strlenW(text) + 1;
93
94     if (wndPtr->text) HeapFree(SystemHeap, 0, wndPtr->text);
95     if ((wndPtr->text = HeapAlloc(SystemHeap, 0, count * sizeof(WCHAR))))
96         strcpyW( wndPtr->text, text );
97     else
98         ERR("Not enough memory for window text");
99
100     wndPtr->pDriver->pSetText(wndPtr, wndPtr->text);
101 }
102
103 /***********************************************************************
104  *           DEFWND_ControlColor
105  *
106  * Default colors for control painting.
107  */
108 HBRUSH DEFWND_ControlColor( HDC hDC, UINT16 ctlType )
109 {
110     if( ctlType == CTLCOLOR_SCROLLBAR)
111     {
112         HBRUSH hb = GetSysColorBrush(COLOR_SCROLLBAR);
113         if (TWEAK_WineLook == WIN31_LOOK) {
114            SetTextColor( hDC, RGB(0, 0, 0) );
115            SetBkColor( hDC, RGB(255, 255, 255) );
116         } else {
117            COLORREF bk = GetSysColor(COLOR_3DHILIGHT);
118            SetTextColor( hDC, GetSysColor(COLOR_3DFACE));
119            SetBkColor( hDC, bk);
120
121            /* if COLOR_WINDOW happens to be the same as COLOR_3DHILIGHT 
122             * we better use 0x55aa bitmap brush to make scrollbar's background
123             * look different from the window background. 
124             */
125            if (bk == GetSysColor(COLOR_WINDOW)) {
126                return CACHE_GetPattern55AABrush();
127            }
128         }
129         UnrealizeObject( hb );
130         return hb;
131     }
132
133     SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT));
134
135     if (TWEAK_WineLook > WIN31_LOOK) {
136         if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
137             SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
138         else {
139             SetBkColor( hDC, GetSysColor(COLOR_3DFACE) );
140             return GetSysColorBrush(COLOR_3DFACE);
141         }
142     }
143     else
144         SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
145     return GetSysColorBrush(COLOR_WINDOW);
146 }
147
148
149 /***********************************************************************
150  *           DEFWND_SetRedraw
151  */
152 static void DEFWND_SetRedraw( WND* wndPtr, WPARAM wParam )
153 {
154     BOOL bVisible = wndPtr->dwStyle & WS_VISIBLE;
155
156     TRACE("%04x %i\n", wndPtr->hwndSelf, (wParam!=0) );
157
158     if( wParam )
159     {
160         if( !bVisible )
161         {
162             wndPtr->dwStyle |= WS_VISIBLE;
163             DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
164         }
165     }
166     else if( bVisible )
167     {
168         if( wndPtr->dwStyle & WS_MINIMIZE ) wParam = RDW_VALIDATE;
169         else wParam = RDW_ALLCHILDREN | RDW_VALIDATE;
170
171         PAINT_RedrawWindow( wndPtr->hwndSelf, NULL, 0, wParam, 0 );
172         DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
173         wndPtr->dwStyle &= ~WS_VISIBLE;
174     }
175 }
176
177 /***********************************************************************
178  *           DEFWND_Print
179  *
180  * This method handles the default behavior for the WM_PRINT message.
181  */
182 static void DEFWND_Print(
183   WND*  wndPtr,
184   HDC   hdc,
185   ULONG uFlags)
186 {
187   /*
188    * Visibility flag.
189    */
190   if ( (uFlags & PRF_CHECKVISIBLE) &&
191        !IsWindowVisible(wndPtr->hwndSelf) )
192       return;
193
194   /*
195    * Unimplemented flags.
196    */
197   if ( (uFlags & PRF_CHILDREN) ||
198        (uFlags & PRF_OWNED)    ||
199        (uFlags & PRF_NONCLIENT) )
200   {
201     WARN("WM_PRINT message with unsupported flags\n");
202   }
203
204   /*
205    * Background
206    */
207   if ( uFlags & PRF_ERASEBKGND)
208     SendMessageA(wndPtr->hwndSelf, WM_ERASEBKGND, (WPARAM)hdc, 0);
209
210   /*
211    * Client area
212    */
213   if ( uFlags & PRF_CLIENT)
214     SendMessageA(wndPtr->hwndSelf, WM_PRINTCLIENT, (WPARAM)hdc, PRF_CLIENT);
215 }
216
217
218 /*
219  * helpers for calling IMM32
220  *
221  * WM_IME_* messages are generated only by IMM32,
222  * so I assume imm32 is already LoadLibrary-ed.
223  */
224 static HWND DEFWND_ImmGetDefaultIMEWnd( HWND hwnd )
225 {
226     HINSTANCE hInstIMM = GetModuleHandleA( "imm32" );
227     HWND (WINAPI* pFunc)(HWND);
228     HWND hwndRet = 0;
229
230     if (!hInstIMM)
231     {
232         ERR( "cannot get IMM32 handle\n" );
233         return 0;
234     }
235
236     pFunc = (void*)GetProcAddress(hInstIMM,"ImmGetDefaultIMEWnd");
237     if ( pFunc != NULL )
238         hwndRet = (*pFunc)( hwnd );
239
240     return hwndRet;
241 }
242
243 static BOOL DEFWND_ImmIsUIMessageA( HWND hwndIME, UINT msg, WPARAM wParam, LPARAM lParam )
244 {
245     HINSTANCE hInstIMM = GetModuleHandleA( "imm32" );
246     BOOL (WINAPI* pFunc)(HWND,UINT,WPARAM,LPARAM);
247     BOOL fRet = FALSE;
248
249     if (!hInstIMM)
250     {
251         ERR( "cannot get IMM32 handle\n" );
252         return FALSE;
253     }
254
255     pFunc = (void*)GetProcAddress(hInstIMM,"ImmIsUIMessageA");
256     if ( pFunc != NULL )
257         fRet = (*pFunc)( hwndIME, msg, wParam, lParam );
258
259     return fRet;
260 }
261
262 static BOOL DEFWND_ImmIsUIMessageW( HWND hwndIME, UINT msg, WPARAM wParam, LPARAM lParam )
263 {
264     HINSTANCE hInstIMM = GetModuleHandleA( "imm32" );
265     BOOL (WINAPI* pFunc)(HWND,UINT,WPARAM,LPARAM);
266     BOOL fRet = FALSE;
267
268     if (!hInstIMM)
269     {
270         ERR( "cannot get IMM32 handle\n" );
271         return FALSE;
272     }
273
274     pFunc = (void*)GetProcAddress(hInstIMM,"ImmIsUIMessageW");
275     if ( pFunc != NULL )
276         fRet = (*pFunc)( hwndIME, msg, wParam, lParam );
277
278     return fRet;
279 }
280
281
282
283 /***********************************************************************
284  *           DEFWND_DefWinProc
285  *
286  * Default window procedure for messages that are the same in Win16 and Win32.
287  */
288 static LRESULT DEFWND_DefWinProc( WND *wndPtr, UINT msg, WPARAM wParam,
289                                   LPARAM lParam )
290 {
291     switch(msg)
292     {
293     case WM_NCPAINT:
294         return NC_HandleNCPaint( wndPtr->hwndSelf, (HRGN)wParam );
295
296     case WM_NCHITTEST:
297         {
298             POINT pt;
299             pt.x = SLOWORD(lParam);
300             pt.y = SHIWORD(lParam);
301             return NC_HandleNCHitTest( wndPtr->hwndSelf, pt );
302         }
303
304     case WM_NCLBUTTONDOWN:
305         return NC_HandleNCLButtonDown( wndPtr, wParam, lParam );
306
307     case WM_LBUTTONDBLCLK:
308     case WM_NCLBUTTONDBLCLK:
309         return NC_HandleNCLButtonDblClk( wndPtr, wParam, lParam );
310
311     case WM_NCRBUTTONDOWN:
312         /* in Windows, capture is taken when right-clicking on the caption bar */
313         if (wParam==HTCAPTION)
314         {
315             SetCapture(wndPtr->hwndSelf);
316         }
317         break;
318
319     case WM_RBUTTONUP:
320         if (wndPtr->hwndSelf == GetCapture())
321         {
322             /* release capture if we took it on WM_NCRBUTTONDOWN */
323             ReleaseCapture();
324         }
325         if ((wndPtr->flags & WIN_ISWIN32) || (TWEAK_WineLook > WIN31_LOOK))
326         {
327             ClientToScreen16(wndPtr->hwndSelf, (LPPOINT16)&lParam);
328             SendMessageA( wndPtr->hwndSelf, WM_CONTEXTMENU,
329                           wndPtr->hwndSelf, lParam);
330         }
331         break;
332
333     case WM_NCRBUTTONUP:
334         /*
335          * FIXME : we must NOT send WM_CONTEXTMENU on a WM_NCRBUTTONUP (checked 
336          * in Windows), but what _should_ we do? According to MSDN : 
337          * "If it is appropriate to do so, the system sends the WM_SYSCOMMAND 
338          * message to the window". When is it appropriate?
339          */
340         break;
341
342     case WM_CONTEXTMENU:
343         if( wndPtr->dwStyle & WS_CHILD )
344             SendMessageA( wndPtr->parent->hwndSelf, msg, wParam, lParam );
345         else if (wndPtr->hSysMenu)
346         {
347             LONG hitcode;
348             POINT pt;
349             pt.x = SLOWORD(lParam);
350             pt.y = SHIWORD(lParam);
351
352             /*
353              * WM_CONTEXTMENU coordinates are relative to screen, but 
354              * NC_HandleNCHitTest expects coordinates relative to the parent's 
355              * client area (to compare with the rectangle returned by 
356              * GetWindowRect)
357              */
358             if (wndPtr->parent)
359                 ScreenToClient(wndPtr->parent->hwndSelf, &pt);
360
361             hitcode = NC_HandleNCHitTest(wndPtr->hwndSelf, pt);
362
363             /* Track system popup if click was in the caption area. */
364             if (hitcode==HTCAPTION || hitcode==HTSYSMENU)
365                TrackPopupMenu(GetSystemMenu(wndPtr->hwndSelf, FALSE),
366                                TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
367                                pt.x, pt.y, 0, wndPtr->hwndSelf, NULL);
368         }
369         break;
370
371     case WM_NCACTIVATE:
372         return NC_HandleNCActivate( wndPtr, wParam );
373
374     case WM_NCDESTROY:
375         if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
376         wndPtr->text = NULL;
377         if (wndPtr->pVScroll) HeapFree( SystemHeap, 0, wndPtr->pVScroll );
378         if (wndPtr->pHScroll) HeapFree( SystemHeap, 0, wndPtr->pHScroll );
379         wndPtr->pVScroll = wndPtr->pHScroll = NULL;
380         return 0;
381
382     case WM_PRINT:
383         DEFWND_Print(wndPtr, (HDC)wParam, lParam);
384         return 0;
385
386     case WM_PAINTICON:
387     case WM_PAINT:
388         {
389             PAINTSTRUCT16 ps;
390             HDC16 hdc = BeginPaint16( wndPtr->hwndSelf, &ps );
391             if( hdc ) 
392             {
393               HICON hIcon;
394               if( (wndPtr->dwStyle & WS_MINIMIZE) && ((hIcon = GetClassLongA( wndPtr->hwndSelf, GCL_HICON))) )
395               {
396                 int x = (wndPtr->rectWindow.right - wndPtr->rectWindow.left -
397                         GetSystemMetrics(SM_CXICON))/2;
398                 int y = (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top -
399                         GetSystemMetrics(SM_CYICON))/2;
400                 TRACE("Painting class icon: vis rect=(%i,%i - %i,%i)\n",
401                 ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
402                 DrawIcon( hdc, x, y, hIcon );
403               }
404               EndPaint16( wndPtr->hwndSelf, &ps );
405             }
406             return 0;
407         }
408
409     case WM_SYNCPAINT:
410         if (wndPtr->hrgnUpdate) 
411         {
412            RedrawWindow ( wndPtr->hwndSelf, 0, wndPtr->hrgnUpdate,
413                     RDW_ERASENOW | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
414         }
415         return 0;
416
417     case WM_SETREDRAW:
418         DEFWND_SetRedraw( wndPtr, wParam );
419         return 0;
420
421     case WM_CLOSE:
422         DestroyWindow( wndPtr->hwndSelf );
423         return 0;
424
425     case WM_MOUSEACTIVATE:
426         if (wndPtr->dwStyle & WS_CHILD)
427         {
428             LONG ret = SendMessage16( wndPtr->parent->hwndSelf,
429                                       WM_MOUSEACTIVATE, wParam, lParam );
430             if (ret) return ret;
431         }
432
433         /* Caption clicks are handled by the NC_HandleNCLButtonDown() */ 
434         return (LOWORD(lParam) >= HTCLIENT) ? MA_ACTIVATE : MA_NOACTIVATE;
435
436     case WM_ACTIVATE:
437         /* The default action in Windows is to set the keyboard focus to
438          * the window, if it's being activated and not minimized */
439         if (LOWORD(wParam) != WA_INACTIVE) {
440                 if (!(wndPtr->dwStyle & WS_MINIMIZE))
441                         SetFocus(wndPtr->hwndSelf);
442         }
443         break;
444
445      case WM_MOUSEWHEEL:
446         if (wndPtr->dwStyle & WS_CHILD)
447         {
448             return SendMessageA( wndPtr->parent->hwndSelf,
449                                  WM_MOUSEWHEEL, wParam, lParam );
450         }
451         break;
452
453     case WM_ERASEBKGND:
454     case WM_ICONERASEBKGND:
455         {
456             RECT rect;
457             HBRUSH hbr = GetClassLongA( wndPtr->hwndSelf, GCL_HBRBACKGROUND );
458             if (!hbr) return 0;
459
460             /*  Since WM_ERASEBKGND may receive either a window dc or a    */ 
461             /*  client dc, the area to be erased has to be retrieved from  */
462             /*  the device context.                                        */
463             GetClipBox( (HDC)wParam, &rect );
464
465             /* Always call the Win32 variant of FillRect even on Win16,
466              * since despite the fact that Win16, as well as Win32,
467              * supports special background brushes for a window class,
468              * the Win16 variant of FillRect does not.
469              */
470             FillRect( (HDC) wParam, &rect, hbr );
471             return 1;
472         }
473
474     case WM_GETDLGCODE:
475         return 0;
476
477     case WM_CTLCOLORMSGBOX:
478     case WM_CTLCOLOREDIT:
479     case WM_CTLCOLORLISTBOX:
480     case WM_CTLCOLORBTN:
481     case WM_CTLCOLORDLG:
482     case WM_CTLCOLORSTATIC:
483     case WM_CTLCOLORSCROLLBAR:
484         return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
485
486     case WM_CTLCOLOR:
487         return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
488         
489     case WM_GETTEXTLENGTH:
490         if (wndPtr->text) return (LRESULT)strlenW(wndPtr->text);
491         return 0;
492
493     case WM_SETCURSOR:
494         if (wndPtr->dwStyle & WS_CHILD)
495             if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
496                             wParam, lParam))
497                 return TRUE;
498         return NC_HandleSetCursor( wndPtr->hwndSelf, wParam, lParam );
499
500     case WM_SYSCOMMAND:
501         {
502             POINT pt;
503             pt.x = SLOWORD(lParam);
504             pt.y = SHIWORD(lParam);
505             return NC_HandleSysCommand( wndPtr->hwndSelf, wParam, pt );
506         }
507
508     case WM_KEYDOWN:
509         if(wParam == VK_F10) iF10Key = VK_F10;
510         break;
511
512     case WM_SYSKEYDOWN:
513         if( HIWORD(lParam) & KEYDATA_ALT )
514         {
515             /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
516               if( wParam == VK_MENU && !iMenuSysKey )
517                 iMenuSysKey = 1;
518               else
519                 iMenuSysKey = 0;
520             
521             iF10Key = 0;
522
523             if( wParam == VK_F4 )       /* try to close the window */
524             {
525                 HWND hWnd = WIN_GetTopParent( wndPtr->hwndSelf );
526                 wndPtr = WIN_FindWndPtr( hWnd );
527                 if( wndPtr && !(wndPtr->clsStyle & CS_NOCLOSE) )
528                     PostMessage16( hWnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
529                 WIN_ReleaseWndPtr(wndPtr);
530             }
531         } 
532         else if( wParam == VK_F10 )
533                 iF10Key = 1;
534              else
535                 if( wParam == VK_ESCAPE && (GetKeyState(VK_SHIFT) & 0x8000))
536                     SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
537                                   (WPARAM16)SC_KEYMENU, (LPARAM)VK_SPACE);
538         break;
539
540     case WM_KEYUP:
541     case WM_SYSKEYUP:
542         /* Press and release F10 or ALT */
543         if (((wParam == VK_MENU) && iMenuSysKey) ||
544             ((wParam == VK_F10) && iF10Key))
545               SendMessage16( WIN_GetTopParent(wndPtr->hwndSelf),
546                              WM_SYSCOMMAND, SC_KEYMENU, 0L );
547         iMenuSysKey = iF10Key = 0;
548         break;
549
550     case WM_SYSCHAR:
551         iMenuSysKey = 0;
552         if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
553         {
554             PostMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
555                            (WPARAM16)SC_RESTORE, 0L ); 
556             break;
557         } 
558         if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
559         {
560             if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
561             if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
562                 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
563             else
564                 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
565                                (WPARAM16)SC_KEYMENU, (LPARAM)(DWORD)wParam );
566         } 
567         else /* check for Ctrl-Esc */
568             if (wParam != VK_ESCAPE) MessageBeep(0);
569         break;
570
571     case WM_SHOWWINDOW:
572         if (!lParam) return 0; /* sent from ShowWindow */
573         if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
574         if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
575         else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
576         ShowWindow( wndPtr->hwndSelf, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
577         break; 
578
579     case WM_CANCELMODE:
580         if (wndPtr->parent == WIN_GetDesktop()) EndMenu();
581         if (GetCapture() == wndPtr->hwndSelf) ReleaseCapture();
582         WIN_ReleaseDesktop();
583         break;
584
585     case WM_VKEYTOITEM:
586     case WM_CHARTOITEM:
587         return -1;
588
589     case WM_DROPOBJECT:
590         return DRAG_FILE;  
591
592     case WM_QUERYDROPOBJECT:
593         if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
594         break;
595
596     case WM_QUERYDRAGICON:
597         {
598             UINT16 len;
599
600             HICON hIcon = GetClassLongA( wndPtr->hwndSelf, GCL_HICON );
601             if (hIcon) return hIcon;
602             for(len=1; len<64; len++)
603                 if((hIcon=LoadIconA(wndPtr->hInstance,MAKEINTRESOURCEA(len))))
604                     return (LRESULT)hIcon;
605             return (LRESULT)LoadIconA(0,IDI_APPLICATIONA);
606         }
607         break;
608
609     case WM_ISACTIVEICON:
610         return ((wndPtr->flags & WIN_NCACTIVATED) != 0);
611
612     case WM_NOTIFYFORMAT:
613       if (IsWindowUnicode(wndPtr->hwndSelf)) return NFR_UNICODE;
614       else return NFR_ANSI;
615         
616     case WM_QUERYOPEN:
617     case WM_QUERYENDSESSION:
618         return 1;
619
620     case WM_SETICON:
621         {
622                 int index = (wParam != ICON_SMALL) ? GCL_HICON : GCL_HICONSM;
623                 HICON16 hOldIcon = GetClassLongA(wndPtr->hwndSelf, index); 
624                 SetClassLongA(wndPtr->hwndSelf, index, lParam);
625
626                 SetWindowPos(wndPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED
627                          | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE
628                          | SWP_NOZORDER);
629
630                 if( wndPtr->flags & WIN_NATIVE )
631                     wndPtr->pDriver->pSetHostAttr(wndPtr, HAK_ICONS, 0);
632
633                 return hOldIcon;
634         }
635
636     case WM_GETICON:
637         {
638                 int index = (wParam != ICON_SMALL) ? GCL_HICON : GCL_HICONSM;
639                 return GetClassLongA(wndPtr->hwndSelf, index); 
640         }
641
642     case WM_HELP:
643         SendMessageA( wndPtr->parent->hwndSelf, msg, wParam, lParam );
644         break;
645     }
646
647     return 0;
648 }
649
650
651
652 /***********************************************************************
653  *              DefWindowProc (USER.107)
654  */
655 LRESULT WINAPI DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
656                                 LPARAM lParam )
657 {
658     WND * wndPtr = WIN_FindWndPtr( hwnd );
659     LRESULT result = 0;
660
661     if (!wndPtr) return 0;
662     SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
663
664     switch(msg)
665     {
666     case WM_NCCREATE:
667         {
668             CREATESTRUCT16 *cs = MapSL(lParam);
669             if (cs->lpszName)
670                 DEFWND_SetTextA( wndPtr, MapSL(cs->lpszName) );
671             result = 1;
672         }
673         break;
674
675     case WM_NCCALCSIZE:
676         {
677             RECT rect32;
678             CONV_RECT16TO32( MapSL(lParam), &rect32 );
679             result = NC_HandleNCCalcSize( wndPtr, &rect32 );
680             CONV_RECT32TO16( &rect32, MapSL(lParam) );
681         }
682         break;
683
684     case WM_WINDOWPOSCHANGING:
685         result = WINPOS_HandleWindowPosChanging16( wndPtr, MapSL(lParam) );
686         break;
687
688     case WM_WINDOWPOSCHANGED:
689         {
690             WINDOWPOS16 * winPos = MapSL(lParam);
691             DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
692         }
693         break;
694
695     case WM_GETTEXT:
696         if (wParam && wndPtr->text)
697         {
698             LPSTR dest = MapSL(lParam);
699             if (!WideCharToMultiByte( CP_ACP, 0, wndPtr->text, -1, dest, wParam, NULL, NULL ))
700                 dest[wParam-1] = 0;
701             result = strlen(dest);
702         }
703         break;
704
705     case WM_SETTEXT:
706         DEFWND_SetTextA( wndPtr, MapSL(lParam) );
707         if( (wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION )
708             NC_HandleNCPaint( hwnd , (HRGN)1 );
709         result = 1; /* success. FIXME: check text length */
710         break;
711
712     default:
713         result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
714         break;
715     }
716
717     WIN_ReleaseWndPtr(wndPtr);
718     SPY_ExitMessage( SPY_RESULT_DEFWND16, hwnd, msg, result, wParam, lParam );
719     return result;
720 }
721
722
723 /***********************************************************************
724  *              DefWindowProcA (USER32.@)
725  *
726  */
727 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam,
728                                  LPARAM lParam )
729 {
730     WND * wndPtr = WIN_FindWndPtr( hwnd );
731     LRESULT result = 0;
732
733     if (!wndPtr) return 0;
734     SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
735
736     switch(msg)
737     {
738     case WM_NCCREATE:
739         {
740             CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
741             if (cs->lpszName) DEFWND_SetTextA( wndPtr, cs->lpszName );
742             result = 1;
743         }
744         break;
745
746     case WM_NCCALCSIZE:
747         result = NC_HandleNCCalcSize( wndPtr, (RECT *)lParam );
748         break;
749
750     case WM_WINDOWPOSCHANGING:
751         result = WINPOS_HandleWindowPosChanging( wndPtr,
752                                                    (WINDOWPOS *)lParam );
753         break;
754
755     case WM_WINDOWPOSCHANGED:
756         {
757             WINDOWPOS * winPos = (WINDOWPOS *)lParam;
758             DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
759         }
760         break;
761
762     case WM_GETTEXT:
763         if (wParam && wndPtr->text)
764         {
765             if (!WideCharToMultiByte( CP_ACP, 0, wndPtr->text, -1,
766                                       (LPSTR)lParam, wParam, NULL, NULL ))
767                 ((LPSTR)lParam)[wParam-1] = 0;
768             result = (LRESULT)strlen( (LPSTR)lParam );
769         }
770         break;
771
772     case WM_SETTEXT:
773         DEFWND_SetTextA( wndPtr, (LPCSTR)lParam );
774         if( (wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION )
775             NC_HandleNCPaint( hwnd , (HRGN)1 );  /* Repaint caption */
776         result = 1; /* success. FIXME: check text length */
777         break;
778
779     /* for far east users (IMM32) - <hidenori@a2.ctktv.ne.jp> */
780     case WM_IME_CHAR:
781         {
782             CHAR    chChar1 = (CHAR)( (wParam>>8) & 0xff );
783             CHAR    chChar2 = (CHAR)( wParam & 0xff );
784
785             SendMessageA( hwnd, WM_CHAR, (WPARAM)chChar1, lParam );
786             if ( IsDBCSLeadByte( chChar1 ) )
787                 SendMessageA( hwnd, WM_CHAR, (WPARAM)chChar2, lParam );
788         }
789         break;
790     case WM_IME_KEYDOWN:
791         result = SendMessageA( hwnd, WM_KEYDOWN, wParam, lParam );
792         break;
793     case WM_IME_KEYUP:
794         result = SendMessageA( hwnd, WM_KEYUP, wParam, lParam );
795         break;
796
797     case WM_IME_STARTCOMPOSITION:
798     case WM_IME_COMPOSITION:
799     case WM_IME_ENDCOMPOSITION:
800     case WM_IME_SELECT:
801         {
802             HWND hwndIME;
803
804             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
805             if (hwndIME)
806                 result = SendMessageA( hwndIME, msg, wParam, lParam );
807         }
808         break;
809     case WM_IME_SETCONTEXT:
810         {
811             HWND hwndIME;
812
813             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
814             if (hwndIME)
815                 result = DEFWND_ImmIsUIMessageA( hwndIME, msg, wParam, lParam );
816         }
817         break;
818
819     default:
820         result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
821         break;
822     }
823
824     WIN_ReleaseWndPtr(wndPtr);
825     SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
826     return result;
827 }
828
829
830 /***********************************************************************
831  *              DefWindowProcW (USER32.@) Calls default window message handler
832  * 
833  * Calls default window procedure for messages not processed 
834  *  by application.
835  *
836  *  RETURNS
837  *     Return value is dependent upon the message.
838 */
839 LRESULT WINAPI DefWindowProcW( 
840     HWND hwnd,      /* [in] window procedure recieving message */
841     UINT msg,       /* [in] message identifier */
842     WPARAM wParam,  /* [in] first message parameter */
843     LPARAM lParam )   /* [in] second message parameter */
844 {
845     WND * wndPtr = WIN_FindWndPtr( hwnd );
846     LRESULT result = 0;
847
848     if (!wndPtr) return 0;
849     SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
850
851     switch(msg)
852     {
853     case WM_NCCREATE:
854         {
855             CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
856             if (cs->lpszName) DEFWND_SetTextW( wndPtr, cs->lpszName );
857             result = 1;
858         }
859         break;
860
861     case WM_GETTEXT:
862         if (wParam && wndPtr->text)
863         {
864             lstrcpynW( (LPWSTR)lParam, wndPtr->text, wParam );
865             result = strlenW( (LPWSTR)lParam );
866         }
867         break;
868
869     case WM_SETTEXT:
870         DEFWND_SetTextW( wndPtr, (LPCWSTR)lParam );
871         if( (wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION )
872             NC_HandleNCPaint( hwnd , (HRGN)1 );  /* Repaint caption */
873         result = 1; /* success. FIXME: check text length */
874         break;
875
876     /* for far east users (IMM32) - <hidenori@a2.ctktv.ne.jp> */
877     case WM_IME_CHAR:
878         SendMessageW( hwnd, WM_CHAR, wParam, lParam );
879         break;
880     case WM_IME_SETCONTEXT:
881         {
882             HWND hwndIME;
883
884             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
885             if (hwndIME)
886                 result = DEFWND_ImmIsUIMessageW( hwndIME, msg, wParam, lParam );
887         }
888         break;
889
890     default:
891         result = DefWindowProcA( hwnd, msg, wParam, lParam );
892         break;
893     }
894     WIN_ReleaseWndPtr(wndPtr);
895     SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
896     return result;
897 }