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