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