ScrollDC should take into account overlapped windows. With this change
[wine] / windows / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <string.h>
26
27 #include "win.h"
28 #include "user.h"
29 #include "nonclient.h"
30 #include "winpos.h"
31 #include "dce.h"
32 #include "windef.h"
33 #include "wingdi.h"
34 #include "winnls.h"
35 #include "imm.h"
36 #include "message.h"
37 #include "wine/unicode.h"
38 #include "wine/winuser16.h"
39 #include "wine/server.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(win);
43
44   /* bits in the dwKeyData */
45 #define KEYDATA_ALT             0x2000
46 #define KEYDATA_PREVSTATE       0x4000
47
48 static short iF10Key = 0;
49 static short iMenuSysKey = 0;
50
51 /***********************************************************************
52  *           DEFWND_HandleWindowPosChanged
53  *
54  * Handle the WM_WINDOWPOSCHANGED message.
55  */
56 static void DEFWND_HandleWindowPosChanged( HWND hwnd, UINT flags )
57 {
58     RECT rect;
59     WND *wndPtr = WIN_GetPtr( hwnd );
60
61     rect = wndPtr->rectClient;
62     WIN_ReleasePtr( wndPtr );
63
64     if (!(flags & SWP_NOCLIENTMOVE))
65         SendMessageW( hwnd, WM_MOVE, 0, MAKELONG(rect.left, rect.top));
66
67     if (!(flags & SWP_NOCLIENTSIZE))
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         if (wndPtr->text) 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     if (USER_Driver.pSetWindowText) 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     if (wndPtr->text) 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     if (USER_Driver.pSetWindowText) 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 CACHE_GetPattern55AABrush();
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_SetRedraw
187  */
188 static void DEFWND_SetRedraw( HWND hwnd, WPARAM wParam )
189 {
190     WND *wndPtr = WIN_FindWndPtr( hwnd );
191     BOOL bVisible = wndPtr->dwStyle & WS_VISIBLE;
192
193     TRACE("%p %i\n", hwnd, (wParam!=0) );
194
195     if( wParam )
196     {
197         if( !bVisible )
198         {
199             WIN_SetStyle( hwnd, wndPtr->dwStyle | WS_VISIBLE );
200             DCE_InvalidateDCE( hwnd, &wndPtr->rectWindow );
201         }
202     }
203     else if( bVisible )
204     {
205         if( wndPtr->dwStyle & WS_MINIMIZE ) wParam = RDW_VALIDATE;
206         else wParam = RDW_ALLCHILDREN | RDW_VALIDATE;
207
208         RedrawWindow( hwnd, NULL, 0, wParam );
209         DCE_InvalidateDCE( hwnd, &wndPtr->rectWindow );
210         WIN_SetStyle( hwnd, wndPtr->dwStyle & ~WS_VISIBLE );
211     }
212     WIN_ReleaseWndPtr( wndPtr );
213 }
214
215 /***********************************************************************
216  *           DEFWND_Print
217  *
218  * This method handles the default behavior for the WM_PRINT message.
219  */
220 static void DEFWND_Print( HWND hwnd, HDC hdc, ULONG uFlags)
221 {
222   /*
223    * Visibility flag.
224    */
225   if ( (uFlags & PRF_CHECKVISIBLE) &&
226        !IsWindowVisible(hwnd) )
227       return;
228
229   /*
230    * Unimplemented flags.
231    */
232   if ( (uFlags & PRF_CHILDREN) ||
233        (uFlags & PRF_OWNED)    ||
234        (uFlags & PRF_NONCLIENT) )
235   {
236     WARN("WM_PRINT message with unsupported flags\n");
237   }
238
239   /*
240    * Background
241    */
242   if ( uFlags & PRF_ERASEBKGND)
243     SendMessageW(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
244
245   /*
246    * Client area
247    */
248   if ( uFlags & PRF_CLIENT)
249     SendMessageW(hwnd, WM_PRINTCLIENT, (WPARAM)hdc, PRF_CLIENT);
250 }
251
252
253 /*
254  * helpers for calling IMM32
255  *
256  * WM_IME_* messages are generated only by IMM32,
257  * so I assume imm32 is already LoadLibrary-ed.
258  */
259 static HWND DEFWND_ImmGetDefaultIMEWnd( HWND hwnd )
260 {
261     HINSTANCE hInstIMM = GetModuleHandleA( "imm32" );
262     HWND (WINAPI *pFunc)(HWND);
263     HWND hwndRet = 0;
264
265     if (!hInstIMM)
266     {
267         ERR( "cannot get IMM32 handle\n" );
268         return 0;
269     }
270
271     pFunc = (void*)GetProcAddress(hInstIMM,"ImmGetDefaultIMEWnd");
272     if ( pFunc != NULL )
273         hwndRet = (*pFunc)( hwnd );
274
275     return hwndRet;
276 }
277
278 static BOOL DEFWND_ImmIsUIMessageA( HWND hwndIME, UINT msg, WPARAM wParam, LPARAM lParam )
279 {
280     HINSTANCE hInstIMM = GetModuleHandleA( "imm32" );
281     BOOL (WINAPI *pFunc)(HWND,UINT,WPARAM,LPARAM);
282     BOOL fRet = FALSE;
283
284     if (!hInstIMM)
285     {
286         ERR( "cannot get IMM32 handle\n" );
287         return FALSE;
288     }
289
290     pFunc = (void*)GetProcAddress(hInstIMM,"ImmIsUIMessageA");
291     if ( pFunc != NULL )
292         fRet = (*pFunc)( hwndIME, msg, wParam, lParam );
293
294     return fRet;
295 }
296
297 static BOOL DEFWND_ImmIsUIMessageW( HWND hwndIME, UINT msg, WPARAM wParam, LPARAM lParam )
298 {
299     HINSTANCE hInstIMM = GetModuleHandleA( "imm32" );
300     BOOL (WINAPI *pFunc)(HWND,UINT,WPARAM,LPARAM);
301     BOOL fRet = FALSE;
302
303     if (!hInstIMM)
304     {
305         ERR( "cannot get IMM32 handle\n" );
306         return FALSE;
307     }
308
309     pFunc = (void*)GetProcAddress(hInstIMM,"ImmIsUIMessageW");
310     if ( pFunc != NULL )
311         fRet = (*pFunc)( hwndIME, msg, wParam, lParam );
312
313     return fRet;
314 }
315
316
317
318 /***********************************************************************
319  *           DEFWND_DefWinProc
320  *
321  * Default window procedure for messages that are the same in Win16 and Win32.
322  */
323 static LRESULT DEFWND_DefWinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
324 {
325     switch(msg)
326     {
327     case WM_NCPAINT:
328         return NC_HandleNCPaint( hwnd, (HRGN)wParam );
329
330     case WM_NCHITTEST:
331         {
332             POINT pt;
333             pt.x = (short)LOWORD(lParam);
334             pt.y = (short)HIWORD(lParam);
335             return NC_HandleNCHitTest( hwnd, pt );
336         }
337
338     case WM_NCLBUTTONDOWN:
339         return NC_HandleNCLButtonDown( hwnd, wParam, lParam );
340
341     case WM_LBUTTONDBLCLK:
342     case WM_NCLBUTTONDBLCLK:
343         return NC_HandleNCLButtonDblClk( hwnd, wParam, lParam );
344
345     case WM_NCRBUTTONDOWN:
346         /* in Windows, capture is taken when right-clicking on the caption bar */
347         if (wParam==HTCAPTION)
348         {
349             SetCapture(hwnd);
350         }
351         break;
352
353     case WM_RBUTTONUP:
354         {
355             POINT pt;
356
357             if (hwnd == GetCapture())
358                 /* release capture if we took it on WM_NCRBUTTONDOWN */
359                 ReleaseCapture();
360
361             pt.x = (short)LOWORD(lParam);
362             pt.y = (short)HIWORD(lParam);
363             ClientToScreen(hwnd, &pt);
364             SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM)hwnd, MAKELPARAM(pt.x, pt.y) );
365         }
366         break;
367
368     case WM_NCRBUTTONUP:
369         /*
370          * FIXME : we must NOT send WM_CONTEXTMENU on a WM_NCRBUTTONUP (checked
371          * in Windows), but what _should_ we do? According to MSDN :
372          * "If it is appropriate to do so, the system sends the WM_SYSCOMMAND
373          * message to the window". When is it appropriate?
374          */
375         break;
376
377     case WM_CONTEXTMENU:
378         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
379             SendMessageW( GetParent(hwnd), msg, wParam, lParam );
380         else
381         {
382             LONG hitcode;
383             POINT pt;
384             WND *wndPtr = WIN_GetPtr( hwnd );
385             HMENU hMenu = wndPtr->hSysMenu;
386             WIN_ReleasePtr( wndPtr );
387             if (!hMenu) return 0;
388             pt.x = (short)LOWORD(lParam);
389             pt.y = (short)HIWORD(lParam);
390             hitcode = NC_HandleNCHitTest(hwnd, pt);
391
392             /* Track system popup if click was in the caption area. */
393             if (hitcode==HTCAPTION || hitcode==HTSYSMENU)
394                TrackPopupMenu(GetSystemMenu(hwnd, FALSE),
395                                TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
396                                pt.x, pt.y, 0, hwnd, NULL);
397         }
398         break;
399
400     case WM_NCACTIVATE:
401         return NC_HandleNCActivate( hwnd, wParam );
402
403     case WM_NCDESTROY:
404         {
405             WND *wndPtr = WIN_GetPtr( hwnd );
406             if (!wndPtr) return 0;
407             if (wndPtr->text) HeapFree( GetProcessHeap(), 0, wndPtr->text );
408             wndPtr->text = NULL;
409             if (wndPtr->pVScroll) HeapFree( GetProcessHeap(), 0, wndPtr->pVScroll );
410             if (wndPtr->pHScroll) HeapFree( GetProcessHeap(), 0, wndPtr->pHScroll );
411             wndPtr->pVScroll = wndPtr->pHScroll = NULL;
412             WIN_ReleasePtr( wndPtr );
413             return 0;
414         }
415
416     case WM_PRINT:
417         DEFWND_Print(hwnd, (HDC)wParam, lParam);
418         return 0;
419
420     case WM_PAINTICON:
421     case WM_PAINT:
422         {
423             PAINTSTRUCT ps;
424             HDC hdc = BeginPaint( hwnd, &ps );
425             if( hdc )
426             {
427               HICON hIcon;
428               if (IsIconic(hwnd) && ((hIcon = (HICON)GetClassLongW( hwnd, GCL_HICON))) )
429               {
430                   RECT rc;
431                   int x, y;
432
433                   GetClientRect( hwnd, &rc );
434                   x = (rc.right - rc.left - GetSystemMetrics(SM_CXICON))/2;
435                   y = (rc.bottom - rc.top - GetSystemMetrics(SM_CYICON))/2;
436                   TRACE("Painting class icon: vis rect=(%ld,%ld - %ld,%ld)\n",
437                         ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
438                   DrawIcon( hdc, x, y, hIcon );
439               }
440               EndPaint( hwnd, &ps );
441             }
442             return 0;
443         }
444
445     case WM_SYNCPAINT:
446         RedrawWindow ( hwnd, NULL, 0, RDW_ERASENOW | RDW_ERASE | RDW_ALLCHILDREN );
447         return 0;
448
449     case WM_SETREDRAW:
450         DEFWND_SetRedraw( hwnd, wParam );
451         return 0;
452
453     case WM_CLOSE:
454         DestroyWindow( hwnd );
455         return 0;
456
457     case WM_MOUSEACTIVATE:
458         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
459         {
460             LONG ret = SendMessageW( GetParent(hwnd), WM_MOUSEACTIVATE, wParam, lParam );
461             if (ret) return ret;
462         }
463
464         /* Caption clicks are handled by the NC_HandleNCLButtonDown() */
465         return (LOWORD(lParam) >= HTCLIENT) ? MA_ACTIVATE : MA_NOACTIVATE;
466
467     case WM_ACTIVATE:
468         /* The default action in Windows is to set the keyboard focus to
469          * the window, if it's being activated and not minimized */
470         if (LOWORD(wParam) != WA_INACTIVE) {
471             if (!IsIconic(hwnd)) SetFocus(hwnd);
472         }
473         break;
474
475     case WM_MOUSEWHEEL:
476         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
477             return SendMessageW( GetParent(hwnd), WM_MOUSEWHEEL, wParam, lParam );
478         break;
479
480     case WM_ERASEBKGND:
481     case WM_ICONERASEBKGND:
482         {
483             RECT rect;
484             HDC hdc = (HDC)wParam;
485             HBRUSH hbr = (HBRUSH)GetClassLongW( hwnd, GCL_HBRBACKGROUND );
486             if (!hbr) return 0;
487
488             if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
489             {
490                 /* can't use GetClipBox with a parent DC or we fill the whole parent */
491                 GetClientRect( hwnd, &rect );
492                 DPtoLP( hdc, (LPPOINT)&rect, 2 );
493             }
494             else GetClipBox( hdc, &rect );
495             FillRect( hdc, &rect, hbr );
496             return 1;
497         }
498
499     case WM_GETDLGCODE:
500         return 0;
501
502     case WM_CTLCOLORMSGBOX:
503     case WM_CTLCOLOREDIT:
504     case WM_CTLCOLORLISTBOX:
505     case WM_CTLCOLORBTN:
506     case WM_CTLCOLORDLG:
507     case WM_CTLCOLORSTATIC:
508     case WM_CTLCOLORSCROLLBAR:
509         return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
510
511     case WM_CTLCOLOR:
512         return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
513
514     case WM_SETCURSOR:
515         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
516         {
517             /* with the exception of the border around a resizable wnd,
518              * give the parent first chance to set the cursor */
519             if ((LOWORD(lParam) < HTSIZEFIRST) || (LOWORD(lParam) > HTSIZELAST))
520             {
521                 if (SendMessageW(GetParent(hwnd), WM_SETCURSOR, wParam, lParam)) return TRUE;
522             }
523         }
524         NC_HandleSetCursor( hwnd, wParam, lParam );
525         break;
526
527     case WM_SYSCOMMAND:
528         return NC_HandleSysCommand( hwnd, wParam, lParam );
529
530     case WM_KEYDOWN:
531         if(wParam == VK_F10) iF10Key = VK_F10;
532         break;
533
534     case WM_SYSKEYDOWN:
535         if( HIWORD(lParam) & KEYDATA_ALT )
536         {
537             /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
538               if( wParam == VK_MENU && !iMenuSysKey )
539                 iMenuSysKey = 1;
540               else
541                 iMenuSysKey = 0;
542
543             iF10Key = 0;
544
545             if( wParam == VK_F4 )       /* try to close the window */
546             {
547                 HWND top = GetAncestor( hwnd, GA_ROOT );
548                 if (!(GetClassLongW( top, GCL_STYLE ) & CS_NOCLOSE))
549                     PostMessageW( top, WM_SYSCOMMAND, SC_CLOSE, 0 );
550             }
551         }
552         else if( wParam == VK_F10 )
553                 iF10Key = 1;
554              else
555                 if( wParam == VK_ESCAPE && (GetKeyState(VK_SHIFT) & 0x8000))
556                     SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, ' ' );
557         break;
558
559     case WM_KEYUP:
560     case WM_SYSKEYUP:
561         /* Press and release F10 or ALT */
562         if (((wParam == VK_MENU) && iMenuSysKey) ||
563             ((wParam == VK_F10) && iF10Key))
564               SendMessageW( GetAncestor( hwnd, GA_ROOT ), WM_SYSCOMMAND, SC_KEYMENU, 0L );
565         iMenuSysKey = iF10Key = 0;
566         break;
567
568     case WM_SYSCHAR:
569     {
570         iMenuSysKey = 0;
571         if (wParam == '\r' && IsIconic(hwnd))
572         {
573             PostMessageW( hwnd, WM_SYSCOMMAND, SC_RESTORE, 0L );
574             break;
575         }
576         if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
577         {
578             if (wParam == '\t' || wParam == '\x1b') break;
579             if (wParam == ' ' && (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD))
580                 SendMessageW( GetParent(hwnd), msg, wParam, lParam );
581             else
582                 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, wParam );
583         }
584         else /* check for Ctrl-Esc */
585             if (wParam != '\x1b') MessageBeep(0);
586         break;
587     }
588
589     case WM_SHOWWINDOW:
590         {
591             LONG style = GetWindowLongW( hwnd, GWL_STYLE );
592             if (!lParam) return 0; /* sent from ShowWindow */
593             if (!(style & WS_POPUP)) return 0;
594             if ((style & WS_VISIBLE) && wParam) return 0;
595             if (!(style & WS_VISIBLE) && !wParam) return 0;
596             if (!GetWindow( hwnd, GW_OWNER )) return 0;
597             ShowWindow( hwnd, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
598             break;
599         }
600
601     case WM_CANCELMODE:
602         if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)) EndMenu();
603         if (GetCapture() == hwnd) ReleaseCapture();
604         break;
605
606     case WM_VKEYTOITEM:
607     case WM_CHARTOITEM:
608         return -1;
609
610     case WM_DROPOBJECT:
611         return DRAG_FILE;
612
613     case WM_QUERYDROPOBJECT:
614         return (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES) != 0;
615
616     case WM_QUERYDRAGICON:
617         {
618             UINT len;
619
620             HICON hIcon = (HICON)GetClassLongW( hwnd, GCL_HICON );
621             HINSTANCE instance = (HINSTANCE)GetWindowLongW( hwnd, GWL_HINSTANCE );
622             if (hIcon) return (LRESULT)hIcon;
623             for(len=1; len<64; len++)
624                 if((hIcon = LoadIconW(instance, MAKEINTRESOURCEW(len))))
625                     return (LRESULT)hIcon;
626             return (LRESULT)LoadIconW(0, (LPWSTR)IDI_APPLICATION);
627         }
628         break;
629
630     case WM_ISACTIVEICON:
631         {
632             WND *wndPtr = WIN_GetPtr( hwnd );
633             BOOL ret = (wndPtr->flags & WIN_NCACTIVATED) != 0;
634             WIN_ReleasePtr( wndPtr );
635             return ret;
636         }
637
638     case WM_NOTIFYFORMAT:
639       if (IsWindowUnicode(hwnd)) return NFR_UNICODE;
640       else return NFR_ANSI;
641
642     case WM_QUERYOPEN:
643     case WM_QUERYENDSESSION:
644         return 1;
645
646     case WM_SETICON:
647         {
648             HICON ret;
649             WND *wndPtr = WIN_GetPtr( hwnd );
650
651             switch(wParam)
652             {
653             case ICON_SMALL:
654                 ret = wndPtr->hIconSmall;
655                 wndPtr->hIconSmall = (HICON)lParam;
656                 break;
657             case ICON_BIG:
658                 ret = wndPtr->hIcon;
659                 wndPtr->hIcon = (HICON)lParam;
660                 break;
661             default:
662                 ret = 0;
663                 break;
664             }
665             WIN_ReleasePtr( wndPtr );
666
667             if (USER_Driver.pSetWindowIcon)
668                 USER_Driver.pSetWindowIcon( hwnd, wParam, (HICON)lParam );
669
670             SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE |
671                          SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
672
673             return (LRESULT)ret;
674         }
675
676     case WM_GETICON:
677         {
678             HICON ret;
679             WND *wndPtr = WIN_GetPtr( hwnd );
680
681             switch(wParam)
682             {
683             case ICON_SMALL:
684                 ret = wndPtr->hIconSmall;
685                 break;
686             case ICON_BIG:
687                 ret = wndPtr->hIcon;
688                 break;
689             case ICON_SMALL2:
690                 ret = wndPtr->hIconSmall;
691                 if (!ret) ret = (HICON)GetClassLongA( hwnd, GCL_HICONSM );
692                 /* FIXME: should have a default here if class icon is null */
693                 break;
694             default:
695                 ret = 0;
696                 break;
697             }
698             WIN_ReleasePtr( wndPtr );
699             return (LRESULT)ret;
700         }
701
702     case WM_HELP:
703         SendMessageW( GetParent(hwnd), msg, wParam, lParam );
704         break;
705     }
706
707     return 0;
708 }
709
710
711
712 /***********************************************************************
713  *              DefWindowProc (USER.107)
714  */
715 LRESULT WINAPI DefWindowProc16( HWND16 hwnd16, UINT16 msg, WPARAM16 wParam,
716                                 LPARAM lParam )
717 {
718     LRESULT result = 0;
719     HWND hwnd = WIN_Handle32( hwnd16 );
720
721     if (!WIN_IsCurrentProcess( hwnd ))
722     {
723         if (!IsWindow( hwnd )) return 0;
724         ERR( "called for other process window %p\n", hwnd );
725         return 0;
726     }
727     SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
728
729     switch(msg)
730     {
731     case WM_NCCREATE:
732         {
733             CREATESTRUCT16 *cs = MapSL(lParam);
734             /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
735              * may have child window IDs instead of window name */
736             if (HIWORD(cs->lpszName))
737                 DEFWND_SetTextA( hwnd, MapSL(cs->lpszName) );
738             result = 1;
739         }
740         break;
741
742     case WM_NCCALCSIZE:
743         {
744             RECT rect32;
745             CONV_RECT16TO32( MapSL(lParam), &rect32 );
746             result = NC_HandleNCCalcSize( hwnd, &rect32 );
747             CONV_RECT32TO16( &rect32, MapSL(lParam) );
748         }
749         break;
750
751     case WM_WINDOWPOSCHANGING:
752         result = WINPOS_HandleWindowPosChanging16( hwnd, MapSL(lParam) );
753         break;
754
755     case WM_WINDOWPOSCHANGED:
756         {
757             WINDOWPOS16 * winPos = MapSL(lParam);
758             DEFWND_HandleWindowPosChanged( hwnd, winPos->flags );
759         }
760         break;
761
762     case WM_GETTEXT:
763     case WM_SETTEXT:
764         result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)MapSL(lParam) );
765         break;
766
767     default:
768         result = DefWindowProcA( hwnd, msg, wParam, lParam );
769         break;
770     }
771
772     SPY_ExitMessage( SPY_RESULT_DEFWND16, hwnd, msg, result, wParam, lParam );
773     return result;
774 }
775
776
777 /***********************************************************************
778  *              DefWindowProcA (USER32.@)
779  *
780  */
781 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
782 {
783     LRESULT result = 0;
784     HWND full_handle;
785
786     if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
787     {
788         if (!IsWindow( hwnd )) return 0;
789         ERR( "called for other process window %p\n", hwnd );
790         return 0;
791     }
792     hwnd = full_handle;
793
794     SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
795
796     switch(msg)
797     {
798     case WM_NCCREATE:
799         {
800             CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
801             /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
802              * may have child window IDs instead of window name */
803             if (HIWORD(cs->lpszName))
804                 DEFWND_SetTextA( hwnd, cs->lpszName );
805             result = 1;
806         }
807         break;
808
809     case WM_NCCALCSIZE:
810         result = NC_HandleNCCalcSize( hwnd, (RECT *)lParam );
811         break;
812
813     case WM_WINDOWPOSCHANGING:
814         result = WINPOS_HandleWindowPosChanging( hwnd, (WINDOWPOS *)lParam );
815         break;
816
817     case WM_WINDOWPOSCHANGED:
818         {
819             WINDOWPOS * winPos = (WINDOWPOS *)lParam;
820             DEFWND_HandleWindowPosChanged( hwnd, winPos->flags );
821         }
822         break;
823
824     case WM_GETTEXTLENGTH:
825         {
826             WND *wndPtr = WIN_GetPtr( hwnd );
827             if (wndPtr && wndPtr->text)
828                 result = WideCharToMultiByte( CP_ACP, 0, wndPtr->text, strlenW(wndPtr->text),
829                                               NULL, 0, NULL, NULL );
830             WIN_ReleasePtr( wndPtr );
831         }
832         break;
833
834     case WM_GETTEXT:
835         if (wParam)
836         {
837             LPSTR dest = (LPSTR)lParam;
838             WND *wndPtr = WIN_GetPtr( hwnd );
839
840             if (!wndPtr) break;
841             if (wndPtr->text)
842             {
843                 if (!WideCharToMultiByte( CP_ACP, 0, wndPtr->text, -1,
844                                           dest, wParam, NULL, NULL )) dest[wParam-1] = 0;
845                 result = strlen( dest );
846             }
847             else dest[0] = '\0';
848             WIN_ReleasePtr( wndPtr );
849         }
850         break;
851
852     case WM_SETTEXT:
853         DEFWND_SetTextA( hwnd, (LPCSTR)lParam );
854         if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
855             NC_HandleNCPaint( hwnd , (HRGN)1 );  /* Repaint caption */
856         result = 1; /* success. FIXME: check text length */
857         break;
858
859     /* for far east users (IMM32) - <hidenori@a2.ctktv.ne.jp> */
860     case WM_IME_CHAR:
861         {
862             CHAR    chChar1 = (CHAR)( (wParam>>8) & 0xff );
863             CHAR    chChar2 = (CHAR)( wParam & 0xff );
864
865             if (chChar1)
866                 SendMessageA( hwnd, WM_CHAR, (WPARAM)chChar1, lParam );
867             SendMessageA( hwnd, WM_CHAR, (WPARAM)chChar2, lParam );
868         }
869         break;
870     case WM_IME_KEYDOWN:
871         result = SendMessageA( hwnd, WM_KEYDOWN, wParam, lParam );
872         break;
873     case WM_IME_KEYUP:
874         result = SendMessageA( hwnd, WM_KEYUP, wParam, lParam );
875         break;
876
877     case WM_IME_STARTCOMPOSITION:
878     case WM_IME_COMPOSITION:
879     case WM_IME_ENDCOMPOSITION:
880     case WM_IME_SELECT:
881         {
882             HWND hwndIME;
883
884             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
885             if (hwndIME)
886                 result = SendMessageA( hwndIME, msg, wParam, lParam );
887         }
888         break;
889     case WM_IME_SETCONTEXT:
890         {
891             HWND hwndIME;
892
893             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
894             if (hwndIME)
895                 result = DEFWND_ImmIsUIMessageA( hwndIME, msg, wParam, lParam );
896         }
897         break;
898
899     case WM_INPUTLANGCHANGEREQUEST:
900         /* notify about the switch only if it's really our current layout */
901         if ((HKL)lParam == GetKeyboardLayout(0))
902             result = SendMessageA( hwnd, WM_INPUTLANGCHANGE, wParam, lParam );
903         else
904             result = 0;
905         break;
906
907     case WM_SYSCHAR:
908     {
909         BYTE ch = LOWORD(wParam);
910         WCHAR wch;
911         MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
912         wParam = MAKEWPARAM( wch, HIWORD(wParam) );
913     }
914     /* fall through */
915     default:
916         result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
917         break;
918     }
919
920     SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
921     return result;
922 }
923
924
925 /***********************************************************************
926  *              DefWindowProcW (USER32.@) Calls default window message handler
927  *
928  * Calls default window procedure for messages not processed
929  *  by application.
930  *
931  *  RETURNS
932  *     Return value is dependent upon the message.
933 */
934 LRESULT WINAPI DefWindowProcW(
935     HWND hwnd,      /* [in] window procedure receiving message */
936     UINT msg,       /* [in] message identifier */
937     WPARAM wParam,  /* [in] first message parameter */
938     LPARAM lParam )   /* [in] second message parameter */
939 {
940     LRESULT result = 0;
941     HWND full_handle;
942
943     if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
944     {
945         if (!IsWindow( hwnd )) return 0;
946         ERR( "called for other process window %p\n", hwnd );
947         return 0;
948     }
949     hwnd = full_handle;
950     SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
951
952     switch(msg)
953     {
954     case WM_NCCREATE:
955         {
956             CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
957             /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
958              * may have child window IDs instead of window name */
959             if (HIWORD(cs->lpszName))
960                 DEFWND_SetTextW( hwnd, cs->lpszName );
961             result = 1;
962         }
963         break;
964
965     case WM_NCCALCSIZE:
966         result = NC_HandleNCCalcSize( hwnd, (RECT *)lParam );
967         break;
968
969     case WM_WINDOWPOSCHANGING:
970         result = WINPOS_HandleWindowPosChanging( hwnd, (WINDOWPOS *)lParam );
971         break;
972
973     case WM_WINDOWPOSCHANGED:
974         {
975             WINDOWPOS * winPos = (WINDOWPOS *)lParam;
976             DEFWND_HandleWindowPosChanged( hwnd, winPos->flags );
977         }
978         break;
979
980     case WM_GETTEXTLENGTH:
981         {
982             WND *wndPtr = WIN_GetPtr( hwnd );
983             if (wndPtr && wndPtr->text) result = (LRESULT)strlenW(wndPtr->text);
984             WIN_ReleasePtr( wndPtr );
985         }
986         break;
987
988     case WM_GETTEXT:
989         if (wParam)
990         {
991             LPWSTR dest = (LPWSTR)lParam;
992             WND *wndPtr = WIN_GetPtr( hwnd );
993
994             if (!wndPtr) break;
995             if (wndPtr->text)
996             {
997                 lstrcpynW( dest, wndPtr->text, wParam );
998                 result = strlenW( dest );
999             }
1000             else dest[0] = '\0';
1001             WIN_ReleasePtr( wndPtr );
1002         }
1003         break;
1004
1005     case WM_SETTEXT:
1006         DEFWND_SetTextW( hwnd, (LPCWSTR)lParam );
1007         if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
1008             NC_HandleNCPaint( hwnd , (HRGN)1 );  /* Repaint caption */
1009         result = 1; /* success. FIXME: check text length */
1010         break;
1011
1012     /* for far east users (IMM32) - <hidenori@a2.ctktv.ne.jp> */
1013     case WM_IME_CHAR:
1014         SendMessageW( hwnd, WM_CHAR, wParam, lParam );
1015         break;
1016     case WM_IME_SETCONTEXT:
1017         {
1018             HWND hwndIME;
1019
1020             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
1021             if (hwndIME)
1022                 result = DEFWND_ImmIsUIMessageW( hwndIME, msg, wParam, lParam );
1023         }
1024         break;
1025
1026     case WM_INPUTLANGCHANGEREQUEST:
1027         /* notify about the switch only if it's really our current layout */
1028         if ((HKL)lParam == GetKeyboardLayout(0))
1029             result = SendMessageW( hwnd, WM_INPUTLANGCHANGE, wParam, lParam );
1030         else
1031             result = 0;
1032         break;
1033
1034     default:
1035         result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
1036         break;
1037     }
1038     SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
1039     return result;
1040 }