msi/tests: Ignore case when comparing file names.
[wine] / dlls / user32 / defwnd.c
1 /*
2  * Default window procedure
3  *
4  * Copyright 1993, 1996 Alexandre Julliard
5  *           1995 Alex Korobka
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <string.h>
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winnls.h"
32 #include "win.h"
33 #include "user_private.h"
34 #include "controls.h"
35 #include "wine/unicode.h"
36 #include "wine/winuser16.h"
37 #include "wine/server.h"
38 #include "wine/exception.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(win);
42
43   /* bits in the dwKeyData */
44 #define KEYDATA_ALT             0x2000
45 #define KEYDATA_PREVSTATE       0x4000
46
47 static short iF10Key = 0;
48 static short iMenuSysKey = 0;
49 static const WCHAR imm32W[] = { 'i','m','m','3','2','\0' };
50
51 /***********************************************************************
52  *           DEFWND_HandleWindowPosChanged
53  *
54  * Handle the WM_WINDOWPOSCHANGED message.
55  */
56 static void DEFWND_HandleWindowPosChanged( HWND hwnd, const WINDOWPOS *winpos )
57 {
58     RECT rect;
59     WND *wndPtr = WIN_GetPtr( hwnd );
60
61     rect = wndPtr->rectClient;
62     WIN_ReleasePtr( wndPtr );
63
64     if (!(winpos->flags & SWP_NOCLIENTMOVE))
65         SendMessageW( hwnd, WM_MOVE, 0, MAKELONG(rect.left, rect.top));
66
67     if (!(winpos->flags & SWP_NOCLIENTSIZE) || (winpos->flags & SWP_STATECHANGED))
68     {
69         WPARAM wp = SIZE_RESTORED;
70         if (IsZoomed(hwnd)) wp = SIZE_MAXIMIZED;
71         else if (IsIconic(hwnd)) wp = SIZE_MINIMIZED;
72
73         SendMessageW( hwnd, WM_SIZE, wp, MAKELONG(rect.right-rect.left, rect.bottom-rect.top) );
74     }
75 }
76
77
78 /***********************************************************************
79  *           DEFWND_SetTextA
80  *
81  * Set the window text.
82  */
83 static void DEFWND_SetTextA( HWND hwnd, LPCSTR text )
84 {
85     int count;
86     WCHAR *textW;
87     WND *wndPtr;
88
89     if (!text) text = "";
90     count = MultiByteToWideChar( CP_ACP, 0, text, -1, NULL, 0 );
91
92     if (!(wndPtr = WIN_GetPtr( hwnd ))) return;
93     if ((textW = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR))))
94     {
95         HeapFree(GetProcessHeap(), 0, wndPtr->text);
96         wndPtr->text = textW;
97         MultiByteToWideChar( CP_ACP, 0, text, -1, textW, count );
98         SERVER_START_REQ( set_window_text )
99         {
100             req->handle = wine_server_user_handle( hwnd );
101             wine_server_add_data( req, textW, (count-1) * sizeof(WCHAR) );
102             wine_server_call( req );
103         }
104         SERVER_END_REQ;
105     }
106     else
107         ERR("Not enough memory for window text\n");
108     WIN_ReleasePtr( wndPtr );
109
110     USER_Driver->pSetWindowText( hwnd, textW );
111 }
112
113 /***********************************************************************
114  *           DEFWND_SetTextW
115  *
116  * Set the window text.
117  */
118 static void DEFWND_SetTextW( HWND hwnd, LPCWSTR text )
119 {
120     static const WCHAR empty_string[] = {0};
121     WND *wndPtr;
122     int count;
123
124     if (!text) text = empty_string;
125     count = strlenW(text) + 1;
126
127     if (!(wndPtr = WIN_GetPtr( hwnd ))) return;
128     HeapFree(GetProcessHeap(), 0, wndPtr->text);
129     if ((wndPtr->text = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR))))
130     {
131         strcpyW( wndPtr->text, text );
132         SERVER_START_REQ( set_window_text )
133         {
134             req->handle = wine_server_user_handle( hwnd );
135             wine_server_add_data( req, wndPtr->text, (count-1) * sizeof(WCHAR) );
136             wine_server_call( req );
137         }
138         SERVER_END_REQ;
139     }
140     else
141         ERR("Not enough memory for window text\n");
142     text = wndPtr->text;
143     WIN_ReleasePtr( wndPtr );
144
145     USER_Driver->pSetWindowText( hwnd, text );
146 }
147
148 /***********************************************************************
149  *           DEFWND_ControlColor
150  *
151  * Default colors for control painting.
152  */
153 HBRUSH DEFWND_ControlColor( HDC hDC, UINT ctlType )
154 {
155     if( ctlType == CTLCOLOR_SCROLLBAR)
156     {
157         HBRUSH hb = GetSysColorBrush(COLOR_SCROLLBAR);
158         COLORREF bk = GetSysColor(COLOR_3DHILIGHT);
159         SetTextColor( hDC, GetSysColor(COLOR_3DFACE));
160         SetBkColor( hDC, bk);
161
162         /* if COLOR_WINDOW happens to be the same as COLOR_3DHILIGHT
163          * we better use 0x55aa bitmap brush to make scrollbar's background
164          * look different from the window background.
165          */
166         if (bk == GetSysColor(COLOR_WINDOW))
167             return SYSCOLOR_55AABrush;
168
169         UnrealizeObject( hb );
170         return hb;
171     }
172
173     SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT));
174
175     if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
176         SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
177     else {
178         SetBkColor( hDC, GetSysColor(COLOR_3DFACE) );
179         return GetSysColorBrush(COLOR_3DFACE);
180     }
181     return GetSysColorBrush(COLOR_WINDOW);
182 }
183
184
185 /***********************************************************************
186  *           DEFWND_Print
187  *
188  * This method handles the default behavior for the WM_PRINT message.
189  */
190 static void DEFWND_Print( HWND hwnd, HDC hdc, ULONG uFlags)
191 {
192   /*
193    * Visibility flag.
194    */
195   if ( (uFlags & PRF_CHECKVISIBLE) &&
196        !IsWindowVisible(hwnd) )
197       return;
198
199   /*
200    * Unimplemented flags.
201    */
202   if ( (uFlags & PRF_CHILDREN) ||
203        (uFlags & PRF_OWNED)    ||
204        (uFlags & PRF_NONCLIENT) )
205   {
206     WARN("WM_PRINT message with unsupported flags\n");
207   }
208
209   /*
210    * Background
211    */
212   if ( uFlags & PRF_ERASEBKGND)
213     SendMessageW(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
214
215   /*
216    * Client area
217    */
218   if ( uFlags & PRF_CLIENT)
219     SendMessageW(hwnd, WM_PRINTCLIENT, (WPARAM)hdc, PRF_CLIENT);
220 }
221
222
223 /*
224  * helpers for calling IMM32
225  *
226  * WM_IME_* messages are generated only by IMM32,
227  * so I assume imm32 is already LoadLibrary-ed.
228  */
229 static HWND DEFWND_ImmGetDefaultIMEWnd( HWND hwnd )
230 {
231     HINSTANCE hInstIMM = GetModuleHandleW( imm32W );
232     HWND (WINAPI *pFunc)(HWND);
233     HWND hwndRet = 0;
234
235     if (!hInstIMM)
236     {
237         ERR( "cannot get IMM32 handle\n" );
238         return 0;
239     }
240
241     pFunc = (void*)GetProcAddress(hInstIMM,"ImmGetDefaultIMEWnd");
242     if ( pFunc != NULL )
243         hwndRet = (*pFunc)( hwnd );
244
245     return hwndRet;
246 }
247
248 static BOOL DEFWND_ImmIsUIMessageA( HWND hwndIME, UINT msg, WPARAM wParam, LPARAM lParam )
249 {
250     HINSTANCE hInstIMM = GetModuleHandleW( imm32W );
251     BOOL (WINAPI *pFunc)(HWND,UINT,WPARAM,LPARAM);
252     BOOL fRet = FALSE;
253
254     if (!hInstIMM)
255     {
256         ERR( "cannot get IMM32 handle\n" );
257         return FALSE;
258     }
259
260     pFunc = (void*)GetProcAddress(hInstIMM,"ImmIsUIMessageA");
261     if ( pFunc != NULL )
262         fRet = (*pFunc)( hwndIME, msg, wParam, lParam );
263
264     return fRet;
265 }
266
267 static BOOL DEFWND_ImmIsUIMessageW( HWND hwndIME, UINT msg, WPARAM wParam, LPARAM lParam )
268 {
269     HINSTANCE hInstIMM = GetModuleHandleW( imm32W );
270     BOOL (WINAPI *pFunc)(HWND,UINT,WPARAM,LPARAM);
271     BOOL fRet = FALSE;
272
273     if (!hInstIMM)
274     {
275         ERR( "cannot get IMM32 handle\n" );
276         return FALSE;
277     }
278
279     pFunc = (void*)GetProcAddress(hInstIMM,"ImmIsUIMessageW");
280     if ( pFunc != NULL )
281         fRet = (*pFunc)( hwndIME, msg, wParam, lParam );
282
283     return fRet;
284 }
285
286
287
288 /***********************************************************************
289  *           DEFWND_DefWinProc
290  *
291  * Default window procedure for messages that are the same in Ansi and Unicode.
292  */
293 static LRESULT DEFWND_DefWinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
294 {
295     switch(msg)
296     {
297     case WM_NCPAINT:
298         return NC_HandleNCPaint( hwnd, (HRGN)wParam );
299
300     case WM_NCHITTEST:
301         {
302             POINT pt;
303             pt.x = (short)LOWORD(lParam);
304             pt.y = (short)HIWORD(lParam);
305             return NC_HandleNCHitTest( hwnd, pt );
306         }
307
308     case WM_NCCALCSIZE:
309         return NC_HandleNCCalcSize( hwnd, (RECT *)lParam );
310
311     case WM_WINDOWPOSCHANGING:
312         return WINPOS_HandleWindowPosChanging( hwnd, (WINDOWPOS *)lParam );
313
314     case WM_WINDOWPOSCHANGED:
315         DEFWND_HandleWindowPosChanged( hwnd, (const WINDOWPOS *)lParam );
316         break;
317
318     case WM_LBUTTONDOWN:
319     case WM_RBUTTONDOWN:
320     case WM_MBUTTONDOWN:
321         iF10Key = iMenuSysKey = 0;
322         break;
323
324     case WM_NCLBUTTONDOWN:
325         return NC_HandleNCLButtonDown( hwnd, wParam, lParam );
326
327     case WM_LBUTTONDBLCLK:
328         return NC_HandleNCLButtonDblClk( hwnd, HTCLIENT, lParam );
329
330     case WM_NCLBUTTONDBLCLK:
331         return NC_HandleNCLButtonDblClk( hwnd, wParam, lParam );
332
333     case WM_NCRBUTTONDOWN:
334         /* in Windows, capture is taken when right-clicking on the caption bar */
335         if (wParam==HTCAPTION)
336         {
337             SetCapture(hwnd);
338         }
339         break;
340
341     case WM_RBUTTONUP:
342         {
343             POINT pt;
344
345             if (hwnd == GetCapture())
346                 /* release capture if we took it on WM_NCRBUTTONDOWN */
347                 ReleaseCapture();
348
349             pt.x = (short)LOWORD(lParam);
350             pt.y = (short)HIWORD(lParam);
351             ClientToScreen(hwnd, &pt);
352             SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM)hwnd, MAKELPARAM(pt.x, pt.y) );
353         }
354         break;
355
356     case WM_NCRBUTTONUP:
357         /*
358          * FIXME : we must NOT send WM_CONTEXTMENU on a WM_NCRBUTTONUP (checked
359          * in Windows), but what _should_ we do? According to MSDN :
360          * "If it is appropriate to do so, the system sends the WM_SYSCOMMAND
361          * message to the window". When is it appropriate?
362          */
363         break;
364
365     case WM_CONTEXTMENU:
366         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
367             SendMessageW( GetParent(hwnd), msg, wParam, lParam );
368         else
369         {
370             LONG hitcode;
371             POINT pt;
372             WND *wndPtr = WIN_GetPtr( hwnd );
373             HMENU hMenu = wndPtr->hSysMenu;
374             WIN_ReleasePtr( wndPtr );
375             if (!hMenu) return 0;
376             pt.x = (short)LOWORD(lParam);
377             pt.y = (short)HIWORD(lParam);
378             hitcode = NC_HandleNCHitTest(hwnd, pt);
379
380             /* Track system popup if click was in the caption area. */
381             if (hitcode==HTCAPTION || hitcode==HTSYSMENU)
382                TrackPopupMenu(GetSystemMenu(hwnd, FALSE),
383                                TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
384                                pt.x, pt.y, 0, hwnd, NULL);
385         }
386         break;
387
388     case WM_POPUPSYSTEMMENU:
389         {
390             /* This is an undocumented message used by the windows taskbar to
391                display the system menu of windows that belong to other processes. */
392             HMENU menu = GetSystemMenu(hwnd, FALSE);
393
394             if (menu)
395                 TrackPopupMenu(menu, TPM_LEFTBUTTON|TPM_RIGHTBUTTON,
396                                LOWORD(lParam), HIWORD(lParam), 0, hwnd, NULL);
397             return 0;
398         }
399
400     case WM_NCACTIVATE:
401         return NC_HandleNCActivate( hwnd, wParam, lParam );
402
403     case WM_NCDESTROY:
404         {
405             WND *wndPtr = WIN_GetPtr( hwnd );
406             if (!wndPtr) return 0;
407             HeapFree( GetProcessHeap(), 0, wndPtr->text );
408             wndPtr->text = NULL;
409             HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
410             wndPtr->pScroll = NULL;
411             WIN_ReleasePtr( wndPtr );
412             return 0;
413         }
414
415     case WM_PRINT:
416         DEFWND_Print(hwnd, (HDC)wParam, lParam);
417         return 0;
418
419     case WM_PAINTICON:
420     case WM_PAINT:
421         {
422             PAINTSTRUCT ps;
423             HDC hdc = BeginPaint( hwnd, &ps );
424             if( hdc )
425             {
426               HICON hIcon;
427               if (IsIconic(hwnd) && ((hIcon = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON))) )
428               {
429                   RECT rc;
430                   int x, y;
431
432                   GetClientRect( hwnd, &rc );
433                   x = (rc.right - rc.left - GetSystemMetrics(SM_CXICON))/2;
434                   y = (rc.bottom - rc.top - GetSystemMetrics(SM_CYICON))/2;
435                   TRACE("Painting class icon: vis rect=(%s)\n",
436                         wine_dbgstr_rect(&ps.rcPaint));
437                   DrawIcon( hdc, x, y, hIcon );
438               }
439               EndPaint( hwnd, &ps );
440             }
441             return 0;
442         }
443
444     case WM_SYNCPAINT:
445         RedrawWindow ( hwnd, NULL, 0, RDW_ERASENOW | RDW_ERASE | RDW_ALLCHILDREN );
446         return 0;
447
448     case WM_SETREDRAW:
449         if (wParam) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
450         else
451         {
452             RedrawWindow( hwnd, NULL, 0, RDW_ALLCHILDREN | RDW_VALIDATE );
453             WIN_SetStyle( hwnd, 0, WS_VISIBLE );
454         }
455         return 0;
456
457     case WM_CLOSE:
458         DestroyWindow( hwnd );
459         return 0;
460
461     case WM_MOUSEACTIVATE:
462         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
463         {
464             LONG ret = SendMessageW( GetParent(hwnd), WM_MOUSEACTIVATE, wParam, lParam );
465             if (ret) return ret;
466         }
467
468         /* Caption clicks are handled by NC_HandleNCLButtonDown() */
469         return MA_ACTIVATE;
470
471     case WM_ACTIVATE:
472         /* The default action in Windows is to set the keyboard focus to
473          * the window, if it's being activated and not minimized */
474         if (LOWORD(wParam) != WA_INACTIVE) {
475             if (!IsIconic(hwnd)) SetFocus(hwnd);
476         }
477         break;
478
479     case WM_MOUSEWHEEL:
480         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
481             return SendMessageW( GetParent(hwnd), WM_MOUSEWHEEL, wParam, lParam );
482         break;
483
484     case WM_ERASEBKGND:
485     case WM_ICONERASEBKGND:
486         {
487             RECT rect;
488             HDC hdc = (HDC)wParam;
489             HBRUSH hbr = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND );
490             if (!hbr) return 0;
491
492             if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
493             {
494                 /* can't use GetClipBox with a parent DC or we fill the whole parent */
495                 GetClientRect( hwnd, &rect );
496                 DPtoLP( hdc, (LPPOINT)&rect, 2 );
497             }
498             else GetClipBox( hdc, &rect );
499             FillRect( hdc, &rect, hbr );
500             return 1;
501         }
502
503     case WM_GETDLGCODE:
504         return 0;
505
506     case WM_CTLCOLORMSGBOX:
507     case WM_CTLCOLOREDIT:
508     case WM_CTLCOLORLISTBOX:
509     case WM_CTLCOLORBTN:
510     case WM_CTLCOLORDLG:
511     case WM_CTLCOLORSTATIC:
512     case WM_CTLCOLORSCROLLBAR:
513         return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
514
515     case WM_CTLCOLOR:
516         return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
517
518     case WM_SETCURSOR:
519         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
520         {
521             /* with the exception of the border around a resizable wnd,
522              * give the parent first chance to set the cursor */
523             if ((LOWORD(lParam) < HTSIZEFIRST) || (LOWORD(lParam) > HTSIZELAST))
524             {
525                 if (SendMessageW(GetParent(hwnd), WM_SETCURSOR, wParam, lParam)) return TRUE;
526             }
527         }
528         NC_HandleSetCursor( hwnd, wParam, lParam );
529         break;
530
531     case WM_SYSCOMMAND:
532         return NC_HandleSysCommand( hwnd, wParam, lParam );
533
534     case WM_KEYDOWN:
535         if(wParam == VK_F10) iF10Key = VK_F10;
536         break;
537
538     case WM_SYSKEYDOWN:
539         if( HIWORD(lParam) & KEYDATA_ALT )
540         {
541             /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
542               if ( (wParam == VK_MENU || wParam == VK_LMENU
543                     || wParam == VK_RMENU) && !iMenuSysKey )
544                 iMenuSysKey = 1;
545               else
546                 iMenuSysKey = 0;
547
548             iF10Key = 0;
549
550             if( wParam == VK_F4 )       /* try to close the window */
551             {
552                 HWND top = GetAncestor( hwnd, GA_ROOT );
553                 if (!(GetClassLongW( top, GCL_STYLE ) & CS_NOCLOSE))
554                     PostMessageW( top, WM_SYSCOMMAND, SC_CLOSE, 0 );
555             }
556         }
557         else if( wParam == VK_F10 )
558             iF10Key = 1;
559         else if( wParam == VK_ESCAPE && (GetKeyState(VK_SHIFT) & 0x8000))
560             SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, ' ' );
561         break;
562
563     case WM_KEYUP:
564     case WM_SYSKEYUP:
565         /* Press and release F10 or ALT */
566         if (((wParam == VK_MENU || wParam == VK_LMENU || wParam == VK_RMENU)
567              && iMenuSysKey) || ((wParam == VK_F10) && iF10Key))
568               SendMessageW( GetAncestor( hwnd, GA_ROOT ), WM_SYSCOMMAND, SC_KEYMENU, 0L );
569         iMenuSysKey = iF10Key = 0;
570         break;
571
572     case WM_SYSCHAR:
573     {
574         iMenuSysKey = 0;
575         if (wParam == '\r' && IsIconic(hwnd))
576         {
577             PostMessageW( hwnd, WM_SYSCOMMAND, SC_RESTORE, 0L );
578             break;
579         }
580         if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
581         {
582             if (wParam == '\t' || wParam == '\x1b') break;
583             if (wParam == ' ' && (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD))
584                 SendMessageW( GetParent(hwnd), msg, wParam, lParam );
585             else
586                 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, wParam );
587         }
588         else /* check for Ctrl-Esc */
589             if (wParam != '\x1b') MessageBeep(0);
590         break;
591     }
592
593     case WM_SHOWWINDOW:
594         {
595             LONG style = GetWindowLongW( hwnd, GWL_STYLE );
596             WND *pWnd;
597             if (!lParam) return 0; /* sent from ShowWindow */
598             if ((style & WS_VISIBLE) && wParam) return 0;
599             if (!(style & WS_VISIBLE) && !wParam) return 0;
600             if (!GetWindow( hwnd, GW_OWNER )) return 0;
601             if (!(pWnd = WIN_GetPtr( hwnd ))) return 0;
602             if (pWnd == WND_OTHER_PROCESS) return 0;
603             if (wParam)
604             {
605                 if (!(pWnd->flags & WIN_NEEDS_SHOW_OWNEDPOPUP))
606                 {
607                     WIN_ReleasePtr( pWnd );
608                     return 0;
609                 }
610                 pWnd->flags &= ~WIN_NEEDS_SHOW_OWNEDPOPUP;
611             }
612             else pWnd->flags |= WIN_NEEDS_SHOW_OWNEDPOPUP;
613             WIN_ReleasePtr( pWnd );
614             ShowWindow( hwnd, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
615             break;
616         }
617
618     case WM_CANCELMODE:
619         iMenuSysKey = 0;
620         MENU_EndMenu( hwnd );
621         if (GetCapture() == hwnd) ReleaseCapture();
622         break;
623
624     case WM_VKEYTOITEM:
625     case WM_CHARTOITEM:
626         return -1;
627
628     case WM_DROPOBJECT:
629         return DRAG_FILE;
630
631     case WM_QUERYDROPOBJECT:
632         return (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES) != 0;
633
634     case WM_QUERYDRAGICON:
635         {
636             UINT len;
637
638             HICON hIcon = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON );
639             HINSTANCE instance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
640             if (hIcon) return (LRESULT)hIcon;
641             for(len=1; len<64; len++)
642                 if((hIcon = LoadIconW(instance, MAKEINTRESOURCEW(len))))
643                     return (LRESULT)hIcon;
644             return (LRESULT)LoadIconW(0, (LPWSTR)IDI_APPLICATION);
645         }
646         break;
647
648     case WM_ISACTIVEICON:
649         {
650             WND *wndPtr = WIN_GetPtr( hwnd );
651             BOOL ret = (wndPtr->flags & WIN_NCACTIVATED) != 0;
652             WIN_ReleasePtr( wndPtr );
653             return ret;
654         }
655
656     case WM_NOTIFYFORMAT:
657       if (IsWindowUnicode(hwnd)) return NFR_UNICODE;
658       else return NFR_ANSI;
659
660     case WM_QUERYOPEN:
661     case WM_QUERYENDSESSION:
662         return 1;
663
664     case WM_SETICON:
665         {
666             HICON ret;
667             WND *wndPtr = WIN_GetPtr( hwnd );
668
669             switch(wParam)
670             {
671             case ICON_SMALL:
672                 ret = wndPtr->hIconSmall;
673                 wndPtr->hIconSmall = (HICON)lParam;
674                 break;
675             case ICON_BIG:
676                 ret = wndPtr->hIcon;
677                 wndPtr->hIcon = (HICON)lParam;
678                 break;
679             default:
680                 ret = 0;
681                 break;
682             }
683             WIN_ReleasePtr( wndPtr );
684
685             USER_Driver->pSetWindowIcon( hwnd, wParam, (HICON)lParam );
686
687             if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
688                 NC_HandleNCPaint( hwnd , (HRGN)1 );  /* Repaint caption */
689
690             return (LRESULT)ret;
691         }
692
693     case WM_GETICON:
694         {
695             HICON ret;
696             WND *wndPtr = WIN_GetPtr( hwnd );
697
698             switch(wParam)
699             {
700             case ICON_SMALL:
701                 ret = wndPtr->hIconSmall;
702                 break;
703             case ICON_BIG:
704                 ret = wndPtr->hIcon;
705                 break;
706             case ICON_SMALL2:
707                 ret = wndPtr->hIconSmall;
708                 if (!ret) ret = (HICON)GetClassLongPtrW( hwnd, GCLP_HICONSM );
709                 /* FIXME: should have a default here if class icon is null */
710                 break;
711             default:
712                 ret = 0;
713                 break;
714             }
715             WIN_ReleasePtr( wndPtr );
716             return (LRESULT)ret;
717         }
718
719     case WM_HELP:
720         SendMessageW( GetParent(hwnd), msg, wParam, lParam );
721         break;
722
723     case WM_APPCOMMAND:
724         {
725             HWND parent = GetParent(hwnd);
726             if(!parent)
727                 HOOK_CallHooks(WH_SHELL, HSHELL_APPCOMMAND, wParam, lParam, TRUE);
728             else
729                 SendMessageW( parent, msg, wParam, lParam );
730             break;
731         }
732     case WM_KEYF1:
733         {
734             HELPINFO hi;
735
736             hi.cbSize = sizeof(HELPINFO);
737             GetCursorPos( &hi.MousePos );
738             if (MENU_IsMenuActive())
739             {
740                 hi.iContextType = HELPINFO_MENUITEM;
741                 hi.hItemHandle = MENU_IsMenuActive();
742                 hi.iCtrlId = MenuItemFromPoint( hwnd, hi.hItemHandle, hi.MousePos );
743                 hi.dwContextId = GetMenuContextHelpId( hi.hItemHandle );
744             }
745             else
746             {
747                 hi.iContextType = HELPINFO_WINDOW;
748                 hi.hItemHandle = hwnd;
749                 hi.iCtrlId = GetWindowLongPtrA( hwnd, GWLP_ID );
750                 hi.dwContextId = GetWindowContextHelpId( hwnd );
751             }
752             SendMessageW( hwnd, WM_HELP, 0, (LPARAM)&hi );
753             break;
754         }
755
756     case WM_INPUTLANGCHANGEREQUEST:
757         ActivateKeyboardLayout( (HKL)lParam, 0 );
758         break;
759
760     case WM_INPUTLANGCHANGE:
761         {
762             int count = 0;
763             HWND *win_array = WIN_ListChildren( hwnd );
764
765             if (!win_array)
766                 break;
767             while (win_array[count])
768                 SendMessageW( win_array[count++], WM_INPUTLANGCHANGE, wParam, lParam);
769             HeapFree(GetProcessHeap(),0,win_array);
770             break;
771         }
772
773     }
774
775     return 0;
776 }
777
778 static LPARAM DEFWND_GetTextA( WND *wndPtr, LPSTR dest, WPARAM wParam )
779 {
780     LPARAM result = 0;
781
782     __TRY
783     {
784         if (wndPtr->text)
785         {
786             if (!WideCharToMultiByte( CP_ACP, 0, wndPtr->text, -1,
787                                       dest, wParam, NULL, NULL )) dest[wParam-1] = 0;
788             result = strlen( dest );
789         }
790         else dest[0] = '\0';
791     }
792     __EXCEPT_PAGE_FAULT
793     {
794         return 0;
795     }
796     __ENDTRY
797     return result;
798 }
799
800 /***********************************************************************
801  *              DefWindowProcA (USER32.@)
802  *
803  * See DefWindowProcW.
804  */
805 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
806 {
807     LRESULT result = 0;
808     HWND full_handle;
809
810     if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
811     {
812         if (!IsWindow( hwnd )) return 0;
813         ERR( "called for other process window %p\n", hwnd );
814         return 0;
815     }
816     hwnd = full_handle;
817
818     SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
819
820     switch(msg)
821     {
822     case WM_NCCREATE:
823         if (lParam)
824         {
825             CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
826             /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
827              * may have child window IDs instead of window name */
828             if (HIWORD(cs->lpszName))
829                 DEFWND_SetTextA( hwnd, cs->lpszName );
830             result = 1;
831         }
832         break;
833
834     case WM_GETTEXTLENGTH:
835         {
836             WND *wndPtr = WIN_GetPtr( hwnd );
837             if (wndPtr && wndPtr->text)
838                 result = WideCharToMultiByte( CP_ACP, 0, wndPtr->text, strlenW(wndPtr->text),
839                                               NULL, 0, NULL, NULL );
840             WIN_ReleasePtr( wndPtr );
841         }
842         break;
843
844     case WM_GETTEXT:
845         if (wParam)
846         {
847             LPSTR dest = (LPSTR)lParam;
848             WND *wndPtr = WIN_GetPtr( hwnd );
849
850             if (!wndPtr) break;
851             result = DEFWND_GetTextA( wndPtr, dest, wParam );
852
853             WIN_ReleasePtr( wndPtr );
854         }
855         break;
856
857     case WM_SETTEXT:
858         DEFWND_SetTextA( hwnd, (LPCSTR)lParam );
859         if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
860             NC_HandleNCPaint( hwnd , (HRGN)1 );  /* Repaint caption */
861         result = 1; /* success. FIXME: check text length */
862         break;
863
864     case WM_IME_CHAR:
865         if (HIBYTE(wParam)) PostMessageA( hwnd, WM_CHAR, HIBYTE(wParam), lParam );
866         PostMessageA( hwnd, WM_CHAR, LOBYTE(wParam), lParam );
867         break;
868
869     case WM_IME_KEYDOWN:
870         result = PostMessageA( hwnd, WM_KEYDOWN, wParam, lParam );
871         break;
872
873     case WM_IME_KEYUP:
874         result = PostMessageA( hwnd, WM_KEYUP, wParam, lParam );
875         break;
876
877     case WM_IME_STARTCOMPOSITION:
878     case WM_IME_COMPOSITION:
879     case WM_IME_ENDCOMPOSITION:
880     case WM_IME_SELECT:
881     case WM_IME_NOTIFY:
882         {
883             HWND hwndIME;
884
885             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
886             if (hwndIME)
887                 result = SendMessageA( hwndIME, msg, wParam, lParam );
888         }
889         break;
890     case WM_IME_SETCONTEXT:
891         {
892             HWND hwndIME;
893
894             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
895             if (hwndIME)
896                 result = DEFWND_ImmIsUIMessageA( hwndIME, msg, wParam, lParam );
897         }
898         break;
899
900     case WM_SYSCHAR:
901     {
902         CHAR ch = LOWORD(wParam);
903         WCHAR wch;
904         MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
905         wParam = MAKEWPARAM( wch, HIWORD(wParam) );
906     }
907     /* fall through */
908     default:
909         result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
910         break;
911     }
912
913     SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
914     return result;
915 }
916
917
918 static LPARAM DEFWND_GetTextW( WND *wndPtr, LPWSTR dest, WPARAM wParam )
919 {
920     LPARAM result = 0;
921
922     __TRY
923     {
924         if (wndPtr->text)
925         {
926             lstrcpynW( dest, wndPtr->text, wParam );
927             result = strlenW( dest );
928         }
929         else dest[0] = '\0';
930     }
931     __EXCEPT_PAGE_FAULT
932     {
933         return 0;
934     }
935     __ENDTRY
936
937     return result;
938 }
939
940 /***********************************************************************
941  *              DefWindowProcW (USER32.@) Calls default window message handler
942  *
943  * Calls default window procedure for messages not processed
944  *  by application.
945  *
946  *  RETURNS
947  *     Return value is dependent upon the message.
948 */
949 LRESULT WINAPI DefWindowProcW(
950     HWND hwnd,      /* [in] window procedure receiving message */
951     UINT msg,       /* [in] message identifier */
952     WPARAM wParam,  /* [in] first message parameter */
953     LPARAM lParam )   /* [in] second message parameter */
954 {
955     LRESULT result = 0;
956     HWND full_handle;
957
958     if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
959     {
960         if (!IsWindow( hwnd )) return 0;
961         ERR( "called for other process window %p\n", hwnd );
962         return 0;
963     }
964     hwnd = full_handle;
965     SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
966
967     switch(msg)
968     {
969     case WM_NCCREATE:
970         if (lParam)
971         {
972             CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
973             /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
974              * may have child window IDs instead of window name */
975             if (HIWORD(cs->lpszName))
976                 DEFWND_SetTextW( hwnd, cs->lpszName );
977             result = 1;
978         }
979         break;
980
981     case WM_GETTEXTLENGTH:
982         {
983             WND *wndPtr = WIN_GetPtr( hwnd );
984             if (wndPtr && wndPtr->text) result = (LRESULT)strlenW(wndPtr->text);
985             WIN_ReleasePtr( wndPtr );
986         }
987         break;
988
989     case WM_GETTEXT:
990         if (wParam)
991         {
992             LPWSTR dest = (LPWSTR)lParam;
993             WND *wndPtr = WIN_GetPtr( hwnd );
994
995             if (!wndPtr) break;
996             result = DEFWND_GetTextW( wndPtr, dest, wParam );
997             WIN_ReleasePtr( wndPtr );
998         }
999         break;
1000
1001     case WM_SETTEXT:
1002         DEFWND_SetTextW( hwnd, (LPCWSTR)lParam );
1003         if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
1004             NC_HandleNCPaint( hwnd , (HRGN)1 );  /* Repaint caption */
1005         result = 1; /* success. FIXME: check text length */
1006         break;
1007
1008     case WM_IME_CHAR:
1009         PostMessageW( hwnd, WM_CHAR, wParam, lParam );
1010         break;
1011
1012     case WM_IME_KEYDOWN:
1013         result = PostMessageW( hwnd, WM_KEYDOWN, wParam, lParam );
1014         break;
1015
1016     case WM_IME_KEYUP:
1017         result = PostMessageW( hwnd, WM_KEYUP, wParam, lParam );
1018         break;
1019
1020     case WM_IME_SETCONTEXT:
1021         {
1022             HWND hwndIME;
1023
1024             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
1025             if (hwndIME)
1026                 result = DEFWND_ImmIsUIMessageW( hwndIME, msg, wParam, lParam );
1027         }
1028         break;
1029
1030     case WM_IME_STARTCOMPOSITION:
1031     case WM_IME_COMPOSITION:
1032     case WM_IME_ENDCOMPOSITION:
1033     case WM_IME_SELECT:
1034     case WM_IME_NOTIFY:
1035         {
1036             HWND hwndIME;
1037
1038             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
1039             if (hwndIME)
1040                 result = SendMessageW( hwndIME, msg, wParam, lParam );
1041         }
1042         break;
1043
1044     default:
1045         result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
1046         break;
1047     }
1048     SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
1049     return result;
1050 }