quartz: Make FileAsyncReader_WaitForNext return an empty sample in flushing state.
[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 = 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 = 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_NCACTIVATE:
389         return NC_HandleNCActivate( hwnd, wParam, lParam );
390
391     case WM_NCDESTROY:
392         {
393             WND *wndPtr = WIN_GetPtr( hwnd );
394             if (!wndPtr) return 0;
395             HeapFree( GetProcessHeap(), 0, wndPtr->text );
396             wndPtr->text = NULL;
397             HeapFree( GetProcessHeap(), 0, wndPtr->pVScroll );
398             HeapFree( GetProcessHeap(), 0, wndPtr->pHScroll );
399             wndPtr->pVScroll = wndPtr->pHScroll = NULL;
400             WIN_ReleasePtr( wndPtr );
401             return 0;
402         }
403
404     case WM_PRINT:
405         DEFWND_Print(hwnd, (HDC)wParam, lParam);
406         return 0;
407
408     case WM_PAINTICON:
409     case WM_PAINT:
410         {
411             PAINTSTRUCT ps;
412             HDC hdc = BeginPaint( hwnd, &ps );
413             if( hdc )
414             {
415               HICON hIcon;
416               if (IsIconic(hwnd) && ((hIcon = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON))) )
417               {
418                   RECT rc;
419                   int x, y;
420
421                   GetClientRect( hwnd, &rc );
422                   x = (rc.right - rc.left - GetSystemMetrics(SM_CXICON))/2;
423                   y = (rc.bottom - rc.top - GetSystemMetrics(SM_CYICON))/2;
424                   TRACE("Painting class icon: vis rect=(%s)\n",
425                         wine_dbgstr_rect(&ps.rcPaint));
426                   DrawIcon( hdc, x, y, hIcon );
427               }
428               EndPaint( hwnd, &ps );
429             }
430             return 0;
431         }
432
433     case WM_SYNCPAINT:
434         RedrawWindow ( hwnd, NULL, 0, RDW_ERASENOW | RDW_ERASE | RDW_ALLCHILDREN );
435         return 0;
436
437     case WM_SETREDRAW:
438         if (wParam) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
439         else
440         {
441             RedrawWindow( hwnd, NULL, 0, RDW_ALLCHILDREN | RDW_VALIDATE );
442             WIN_SetStyle( hwnd, 0, WS_VISIBLE );
443         }
444         return 0;
445
446     case WM_CLOSE:
447         DestroyWindow( hwnd );
448         return 0;
449
450     case WM_MOUSEACTIVATE:
451         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
452         {
453             LONG ret = SendMessageW( GetParent(hwnd), WM_MOUSEACTIVATE, wParam, lParam );
454             if (ret) return ret;
455         }
456
457         /* Caption clicks are handled by NC_HandleNCLButtonDown() */
458         return MA_ACTIVATE;
459
460     case WM_ACTIVATE:
461         /* The default action in Windows is to set the keyboard focus to
462          * the window, if it's being activated and not minimized */
463         if (LOWORD(wParam) != WA_INACTIVE) {
464             if (!IsIconic(hwnd)) SetFocus(hwnd);
465         }
466         break;
467
468     case WM_MOUSEWHEEL:
469         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
470             return SendMessageW( GetParent(hwnd), WM_MOUSEWHEEL, wParam, lParam );
471         break;
472
473     case WM_ERASEBKGND:
474     case WM_ICONERASEBKGND:
475         {
476             RECT rect;
477             HDC hdc = (HDC)wParam;
478             HBRUSH hbr = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND );
479             if (!hbr) return 0;
480
481             if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
482             {
483                 /* can't use GetClipBox with a parent DC or we fill the whole parent */
484                 GetClientRect( hwnd, &rect );
485                 DPtoLP( hdc, (LPPOINT)&rect, 2 );
486             }
487             else GetClipBox( hdc, &rect );
488             FillRect( hdc, &rect, hbr );
489             return 1;
490         }
491
492     case WM_GETDLGCODE:
493         return 0;
494
495     case WM_CTLCOLORMSGBOX:
496     case WM_CTLCOLOREDIT:
497     case WM_CTLCOLORLISTBOX:
498     case WM_CTLCOLORBTN:
499     case WM_CTLCOLORDLG:
500     case WM_CTLCOLORSTATIC:
501     case WM_CTLCOLORSCROLLBAR:
502         return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
503
504     case WM_CTLCOLOR:
505         return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
506
507     case WM_SETCURSOR:
508         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
509         {
510             /* with the exception of the border around a resizable wnd,
511              * give the parent first chance to set the cursor */
512             if ((LOWORD(lParam) < HTSIZEFIRST) || (LOWORD(lParam) > HTSIZELAST))
513             {
514                 if (SendMessageW(GetParent(hwnd), WM_SETCURSOR, wParam, lParam)) return TRUE;
515             }
516         }
517         NC_HandleSetCursor( hwnd, wParam, lParam );
518         break;
519
520     case WM_SYSCOMMAND:
521         return NC_HandleSysCommand( hwnd, wParam, lParam );
522
523     case WM_KEYDOWN:
524         if(wParam == VK_F10) iF10Key = VK_F10;
525         break;
526
527     case WM_SYSKEYDOWN:
528         if( HIWORD(lParam) & KEYDATA_ALT )
529         {
530             /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
531               if ( (wParam == VK_MENU || wParam == VK_LMENU
532                     || wParam == VK_RMENU) && !iMenuSysKey )
533                 iMenuSysKey = 1;
534               else
535                 iMenuSysKey = 0;
536
537             iF10Key = 0;
538
539             if( wParam == VK_F4 )       /* try to close the window */
540             {
541                 HWND top = GetAncestor( hwnd, GA_ROOT );
542                 if (!(GetClassLongW( top, GCL_STYLE ) & CS_NOCLOSE))
543                     PostMessageW( top, WM_SYSCOMMAND, SC_CLOSE, 0 );
544             }
545         }
546         else if( wParam == VK_F10 )
547             iF10Key = 1;
548         else if( wParam == VK_ESCAPE && (GetKeyState(VK_SHIFT) & 0x8000))
549             SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, ' ' );
550         break;
551
552     case WM_KEYUP:
553     case WM_SYSKEYUP:
554         /* Press and release F10 or ALT */
555         if (((wParam == VK_MENU || wParam == VK_LMENU || wParam == VK_RMENU)
556              && iMenuSysKey) || ((wParam == VK_F10) && iF10Key))
557               SendMessageW( GetAncestor( hwnd, GA_ROOT ), WM_SYSCOMMAND, SC_KEYMENU, 0L );
558         iMenuSysKey = iF10Key = 0;
559         break;
560
561     case WM_SYSCHAR:
562     {
563         iMenuSysKey = 0;
564         if (wParam == '\r' && IsIconic(hwnd))
565         {
566             PostMessageW( hwnd, WM_SYSCOMMAND, SC_RESTORE, 0L );
567             break;
568         }
569         if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
570         {
571             if (wParam == '\t' || wParam == '\x1b') break;
572             if (wParam == ' ' && (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD))
573                 SendMessageW( GetParent(hwnd), msg, wParam, lParam );
574             else
575                 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, wParam );
576         }
577         else /* check for Ctrl-Esc */
578             if (wParam != '\x1b') MessageBeep(0);
579         break;
580     }
581
582     case WM_SHOWWINDOW:
583         {
584             LONG style = GetWindowLongW( hwnd, GWL_STYLE );
585             WND *pWnd;
586             if (!lParam) return 0; /* sent from ShowWindow */
587             if ((style & WS_VISIBLE) && wParam) return 0;
588             if (!(style & WS_VISIBLE) && !wParam) return 0;
589             if (!GetWindow( hwnd, GW_OWNER )) return 0;
590             if (!(pWnd = WIN_GetPtr( hwnd ))) return 0;
591             if (pWnd == WND_OTHER_PROCESS) return 0;
592             if (wParam)
593             {
594                 if (!(pWnd->flags & WIN_NEEDS_SHOW_OWNEDPOPUP))
595                 {
596                     WIN_ReleasePtr( pWnd );
597                     return 0;
598                 }
599                 pWnd->flags &= ~WIN_NEEDS_SHOW_OWNEDPOPUP;
600             }
601             else pWnd->flags |= WIN_NEEDS_SHOW_OWNEDPOPUP;
602             WIN_ReleasePtr( pWnd );
603             ShowWindow( hwnd, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
604             break;
605         }
606
607     case WM_CANCELMODE:
608         iMenuSysKey = 0;
609         if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)) EndMenu();
610         if (GetCapture() == hwnd) ReleaseCapture();
611         break;
612
613     case WM_VKEYTOITEM:
614     case WM_CHARTOITEM:
615         return -1;
616
617     case WM_DROPOBJECT:
618         return DRAG_FILE;
619
620     case WM_QUERYDROPOBJECT:
621         return (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES) != 0;
622
623     case WM_QUERYDRAGICON:
624         {
625             UINT len;
626
627             HICON hIcon = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON );
628             HINSTANCE instance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
629             if (hIcon) return (LRESULT)hIcon;
630             for(len=1; len<64; len++)
631                 if((hIcon = LoadIconW(instance, MAKEINTRESOURCEW(len))))
632                     return (LRESULT)hIcon;
633             return (LRESULT)LoadIconW(0, (LPWSTR)IDI_APPLICATION);
634         }
635         break;
636
637     case WM_ISACTIVEICON:
638         {
639             WND *wndPtr = WIN_GetPtr( hwnd );
640             BOOL ret = (wndPtr->flags & WIN_NCACTIVATED) != 0;
641             WIN_ReleasePtr( wndPtr );
642             return ret;
643         }
644
645     case WM_NOTIFYFORMAT:
646       if (IsWindowUnicode(hwnd)) return NFR_UNICODE;
647       else return NFR_ANSI;
648
649     case WM_QUERYOPEN:
650     case WM_QUERYENDSESSION:
651         return 1;
652
653     case WM_ENDSESSION:
654         if (wParam)
655             PostQuitMessage(0);
656         return 0;
657
658     case WM_SETICON:
659         {
660             HICON ret;
661             WND *wndPtr = WIN_GetPtr( hwnd );
662
663             switch(wParam)
664             {
665             case ICON_SMALL:
666                 ret = wndPtr->hIconSmall;
667                 wndPtr->hIconSmall = (HICON)lParam;
668                 break;
669             case ICON_BIG:
670                 ret = wndPtr->hIcon;
671                 wndPtr->hIcon = (HICON)lParam;
672                 break;
673             default:
674                 ret = 0;
675                 break;
676             }
677             WIN_ReleasePtr( wndPtr );
678
679             USER_Driver->pSetWindowIcon( hwnd, wParam, (HICON)lParam );
680
681             if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
682                 NC_HandleNCPaint( hwnd , (HRGN)1 );  /* Repaint caption */
683
684             return (LRESULT)ret;
685         }
686
687     case WM_GETICON:
688         {
689             HICON ret;
690             WND *wndPtr = WIN_GetPtr( hwnd );
691
692             switch(wParam)
693             {
694             case ICON_SMALL:
695                 ret = wndPtr->hIconSmall;
696                 break;
697             case ICON_BIG:
698                 ret = wndPtr->hIcon;
699                 break;
700             case ICON_SMALL2:
701                 ret = wndPtr->hIconSmall;
702                 if (!ret) ret = (HICON)GetClassLongPtrW( hwnd, GCLP_HICONSM );
703                 /* FIXME: should have a default here if class icon is null */
704                 break;
705             default:
706                 ret = 0;
707                 break;
708             }
709             WIN_ReleasePtr( wndPtr );
710             return (LRESULT)ret;
711         }
712
713     case WM_HELP:
714         SendMessageW( GetParent(hwnd), msg, wParam, lParam );
715         break;
716
717     case WM_APPCOMMAND:
718         {
719             HWND parent = GetParent(hwnd);
720             if(!parent)
721                 HOOK_CallHooks(WH_SHELL, HSHELL_APPCOMMAND, wParam, lParam, TRUE);
722             else
723                 SendMessageW( parent, msg, wParam, lParam );
724             break;
725         }
726     case WM_KEYF1:
727         {
728             HELPINFO hi;
729
730             hi.cbSize = sizeof(HELPINFO);
731             GetCursorPos( &hi.MousePos );
732             if (MENU_IsMenuActive())
733             {
734                 hi.iContextType = HELPINFO_MENUITEM;
735                 hi.hItemHandle = MENU_IsMenuActive();
736                 hi.iCtrlId = MenuItemFromPoint( hwnd, hi.hItemHandle, hi.MousePos );
737                 hi.dwContextId = GetMenuContextHelpId( hi.hItemHandle );
738             }
739             else
740             {
741                 hi.iContextType = HELPINFO_WINDOW;
742                 hi.hItemHandle = hwnd;
743                 hi.iCtrlId = GetWindowLongPtrA( hwnd, GWLP_ID );
744                 hi.dwContextId = GetWindowContextHelpId( hwnd );
745             }
746             SendMessageW( hwnd, WM_HELP, 0, (LPARAM)&hi );
747             break;
748         }
749     }
750
751     return 0;
752 }
753
754 static LPARAM DEFWND_GetTextA( WND *wndPtr, LPSTR dest, WPARAM wParam )
755 {
756     LPARAM result = 0;
757
758     __TRY
759     {
760         if (wndPtr->text)
761         {
762             if (!WideCharToMultiByte( CP_ACP, 0, wndPtr->text, -1,
763                                       dest, wParam, NULL, NULL )) dest[wParam-1] = 0;
764             result = strlen( dest );
765         }
766         else dest[0] = '\0';
767     }
768     __EXCEPT_PAGE_FAULT
769     {
770         return 0;
771     }
772     __ENDTRY
773     return result;
774 }
775
776 /***********************************************************************
777  *              DefWindowProcA (USER32.@)
778  *
779  * See DefWindowProcW.
780  */
781 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
782 {
783     LRESULT result = 0;
784     HWND full_handle;
785
786     if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
787     {
788         if (!IsWindow( hwnd )) return 0;
789         ERR( "called for other process window %p\n", hwnd );
790         return 0;
791     }
792     hwnd = full_handle;
793
794     SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
795
796     switch(msg)
797     {
798     case WM_NCCREATE:
799         if (lParam)
800         {
801             CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
802             /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
803              * may have child window IDs instead of window name */
804             if (HIWORD(cs->lpszName))
805                 DEFWND_SetTextA( hwnd, cs->lpszName );
806             result = 1;
807         }
808         break;
809
810     case WM_GETTEXTLENGTH:
811         {
812             WND *wndPtr = WIN_GetPtr( hwnd );
813             if (wndPtr && wndPtr->text)
814                 result = WideCharToMultiByte( CP_ACP, 0, wndPtr->text, strlenW(wndPtr->text),
815                                               NULL, 0, NULL, NULL );
816             WIN_ReleasePtr( wndPtr );
817         }
818         break;
819
820     case WM_GETTEXT:
821         if (wParam)
822         {
823             LPSTR dest = (LPSTR)lParam;
824             WND *wndPtr = WIN_GetPtr( hwnd );
825
826             if (!wndPtr) break;
827             result = DEFWND_GetTextA( wndPtr, dest, wParam );
828
829             WIN_ReleasePtr( wndPtr );
830         }
831         break;
832
833     case WM_SETTEXT:
834         DEFWND_SetTextA( hwnd, (LPCSTR)lParam );
835         if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
836             NC_HandleNCPaint( hwnd , (HRGN)1 );  /* Repaint caption */
837         result = 1; /* success. FIXME: check text length */
838         break;
839
840     case WM_IME_CHAR:
841         if (HIBYTE(wParam)) PostMessageA( hwnd, WM_CHAR, HIBYTE(wParam), lParam );
842         PostMessageA( hwnd, WM_CHAR, LOBYTE(wParam), lParam );
843         break;
844
845     case WM_IME_KEYDOWN:
846         result = SendMessageA( hwnd, WM_KEYDOWN, wParam, lParam );
847         break;
848
849     case WM_IME_KEYUP:
850         result = SendMessageA( hwnd, WM_KEYUP, wParam, lParam );
851         break;
852
853     case WM_IME_STARTCOMPOSITION:
854     case WM_IME_COMPOSITION:
855     case WM_IME_ENDCOMPOSITION:
856     case WM_IME_SELECT:
857         {
858             HWND hwndIME;
859
860             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
861             if (hwndIME)
862                 result = SendMessageA( hwndIME, msg, wParam, lParam );
863         }
864         break;
865     case WM_IME_SETCONTEXT:
866         {
867             HWND hwndIME;
868
869             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
870             if (hwndIME)
871                 result = DEFWND_ImmIsUIMessageA( hwndIME, msg, wParam, lParam );
872         }
873         break;
874
875     case WM_INPUTLANGCHANGEREQUEST:
876         /* notify about the switch only if it's really our current layout */
877         if ((HKL)lParam == GetKeyboardLayout(0))
878             result = SendMessageA( hwnd, WM_INPUTLANGCHANGE, wParam, lParam );
879         else
880             result = 0;
881         break;
882
883     case WM_SYSCHAR:
884     {
885         CHAR ch = LOWORD(wParam);
886         WCHAR wch;
887         MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
888         wParam = MAKEWPARAM( wch, HIWORD(wParam) );
889     }
890     /* fall through */
891     default:
892         result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
893         break;
894     }
895
896     SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
897     return result;
898 }
899
900
901 static LPARAM DEFWND_GetTextW( WND *wndPtr, LPWSTR dest, WPARAM wParam )
902 {
903     LPARAM result = 0;
904
905     __TRY
906     {
907         if (wndPtr->text)
908         {
909             lstrcpynW( dest, wndPtr->text, wParam );
910             result = strlenW( dest );
911         }
912         else dest[0] = '\0';
913     }
914     __EXCEPT_PAGE_FAULT
915     {
916         return 0;
917     }
918     __ENDTRY
919
920     return result;
921 }
922
923 /***********************************************************************
924  *              DefWindowProcW (USER32.@) Calls default window message handler
925  *
926  * Calls default window procedure for messages not processed
927  *  by application.
928  *
929  *  RETURNS
930  *     Return value is dependent upon the message.
931 */
932 LRESULT WINAPI DefWindowProcW(
933     HWND hwnd,      /* [in] window procedure receiving message */
934     UINT msg,       /* [in] message identifier */
935     WPARAM wParam,  /* [in] first message parameter */
936     LPARAM lParam )   /* [in] second message parameter */
937 {
938     LRESULT result = 0;
939     HWND full_handle;
940
941     if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
942     {
943         if (!IsWindow( hwnd )) return 0;
944         ERR( "called for other process window %p\n", hwnd );
945         return 0;
946     }
947     hwnd = full_handle;
948     SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
949
950     switch(msg)
951     {
952     case WM_NCCREATE:
953         if (lParam)
954         {
955             CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
956             /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
957              * may have child window IDs instead of window name */
958             if (HIWORD(cs->lpszName))
959                 DEFWND_SetTextW( hwnd, cs->lpszName );
960             result = 1;
961         }
962         break;
963
964     case WM_GETTEXTLENGTH:
965         {
966             WND *wndPtr = WIN_GetPtr( hwnd );
967             if (wndPtr && wndPtr->text) result = (LRESULT)strlenW(wndPtr->text);
968             WIN_ReleasePtr( wndPtr );
969         }
970         break;
971
972     case WM_GETTEXT:
973         if (wParam)
974         {
975             LPWSTR dest = (LPWSTR)lParam;
976             WND *wndPtr = WIN_GetPtr( hwnd );
977
978             if (!wndPtr) break;
979             result = DEFWND_GetTextW( wndPtr, dest, wParam );
980             WIN_ReleasePtr( wndPtr );
981         }
982         break;
983
984     case WM_SETTEXT:
985         DEFWND_SetTextW( hwnd, (LPCWSTR)lParam );
986         if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
987             NC_HandleNCPaint( hwnd , (HRGN)1 );  /* Repaint caption */
988         result = 1; /* success. FIXME: check text length */
989         break;
990
991     case WM_IME_CHAR:
992         PostMessageW( hwnd, WM_CHAR, wParam, lParam );
993         break;
994
995     case WM_IME_SETCONTEXT:
996         {
997             HWND hwndIME;
998
999             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
1000             if (hwndIME)
1001                 result = DEFWND_ImmIsUIMessageW( hwndIME, msg, wParam, lParam );
1002         }
1003         break;
1004
1005     case WM_IME_STARTCOMPOSITION:
1006     case WM_IME_COMPOSITION:
1007     case WM_IME_ENDCOMPOSITION:
1008     case WM_IME_SELECT:
1009         {
1010             HWND hwndIME;
1011
1012             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
1013             if (hwndIME)
1014                 result = SendMessageW( hwndIME, msg, wParam, lParam );
1015         }
1016         break;
1017
1018     case WM_INPUTLANGCHANGEREQUEST:
1019         /* notify about the switch only if it's really our current layout */
1020         if ((HKL)lParam == GetKeyboardLayout(0))
1021             result = SendMessageW( hwnd, WM_INPUTLANGCHANGE, wParam, lParam );
1022         else
1023             result = 0;
1024         break;
1025
1026     default:
1027         result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
1028         break;
1029     }
1030     SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
1031     return result;
1032 }