mfplat: Implement MFGetService().
[wine.git] / dlls / user32 / defwnd.c
blobc0d1190710b99ae6408f2a7182caad1c726fd979
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 "config.h"
23 #include "wine/port.h"
25 #include <string.h>
26 #include <stdarg.h>
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 "wine/unicode.h"
37 #include "wine/server.h"
38 #include "wine/exception.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(win);
43 /* bits in the dwKeyData */
44 #define KEYDATA_ALT 0x2000
45 #define KEYDATA_PREVSTATE 0x4000
47 #define DRAG_FILE 0x454C4946
49 static short iF10Key = 0;
50 static short iMenuSysKey = 0;
52 /***********************************************************************
53 * DEFWND_HandleWindowPosChanged
55 * Handle the WM_WINDOWPOSCHANGED message.
57 static void DEFWND_HandleWindowPosChanged( HWND hwnd, const WINDOWPOS *winpos )
59 RECT rect;
61 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
62 if (!(winpos->flags & SWP_NOCLIENTMOVE))
63 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG(rect.left, rect.top));
65 if (!(winpos->flags & SWP_NOCLIENTSIZE) || (winpos->flags & SWP_STATECHANGED))
67 if (IsIconic( hwnd ))
69 SendMessageW( hwnd, WM_SIZE, SIZE_MINIMIZED, 0 );
71 else
73 WPARAM wp = IsZoomed( hwnd ) ? SIZE_MAXIMIZED : SIZE_RESTORED;
75 SendMessageW( hwnd, WM_SIZE, wp, MAKELONG(rect.right-rect.left, rect.bottom-rect.top) );
81 /***********************************************************************
82 * DEFWND_SetTextA
84 * Set the window text.
86 static LRESULT DEFWND_SetTextA( HWND hwnd, LPCSTR text )
88 int count;
89 WCHAR *textW;
90 WND *wndPtr;
92 /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
93 * may have child window IDs instead of window name */
94 if (text && IS_INTRESOURCE(text))
95 return 0;
97 if (!text) text = "";
98 count = MultiByteToWideChar( CP_ACP, 0, text, -1, NULL, 0 );
100 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
101 if ((textW = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR))))
103 HeapFree(GetProcessHeap(), 0, wndPtr->text);
104 wndPtr->text = textW;
105 MultiByteToWideChar( CP_ACP, 0, text, -1, textW, count );
106 SERVER_START_REQ( set_window_text )
108 req->handle = wine_server_user_handle( hwnd );
109 wine_server_add_data( req, textW, (count-1) * sizeof(WCHAR) );
110 wine_server_call( req );
112 SERVER_END_REQ;
114 else
115 ERR("Not enough memory for window text\n");
116 WIN_ReleasePtr( wndPtr );
118 USER_Driver->pSetWindowText( hwnd, textW );
120 return 1;
123 /***********************************************************************
124 * DEFWND_SetTextW
126 * Set the window text.
128 static LRESULT DEFWND_SetTextW( HWND hwnd, LPCWSTR text )
130 static const WCHAR empty_string[] = {0};
131 WND *wndPtr;
132 int count;
134 /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
135 * may have child window IDs instead of window name */
136 if (text && IS_INTRESOURCE(text))
137 return 0;
139 if (!text) text = empty_string;
140 count = strlenW(text) + 1;
142 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
143 HeapFree(GetProcessHeap(), 0, wndPtr->text);
144 if ((wndPtr->text = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR))))
146 strcpyW( wndPtr->text, text );
147 SERVER_START_REQ( set_window_text )
149 req->handle = wine_server_user_handle( hwnd );
150 wine_server_add_data( req, wndPtr->text, (count-1) * sizeof(WCHAR) );
151 wine_server_call( req );
153 SERVER_END_REQ;
155 else
156 ERR("Not enough memory for window text\n");
157 text = wndPtr->text;
158 WIN_ReleasePtr( wndPtr );
160 USER_Driver->pSetWindowText( hwnd, text );
162 return 1;
165 /***********************************************************************
166 * DEFWND_ControlColor
168 * Default colors for control painting.
170 HBRUSH DEFWND_ControlColor( HDC hDC, UINT ctlType )
172 if( ctlType == CTLCOLOR_SCROLLBAR)
174 HBRUSH hb = GetSysColorBrush(COLOR_SCROLLBAR);
175 COLORREF bk = GetSysColor(COLOR_3DHILIGHT);
176 SetTextColor( hDC, GetSysColor(COLOR_3DFACE));
177 SetBkColor( hDC, bk);
179 /* if COLOR_WINDOW happens to be the same as COLOR_3DHILIGHT
180 * we better use 0x55aa bitmap brush to make scrollbar's background
181 * look different from the window background.
183 if (bk == GetSysColor(COLOR_WINDOW))
184 return SYSCOLOR_Get55AABrush();
186 UnrealizeObject( hb );
187 return hb;
190 SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT));
192 if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
193 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
194 else {
195 SetBkColor( hDC, GetSysColor(COLOR_3DFACE) );
196 return GetSysColorBrush(COLOR_3DFACE);
198 return GetSysColorBrush(COLOR_WINDOW);
202 /***********************************************************************
203 * DEFWND_Print
205 * This method handles the default behavior for the WM_PRINT message.
207 static void DEFWND_Print( HWND hwnd, HDC hdc, ULONG uFlags)
210 * Visibility flag.
212 if ( (uFlags & PRF_CHECKVISIBLE) &&
213 !IsWindowVisible(hwnd) )
214 return;
217 * Unimplemented flags.
219 if ( (uFlags & PRF_CHILDREN) ||
220 (uFlags & PRF_OWNED) ||
221 (uFlags & PRF_NONCLIENT) )
223 WARN("WM_PRINT message with unsupported flags\n");
227 * Background
229 if ( uFlags & PRF_ERASEBKGND)
230 SendMessageW(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
233 * Client area
235 if ( uFlags & PRF_CLIENT)
236 SendMessageW(hwnd, WM_PRINTCLIENT, (WPARAM)hdc, uFlags);
241 /***********************************************************************
242 * DEFWND_DefWinProc
244 * Default window procedure for messages that are the same in Ansi and Unicode.
246 static LRESULT DEFWND_DefWinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
248 switch(msg)
250 case WM_NCPAINT:
251 return NC_HandleNCPaint( hwnd, (HRGN)wParam );
253 case WM_NCHITTEST:
255 POINT pt;
256 pt.x = (short)LOWORD(lParam);
257 pt.y = (short)HIWORD(lParam);
258 return NC_HandleNCHitTest( hwnd, pt );
261 case WM_NCCALCSIZE:
262 return NC_HandleNCCalcSize( hwnd, wParam, (RECT *)lParam );
264 case WM_WINDOWPOSCHANGING:
265 return WINPOS_HandleWindowPosChanging( hwnd, (WINDOWPOS *)lParam );
267 case WM_WINDOWPOSCHANGED:
268 DEFWND_HandleWindowPosChanged( hwnd, (const WINDOWPOS *)lParam );
269 break;
271 case WM_LBUTTONDOWN:
272 case WM_RBUTTONDOWN:
273 case WM_MBUTTONDOWN:
274 iF10Key = iMenuSysKey = 0;
275 break;
277 case WM_NCLBUTTONDOWN:
278 return NC_HandleNCLButtonDown( hwnd, wParam, lParam );
280 case WM_LBUTTONDBLCLK:
281 return NC_HandleNCLButtonDblClk( hwnd, HTCLIENT, lParam );
283 case WM_NCLBUTTONDBLCLK:
284 return NC_HandleNCLButtonDblClk( hwnd, wParam, lParam );
286 case WM_NCRBUTTONDOWN:
287 return NC_HandleNCRButtonDown( hwnd, wParam, lParam );
289 case WM_RBUTTONUP:
291 POINT pt;
292 pt.x = (short)LOWORD(lParam);
293 pt.y = (short)HIWORD(lParam);
294 ClientToScreen(hwnd, &pt);
295 SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM)hwnd, MAKELPARAM(pt.x, pt.y) );
297 break;
299 case WM_NCRBUTTONUP:
301 * FIXME : we must NOT send WM_CONTEXTMENU on a WM_NCRBUTTONUP (checked
302 * in Windows), but what _should_ we do? According to MSDN :
303 * "If it is appropriate to do so, the system sends the WM_SYSCOMMAND
304 * message to the window". When is it appropriate?
306 break;
308 case WM_XBUTTONUP:
309 case WM_NCXBUTTONUP:
310 if (HIWORD(wParam) == XBUTTON1 || HIWORD(wParam) == XBUTTON2)
312 SendMessageW(hwnd, WM_APPCOMMAND, (WPARAM)hwnd,
313 MAKELPARAM(LOWORD(wParam), FAPPCOMMAND_MOUSE | HIWORD(wParam)));
315 break;
317 case WM_CONTEXTMENU:
318 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
319 SendMessageW( GetParent(hwnd), msg, wParam, lParam );
320 else
322 LONG hitcode;
323 POINT pt;
324 pt.x = (short)LOWORD(lParam);
325 pt.y = (short)HIWORD(lParam);
326 hitcode = NC_HandleNCHitTest(hwnd, pt);
328 /* Track system popup if click was in the caption area. */
329 if (hitcode==HTCAPTION || hitcode==HTSYSMENU)
330 TrackPopupMenu(GetSystemMenu(hwnd, FALSE),
331 TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
332 pt.x, pt.y, 0, hwnd, NULL);
334 break;
336 case WM_POPUPSYSTEMMENU:
337 /* This is an undocumented message used by the windows taskbar to
338 display the system menu of windows that belong to other processes. */
339 TrackPopupMenu( GetSystemMenu(hwnd, FALSE), TPM_LEFTBUTTON|TPM_RIGHTBUTTON,
340 (short)LOWORD(lParam), (short)HIWORD(lParam), 0, hwnd, NULL );
341 return 0;
343 case WM_NCACTIVATE:
344 return NC_HandleNCActivate( hwnd, wParam, lParam );
346 case WM_NCDESTROY:
348 WND *wndPtr = WIN_GetPtr( hwnd );
349 if (!wndPtr) return 0;
350 HeapFree( GetProcessHeap(), 0, wndPtr->text );
351 wndPtr->text = NULL;
352 HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
353 wndPtr->pScroll = NULL;
354 WIN_ReleasePtr( wndPtr );
355 return 0;
358 case WM_PRINT:
359 DEFWND_Print(hwnd, (HDC)wParam, lParam);
360 return 0;
362 case WM_PAINTICON:
363 case WM_PAINT:
365 PAINTSTRUCT ps;
366 HDC hdc = BeginPaint( hwnd, &ps );
367 if( hdc )
369 HICON hIcon;
370 if (IsIconic(hwnd) && ((hIcon = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON))) )
372 RECT rc;
373 int x, y;
375 GetClientRect( hwnd, &rc );
376 x = (rc.right - rc.left - GetSystemMetrics(SM_CXICON))/2;
377 y = (rc.bottom - rc.top - GetSystemMetrics(SM_CYICON))/2;
378 TRACE("Painting class icon: vis rect=(%s)\n",
379 wine_dbgstr_rect(&ps.rcPaint));
380 DrawIcon( hdc, x, y, hIcon );
382 EndPaint( hwnd, &ps );
384 return 0;
387 case WM_SYNCPAINT:
388 RedrawWindow ( hwnd, NULL, 0, RDW_ERASENOW | RDW_ERASE | RDW_ALLCHILDREN );
389 return 0;
391 case WM_SETREDRAW:
392 if (wParam) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
393 else
395 RedrawWindow( hwnd, NULL, 0, RDW_ALLCHILDREN | RDW_VALIDATE );
396 WIN_SetStyle( hwnd, 0, WS_VISIBLE );
398 return 0;
400 case WM_CLOSE:
401 DestroyWindow( hwnd );
402 return 0;
404 case WM_MOUSEACTIVATE:
405 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
407 LONG ret = SendMessageW( GetParent(hwnd), WM_MOUSEACTIVATE, wParam, lParam );
408 if (ret) return ret;
411 /* Caption clicks are handled by NC_HandleNCLButtonDown() */
412 return MA_ACTIVATE;
414 case WM_ACTIVATE:
415 /* The default action in Windows is to set the keyboard focus to
416 * the window, if it's being activated and not minimized */
417 if (LOWORD(wParam) != WA_INACTIVE) {
418 if (!IsIconic(hwnd)) SetFocus(hwnd);
420 break;
422 case WM_MOUSEWHEEL:
423 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
424 return SendMessageW( GetParent(hwnd), WM_MOUSEWHEEL, wParam, lParam );
425 break;
427 case WM_ERASEBKGND:
428 case WM_ICONERASEBKGND:
430 RECT rect;
431 HDC hdc = (HDC)wParam;
432 HBRUSH hbr = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND );
433 if (!hbr) return 0;
435 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
437 /* can't use GetClipBox with a parent DC or we fill the whole parent */
438 GetClientRect( hwnd, &rect );
439 DPtoLP( hdc, (LPPOINT)&rect, 2 );
441 else GetClipBox( hdc, &rect );
442 FillRect( hdc, &rect, hbr );
443 return 1;
446 case WM_GETDLGCODE:
447 return 0;
449 case WM_CTLCOLORMSGBOX:
450 case WM_CTLCOLOREDIT:
451 case WM_CTLCOLORLISTBOX:
452 case WM_CTLCOLORBTN:
453 case WM_CTLCOLORDLG:
454 case WM_CTLCOLORSTATIC:
455 case WM_CTLCOLORSCROLLBAR:
456 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
458 case WM_CTLCOLOR:
459 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
461 case WM_SETCURSOR:
462 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
464 /* with the exception of the border around a resizable wnd,
465 * give the parent first chance to set the cursor */
466 if ((LOWORD(lParam) < HTSIZEFIRST) || (LOWORD(lParam) > HTSIZELAST))
468 HWND parent = GetParent( hwnd );
469 if (parent != GetDesktopWindow() &&
470 SendMessageW( parent, WM_SETCURSOR, wParam, lParam )) return TRUE;
473 NC_HandleSetCursor( hwnd, wParam, lParam );
474 break;
476 case WM_SYSCOMMAND:
477 return NC_HandleSysCommand( hwnd, wParam, lParam );
479 case WM_KEYDOWN:
480 if(wParam == VK_F10) iF10Key = VK_F10;
481 break;
483 case WM_SYSKEYDOWN:
484 if( HIWORD(lParam) & KEYDATA_ALT )
486 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
487 if ( (wParam == VK_MENU || wParam == VK_LMENU
488 || wParam == VK_RMENU) && !iMenuSysKey )
489 iMenuSysKey = 1;
490 else
491 iMenuSysKey = 0;
493 iF10Key = 0;
495 if( wParam == VK_F4 ) /* try to close the window */
497 HWND top = GetAncestor( hwnd, GA_ROOT );
498 if (!(GetClassLongW( top, GCL_STYLE ) & CS_NOCLOSE))
499 PostMessageW( top, WM_SYSCOMMAND, SC_CLOSE, 0 );
502 else if( wParam == VK_F10 )
504 if (GetKeyState(VK_SHIFT) & 0x8000)
505 SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM)hwnd, -1 );
506 iF10Key = 1;
508 else if( wParam == VK_ESCAPE && (GetKeyState(VK_SHIFT) & 0x8000))
509 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, ' ' );
510 break;
512 case WM_KEYUP:
513 case WM_SYSKEYUP:
514 /* Press and release F10 or ALT */
515 if (((wParam == VK_MENU || wParam == VK_LMENU || wParam == VK_RMENU)
516 && iMenuSysKey) || ((wParam == VK_F10) && iF10Key))
517 SendMessageW( GetAncestor( hwnd, GA_ROOT ), WM_SYSCOMMAND, SC_KEYMENU, 0L );
518 iMenuSysKey = iF10Key = 0;
519 break;
521 case WM_SYSCHAR:
523 iMenuSysKey = 0;
524 if (wParam == '\r' && IsIconic(hwnd))
526 PostMessageW( hwnd, WM_SYSCOMMAND, SC_RESTORE, 0L );
527 break;
529 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
531 if (wParam == '\t' || wParam == '\x1b') break;
532 if (wParam == ' ' && (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD))
533 SendMessageW( GetParent(hwnd), msg, wParam, lParam );
534 else
535 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, wParam );
537 else /* check for Ctrl-Esc */
538 if (wParam != '\x1b') MessageBeep(0);
539 break;
542 case WM_SHOWWINDOW:
544 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
545 WND *pWnd;
546 if (!lParam) return 0; /* sent from ShowWindow */
547 if ((style & WS_VISIBLE) && wParam) return 0;
548 if (!(style & WS_VISIBLE) && !wParam) return 0;
549 if (!GetWindow( hwnd, GW_OWNER )) return 0;
550 if (!(pWnd = WIN_GetPtr( hwnd ))) return 0;
551 if (pWnd == WND_OTHER_PROCESS) return 0;
552 if (wParam)
554 if (!(pWnd->flags & WIN_NEEDS_SHOW_OWNEDPOPUP))
556 WIN_ReleasePtr( pWnd );
557 return 0;
559 pWnd->flags &= ~WIN_NEEDS_SHOW_OWNEDPOPUP;
561 else pWnd->flags |= WIN_NEEDS_SHOW_OWNEDPOPUP;
562 WIN_ReleasePtr( pWnd );
563 ShowWindow( hwnd, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
564 break;
567 case WM_CANCELMODE:
568 iMenuSysKey = 0;
569 MENU_EndMenu( hwnd );
570 if (GetCapture() == hwnd) ReleaseCapture();
571 break;
573 case WM_VKEYTOITEM:
574 case WM_CHARTOITEM:
575 return -1;
577 case WM_DROPOBJECT:
578 return DRAG_FILE;
580 case WM_QUERYDROPOBJECT:
581 return (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES) != 0;
583 case WM_QUERYDRAGICON:
585 UINT len;
587 HICON hIcon = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON );
588 HINSTANCE instance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
589 if (hIcon) return (LRESULT)hIcon;
590 for(len=1; len<64; len++)
591 if((hIcon = LoadIconW(instance, MAKEINTRESOURCEW(len))))
592 return (LRESULT)hIcon;
593 return (LRESULT)LoadIconW(0, (LPWSTR)IDI_APPLICATION);
595 break;
597 case WM_ISACTIVEICON:
598 return (win_get_flags( hwnd ) & WIN_NCACTIVATED) != 0;
600 case WM_NOTIFYFORMAT:
601 if (IsWindowUnicode(hwnd)) return NFR_UNICODE;
602 else return NFR_ANSI;
604 case WM_QUERYOPEN:
605 case WM_QUERYENDSESSION:
606 return 1;
608 case WM_SETICON:
610 HICON ret;
611 WND *wndPtr = WIN_GetPtr( hwnd );
613 switch(wParam)
615 case ICON_SMALL:
616 ret = wndPtr->hIconSmall;
617 if (ret && !lParam && wndPtr->hIcon)
619 wndPtr->hIconSmall2 = CopyImage( wndPtr->hIcon, IMAGE_ICON,
620 GetSystemMetrics( SM_CXSMICON ),
621 GetSystemMetrics( SM_CYSMICON ), 0 );
623 else if (lParam && wndPtr->hIconSmall2)
625 DestroyIcon( wndPtr->hIconSmall2 );
626 wndPtr->hIconSmall2 = NULL;
628 wndPtr->hIconSmall = (HICON)lParam;
629 break;
630 case ICON_BIG:
631 ret = wndPtr->hIcon;
632 if (wndPtr->hIconSmall2)
634 DestroyIcon( wndPtr->hIconSmall2 );
635 wndPtr->hIconSmall2 = NULL;
637 if (lParam && !wndPtr->hIconSmall)
639 wndPtr->hIconSmall2 = CopyImage( (HICON)lParam, IMAGE_ICON,
640 GetSystemMetrics( SM_CXSMICON ),
641 GetSystemMetrics( SM_CYSMICON ), 0 );
643 wndPtr->hIcon = (HICON)lParam;
644 break;
645 default:
646 ret = 0;
647 break;
649 WIN_ReleasePtr( wndPtr );
651 USER_Driver->pSetWindowIcon( hwnd, wParam, (HICON)lParam );
653 if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
654 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
656 return (LRESULT)ret;
659 case WM_GETICON:
661 HICON ret;
662 WND *wndPtr = WIN_GetPtr( hwnd );
664 switch(wParam)
666 case ICON_SMALL:
667 ret = wndPtr->hIconSmall;
668 break;
669 case ICON_BIG:
670 ret = wndPtr->hIcon;
671 break;
672 case ICON_SMALL2:
673 ret = wndPtr->hIconSmall ? wndPtr->hIconSmall : wndPtr->hIconSmall2;
674 break;
675 default:
676 ret = 0;
677 break;
679 WIN_ReleasePtr( wndPtr );
680 return (LRESULT)ret;
683 case WM_HELP:
684 SendMessageW( GetParent(hwnd), msg, wParam, lParam );
685 break;
687 case WM_STYLECHANGED:
688 if (wParam == GWL_STYLE && (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED))
690 STYLESTRUCT *style = (STYLESTRUCT *)lParam;
691 if ((style->styleOld ^ style->styleNew) & (WS_CAPTION|WS_THICKFRAME|WS_VSCROLL|WS_HSCROLL))
692 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOZORDER |
693 SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE );
695 break;
697 case WM_APPCOMMAND:
699 HWND parent = GetParent(hwnd);
700 if(!parent)
701 HOOK_CallHooks(WH_SHELL, HSHELL_APPCOMMAND, wParam, lParam, TRUE);
702 else
703 SendMessageW( parent, msg, wParam, lParam );
704 break;
706 case WM_KEYF1:
708 HELPINFO hi;
710 hi.cbSize = sizeof(HELPINFO);
711 GetCursorPos( &hi.MousePos );
712 if (MENU_IsMenuActive())
714 hi.iContextType = HELPINFO_MENUITEM;
715 hi.hItemHandle = MENU_IsMenuActive();
716 hi.iCtrlId = MenuItemFromPoint( hwnd, hi.hItemHandle, hi.MousePos );
717 hi.dwContextId = GetMenuContextHelpId( hi.hItemHandle );
719 else
721 hi.iContextType = HELPINFO_WINDOW;
722 hi.hItemHandle = hwnd;
723 hi.iCtrlId = GetWindowLongPtrA( hwnd, GWLP_ID );
724 hi.dwContextId = GetWindowContextHelpId( hwnd );
726 SendMessageW( hwnd, WM_HELP, 0, (LPARAM)&hi );
727 break;
730 case WM_INPUTLANGCHANGEREQUEST:
731 ActivateKeyboardLayout( (HKL)lParam, 0 );
732 break;
734 case WM_INPUTLANGCHANGE:
736 int count = 0;
737 HWND *win_array = WIN_ListChildren( hwnd );
739 if (!win_array)
740 break;
741 while (win_array[count])
742 SendMessageW( win_array[count++], WM_INPUTLANGCHANGE, wParam, lParam);
743 HeapFree(GetProcessHeap(),0,win_array);
744 break;
749 return 0;
752 static LPARAM DEFWND_GetTextA( WND *wndPtr, LPSTR dest, WPARAM wParam )
754 LPARAM result = 0;
756 __TRY
758 if (wndPtr->text)
760 if (!WideCharToMultiByte( CP_ACP, 0, wndPtr->text, -1,
761 dest, wParam, NULL, NULL )) dest[wParam-1] = 0;
762 result = strlen( dest );
764 else dest[0] = '\0';
766 __EXCEPT_PAGE_FAULT
768 return 0;
770 __ENDTRY
771 return result;
774 /***********************************************************************
775 * DefWindowProcA (USER32.@)
777 * See DefWindowProcW.
779 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
781 LRESULT result = 0;
782 HWND full_handle;
784 if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
786 if (!IsWindow( hwnd )) return 0;
787 ERR( "called for other process window %p\n", hwnd );
788 return 0;
790 hwnd = full_handle;
792 SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
794 switch(msg)
796 case WM_NCCREATE:
797 if (lParam)
799 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
801 DEFWND_SetTextA( hwnd, cs->lpszName );
802 result = 1;
804 if(cs->style & (WS_HSCROLL | WS_VSCROLL))
806 SCROLLINFO si = {sizeof si, SIF_ALL, 0, 100, 0, 0, 0};
807 SetScrollInfo( hwnd, SB_HORZ, &si, FALSE );
808 SetScrollInfo( hwnd, SB_VERT, &si, FALSE );
811 break;
813 case WM_GETTEXTLENGTH:
815 WND *wndPtr = WIN_GetPtr( hwnd );
816 if (wndPtr && wndPtr->text)
817 result = WideCharToMultiByte( CP_ACP, 0, wndPtr->text, strlenW(wndPtr->text),
818 NULL, 0, NULL, NULL );
819 WIN_ReleasePtr( wndPtr );
821 break;
823 case WM_GETTEXT:
824 if (wParam)
826 LPSTR dest = (LPSTR)lParam;
827 WND *wndPtr = WIN_GetPtr( hwnd );
829 if (!wndPtr) break;
830 result = DEFWND_GetTextA( wndPtr, dest, wParam );
832 WIN_ReleasePtr( wndPtr );
834 break;
836 case WM_SETTEXT:
837 if (!DEFWND_SetTextA( hwnd, (LPCSTR)lParam ))
838 break;
839 if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
840 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
841 result = 1; /* success. FIXME: check text length */
842 break;
844 case WM_IME_CHAR:
845 if (HIBYTE(wParam)) PostMessageA( hwnd, WM_CHAR, HIBYTE(wParam), lParam );
846 PostMessageA( hwnd, WM_CHAR, LOBYTE(wParam), lParam );
847 break;
849 case WM_IME_KEYDOWN:
850 result = PostMessageA( hwnd, WM_KEYDOWN, wParam, lParam );
851 break;
853 case WM_IME_KEYUP:
854 result = PostMessageA( hwnd, WM_KEYUP, wParam, lParam );
855 break;
857 case WM_IME_COMPOSITION:
858 if (lParam & GCS_RESULTSTR)
860 LONG size, i;
861 unsigned char lead = 0;
862 char *buf = NULL;
863 HIMC himc = ImmGetContext( hwnd );
865 if (himc)
867 if ((size = ImmGetCompositionStringA( himc, GCS_RESULTSTR, NULL, 0 )))
869 if (!(buf = HeapAlloc( GetProcessHeap(), 0, size ))) size = 0;
870 else size = ImmGetCompositionStringA( himc, GCS_RESULTSTR, buf, size );
872 ImmReleaseContext( hwnd, himc );
874 for (i = 0; i < size; i++)
876 unsigned char c = buf[i];
877 if (!lead)
879 if (IsDBCSLeadByte( c ))
880 lead = c;
881 else
882 SendMessageA( hwnd, WM_IME_CHAR, c, 1 );
884 else
886 SendMessageA( hwnd, WM_IME_CHAR, MAKEWORD(c, lead), 1 );
887 lead = 0;
890 HeapFree( GetProcessHeap(), 0, buf );
893 /* fall through */
894 case WM_IME_STARTCOMPOSITION:
895 case WM_IME_ENDCOMPOSITION:
896 case WM_IME_SELECT:
897 case WM_IME_NOTIFY:
898 case WM_IME_CONTROL:
900 HWND hwndIME = ImmGetDefaultIMEWnd( hwnd );
901 if (hwndIME)
902 result = SendMessageA( hwndIME, msg, wParam, lParam );
904 break;
905 case WM_IME_SETCONTEXT:
907 HWND hwndIME = ImmGetDefaultIMEWnd( hwnd );
908 if (hwndIME) result = ImmIsUIMessageA( hwndIME, msg, wParam, lParam );
910 break;
912 case WM_SYSCHAR:
914 CHAR ch = LOWORD(wParam);
915 WCHAR wch;
916 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
917 wParam = MAKEWPARAM( wch, HIWORD(wParam) );
919 /* fall through */
920 default:
921 result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
922 break;
925 SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
926 return result;
930 static LPARAM DEFWND_GetTextW( WND *wndPtr, LPWSTR dest, WPARAM wParam )
932 LPARAM result = 0;
934 __TRY
936 if (wndPtr->text)
938 lstrcpynW( dest, wndPtr->text, wParam );
939 result = strlenW( dest );
941 else dest[0] = '\0';
943 __EXCEPT_PAGE_FAULT
945 return 0;
947 __ENDTRY
949 return result;
952 /***********************************************************************
953 * DefWindowProcW (USER32.@) Calls default window message handler
955 * Calls default window procedure for messages not processed
956 * by application.
958 * RETURNS
959 * Return value is dependent upon the message.
961 LRESULT WINAPI DefWindowProcW(
962 HWND hwnd, /* [in] window procedure receiving message */
963 UINT msg, /* [in] message identifier */
964 WPARAM wParam, /* [in] first message parameter */
965 LPARAM lParam ) /* [in] second message parameter */
967 LRESULT result = 0;
968 HWND full_handle;
970 if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
972 if (!IsWindow( hwnd )) return 0;
973 ERR( "called for other process window %p\n", hwnd );
974 return 0;
976 hwnd = full_handle;
977 SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
979 switch(msg)
981 case WM_NCCREATE:
982 if (lParam)
984 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
986 DEFWND_SetTextW( hwnd, cs->lpszName );
987 result = 1;
989 if(cs->style & (WS_HSCROLL | WS_VSCROLL))
991 SCROLLINFO si = {sizeof si, SIF_ALL, 0, 100, 0, 0, 0};
992 SetScrollInfo( hwnd, SB_HORZ, &si, FALSE );
993 SetScrollInfo( hwnd, SB_VERT, &si, FALSE );
996 break;
998 case WM_GETTEXTLENGTH:
1000 WND *wndPtr = WIN_GetPtr( hwnd );
1001 if (wndPtr && wndPtr->text) result = (LRESULT)strlenW(wndPtr->text);
1002 WIN_ReleasePtr( wndPtr );
1004 break;
1006 case WM_GETTEXT:
1007 if (wParam)
1009 LPWSTR dest = (LPWSTR)lParam;
1010 WND *wndPtr = WIN_GetPtr( hwnd );
1012 if (!wndPtr) break;
1013 result = DEFWND_GetTextW( wndPtr, dest, wParam );
1014 WIN_ReleasePtr( wndPtr );
1016 break;
1018 case WM_SETTEXT:
1019 if (!DEFWND_SetTextW( hwnd, (LPCWSTR)lParam ))
1020 break;
1021 if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
1022 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
1023 result = 1; /* success. FIXME: check text length */
1024 break;
1026 case WM_IME_CHAR:
1027 PostMessageW( hwnd, WM_CHAR, wParam, lParam );
1028 break;
1030 case WM_IME_KEYDOWN:
1031 result = PostMessageW( hwnd, WM_KEYDOWN, wParam, lParam );
1032 break;
1034 case WM_IME_KEYUP:
1035 result = PostMessageW( hwnd, WM_KEYUP, wParam, lParam );
1036 break;
1038 case WM_IME_SETCONTEXT:
1040 HWND hwndIME = ImmGetDefaultIMEWnd( hwnd );
1041 if (hwndIME) result = ImmIsUIMessageW( hwndIME, msg, wParam, lParam );
1043 break;
1045 case WM_IME_COMPOSITION:
1046 if (lParam & GCS_RESULTSTR)
1048 LONG size, i;
1049 WCHAR *buf = NULL;
1050 HIMC himc = ImmGetContext( hwnd );
1052 if (himc)
1054 if ((size = ImmGetCompositionStringW( himc, GCS_RESULTSTR, NULL, 0 )))
1056 if (!(buf = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) size = 0;
1057 else size = ImmGetCompositionStringW( himc, GCS_RESULTSTR, buf, size * sizeof(WCHAR) );
1059 ImmReleaseContext( hwnd, himc );
1061 for (i = 0; i < size / sizeof(WCHAR); i++)
1062 SendMessageW( hwnd, WM_IME_CHAR, buf[i], 1 );
1063 HeapFree( GetProcessHeap(), 0, buf );
1066 /* fall through */
1067 case WM_IME_STARTCOMPOSITION:
1068 case WM_IME_ENDCOMPOSITION:
1069 case WM_IME_SELECT:
1070 case WM_IME_NOTIFY:
1071 case WM_IME_CONTROL:
1073 HWND hwndIME = ImmGetDefaultIMEWnd( hwnd );
1074 if (hwndIME)
1075 result = SendMessageW( hwndIME, msg, wParam, lParam );
1077 break;
1079 default:
1080 result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
1081 break;
1083 SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
1084 return result;