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