winevulkan: Avoid zero-sized allocations.
[wine.git] / dlls / user32 / defwnd.c
blob4752f50f24e6e3ee5c5bce9d335c7f4e84d4d5a7
1 /*
2 * Default window procedure
4 * Copyright 1993, 1996 Alexandre Julliard
5 * 1995 Alex Korobka
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.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <string.h>
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winnls.h"
29 #include "imm.h"
30 #include "win.h"
31 #include "user_private.h"
32 #include "controls.h"
33 #include "wine/server.h"
34 #include "wine/exception.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(win);
39 /* bits in the dwKeyData */
40 #define KEYDATA_ALT 0x2000
41 #define KEYDATA_PREVSTATE 0x4000
43 #define DRAG_FILE 0x454C4946
45 static short iF10Key = 0;
46 static short iMenuSysKey = 0;
48 /***********************************************************************
49 * DEFWND_HandleWindowPosChanged
51 * Handle the WM_WINDOWPOSCHANGED message.
53 static void DEFWND_HandleWindowPosChanged( HWND hwnd, const WINDOWPOS *winpos )
55 RECT rect;
57 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
58 if (!(winpos->flags & SWP_NOCLIENTMOVE))
59 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG(rect.left, rect.top));
61 if (!(winpos->flags & SWP_NOCLIENTSIZE) || (winpos->flags & SWP_STATECHANGED))
63 if (IsIconic( hwnd ))
65 SendMessageW( hwnd, WM_SIZE, SIZE_MINIMIZED, 0 );
67 else
69 WPARAM wp = IsZoomed( hwnd ) ? SIZE_MAXIMIZED : SIZE_RESTORED;
71 SendMessageW( hwnd, WM_SIZE, wp, MAKELONG(rect.right-rect.left, rect.bottom-rect.top) );
77 /***********************************************************************
78 * DEFWND_SetTextA
80 * Set the window text.
82 static LRESULT DEFWND_SetTextA( HWND hwnd, LPCSTR text )
84 int count;
85 WCHAR *textW;
86 WND *wndPtr;
88 /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
89 * may have child window IDs instead of window name */
90 if (text && IS_INTRESOURCE(text))
91 return 0;
93 if (!text) text = "";
94 count = MultiByteToWideChar( CP_ACP, 0, text, -1, NULL, 0 );
96 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
97 if ((textW = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR))))
99 HeapFree(GetProcessHeap(), 0, wndPtr->text);
100 wndPtr->text = textW;
101 MultiByteToWideChar( CP_ACP, 0, text, -1, textW, count );
102 SERVER_START_REQ( set_window_text )
104 req->handle = wine_server_user_handle( hwnd );
105 wine_server_add_data( req, textW, (count-1) * sizeof(WCHAR) );
106 wine_server_call( req );
108 SERVER_END_REQ;
110 else
111 ERR("Not enough memory for window text\n");
112 WIN_ReleasePtr( wndPtr );
114 USER_Driver->pSetWindowText( hwnd, textW );
116 return 1;
119 /***********************************************************************
120 * DEFWND_SetTextW
122 * Set the window text.
124 static LRESULT DEFWND_SetTextW( HWND hwnd, LPCWSTR text )
126 WND *wndPtr;
127 int count;
129 /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
130 * may have child window IDs instead of window name */
131 if (text && IS_INTRESOURCE(text))
132 return 0;
134 if (!text) text = L"";
135 count = lstrlenW(text) + 1;
137 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
138 HeapFree(GetProcessHeap(), 0, wndPtr->text);
139 if ((wndPtr->text = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR))))
141 lstrcpyW( wndPtr->text, text );
142 SERVER_START_REQ( set_window_text )
144 req->handle = wine_server_user_handle( hwnd );
145 wine_server_add_data( req, wndPtr->text, (count-1) * sizeof(WCHAR) );
146 wine_server_call( req );
148 SERVER_END_REQ;
150 else
151 ERR("Not enough memory for window text\n");
152 text = wndPtr->text;
153 WIN_ReleasePtr( wndPtr );
155 USER_Driver->pSetWindowText( hwnd, text );
157 return 1;
160 /***********************************************************************
161 * DEFWND_ControlColor
163 * Default colors for control painting.
165 HBRUSH DEFWND_ControlColor( HDC hDC, UINT ctlType )
167 if( ctlType == CTLCOLOR_SCROLLBAR)
169 HBRUSH hb = GetSysColorBrush(COLOR_SCROLLBAR);
170 COLORREF bk = GetSysColor(COLOR_3DHILIGHT);
171 SetTextColor( hDC, GetSysColor(COLOR_3DFACE));
172 SetBkColor( hDC, bk);
174 /* if COLOR_WINDOW happens to be the same as COLOR_3DHILIGHT
175 * we better use 0x55aa bitmap brush to make scrollbar's background
176 * look different from the window background.
178 if (bk == GetSysColor(COLOR_WINDOW))
179 return SYSCOLOR_Get55AABrush();
181 UnrealizeObject( hb );
182 return hb;
185 SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT));
187 if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
188 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
189 else {
190 SetBkColor( hDC, GetSysColor(COLOR_3DFACE) );
191 return GetSysColorBrush(COLOR_3DFACE);
193 return GetSysColorBrush(COLOR_WINDOW);
197 /***********************************************************************
198 * DEFWND_Print
200 * This method handles the default behavior for the WM_PRINT message.
202 static void DEFWND_Print( HWND hwnd, HDC hdc, ULONG uFlags)
205 * Visibility flag.
207 if ( (uFlags & PRF_CHECKVISIBLE) &&
208 !IsWindowVisible(hwnd) )
209 return;
212 * Unimplemented flags.
214 if ( (uFlags & PRF_CHILDREN) ||
215 (uFlags & PRF_OWNED) ||
216 (uFlags & PRF_NONCLIENT) )
218 WARN("WM_PRINT message with unsupported flags\n");
222 * Background
224 if ( uFlags & PRF_ERASEBKGND)
225 SendMessageW(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
228 * Client area
230 if ( uFlags & PRF_CLIENT)
231 SendMessageW(hwnd, WM_PRINTCLIENT, (WPARAM)hdc, uFlags);
236 /***********************************************************************
237 * DEFWND_DefWinProc
239 * Default window procedure for messages that are the same in Ansi and Unicode.
241 static LRESULT DEFWND_DefWinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
243 switch(msg)
245 case WM_NCPAINT:
246 return NC_HandleNCPaint( hwnd, (HRGN)wParam );
248 case WM_NCMOUSEMOVE:
249 return NC_HandleNCMouseMove( hwnd, wParam, lParam );
251 case WM_NCMOUSELEAVE:
252 return NC_HandleNCMouseLeave( hwnd );
254 case WM_NCHITTEST:
256 POINT pt;
257 pt.x = (short)LOWORD(lParam);
258 pt.y = (short)HIWORD(lParam);
259 return NC_HandleNCHitTest( hwnd, pt );
262 case WM_NCCALCSIZE:
263 NC_HandleNCCalcSize( hwnd, wParam, (RECT *)lParam );
264 break;
266 case WM_WINDOWPOSCHANGING:
267 return WINPOS_HandleWindowPosChanging( hwnd, (WINDOWPOS *)lParam );
269 case WM_WINDOWPOSCHANGED:
270 DEFWND_HandleWindowPosChanged( hwnd, (const WINDOWPOS *)lParam );
271 break;
273 case WM_LBUTTONDOWN:
274 case WM_RBUTTONDOWN:
275 case WM_MBUTTONDOWN:
276 iF10Key = iMenuSysKey = 0;
277 break;
279 case WM_NCLBUTTONDOWN:
280 return NC_HandleNCLButtonDown( hwnd, wParam, lParam );
282 case WM_LBUTTONDBLCLK:
283 return NC_HandleNCLButtonDblClk( hwnd, HTCLIENT, lParam );
285 case WM_NCLBUTTONDBLCLK:
286 return NC_HandleNCLButtonDblClk( hwnd, wParam, lParam );
288 case WM_NCRBUTTONDOWN:
289 return NC_HandleNCRButtonDown( hwnd, wParam, lParam );
291 case WM_RBUTTONUP:
293 POINT pt;
294 pt.x = (short)LOWORD(lParam);
295 pt.y = (short)HIWORD(lParam);
296 ClientToScreen(hwnd, &pt);
297 SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM)hwnd, MAKELPARAM(pt.x, pt.y) );
299 break;
301 case WM_NCRBUTTONUP:
303 * FIXME : we must NOT send WM_CONTEXTMENU on a WM_NCRBUTTONUP (checked
304 * in Windows), but what _should_ we do? According to MSDN :
305 * "If it is appropriate to do so, the system sends the WM_SYSCOMMAND
306 * message to the window". When is it appropriate?
308 break;
310 case WM_XBUTTONUP:
311 case WM_NCXBUTTONUP:
312 if (HIWORD(wParam) == XBUTTON1 || HIWORD(wParam) == XBUTTON2)
314 SendMessageW(hwnd, WM_APPCOMMAND, (WPARAM)hwnd,
315 MAKELPARAM(LOWORD(wParam), FAPPCOMMAND_MOUSE | HIWORD(wParam)));
317 break;
319 case WM_CONTEXTMENU:
320 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
321 SendMessageW( GetParent(hwnd), msg, (WPARAM)hwnd, lParam );
322 else
324 LONG hitcode;
325 POINT pt;
326 pt.x = (short)LOWORD(lParam);
327 pt.y = (short)HIWORD(lParam);
328 hitcode = NC_HandleNCHitTest(hwnd, pt);
330 /* Track system popup if click was in the caption area. */
331 if (hitcode==HTCAPTION || hitcode==HTSYSMENU)
332 TrackPopupMenu(GetSystemMenu(hwnd, FALSE),
333 TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
334 pt.x, pt.y, 0, hwnd, NULL);
336 break;
338 case WM_POPUPSYSTEMMENU:
339 /* This is an undocumented message used by the windows taskbar to
340 display the system menu of windows that belong to other processes. */
341 TrackPopupMenu( GetSystemMenu(hwnd, FALSE), TPM_LEFTBUTTON|TPM_RIGHTBUTTON,
342 (short)LOWORD(lParam), (short)HIWORD(lParam), 0, hwnd, NULL );
343 return 0;
345 case WM_NCACTIVATE:
346 return NC_HandleNCActivate( hwnd, wParam, lParam );
348 case WM_NCDESTROY:
350 WND *wndPtr = WIN_GetPtr( hwnd );
351 if (!wndPtr) return 0;
352 HeapFree( GetProcessHeap(), 0, wndPtr->text );
353 wndPtr->text = NULL;
354 HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
355 wndPtr->pScroll = NULL;
356 WIN_ReleasePtr( wndPtr );
357 return 0;
360 case WM_PRINT:
361 DEFWND_Print(hwnd, (HDC)wParam, lParam);
362 return 0;
364 case WM_PAINTICON:
365 case WM_PAINT:
367 PAINTSTRUCT ps;
368 HDC hdc = BeginPaint( hwnd, &ps );
369 if( hdc )
371 HICON hIcon;
372 if (IsIconic(hwnd) && ((hIcon = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON))) )
374 RECT rc;
375 int x, y;
377 GetClientRect( hwnd, &rc );
378 x = (rc.right - rc.left - GetSystemMetrics(SM_CXICON))/2;
379 y = (rc.bottom - rc.top - GetSystemMetrics(SM_CYICON))/2;
380 TRACE("Painting class icon: vis rect=(%s)\n",
381 wine_dbgstr_rect(&ps.rcPaint));
382 DrawIcon( hdc, x, y, hIcon );
384 EndPaint( hwnd, &ps );
386 return 0;
389 case WM_SYNCPAINT:
390 RedrawWindow ( hwnd, NULL, 0, RDW_ERASENOW | RDW_ERASE | RDW_ALLCHILDREN );
391 return 0;
393 case WM_SETREDRAW:
394 if (wParam) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
395 else
397 RedrawWindow( hwnd, NULL, 0, RDW_ALLCHILDREN | RDW_VALIDATE );
398 WIN_SetStyle( hwnd, 0, WS_VISIBLE );
400 return 0;
402 case WM_CLOSE:
403 DestroyWindow( hwnd );
404 return 0;
406 case WM_MOUSEACTIVATE:
407 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
409 LONG ret = SendMessageW( GetParent(hwnd), WM_MOUSEACTIVATE, wParam, lParam );
410 if (ret) return ret;
413 /* Caption clicks are handled by NC_HandleNCLButtonDown() */
414 return ( HIWORD(lParam) == WM_LBUTTONDOWN && LOWORD(lParam) == HTCAPTION ? MA_NOACTIVATE : MA_ACTIVATE );
416 case WM_ACTIVATE:
417 /* The default action in Windows is to set the keyboard focus to
418 * the window, if it's being activated and not minimized */
419 if (LOWORD(wParam) != WA_INACTIVE) {
420 if (!IsIconic(hwnd)) SetFocus(hwnd);
422 break;
424 case WM_MOUSEWHEEL:
425 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
426 return SendMessageW( GetParent(hwnd), WM_MOUSEWHEEL, wParam, lParam );
427 break;
429 case WM_ERASEBKGND:
430 case WM_ICONERASEBKGND:
432 RECT rect;
433 HDC hdc = (HDC)wParam;
434 HBRUSH hbr = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND );
435 if (!hbr) return 0;
437 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
439 /* can't use GetClipBox with a parent DC or we fill the whole parent */
440 GetClientRect( hwnd, &rect );
441 DPtoLP( hdc, (LPPOINT)&rect, 2 );
443 else GetClipBox( hdc, &rect );
444 FillRect( hdc, &rect, hbr );
445 return 1;
448 case WM_GETDLGCODE:
449 return 0;
451 case WM_CTLCOLORMSGBOX:
452 case WM_CTLCOLOREDIT:
453 case WM_CTLCOLORLISTBOX:
454 case WM_CTLCOLORBTN:
455 case WM_CTLCOLORDLG:
456 case WM_CTLCOLORSTATIC:
457 case WM_CTLCOLORSCROLLBAR:
458 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
460 case WM_CTLCOLOR:
461 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
463 case WM_SETCURSOR:
464 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
466 /* with the exception of the border around a resizable wnd,
467 * give the parent first chance to set the cursor */
468 if ((LOWORD(lParam) < HTSIZEFIRST) || (LOWORD(lParam) > HTSIZELAST))
470 HWND parent = GetParent( hwnd );
471 if (parent != GetDesktopWindow() &&
472 SendMessageW( parent, WM_SETCURSOR, wParam, lParam )) return TRUE;
475 NC_HandleSetCursor( hwnd, wParam, lParam );
476 break;
478 case WM_SYSCOMMAND:
479 return NC_HandleSysCommand( hwnd, wParam, lParam );
481 case WM_KEYDOWN:
482 if(wParam == VK_F10) iF10Key = VK_F10;
483 break;
485 case WM_SYSKEYDOWN:
486 if( HIWORD(lParam) & KEYDATA_ALT )
488 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
489 if ( (wParam == VK_MENU || wParam == VK_LMENU
490 || wParam == VK_RMENU) && !iMenuSysKey )
491 iMenuSysKey = 1;
492 else
493 iMenuSysKey = 0;
495 iF10Key = 0;
497 if( wParam == VK_F4 ) /* try to close the window */
499 HWND top = NtUserGetAncestor( hwnd, GA_ROOT );
500 if (!(GetClassLongW( top, GCL_STYLE ) & CS_NOCLOSE))
501 PostMessageW( top, WM_SYSCOMMAND, SC_CLOSE, 0 );
504 else if( wParam == VK_F10 )
506 if (NtUserGetKeyState(VK_SHIFT) & 0x8000)
507 SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM)hwnd, -1 );
508 iF10Key = 1;
510 else if (wParam == VK_ESCAPE && (NtUserGetKeyState(VK_SHIFT) & 0x8000))
511 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, ' ' );
512 break;
514 case WM_KEYUP:
515 case WM_SYSKEYUP:
516 /* Press and release F10 or ALT */
517 if (((wParam == VK_MENU || wParam == VK_LMENU || wParam == VK_RMENU)
518 && iMenuSysKey) || ((wParam == VK_F10) && iF10Key))
519 SendMessageW( NtUserGetAncestor( hwnd, GA_ROOT ), WM_SYSCOMMAND, SC_KEYMENU, 0L );
520 iMenuSysKey = iF10Key = 0;
521 break;
523 case WM_SYSCHAR:
525 iMenuSysKey = 0;
526 if (wParam == '\r' && IsIconic(hwnd))
528 PostMessageW( hwnd, WM_SYSCOMMAND, SC_RESTORE, 0L );
529 break;
531 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
533 if (wParam == '\t' || wParam == '\x1b') break;
534 if (wParam == ' ' && (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD))
535 SendMessageW( GetParent(hwnd), msg, wParam, lParam );
536 else
537 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, wParam );
539 else /* check for Ctrl-Esc */
540 if (wParam != '\x1b') MessageBeep(0);
541 break;
544 case WM_SHOWWINDOW:
546 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
547 WND *pWnd;
548 if (!lParam) return 0; /* sent from ShowWindow */
549 if ((style & WS_VISIBLE) && wParam) return 0;
550 if (!(style & WS_VISIBLE) && !wParam) return 0;
551 if (!GetWindow( hwnd, GW_OWNER )) return 0;
552 if (!(pWnd = WIN_GetPtr( hwnd ))) return 0;
553 if (pWnd == WND_OTHER_PROCESS) return 0;
554 if (wParam)
556 if (!(pWnd->flags & WIN_NEEDS_SHOW_OWNEDPOPUP))
558 WIN_ReleasePtr( pWnd );
559 return 0;
561 pWnd->flags &= ~WIN_NEEDS_SHOW_OWNEDPOPUP;
563 else pWnd->flags |= WIN_NEEDS_SHOW_OWNEDPOPUP;
564 WIN_ReleasePtr( pWnd );
565 ShowWindow( hwnd, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
566 break;
569 case WM_CANCELMODE:
570 iMenuSysKey = 0;
571 MENU_EndMenu( hwnd );
572 if (GetCapture() == hwnd) ReleaseCapture();
573 break;
575 case WM_VKEYTOITEM:
576 case WM_CHARTOITEM:
577 return -1;
579 case WM_DROPOBJECT:
580 return DRAG_FILE;
582 case WM_QUERYDROPOBJECT:
583 return (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES) != 0;
585 case WM_QUERYDRAGICON:
587 UINT len;
589 HICON hIcon = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON );
590 HINSTANCE instance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
591 if (hIcon) return (LRESULT)hIcon;
592 for(len=1; len<64; len++)
593 if((hIcon = LoadIconW(instance, MAKEINTRESOURCEW(len))))
594 return (LRESULT)hIcon;
595 return (LRESULT)LoadIconW(0, (LPWSTR)IDI_APPLICATION);
597 break;
599 case WM_ISACTIVEICON:
600 return (win_get_flags( hwnd ) & WIN_NCACTIVATED) != 0;
602 case WM_NOTIFYFORMAT:
603 if (IsWindowUnicode(hwnd)) return NFR_UNICODE;
604 else return NFR_ANSI;
606 case WM_QUERYOPEN:
607 case WM_QUERYENDSESSION:
608 return 1;
610 case WM_SETICON:
612 HICON ret;
613 WND *wndPtr = WIN_GetPtr( hwnd );
615 switch(wParam)
617 case ICON_SMALL:
618 ret = wndPtr->hIconSmall;
619 if (ret && !lParam && wndPtr->hIcon)
621 wndPtr->hIconSmall2 = CopyImage( wndPtr->hIcon, IMAGE_ICON,
622 GetSystemMetrics( SM_CXSMICON ),
623 GetSystemMetrics( SM_CYSMICON ), 0 );
625 else if (lParam && wndPtr->hIconSmall2)
627 DestroyIcon( wndPtr->hIconSmall2 );
628 wndPtr->hIconSmall2 = NULL;
630 wndPtr->hIconSmall = (HICON)lParam;
631 break;
632 case ICON_BIG:
633 ret = wndPtr->hIcon;
634 if (wndPtr->hIconSmall2)
636 DestroyIcon( wndPtr->hIconSmall2 );
637 wndPtr->hIconSmall2 = NULL;
639 if (lParam && !wndPtr->hIconSmall)
641 wndPtr->hIconSmall2 = CopyImage( (HICON)lParam, IMAGE_ICON,
642 GetSystemMetrics( SM_CXSMICON ),
643 GetSystemMetrics( SM_CYSMICON ), 0 );
645 wndPtr->hIcon = (HICON)lParam;
646 break;
647 default:
648 ret = 0;
649 break;
651 WIN_ReleasePtr( wndPtr );
653 USER_Driver->pSetWindowIcon( hwnd, wParam, (HICON)lParam );
655 if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
656 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
658 return (LRESULT)ret;
661 case WM_GETICON:
663 HICON ret;
664 WND *wndPtr = WIN_GetPtr( hwnd );
666 switch(wParam)
668 case ICON_SMALL:
669 ret = wndPtr->hIconSmall;
670 break;
671 case ICON_BIG:
672 ret = wndPtr->hIcon;
673 break;
674 case ICON_SMALL2:
675 ret = wndPtr->hIconSmall ? wndPtr->hIconSmall : wndPtr->hIconSmall2;
676 break;
677 default:
678 ret = 0;
679 break;
681 WIN_ReleasePtr( wndPtr );
682 return (LRESULT)ret;
685 case WM_HELP:
686 SendMessageW( GetParent(hwnd), msg, wParam, lParam );
687 break;
689 case WM_STYLECHANGED:
690 if (wParam == GWL_STYLE && (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED))
692 STYLESTRUCT *style = (STYLESTRUCT *)lParam;
693 if ((style->styleOld ^ style->styleNew) & (WS_CAPTION|WS_THICKFRAME|WS_VSCROLL|WS_HSCROLL))
694 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOZORDER |
695 SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE );
697 break;
699 case WM_APPCOMMAND:
701 HWND parent = GetParent(hwnd);
702 if(!parent)
703 HOOK_CallHooks(WH_SHELL, HSHELL_APPCOMMAND, wParam, lParam, TRUE);
704 else
705 SendMessageW( parent, msg, wParam, lParam );
706 break;
708 case WM_KEYF1:
710 HELPINFO hi;
712 hi.cbSize = sizeof(HELPINFO);
713 GetCursorPos( &hi.MousePos );
714 if (MENU_IsMenuActive())
716 hi.iContextType = HELPINFO_MENUITEM;
717 hi.hItemHandle = MENU_IsMenuActive();
718 hi.iCtrlId = MenuItemFromPoint( hwnd, hi.hItemHandle, hi.MousePos );
719 hi.dwContextId = GetMenuContextHelpId( hi.hItemHandle );
721 else
723 hi.iContextType = HELPINFO_WINDOW;
724 hi.hItemHandle = hwnd;
725 hi.iCtrlId = GetWindowLongPtrA( hwnd, GWLP_ID );
726 hi.dwContextId = GetWindowContextHelpId( hwnd );
728 SendMessageW( hwnd, WM_HELP, 0, (LPARAM)&hi );
729 break;
732 case WM_INPUTLANGCHANGEREQUEST:
733 NtUserActivateKeyboardLayout( (HKL)lParam, 0 );
734 break;
736 case WM_INPUTLANGCHANGE:
738 struct user_thread_info *info = get_user_thread_info();
739 int count = 0;
740 HWND *win_array = WIN_ListChildren( hwnd );
741 info->kbd_layout = (HKL)lParam;
743 if (!win_array)
744 break;
745 while (win_array[count])
746 SendMessageW( win_array[count++], WM_INPUTLANGCHANGE, wParam, lParam);
747 HeapFree(GetProcessHeap(),0,win_array);
748 break;
753 return 0;
756 static LPARAM DEFWND_GetTextA( WND *wndPtr, LPSTR dest, WPARAM wParam )
758 LPARAM result = 0;
760 __TRY
762 if (wndPtr->text)
764 if (!WideCharToMultiByte( CP_ACP, 0, wndPtr->text, -1,
765 dest, wParam, NULL, NULL )) dest[wParam-1] = 0;
766 result = strlen( dest );
768 else dest[0] = '\0';
770 __EXCEPT_PAGE_FAULT
772 return 0;
774 __ENDTRY
775 return result;
778 /***********************************************************************
779 * DefWindowProcA (USER32.@)
781 * See DefWindowProcW.
783 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
785 LRESULT result = 0;
786 HWND full_handle;
788 if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
790 if (!IsWindow( hwnd )) return 0;
791 ERR( "called for other process window %p\n", hwnd );
792 return 0;
794 hwnd = full_handle;
796 SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
798 switch(msg)
800 case WM_NCCREATE:
801 if (lParam)
803 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
805 DEFWND_SetTextA( hwnd, cs->lpszName );
806 result = 1;
808 if(cs->style & (WS_HSCROLL | WS_VSCROLL))
810 SCROLLINFO si = {sizeof si, SIF_ALL, 0, 100, 0, 0, 0};
811 SetScrollInfo( hwnd, SB_HORZ, &si, FALSE );
812 SetScrollInfo( hwnd, SB_VERT, &si, FALSE );
815 break;
817 case WM_GETTEXTLENGTH:
819 WND *wndPtr = WIN_GetPtr( hwnd );
820 if (wndPtr && wndPtr->text)
821 result = WideCharToMultiByte( CP_ACP, 0, wndPtr->text, lstrlenW(wndPtr->text),
822 NULL, 0, NULL, NULL );
823 WIN_ReleasePtr( wndPtr );
825 break;
827 case WM_GETTEXT:
828 if (wParam)
830 LPSTR dest = (LPSTR)lParam;
831 WND *wndPtr = WIN_GetPtr( hwnd );
833 if (!wndPtr) break;
834 result = DEFWND_GetTextA( wndPtr, dest, wParam );
836 WIN_ReleasePtr( wndPtr );
838 break;
840 case WM_SETTEXT:
841 if (!DEFWND_SetTextA( hwnd, (LPCSTR)lParam ))
842 break;
843 if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
844 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
845 result = 1; /* success. FIXME: check text length */
846 break;
848 case WM_IME_CHAR:
849 if (HIBYTE(wParam)) PostMessageA( hwnd, WM_CHAR, HIBYTE(wParam), lParam );
850 PostMessageA( hwnd, WM_CHAR, LOBYTE(wParam), lParam );
851 break;
853 case WM_IME_KEYDOWN:
854 result = PostMessageA( hwnd, WM_KEYDOWN, wParam, lParam );
855 break;
857 case WM_IME_KEYUP:
858 result = PostMessageA( hwnd, WM_KEYUP, wParam, lParam );
859 break;
861 case WM_IME_COMPOSITION:
862 if (lParam & GCS_RESULTSTR)
864 LONG size, i;
865 unsigned char lead = 0;
866 char *buf = NULL;
867 HIMC himc = ImmGetContext( hwnd );
869 if (himc)
871 if ((size = ImmGetCompositionStringA( himc, GCS_RESULTSTR, NULL, 0 )))
873 if (!(buf = HeapAlloc( GetProcessHeap(), 0, size ))) size = 0;
874 else size = ImmGetCompositionStringA( himc, GCS_RESULTSTR, buf, size );
876 ImmReleaseContext( hwnd, himc );
878 for (i = 0; i < size; i++)
880 unsigned char c = buf[i];
881 if (!lead)
883 if (IsDBCSLeadByte( c ))
884 lead = c;
885 else
886 SendMessageA( hwnd, WM_IME_CHAR, c, 1 );
888 else
890 SendMessageA( hwnd, WM_IME_CHAR, MAKEWORD(c, lead), 1 );
891 lead = 0;
894 HeapFree( GetProcessHeap(), 0, buf );
897 /* fall through */
898 case WM_IME_STARTCOMPOSITION:
899 case WM_IME_ENDCOMPOSITION:
900 case WM_IME_SELECT:
901 case WM_IME_NOTIFY:
902 case WM_IME_CONTROL:
904 HWND hwndIME = ImmGetDefaultIMEWnd( hwnd );
905 if (hwndIME)
906 result = SendMessageA( hwndIME, msg, wParam, lParam );
908 break;
909 case WM_IME_SETCONTEXT:
911 HWND hwndIME = ImmGetDefaultIMEWnd( hwnd );
912 if (hwndIME) result = ImmIsUIMessageA( hwndIME, msg, wParam, lParam );
914 break;
916 case WM_SYSCHAR:
918 CHAR ch = LOWORD(wParam);
919 WCHAR wch;
920 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
921 wParam = MAKEWPARAM( wch, HIWORD(wParam) );
923 /* fall through */
924 default:
925 result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
926 break;
929 SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
930 return result;
934 static LPARAM DEFWND_GetTextW( WND *wndPtr, LPWSTR dest, WPARAM wParam )
936 LPARAM result = 0;
938 __TRY
940 if (wndPtr->text)
942 lstrcpynW( dest, wndPtr->text, wParam );
943 result = lstrlenW( dest );
945 else dest[0] = '\0';
947 __EXCEPT_PAGE_FAULT
949 return 0;
951 __ENDTRY
953 return result;
956 /***********************************************************************
957 * DefWindowProcW (USER32.@) Calls default window message handler
959 * Calls default window procedure for messages not processed
960 * by application.
962 * RETURNS
963 * Return value is dependent upon the message.
965 LRESULT WINAPI DefWindowProcW(
966 HWND hwnd, /* [in] window procedure receiving message */
967 UINT msg, /* [in] message identifier */
968 WPARAM wParam, /* [in] first message parameter */
969 LPARAM lParam ) /* [in] second message parameter */
971 LRESULT result = 0;
972 HWND full_handle;
974 if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
976 if (!IsWindow( hwnd )) return 0;
977 ERR( "called for other process window %p\n", hwnd );
978 return 0;
980 hwnd = full_handle;
981 SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
983 switch(msg)
985 case WM_NCCREATE:
986 if (lParam)
988 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
990 DEFWND_SetTextW( hwnd, cs->lpszName );
991 result = 1;
993 if(cs->style & (WS_HSCROLL | WS_VSCROLL))
995 SCROLLINFO si = {sizeof si, SIF_ALL, 0, 100, 0, 0, 0};
996 SetScrollInfo( hwnd, SB_HORZ, &si, FALSE );
997 SetScrollInfo( hwnd, SB_VERT, &si, FALSE );
1000 break;
1002 case WM_GETTEXTLENGTH:
1004 WND *wndPtr = WIN_GetPtr( hwnd );
1005 if (wndPtr && wndPtr->text) result = (LRESULT)lstrlenW(wndPtr->text);
1006 WIN_ReleasePtr( wndPtr );
1008 break;
1010 case WM_GETTEXT:
1011 if (wParam)
1013 LPWSTR dest = (LPWSTR)lParam;
1014 WND *wndPtr = WIN_GetPtr( hwnd );
1016 if (!wndPtr) break;
1017 result = DEFWND_GetTextW( wndPtr, dest, wParam );
1018 WIN_ReleasePtr( wndPtr );
1020 break;
1022 case WM_SETTEXT:
1023 if (!DEFWND_SetTextW( hwnd, (LPCWSTR)lParam ))
1024 break;
1025 if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
1026 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
1027 result = 1; /* success. FIXME: check text length */
1028 break;
1030 case WM_IME_CHAR:
1031 PostMessageW( hwnd, WM_CHAR, wParam, lParam );
1032 break;
1034 case WM_IME_KEYDOWN:
1035 result = PostMessageW( hwnd, WM_KEYDOWN, wParam, lParam );
1036 break;
1038 case WM_IME_KEYUP:
1039 result = PostMessageW( hwnd, WM_KEYUP, wParam, lParam );
1040 break;
1042 case WM_IME_SETCONTEXT:
1044 HWND hwndIME = ImmGetDefaultIMEWnd( hwnd );
1045 if (hwndIME) result = ImmIsUIMessageW( hwndIME, msg, wParam, lParam );
1047 break;
1049 case WM_IME_COMPOSITION:
1050 if (lParam & GCS_RESULTSTR)
1052 LONG size, i;
1053 WCHAR *buf = NULL;
1054 HIMC himc = ImmGetContext( hwnd );
1056 if (himc)
1058 if ((size = ImmGetCompositionStringW( himc, GCS_RESULTSTR, NULL, 0 )))
1060 if (!(buf = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) size = 0;
1061 else size = ImmGetCompositionStringW( himc, GCS_RESULTSTR, buf, size * sizeof(WCHAR) );
1063 ImmReleaseContext( hwnd, himc );
1065 for (i = 0; i < size / sizeof(WCHAR); i++)
1066 SendMessageW( hwnd, WM_IME_CHAR, buf[i], 1 );
1067 HeapFree( GetProcessHeap(), 0, buf );
1070 /* fall through */
1071 case WM_IME_STARTCOMPOSITION:
1072 case WM_IME_ENDCOMPOSITION:
1073 case WM_IME_SELECT:
1074 case WM_IME_NOTIFY:
1075 case WM_IME_CONTROL:
1077 HWND hwndIME = ImmGetDefaultIMEWnd( hwnd );
1078 if (hwndIME)
1079 result = SendMessageW( hwndIME, msg, wParam, lParam );
1081 break;
1083 default:
1084 result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
1085 break;
1087 SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
1088 return result;