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