Made server communication faster by using a shared memory block.
[wine/multimedia.git] / windows / defwnd.c
blob3e8a9489c34dd7d6e149ef95c3442cebd9a74098
1 /*
2 * Default window procedure
4 * Copyright 1993, 1996 Alexandre Julliard
5 * 1995 Alex Korobka
6 */
8 #include <string.h>
10 #include "win.h"
11 #include "user.h"
12 #include "heap.h"
13 #include "nonclient.h"
14 #include "winpos.h"
15 #include "dce.h"
16 #include "debug.h"
17 #include "spy.h"
18 #include "tweak.h"
19 #include "wine/winuser16.h"
21 DEFAULT_DEBUG_CHANNEL(win)
23 /* Last COLOR id */
24 #define COLOR_MAX COLOR_BTNHIGHLIGHT
26 /* bits in the dwKeyData */
27 #define KEYDATA_ALT 0x2000
28 #define KEYDATA_PREVSTATE 0x4000
30 static short iF10Key = 0;
31 static short iMenuSysKey = 0;
33 /***********************************************************************
34 * DEFWND_HandleWindowPosChanged
36 * Handle the WM_WINDOWPOSCHANGED message.
38 static void DEFWND_HandleWindowPosChanged( WND *wndPtr, UINT flags )
40 WPARAM16 wp = SIZE_RESTORED;
42 if (!(flags & SWP_NOCLIENTMOVE))
43 SendMessage16( wndPtr->hwndSelf, WM_MOVE, 0,
44 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top));
45 if (!(flags & SWP_NOCLIENTSIZE))
47 if (wndPtr->dwStyle & WS_MAXIMIZE) wp = SIZE_MAXIMIZED;
48 else if (wndPtr->dwStyle & WS_MINIMIZE) wp = SIZE_MINIMIZED;
50 SendMessage16( wndPtr->hwndSelf, WM_SIZE, wp,
51 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
52 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
57 /***********************************************************************
58 * DEFWND_SetText
60 * Set the window text.
62 void DEFWND_SetText( WND *wndPtr, LPCSTR text )
64 if (!text) text = "";
65 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
66 wndPtr->text = HEAP_strdupA( SystemHeap, 0, text );
67 wndPtr->pDriver->pSetText(wndPtr, wndPtr->text);
70 /***********************************************************************
71 * DEFWND_ControlColor
73 * Default colors for control painting.
75 HBRUSH DEFWND_ControlColor( HDC hDC, UINT16 ctlType )
77 if( ctlType == CTLCOLOR_SCROLLBAR)
79 HBRUSH hb = GetSysColorBrush(COLOR_SCROLLBAR);
80 SetBkColor( hDC, RGB(255, 255, 255) );
81 SetTextColor( hDC, RGB(0, 0, 0) );
82 UnrealizeObject( hb );
83 return hb;
86 SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT));
88 if (TWEAK_WineLook > WIN31_LOOK) {
89 if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
90 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
91 else {
92 SetBkColor( hDC, GetSysColor(COLOR_3DFACE) );
93 return GetSysColorBrush(COLOR_3DFACE);
96 else
97 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
98 return GetSysColorBrush(COLOR_WINDOW);
102 /***********************************************************************
103 * DEFWND_SetRedraw
105 static void DEFWND_SetRedraw( WND* wndPtr, WPARAM wParam )
107 BOOL bVisible = wndPtr->dwStyle & WS_VISIBLE;
109 TRACE(win,"%04x %i\n", wndPtr->hwndSelf, (wParam!=0) );
111 if( wParam )
113 if( !bVisible )
115 wndPtr->dwStyle |= WS_VISIBLE;
116 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
119 else if( bVisible )
121 if( wndPtr->dwStyle & WS_MINIMIZE ) wParam = RDW_VALIDATE;
122 else wParam = RDW_ALLCHILDREN | RDW_VALIDATE;
124 PAINT_RedrawWindow( wndPtr->hwndSelf, NULL, 0, wParam, 0 );
125 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
126 wndPtr->dwStyle &= ~WS_VISIBLE;
130 /***********************************************************************
131 * DEFWND_Print
133 * This method handles the default behavior for the WM_PRINT message.
135 static void DEFWND_Print(
136 WND* wndPtr,
137 HDC hdc,
138 ULONG uFlags)
141 * Visibility flag.
143 if ( (uFlags & PRF_CHECKVISIBLE) &&
144 !IsWindowVisible(wndPtr->hwndSelf) )
145 return;
148 * Unimplemented flags.
150 if ( (uFlags & PRF_CHILDREN) ||
151 (uFlags & PRF_OWNED) ||
152 (uFlags & PRF_NONCLIENT) )
154 WARN(win,"WM_PRINT message with unsupported flags\n");
158 * Background
160 if ( uFlags & PRF_ERASEBKGND)
161 SendMessageA(wndPtr->hwndSelf, WM_ERASEBKGND, (WPARAM)hdc, 0);
164 * Client area
166 if ( uFlags & PRF_CLIENT)
167 SendMessageA(wndPtr->hwndSelf, WM_PRINTCLIENT, (WPARAM)hdc, PRF_CLIENT);
170 /***********************************************************************
171 * DEFWND_DefWinProc
173 * Default window procedure for messages that are the same in Win16 and Win32.
175 static LRESULT DEFWND_DefWinProc( WND *wndPtr, UINT msg, WPARAM wParam,
176 LPARAM lParam )
178 switch(msg)
180 case WM_NCPAINT:
181 return NC_HandleNCPaint( wndPtr->hwndSelf, (HRGN)wParam );
183 case WM_NCHITTEST:
184 return NC_HandleNCHitTest( wndPtr->hwndSelf, MAKEPOINT16(lParam) );
186 case WM_NCLBUTTONDOWN:
187 return NC_HandleNCLButtonDown( wndPtr, wParam, lParam );
189 case WM_LBUTTONDBLCLK:
190 case WM_NCLBUTTONDBLCLK:
191 return NC_HandleNCLButtonDblClk( wndPtr, wParam, lParam );
193 case WM_RBUTTONDOWN:
194 case WM_NCRBUTTONDOWN:
195 if ((wndPtr->flags & WIN_ISWIN32) || (TWEAK_WineLook > WIN31_LOOK))
197 ClientToScreen16(wndPtr->hwndSelf, (LPPOINT16)&lParam);
198 SendMessageA( wndPtr->hwndSelf, WM_CONTEXTMENU,
199 wndPtr->hwndSelf, lParam);
201 break;
203 case WM_CONTEXTMENU:
204 if( wndPtr->dwStyle & WS_CHILD )
205 SendMessageA( wndPtr->parent->hwndSelf, msg, wParam, lParam );
206 else
207 if (wndPtr->hSysMenu)
208 { /*
209 TrackPopupMenu32(wndPtr->hSysMenu,TPM_LEFTALIGN | TPM_RETURNCMD,LOWORD(lParam),HIWORD(lParam),0,wndPtr->hwndSelf,NULL);
210 DestroyMenu32(wndPtr->hSysMenu);
212 FIXME(win,"Display default popup menu\n");
213 /* Track system popup if click was in the caption area. */
215 break;
217 case WM_NCACTIVATE:
218 return NC_HandleNCActivate( wndPtr, wParam );
220 case WM_NCDESTROY:
221 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
222 wndPtr->text = NULL;
223 if (wndPtr->pVScroll) HeapFree( SystemHeap, 0, wndPtr->pVScroll );
224 if (wndPtr->pHScroll) HeapFree( SystemHeap, 0, wndPtr->pHScroll );
225 wndPtr->pVScroll = wndPtr->pHScroll = NULL;
226 return 0;
228 case WM_PRINT:
229 DEFWND_Print(wndPtr, (HDC)wParam, lParam);
230 return 0;
232 case WM_PAINTICON:
233 case WM_PAINT:
235 PAINTSTRUCT16 ps;
236 HDC16 hdc = BeginPaint16( wndPtr->hwndSelf, &ps );
237 if( hdc )
239 if( (wndPtr->dwStyle & WS_MINIMIZE) && wndPtr->class->hIcon )
241 int x = (wndPtr->rectWindow.right - wndPtr->rectWindow.left -
242 GetSystemMetrics(SM_CXICON))/2;
243 int y = (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top -
244 GetSystemMetrics(SM_CYICON))/2;
245 TRACE(win,"Painting class icon: vis rect=(%i,%i - %i,%i)\n",
246 ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
247 DrawIcon( hdc, x, y, wndPtr->class->hIcon );
249 EndPaint16( wndPtr->hwndSelf, &ps );
251 return 0;
254 case WM_SETREDRAW:
255 DEFWND_SetRedraw( wndPtr, wParam );
256 return 0;
258 case WM_CLOSE:
259 DestroyWindow( wndPtr->hwndSelf );
260 return 0;
262 case WM_MOUSEACTIVATE:
263 if (wndPtr->dwStyle & WS_CHILD)
265 LONG ret = SendMessage16( wndPtr->parent->hwndSelf,
266 WM_MOUSEACTIVATE, wParam, lParam );
267 if (ret) return ret;
270 /* Caption clicks are handled by the NC_HandleNCLButtonDown() */
271 return (LOWORD(lParam) == HTCAPTION) ? MA_NOACTIVATE : MA_ACTIVATE;
273 case WM_ACTIVATE:
274 /* The default action in Windows is to set the keyboard focus to
275 * the window, if it's being activated and not minimized */
276 if (LOWORD(wParam) != WA_INACTIVE) {
277 /* I don't know who put this SetWindowPos here, it does not
278 * seem very logical to have it here... (FIXME?) */
279 SetWindowPos(wndPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
280 SWP_NOMOVE | SWP_NOSIZE);
281 if (!(wndPtr->dwStyle & WS_MINIMIZE))
282 SetFocus(wndPtr->hwndSelf);
284 break;
286 case WM_ERASEBKGND:
287 case WM_ICONERASEBKGND:
289 if (!wndPtr->class->hbrBackground) return 0;
291 if (wndPtr->class->hbrBackground <= (HBRUSH16)(COLOR_MAX+1))
293 HBRUSH hbrush = CreateSolidBrush(
294 GetSysColor(((DWORD)wndPtr->class->hbrBackground)-1));
295 FillWindow16( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
296 (HDC16)wParam, hbrush);
297 DeleteObject( hbrush );
299 else FillWindow16( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
300 (HDC16)wParam, wndPtr->class->hbrBackground );
301 return 1;
304 case WM_GETDLGCODE:
305 return 0;
307 case WM_CTLCOLORMSGBOX:
308 case WM_CTLCOLOREDIT:
309 case WM_CTLCOLORLISTBOX:
310 case WM_CTLCOLORBTN:
311 case WM_CTLCOLORDLG:
312 case WM_CTLCOLORSTATIC:
313 case WM_CTLCOLORSCROLLBAR:
314 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
316 case WM_CTLCOLOR:
317 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
319 case WM_GETTEXTLENGTH:
320 if (wndPtr->text) return (LRESULT)strlen(wndPtr->text);
321 return 0;
323 case WM_SETCURSOR:
324 if (wndPtr->dwStyle & WS_CHILD)
325 if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
326 wParam, lParam))
327 return TRUE;
328 return NC_HandleSetCursor( wndPtr->hwndSelf, wParam, lParam );
330 case WM_SYSCOMMAND:
331 return NC_HandleSysCommand( wndPtr->hwndSelf, wParam,
332 MAKEPOINT16(lParam) );
334 case WM_KEYDOWN:
335 if(wParam == VK_F10) iF10Key = VK_F10;
336 break;
338 case WM_SYSKEYDOWN:
339 if( HIWORD(lParam) & KEYDATA_ALT )
341 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
342 if( wParam == VK_MENU && !iMenuSysKey )
343 iMenuSysKey = 1;
344 else
345 iMenuSysKey = 0;
347 iF10Key = 0;
349 if( wParam == VK_F4 ) /* try to close the window */
351 HWND hWnd = WIN_GetTopParent( wndPtr->hwndSelf );
352 wndPtr = WIN_FindWndPtr( hWnd );
353 if( wndPtr && !(wndPtr->class->style & CS_NOCLOSE) )
354 PostMessage16( hWnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
355 WIN_ReleaseWndPtr(wndPtr);
358 else if( wParam == VK_F10 )
359 iF10Key = 1;
360 else
361 if( wParam == VK_ESCAPE && (GetKeyState(VK_SHIFT) & 0x8000))
362 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
363 (WPARAM16)SC_KEYMENU, (LPARAM)VK_SPACE);
364 break;
366 case WM_KEYUP:
367 case WM_SYSKEYUP:
368 /* Press and release F10 or ALT */
369 if (((wParam == VK_MENU) && iMenuSysKey) ||
370 ((wParam == VK_F10) && iF10Key))
371 SendMessage16( WIN_GetTopParent(wndPtr->hwndSelf),
372 WM_SYSCOMMAND, SC_KEYMENU, 0L );
373 iMenuSysKey = iF10Key = 0;
374 break;
376 case WM_SYSCHAR:
377 iMenuSysKey = 0;
378 if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
380 PostMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
381 (WPARAM16)SC_RESTORE, 0L );
382 break;
384 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
386 if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
387 if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
388 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
389 else
390 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
391 (WPARAM16)SC_KEYMENU, (LPARAM)(DWORD)wParam );
393 else /* check for Ctrl-Esc */
394 if (wParam != VK_ESCAPE) MessageBeep(0);
395 break;
397 case WM_SHOWWINDOW:
398 if (!lParam) return 0; /* sent from ShowWindow */
399 if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
400 if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
401 else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
402 ShowWindow( wndPtr->hwndSelf, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
403 break;
405 case WM_CANCELMODE:
406 if (wndPtr->parent == WIN_GetDesktop()) EndMenu();
407 if (GetCapture() == wndPtr->hwndSelf) ReleaseCapture();
408 WIN_ReleaseDesktop();
409 break;
411 case WM_VKEYTOITEM:
412 case WM_CHARTOITEM:
413 return -1;
415 case WM_DROPOBJECT:
416 return DRAG_FILE;
418 case WM_QUERYDROPOBJECT:
419 if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
420 break;
422 case WM_QUERYDRAGICON:
424 HICON16 hIcon=0;
425 UINT16 len;
427 if( (hIcon=wndPtr->class->hCursor) ) return (LRESULT)hIcon;
428 for(len=1; len<64; len++)
429 if((hIcon=LoadIcon16(wndPtr->hInstance,MAKEINTRESOURCE16(len))))
430 return (LRESULT)hIcon;
431 return (LRESULT)LoadIcon16(0,IDI_APPLICATION16);
433 break;
435 case WM_ISACTIVEICON:
436 return ((wndPtr->flags & WIN_NCACTIVATED) != 0);
438 case WM_NOTIFYFORMAT:
439 if (IsWindowUnicode(wndPtr->hwndSelf)) return NFR_UNICODE;
440 else return NFR_ANSI;
442 case WM_QUERYOPEN:
443 case WM_QUERYENDSESSION:
444 return 1;
446 return 0;
451 /***********************************************************************
452 * DefWindowProc16 (USER.107)
454 LRESULT WINAPI DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
455 LPARAM lParam )
457 WND * wndPtr = WIN_FindWndPtr( hwnd );
458 LRESULT result = 0;
460 if (!wndPtr) return 0;
461 SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
463 switch(msg)
465 case WM_NCCREATE:
467 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
468 if (cs->lpszName)
469 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(cs->lpszName) );
470 result = 1;
472 break;
474 case WM_NCCALCSIZE:
476 RECT rect32;
477 CONV_RECT16TO32( (RECT16 *)PTR_SEG_TO_LIN(lParam), &rect32 );
478 result = NC_HandleNCCalcSize( wndPtr, &rect32 );
479 CONV_RECT32TO16( &rect32, (RECT16 *)PTR_SEG_TO_LIN(lParam) );
481 break;
483 case WM_WINDOWPOSCHANGING:
484 result = WINPOS_HandleWindowPosChanging16( wndPtr,
485 (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam) );
486 break;
488 case WM_WINDOWPOSCHANGED:
490 WINDOWPOS16 * winPos = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
491 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
493 break;
495 case WM_GETTEXT:
496 if (wParam && wndPtr->text)
498 lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(lParam), wndPtr->text, wParam );
499 result = (LRESULT)strlen( (LPSTR)PTR_SEG_TO_LIN(lParam) );
501 break;
503 case WM_SETTEXT:
504 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(lParam) );
505 if( wndPtr->dwStyle & WS_CAPTION ) NC_HandleNCPaint( hwnd , (HRGN)1 );
506 break;
508 default:
509 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
510 break;
513 WIN_ReleaseWndPtr(wndPtr);
514 SPY_ExitMessage( SPY_RESULT_DEFWND16, hwnd, msg, result );
515 return result;
519 /***********************************************************************
520 * DefWindowProc32A [USER32.126]
523 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam,
524 LPARAM lParam )
526 WND * wndPtr = WIN_FindWndPtr( hwnd );
527 LRESULT result = 0;
529 if (!wndPtr) return 0;
530 SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
532 switch(msg)
534 case WM_NCCREATE:
536 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
537 if (cs->lpszName) DEFWND_SetText( wndPtr, cs->lpszName );
538 result = 1;
540 break;
542 case WM_NCCALCSIZE:
543 result = NC_HandleNCCalcSize( wndPtr, (RECT *)lParam );
544 break;
546 case WM_WINDOWPOSCHANGING:
547 result = WINPOS_HandleWindowPosChanging( wndPtr,
548 (WINDOWPOS *)lParam );
549 break;
551 case WM_WINDOWPOSCHANGED:
553 WINDOWPOS * winPos = (WINDOWPOS *)lParam;
554 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
556 break;
558 case WM_GETTEXT:
559 if (wParam && wndPtr->text)
561 lstrcpynA( (LPSTR)lParam, wndPtr->text, wParam );
562 result = (LRESULT)strlen( (LPSTR)lParam );
564 break;
566 case WM_SETTEXT:
567 DEFWND_SetText( wndPtr, (LPSTR)lParam );
568 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
569 break;
571 default:
572 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
573 break;
576 WIN_ReleaseWndPtr(wndPtr);
577 SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result );
578 return result;
582 /***********************************************************************
583 * DefWindowProc32W [USER32.127] Calls default window message handler
585 * Calls default window procedure for messages not processed
586 * by application.
588 * RETURNS
589 * Return value is dependent upon the message.
591 LRESULT WINAPI DefWindowProcW(
592 HWND hwnd, /* [in] window procedure recieving message */
593 UINT msg, /* [in] message identifier */
594 WPARAM wParam, /* [in] first message parameter */
595 LPARAM lParam ) /* [in] second message parameter */
597 LRESULT result;
599 switch(msg)
601 case WM_NCCREATE:
603 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
604 if (cs->lpszName)
606 WND *wndPtr = WIN_FindWndPtr( hwnd );
607 LPSTR str = HEAP_strdupWtoA(GetProcessHeap(), 0, cs->lpszName);
608 DEFWND_SetText( wndPtr, str );
609 HeapFree( GetProcessHeap(), 0, str );
610 WIN_ReleaseWndPtr(wndPtr);
612 result = 1;
614 break;
616 case WM_GETTEXT:
618 LPSTR str = HeapAlloc( GetProcessHeap(), 0, wParam );
619 result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)str );
620 lstrcpynAtoW( (LPWSTR)lParam, str, wParam );
621 HeapFree( GetProcessHeap(), 0, str );
623 break;
625 case WM_SETTEXT:
627 LPSTR str = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPWSTR)lParam );
628 result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)str );
629 HeapFree( GetProcessHeap(), 0, str );
631 break;
633 default:
634 result = DefWindowProcA( hwnd, msg, wParam, lParam );
635 break;
637 return result;