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