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