Do not change focus if the being activated window is no longer
[wine] / dlls / user / 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 #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 "winpos.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 static const WCHAR imm32W[] = { 'i','m','m','3','2','\0' };
51
52 /***********************************************************************
53  *           DEFWND_HandleWindowPosChanged
54  *
55  * Handle the WM_WINDOWPOSCHANGED message.
56  */
57 static void DEFWND_HandleWindowPosChanged( HWND hwnd, const WINDOWPOS *winpos )
58 {
59     RECT rect;
60     WND *wndPtr = WIN_GetPtr( hwnd );
61
62     rect = wndPtr->rectClient;
63     WIN_ReleasePtr( wndPtr );
64
65     if (!(winpos->flags & SWP_NOCLIENTMOVE))
66         SendMessageW( hwnd, WM_MOVE, 0, MAKELONG(rect.left, rect.top));
67
68     if (!(winpos->flags & SWP_NOCLIENTSIZE))
69     {
70         WPARAM wp = SIZE_RESTORED;
71         if (IsZoomed(hwnd)) wp = SIZE_MAXIMIZED;
72         else if (IsIconic(hwnd)) wp = SIZE_MINIMIZED;
73
74         SendMessageW( hwnd, WM_SIZE, wp, MAKELONG(rect.right-rect.left, rect.bottom-rect.top) );
75     }
76 }
77
78
79 /***********************************************************************
80  *           DEFWND_SetTextA
81  *
82  * Set the window text.
83  */
84 static void DEFWND_SetTextA( HWND hwnd, LPCSTR text )
85 {
86     int count;
87     WCHAR *textW;
88     WND *wndPtr;
89
90     if (!text) text = "";
91     count = MultiByteToWideChar( CP_ACP, 0, text, -1, NULL, 0 );
92
93     if (!(wndPtr = WIN_GetPtr( hwnd ))) return;
94     if ((textW = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR))))
95     {
96         HeapFree(GetProcessHeap(), 0, wndPtr->text);
97         wndPtr->text = textW;
98         MultiByteToWideChar( CP_ACP, 0, text, -1, textW, count );
99         SERVER_START_REQ( set_window_text )
100         {
101             req->handle = hwnd;
102             wine_server_add_data( req, textW, (count-1) * sizeof(WCHAR) );
103             wine_server_call( req );
104         }
105         SERVER_END_REQ;
106     }
107     else
108         ERR("Not enough memory for window text\n");
109     WIN_ReleasePtr( wndPtr );
110
111     USER_Driver->pSetWindowText( hwnd, textW );
112 }
113
114 /***********************************************************************
115  *           DEFWND_SetTextW
116  *
117  * Set the window text.
118  */
119 static void DEFWND_SetTextW( HWND hwnd, LPCWSTR text )
120 {
121     static const WCHAR empty_string[] = {0};
122     WND *wndPtr;
123     int count;
124
125     if (!text) text = empty_string;
126     count = strlenW(text) + 1;
127
128     if (!(wndPtr = WIN_GetPtr( hwnd ))) return;
129     HeapFree(GetProcessHeap(), 0, wndPtr->text);
130     if ((wndPtr->text = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR))))
131     {
132         strcpyW( wndPtr->text, text );
133         SERVER_START_REQ( set_window_text )
134         {
135             req->handle = hwnd;
136             wine_server_add_data( req, wndPtr->text, (count-1) * sizeof(WCHAR) );
137             wine_server_call( req );
138         }
139         SERVER_END_REQ;
140     }
141     else
142         ERR("Not enough memory for window text\n");
143     text = wndPtr->text;
144     WIN_ReleasePtr( wndPtr );
145
146     USER_Driver->pSetWindowText( hwnd, text );
147 }
148
149 /***********************************************************************
150  *           DEFWND_ControlColor
151  *
152  * Default colors for control painting.
153  */
154 HBRUSH DEFWND_ControlColor( HDC hDC, UINT ctlType )
155 {
156     if( ctlType == CTLCOLOR_SCROLLBAR)
157     {
158         HBRUSH hb = GetSysColorBrush(COLOR_SCROLLBAR);
159         COLORREF bk = GetSysColor(COLOR_3DHILIGHT);
160         SetTextColor( hDC, GetSysColor(COLOR_3DFACE));
161         SetBkColor( hDC, bk);
162
163         /* if COLOR_WINDOW happens to be the same as COLOR_3DHILIGHT
164          * we better use 0x55aa bitmap brush to make scrollbar's background
165          * look different from the window background.
166          */
167         if (bk == GetSysColor(COLOR_WINDOW))
168             return SYSCOLOR_55AABrush;
169
170         UnrealizeObject( hb );
171         return hb;
172     }
173
174     SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT));
175
176     if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
177         SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
178     else {
179         SetBkColor( hDC, GetSysColor(COLOR_3DFACE) );
180         return GetSysColorBrush(COLOR_3DFACE);
181     }
182     return GetSysColorBrush(COLOR_WINDOW);
183 }
184
185
186 /***********************************************************************
187  *           DEFWND_Print
188  *
189  * This method handles the default behavior for the WM_PRINT message.
190  */
191 static void DEFWND_Print( HWND hwnd, HDC hdc, ULONG uFlags)
192 {
193   /*
194    * Visibility flag.
195    */
196   if ( (uFlags & PRF_CHECKVISIBLE) &&
197        !IsWindowVisible(hwnd) )
198       return;
199
200   /*
201    * Unimplemented flags.
202    */
203   if ( (uFlags & PRF_CHILDREN) ||
204        (uFlags & PRF_OWNED)    ||
205        (uFlags & PRF_NONCLIENT) )
206   {
207     WARN("WM_PRINT message with unsupported flags\n");
208   }
209
210   /*
211    * Background
212    */
213   if ( uFlags & PRF_ERASEBKGND)
214     SendMessageW(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
215
216   /*
217    * Client area
218    */
219   if ( uFlags & PRF_CLIENT)
220     SendMessageW(hwnd, WM_PRINTCLIENT, (WPARAM)hdc, PRF_CLIENT);
221 }
222
223
224 /*
225  * helpers for calling IMM32
226  *
227  * WM_IME_* messages are generated only by IMM32,
228  * so I assume imm32 is already LoadLibrary-ed.
229  */
230 static HWND DEFWND_ImmGetDefaultIMEWnd( HWND hwnd )
231 {
232     HINSTANCE hInstIMM = GetModuleHandleW( imm32W );
233     HWND (WINAPI *pFunc)(HWND);
234     HWND hwndRet = 0;
235
236     if (!hInstIMM)
237     {
238         ERR( "cannot get IMM32 handle\n" );
239         return 0;
240     }
241
242     pFunc = (void*)GetProcAddress(hInstIMM,"ImmGetDefaultIMEWnd");
243     if ( pFunc != NULL )
244         hwndRet = (*pFunc)( hwnd );
245
246     return hwndRet;
247 }
248
249 static BOOL DEFWND_ImmIsUIMessageA( HWND hwndIME, UINT msg, WPARAM wParam, LPARAM lParam )
250 {
251     HINSTANCE hInstIMM = GetModuleHandleW( imm32W );
252     BOOL (WINAPI *pFunc)(HWND,UINT,WPARAM,LPARAM);
253     BOOL fRet = FALSE;
254
255     if (!hInstIMM)
256     {
257         ERR( "cannot get IMM32 handle\n" );
258         return FALSE;
259     }
260
261     pFunc = (void*)GetProcAddress(hInstIMM,"ImmIsUIMessageA");
262     if ( pFunc != NULL )
263         fRet = (*pFunc)( hwndIME, msg, wParam, lParam );
264
265     return fRet;
266 }
267
268 static BOOL DEFWND_ImmIsUIMessageW( HWND hwndIME, UINT msg, WPARAM wParam, LPARAM lParam )
269 {
270     HINSTANCE hInstIMM = GetModuleHandleW( imm32W );
271     BOOL (WINAPI *pFunc)(HWND,UINT,WPARAM,LPARAM);
272     BOOL fRet = FALSE;
273
274     if (!hInstIMM)
275     {
276         ERR( "cannot get IMM32 handle\n" );
277         return FALSE;
278     }
279
280     pFunc = (void*)GetProcAddress(hInstIMM,"ImmIsUIMessageW");
281     if ( pFunc != NULL )
282         fRet = (*pFunc)( hwndIME, msg, wParam, lParam );
283
284     return fRet;
285 }
286
287
288
289 /***********************************************************************
290  *           DEFWND_DefWinProc
291  *
292  * Default window procedure for messages that are the same in Ansi and Unicode.
293  */
294 static LRESULT DEFWND_DefWinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
295 {
296     switch(msg)
297     {
298     case WM_NCPAINT:
299         return NC_HandleNCPaint( hwnd, (HRGN)wParam );
300
301     case WM_NCHITTEST:
302         {
303             POINT pt;
304             pt.x = (short)LOWORD(lParam);
305             pt.y = (short)HIWORD(lParam);
306             return NC_HandleNCHitTest( hwnd, pt );
307         }
308
309     case WM_NCCALCSIZE:
310         return NC_HandleNCCalcSize( hwnd, (RECT *)lParam );
311
312     case WM_WINDOWPOSCHANGING:
313         return WINPOS_HandleWindowPosChanging( hwnd, (WINDOWPOS *)lParam );
314
315     case WM_WINDOWPOSCHANGED:
316         DEFWND_HandleWindowPosChanged( hwnd, (const WINDOWPOS *)lParam );
317         break;
318
319     case WM_LBUTTONDOWN:
320     case WM_RBUTTONDOWN:
321     case WM_MBUTTONDOWN:
322         iF10Key = iMenuSysKey = 0;
323         break;
324
325     case WM_NCLBUTTONDOWN:
326         return NC_HandleNCLButtonDown( hwnd, wParam, lParam );
327
328     case WM_LBUTTONDBLCLK:
329     case WM_NCLBUTTONDBLCLK:
330         return NC_HandleNCLButtonDblClk( hwnd, wParam, lParam );
331
332     case WM_NCRBUTTONDOWN:
333         /* in Windows, capture is taken when right-clicking on the caption bar */
334         if (wParam==HTCAPTION)
335         {
336             SetCapture(hwnd);
337         }
338         break;
339
340     case WM_RBUTTONUP:
341         {
342             POINT pt;
343
344             if (hwnd == GetCapture())
345                 /* release capture if we took it on WM_NCRBUTTONDOWN */
346                 ReleaseCapture();
347
348             pt.x = (short)LOWORD(lParam);
349             pt.y = (short)HIWORD(lParam);
350             ClientToScreen(hwnd, &pt);
351             SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM)hwnd, MAKELPARAM(pt.x, pt.y) );
352         }
353         break;
354
355     case WM_NCRBUTTONUP:
356         /*
357          * FIXME : we must NOT send WM_CONTEXTMENU on a WM_NCRBUTTONUP (checked
358          * in Windows), but what _should_ we do? According to MSDN :
359          * "If it is appropriate to do so, the system sends the WM_SYSCOMMAND
360          * message to the window". When is it appropriate?
361          */
362         break;
363
364     case WM_CONTEXTMENU:
365         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
366             SendMessageW( GetParent(hwnd), msg, wParam, lParam );
367         else
368         {
369             LONG hitcode;
370             POINT pt;
371             WND *wndPtr = WIN_GetPtr( hwnd );
372             HMENU hMenu = wndPtr->hSysMenu;
373             WIN_ReleasePtr( wndPtr );
374             if (!hMenu) return 0;
375             pt.x = (short)LOWORD(lParam);
376             pt.y = (short)HIWORD(lParam);
377             hitcode = NC_HandleNCHitTest(hwnd, pt);
378
379             /* Track system popup if click was in the caption area. */
380             if (hitcode==HTCAPTION || hitcode==HTSYSMENU)
381                TrackPopupMenu(GetSystemMenu(hwnd, FALSE),
382                                TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
383                                pt.x, pt.y, 0, hwnd, NULL);
384         }
385         break;
386
387     case WM_NCACTIVATE:
388         return NC_HandleNCActivate( hwnd, wParam );
389
390     case WM_NCDESTROY:
391         {
392             WND *wndPtr = WIN_GetPtr( hwnd );
393             if (!wndPtr) return 0;
394             HeapFree( GetProcessHeap(), 0, wndPtr->text );
395             wndPtr->text = NULL;
396             HeapFree( GetProcessHeap(), 0, wndPtr->pVScroll );
397             HeapFree( GetProcessHeap(), 0, wndPtr->pHScroll );
398             wndPtr->pVScroll = wndPtr->pHScroll = NULL;
399             WIN_ReleasePtr( wndPtr );
400             return 0;
401         }
402
403     case WM_PRINT:
404         DEFWND_Print(hwnd, (HDC)wParam, lParam);
405         return 0;
406
407     case WM_PAINTICON:
408     case WM_PAINT:
409         {
410             PAINTSTRUCT ps;
411             HDC hdc = BeginPaint( hwnd, &ps );
412             if( hdc )
413             {
414               HICON hIcon;
415               if (IsIconic(hwnd) && ((hIcon = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON))) )
416               {
417                   RECT rc;
418                   int x, y;
419
420                   GetClientRect( hwnd, &rc );
421                   x = (rc.right - rc.left - GetSystemMetrics(SM_CXICON))/2;
422                   y = (rc.bottom - rc.top - GetSystemMetrics(SM_CYICON))/2;
423                   TRACE("Painting class icon: vis rect=(%ld,%ld - %ld,%ld)\n",
424                         ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
425                   DrawIcon( hdc, x, y, hIcon );
426               }
427               EndPaint( hwnd, &ps );
428             }
429             return 0;
430         }
431
432     case WM_SYNCPAINT:
433         RedrawWindow ( hwnd, NULL, 0, RDW_ERASENOW | RDW_ERASE | RDW_ALLCHILDREN );
434         return 0;
435
436     case WM_SETREDRAW:
437         if (wParam) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
438         else
439         {
440             RedrawWindow( hwnd, NULL, 0, RDW_ALLCHILDREN | RDW_VALIDATE );
441             WIN_SetStyle( hwnd, 0, WS_VISIBLE );
442         }
443         return 0;
444
445     case WM_CLOSE:
446         DestroyWindow( hwnd );
447         return 0;
448
449     case WM_MOUSEACTIVATE:
450         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
451         {
452             LONG ret = SendMessageW( GetParent(hwnd), WM_MOUSEACTIVATE, wParam, lParam );
453             if (ret) return ret;
454         }
455
456         /* Caption clicks are handled by NC_HandleNCLButtonDown() */
457         return MA_ACTIVATE;
458
459     case WM_ACTIVATE:
460         /* The default action in Windows is to set the keyboard focus to
461          * the window, if it's being activated and not minimized */
462         if (LOWORD(wParam) != WA_INACTIVE) {
463             if (!IsIconic(hwnd)) SetFocus(hwnd);
464         }
465         break;
466
467     case WM_MOUSEWHEEL:
468         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
469             return SendMessageW( GetParent(hwnd), WM_MOUSEWHEEL, wParam, lParam );
470         break;
471
472     case WM_ERASEBKGND:
473     case WM_ICONERASEBKGND:
474         {
475             RECT rect;
476             HDC hdc = (HDC)wParam;
477             HBRUSH hbr = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND );
478             if (!hbr) return 0;
479
480             if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
481             {
482                 /* can't use GetClipBox with a parent DC or we fill the whole parent */
483                 GetClientRect( hwnd, &rect );
484                 DPtoLP( hdc, (LPPOINT)&rect, 2 );
485             }
486             else GetClipBox( hdc, &rect );
487             FillRect( hdc, &rect, hbr );
488             return 1;
489         }
490
491     case WM_GETDLGCODE:
492         return 0;
493
494     case WM_CTLCOLORMSGBOX:
495     case WM_CTLCOLOREDIT:
496     case WM_CTLCOLORLISTBOX:
497     case WM_CTLCOLORBTN:
498     case WM_CTLCOLORDLG:
499     case WM_CTLCOLORSTATIC:
500     case WM_CTLCOLORSCROLLBAR:
501         return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
502
503     case WM_CTLCOLOR:
504         return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
505
506     case WM_SETCURSOR:
507         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
508         {
509             /* with the exception of the border around a resizable wnd,
510              * give the parent first chance to set the cursor */
511             if ((LOWORD(lParam) < HTSIZEFIRST) || (LOWORD(lParam) > HTSIZELAST))
512             {
513                 if (SendMessageW(GetParent(hwnd), WM_SETCURSOR, wParam, lParam)) return TRUE;
514             }
515         }
516         NC_HandleSetCursor( hwnd, wParam, lParam );
517         break;
518
519     case WM_SYSCOMMAND:
520         return NC_HandleSysCommand( hwnd, wParam, lParam );
521
522     case WM_KEYDOWN:
523         if(wParam == VK_F10) iF10Key = VK_F10;
524         break;
525
526     case WM_SYSKEYDOWN:
527         if( HIWORD(lParam) & KEYDATA_ALT )
528         {
529             /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
530               if( wParam == VK_MENU && !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) && iMenuSysKey) ||
554             ((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_SETICON:
652         {
653             HICON ret;
654             WND *wndPtr = WIN_GetPtr( hwnd );
655
656             switch(wParam)
657             {
658             case ICON_SMALL:
659                 ret = wndPtr->hIconSmall;
660                 wndPtr->hIconSmall = (HICON)lParam;
661                 break;
662             case ICON_BIG:
663                 ret = wndPtr->hIcon;
664                 wndPtr->hIcon = (HICON)lParam;
665                 break;
666             default:
667                 ret = 0;
668                 break;
669             }
670             WIN_ReleasePtr( wndPtr );
671
672             USER_Driver->pSetWindowIcon( hwnd, wParam, (HICON)lParam );
673
674             SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE |
675                          SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
676
677             return (LRESULT)ret;
678         }
679
680     case WM_GETICON:
681         {
682             HICON ret;
683             WND *wndPtr = WIN_GetPtr( hwnd );
684
685             switch(wParam)
686             {
687             case ICON_SMALL:
688                 ret = wndPtr->hIconSmall;
689                 break;
690             case ICON_BIG:
691                 ret = wndPtr->hIcon;
692                 break;
693             case ICON_SMALL2:
694                 ret = wndPtr->hIconSmall;
695                 if (!ret) ret = (HICON)GetClassLongPtrW( hwnd, GCLP_HICONSM );
696                 /* FIXME: should have a default here if class icon is null */
697                 break;
698             default:
699                 ret = 0;
700                 break;
701             }
702             WIN_ReleasePtr( wndPtr );
703             return (LRESULT)ret;
704         }
705
706     case WM_HELP:
707         SendMessageW( GetParent(hwnd), msg, wParam, lParam );
708         break;
709     }
710
711     return 0;
712 }
713
714
715
716 /***********************************************************************
717  *              DefWindowProcA (USER32.@)
718  *
719  * See DefWindowProcW.
720  */
721 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
722 {
723     LRESULT result = 0;
724     HWND full_handle;
725
726     if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
727     {
728         if (!IsWindow( hwnd )) return 0;
729         ERR( "called for other process window %p\n", hwnd );
730         return 0;
731     }
732     hwnd = full_handle;
733
734     SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
735
736     switch(msg)
737     {
738     case WM_NCCREATE:
739         {
740             CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
741             /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
742              * may have child window IDs instead of window name */
743             if (HIWORD(cs->lpszName))
744                 DEFWND_SetTextA( hwnd, cs->lpszName );
745             result = 1;
746         }
747         break;
748
749     case WM_GETTEXTLENGTH:
750         {
751             WND *wndPtr = WIN_GetPtr( hwnd );
752             if (wndPtr && wndPtr->text)
753                 result = WideCharToMultiByte( CP_ACP, 0, wndPtr->text, strlenW(wndPtr->text),
754                                               NULL, 0, NULL, NULL );
755             WIN_ReleasePtr( wndPtr );
756         }
757         break;
758
759     case WM_GETTEXT:
760         if (wParam)
761         {
762             LPSTR dest = (LPSTR)lParam;
763             WND *wndPtr = WIN_GetPtr( hwnd );
764
765             if (!wndPtr) break;
766             if (wndPtr->text)
767             {
768                 if (!WideCharToMultiByte( CP_ACP, 0, wndPtr->text, -1,
769                                           dest, wParam, NULL, NULL )) dest[wParam-1] = 0;
770                 result = strlen( dest );
771             }
772             else dest[0] = '\0';
773             WIN_ReleasePtr( wndPtr );
774         }
775         break;
776
777     case WM_SETTEXT:
778         DEFWND_SetTextA( hwnd, (LPCSTR)lParam );
779         if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
780             NC_HandleNCPaint( hwnd , (HRGN)1 );  /* Repaint caption */
781         result = 1; /* success. FIXME: check text length */
782         break;
783
784     /* for far east users (IMM32) - <hidenori@a2.ctktv.ne.jp> */
785     case WM_IME_CHAR:
786         {
787             CHAR    chChar1 = (CHAR)( (wParam>>8) & 0xff );
788             CHAR    chChar2 = (CHAR)( wParam & 0xff );
789
790             if (chChar1)
791                 SendMessageA( hwnd, WM_CHAR, (WPARAM)chChar1, lParam );
792             SendMessageA( hwnd, WM_CHAR, (WPARAM)chChar2, lParam );
793         }
794         break;
795     case WM_IME_KEYDOWN:
796         result = SendMessageA( hwnd, WM_KEYDOWN, wParam, lParam );
797         break;
798     case WM_IME_KEYUP:
799         result = SendMessageA( hwnd, WM_KEYUP, wParam, lParam );
800         break;
801
802     case WM_IME_STARTCOMPOSITION:
803     case WM_IME_COMPOSITION:
804     case WM_IME_ENDCOMPOSITION:
805     case WM_IME_SELECT:
806         {
807             HWND hwndIME;
808
809             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
810             if (hwndIME)
811                 result = SendMessageA( hwndIME, msg, wParam, lParam );
812         }
813         break;
814     case WM_IME_SETCONTEXT:
815         {
816             HWND hwndIME;
817
818             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
819             if (hwndIME)
820                 result = DEFWND_ImmIsUIMessageA( hwndIME, msg, wParam, lParam );
821         }
822         break;
823
824     case WM_INPUTLANGCHANGEREQUEST:
825         /* notify about the switch only if it's really our current layout */
826         if ((HKL)lParam == GetKeyboardLayout(0))
827             result = SendMessageA( hwnd, WM_INPUTLANGCHANGE, wParam, lParam );
828         else
829             result = 0;
830         break;
831
832     case WM_SYSCHAR:
833     {
834         CHAR ch = LOWORD(wParam);
835         WCHAR wch;
836         MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
837         wParam = MAKEWPARAM( wch, HIWORD(wParam) );
838     }
839     /* fall through */
840     default:
841         result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
842         break;
843     }
844
845     SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
846     return result;
847 }
848
849
850 /***********************************************************************
851  *              DefWindowProcW (USER32.@) Calls default window message handler
852  *
853  * Calls default window procedure for messages not processed
854  *  by application.
855  *
856  *  RETURNS
857  *     Return value is dependent upon the message.
858 */
859 LRESULT WINAPI DefWindowProcW(
860     HWND hwnd,      /* [in] window procedure receiving message */
861     UINT msg,       /* [in] message identifier */
862     WPARAM wParam,  /* [in] first message parameter */
863     LPARAM lParam )   /* [in] second message parameter */
864 {
865     LRESULT result = 0;
866     HWND full_handle;
867
868     if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
869     {
870         if (!IsWindow( hwnd )) return 0;
871         ERR( "called for other process window %p\n", hwnd );
872         return 0;
873     }
874     hwnd = full_handle;
875     SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
876
877     switch(msg)
878     {
879     case WM_NCCREATE:
880         {
881             CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
882             /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
883              * may have child window IDs instead of window name */
884             if (HIWORD(cs->lpszName))
885                 DEFWND_SetTextW( hwnd, cs->lpszName );
886             result = 1;
887         }
888         break;
889
890     case WM_GETTEXTLENGTH:
891         {
892             WND *wndPtr = WIN_GetPtr( hwnd );
893             if (wndPtr && wndPtr->text) result = (LRESULT)strlenW(wndPtr->text);
894             WIN_ReleasePtr( wndPtr );
895         }
896         break;
897
898     case WM_GETTEXT:
899         if (wParam)
900         {
901             LPWSTR dest = (LPWSTR)lParam;
902             WND *wndPtr = WIN_GetPtr( hwnd );
903
904             if (!wndPtr) break;
905             if (wndPtr->text)
906             {
907                 lstrcpynW( dest, wndPtr->text, wParam );
908                 result = strlenW( dest );
909             }
910             else dest[0] = '\0';
911             WIN_ReleasePtr( wndPtr );
912         }
913         break;
914
915     case WM_SETTEXT:
916         DEFWND_SetTextW( hwnd, (LPCWSTR)lParam );
917         if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
918             NC_HandleNCPaint( hwnd , (HRGN)1 );  /* Repaint caption */
919         result = 1; /* success. FIXME: check text length */
920         break;
921
922     /* for far east users (IMM32) - <hidenori@a2.ctktv.ne.jp> */
923     case WM_IME_CHAR:
924         SendMessageW( hwnd, WM_CHAR, wParam, lParam );
925         break;
926     case WM_IME_SETCONTEXT:
927         {
928             HWND hwndIME;
929
930             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
931             if (hwndIME)
932                 result = DEFWND_ImmIsUIMessageW( hwndIME, msg, wParam, lParam );
933         }
934         break;
935
936     case WM_IME_STARTCOMPOSITION:
937     case WM_IME_COMPOSITION:
938     case WM_IME_ENDCOMPOSITION:
939     case WM_IME_SELECT:
940         {
941             HWND hwndIME;
942
943             hwndIME = DEFWND_ImmGetDefaultIMEWnd( hwnd );
944             if (hwndIME)
945                 result = SendMessageW( hwndIME, msg, wParam, lParam );
946         }
947         break;
948
949     case WM_INPUTLANGCHANGEREQUEST:
950         /* notify about the switch only if it's really our current layout */
951         if ((HKL)lParam == GetKeyboardLayout(0))
952             result = SendMessageW( hwnd, WM_INPUTLANGCHANGE, wParam, lParam );
953         else
954             result = 0;
955         break;
956
957     default:
958         result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
959         break;
960     }
961     SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
962     return result;
963 }