Implemented PolylineTo.
[wine/multimedia.git] / windows / defwnd.c
blob879ff50a6c11a3a7f63bbcd2c54a0154aeb260a2
1 /*
2 * Default window procedure
4 * Copyright 1993, 1996 Alexandre Julliard
5 * 1995 Alex Korobka
6 */
8 #include <stdlib.h>
9 #include "win.h"
10 #include "user.h"
11 #include "heap.h"
12 #include "nonclient.h"
13 #include "winpos.h"
14 #include "dce.h"
15 #include "sysmetrics.h"
16 #include "debug.h"
17 #include "spy.h"
18 #include "tweak.h"
20 /* Last COLOR id */
21 #define COLOR_MAX COLOR_BTNHIGHLIGHT
23 /* bits in the dwKeyData */
24 #define KEYDATA_ALT 0x2000
25 #define KEYDATA_PREVSTATE 0x4000
27 static short iF10Key = 0;
28 static short iMenuSysKey = 0;
30 /***********************************************************************
31 * DEFWND_HandleWindowPosChanged
33 * Handle the WM_WINDOWPOSCHANGED message.
35 static void DEFWND_HandleWindowPosChanged( WND *wndPtr, UINT32 flags )
37 WPARAM16 wp = SIZE_RESTORED;
39 if (!(flags & SWP_NOCLIENTMOVE))
40 SendMessage16( wndPtr->hwndSelf, WM_MOVE, 0,
41 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top));
42 if (!(flags & SWP_NOCLIENTSIZE))
44 if (wndPtr->dwStyle & WS_MAXIMIZE) wp = SIZE_MAXIMIZED;
45 else if (wndPtr->dwStyle & WS_MINIMIZE) wp = SIZE_MINIMIZED;
47 SendMessage16( wndPtr->hwndSelf, WM_SIZE, wp,
48 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
49 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
54 /***********************************************************************
55 * DEFWND_SetText
57 * Set the window text.
59 void DEFWND_SetText( WND *wndPtr, LPCSTR text )
61 if (!text) text = "";
62 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
63 wndPtr->text = HEAP_strdupA( SystemHeap, 0, text );
64 wndPtr->pDriver->pSetText(wndPtr, wndPtr->text);
67 /***********************************************************************
68 * DEFWND_ControlColor
70 * Default colors for control painting.
72 HBRUSH32 DEFWND_ControlColor( HDC32 hDC, UINT16 ctlType )
74 if( ctlType == CTLCOLOR_SCROLLBAR)
76 HBRUSH32 hb = GetSysColorBrush32(COLOR_SCROLLBAR);
77 SetBkColor32( hDC, RGB(255, 255, 255) );
78 SetTextColor32( hDC, RGB(0, 0, 0) );
79 UnrealizeObject32( hb );
80 return hb;
83 SetTextColor32( hDC, GetSysColor32(COLOR_WINDOWTEXT));
85 if (TWEAK_WineLook > WIN31_LOOK) {
86 if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
87 SetBkColor32( hDC, GetSysColor32(COLOR_WINDOW) );
88 else {
89 SetBkColor32( hDC, GetSysColor32(COLOR_3DFACE) );
90 return GetSysColorBrush32(COLOR_3DFACE);
93 else
94 SetBkColor32( hDC, GetSysColor32(COLOR_WINDOW) );
95 return GetSysColorBrush32(COLOR_WINDOW);
99 /***********************************************************************
100 * DEFWND_SetRedraw
102 static void DEFWND_SetRedraw( WND* wndPtr, WPARAM32 wParam )
104 BOOL32 bVisible = wndPtr->dwStyle & WS_VISIBLE;
106 TRACE(win,"%04x %i\n", wndPtr->hwndSelf, (wParam!=0) );
108 if( wParam )
110 if( !bVisible )
112 wndPtr->dwStyle |= WS_VISIBLE;
113 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
116 else if( bVisible )
118 if( wndPtr->dwStyle & WS_MINIMIZE ) wParam = RDW_VALIDATE;
119 else wParam = RDW_ALLCHILDREN | RDW_VALIDATE;
121 PAINT_RedrawWindow( wndPtr->hwndSelf, NULL, 0, wParam, 0 );
122 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
123 wndPtr->dwStyle &= ~WS_VISIBLE;
127 /***********************************************************************
128 * DEFWND_DefWinProc
130 * Default window procedure for messages that are the same in Win16 and Win32.
132 static LRESULT DEFWND_DefWinProc( WND *wndPtr, UINT32 msg, WPARAM32 wParam,
133 LPARAM lParam )
135 switch(msg)
137 case WM_NCPAINT:
138 return NC_HandleNCPaint( wndPtr->hwndSelf, (HRGN32)wParam );
140 case WM_NCHITTEST:
141 return NC_HandleNCHitTest( wndPtr->hwndSelf, MAKEPOINT16(lParam) );
143 case WM_NCLBUTTONDOWN:
144 return NC_HandleNCLButtonDown( wndPtr, wParam, lParam );
146 case WM_LBUTTONDBLCLK:
147 case WM_NCLBUTTONDBLCLK:
148 return NC_HandleNCLButtonDblClk( wndPtr, wParam, lParam );
150 case WM_RBUTTONDOWN:
151 case WM_NCRBUTTONDOWN:
152 if ((wndPtr->flags & WIN_ISWIN32) || (TWEAK_WineLook > WIN31_LOOK))
154 ClientToScreen16(wndPtr->hwndSelf, (LPPOINT16)&lParam);
155 SendMessage32A( wndPtr->hwndSelf, WM_CONTEXTMENU,
156 wndPtr->hwndSelf, lParam);
158 break;
160 case WM_CONTEXTMENU:
161 if( wndPtr->dwStyle & WS_CHILD )
162 SendMessage32A( wndPtr->parent->hwndSelf, msg, wParam, lParam );
163 else
164 if (wndPtr->hSysMenu)
165 { /*
166 TrackPopupMenu32(wndPtr->hSysMenu,TPM_LEFTALIGN | TPM_RETURNCMD,LOWORD(lParam),HIWORD(lParam),0,wndPtr->hwndSelf,NULL);
167 DestroyMenu32(wndPtr->hSysMenu);
169 FIXME(win,"Display default popup menu\n");
170 /* Track system popup if click was in the caption area. */
172 break;
174 case WM_NCACTIVATE:
175 return NC_HandleNCActivate( wndPtr, wParam );
177 case WM_NCDESTROY:
178 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
179 wndPtr->text = NULL;
180 if (wndPtr->pVScroll) HeapFree( SystemHeap, 0, wndPtr->pVScroll );
181 if (wndPtr->pHScroll) HeapFree( SystemHeap, 0, wndPtr->pHScroll );
182 wndPtr->pVScroll = wndPtr->pHScroll = NULL;
183 return 0;
185 case WM_PAINTICON:
186 case WM_PAINT:
188 PAINTSTRUCT16 ps;
189 HDC16 hdc = BeginPaint16( wndPtr->hwndSelf, &ps );
190 if( hdc )
192 if( (wndPtr->dwStyle & WS_MINIMIZE) && wndPtr->class->hIcon )
194 int x = (wndPtr->rectWindow.right - wndPtr->rectWindow.left -
195 SYSMETRICS_CXICON)/2;
196 int y = (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top -
197 SYSMETRICS_CYICON)/2;
198 TRACE(win,"Painting class icon: vis rect=(%i,%i - %i,%i)\n",
199 ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
200 DrawIcon32( hdc, x, y, wndPtr->class->hIcon );
202 EndPaint16( wndPtr->hwndSelf, &ps );
204 return 0;
207 case WM_SETREDRAW:
208 DEFWND_SetRedraw( wndPtr, wParam );
209 return 0;
211 case WM_CLOSE:
212 DestroyWindow32( wndPtr->hwndSelf );
213 return 0;
215 case WM_MOUSEACTIVATE:
216 if (wndPtr->dwStyle & WS_CHILD)
218 LONG ret = SendMessage16( wndPtr->parent->hwndSelf,
219 WM_MOUSEACTIVATE, wParam, lParam );
220 if (ret) return ret;
223 /* Caption clicks are handled by the NC_HandleNCLButtonDown() */
224 return (LOWORD(lParam) == HTCAPTION) ? MA_NOACTIVATE : MA_ACTIVATE;
226 case WM_ACTIVATE:
227 if (LOWORD(wParam) != WA_INACTIVE)
228 SetWindowPos32(wndPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
229 SWP_NOMOVE | SWP_NOSIZE);
230 break;
232 case WM_ERASEBKGND:
233 case WM_ICONERASEBKGND:
235 if (!wndPtr->class->hbrBackground) return 0;
237 if (wndPtr->class->hbrBackground <= (HBRUSH16)(COLOR_MAX+1))
239 HBRUSH32 hbrush = CreateSolidBrush32(
240 GetSysColor32(((DWORD)wndPtr->class->hbrBackground)-1));
241 FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
242 (HDC16)wParam, hbrush);
243 DeleteObject32( hbrush );
245 else FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
246 (HDC16)wParam, wndPtr->class->hbrBackground );
247 return 1;
250 case WM_GETDLGCODE:
251 return 0;
253 case WM_CTLCOLORMSGBOX:
254 case WM_CTLCOLOREDIT:
255 case WM_CTLCOLORLISTBOX:
256 case WM_CTLCOLORBTN:
257 case WM_CTLCOLORDLG:
258 case WM_CTLCOLORSTATIC:
259 case WM_CTLCOLORSCROLLBAR:
260 return (LRESULT)DEFWND_ControlColor( (HDC32)wParam, msg - WM_CTLCOLORMSGBOX );
262 case WM_CTLCOLOR:
263 return (LRESULT)DEFWND_ControlColor( (HDC32)wParam, HIWORD(lParam) );
265 case WM_GETTEXTLENGTH:
266 if (wndPtr->text) return (LRESULT)strlen(wndPtr->text);
267 return 0;
269 case WM_SETCURSOR:
270 if (wndPtr->dwStyle & WS_CHILD)
271 if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
272 wParam, lParam))
273 return TRUE;
274 return NC_HandleSetCursor( wndPtr->hwndSelf, wParam, lParam );
276 case WM_SYSCOMMAND:
277 return NC_HandleSysCommand( wndPtr->hwndSelf, wParam,
278 MAKEPOINT16(lParam) );
280 case WM_KEYDOWN:
281 if(wParam == VK_F10) iF10Key = VK_F10;
282 break;
284 case WM_SYSKEYDOWN:
285 if( HIWORD(lParam) & KEYDATA_ALT )
287 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
288 if( wParam == VK_MENU && !iMenuSysKey )
289 iMenuSysKey = 1;
290 else
291 iMenuSysKey = 0;
293 iF10Key = 0;
295 if( wParam == VK_F4 ) /* try to close the window */
297 HWND32 hWnd = WIN_GetTopParent( wndPtr->hwndSelf );
298 wndPtr = WIN_FindWndPtr( hWnd );
299 if( wndPtr && !(wndPtr->class->style & CS_NOCLOSE) )
300 PostMessage16( hWnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
303 else if( wParam == VK_F10 )
304 iF10Key = 1;
305 else
306 if( wParam == VK_ESCAPE && (GetKeyState32(VK_SHIFT) & 0x8000))
307 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
308 (WPARAM16)SC_KEYMENU, (LPARAM)VK_SPACE);
309 break;
311 case WM_KEYUP:
312 case WM_SYSKEYUP:
313 /* Press and release F10 or ALT */
314 if (((wParam == VK_MENU) && iMenuSysKey) ||
315 ((wParam == VK_F10) && iF10Key))
316 SendMessage16( WIN_GetTopParent(wndPtr->hwndSelf),
317 WM_SYSCOMMAND, SC_KEYMENU, 0L );
318 iMenuSysKey = iF10Key = 0;
319 break;
321 case WM_SYSCHAR:
322 iMenuSysKey = 0;
323 if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
325 PostMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
326 (WPARAM16)SC_RESTORE, 0L );
327 break;
329 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
331 if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
332 if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
333 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
334 else
335 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
336 (WPARAM16)SC_KEYMENU, (LPARAM)(DWORD)wParam );
338 else /* check for Ctrl-Esc */
339 if (wParam != VK_ESCAPE) MessageBeep32(0);
340 break;
342 case WM_SHOWWINDOW:
343 if (!lParam) return 0; /* sent from ShowWindow */
344 if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
345 if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
346 else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
347 ShowWindow32( wndPtr->hwndSelf, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
348 break;
350 case WM_CANCELMODE:
351 if (wndPtr->parent == WIN_GetDesktop()) EndMenu();
352 if (GetCapture32() == wndPtr->hwndSelf) ReleaseCapture();
353 break;
355 case WM_VKEYTOITEM:
356 case WM_CHARTOITEM:
357 return -1;
359 case WM_DROPOBJECT:
360 return DRAG_FILE;
362 case WM_QUERYDROPOBJECT:
363 if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
364 break;
366 case WM_QUERYDRAGICON:
368 HICON16 hIcon=0;
369 UINT16 len;
371 if( (hIcon=wndPtr->class->hCursor) ) return (LRESULT)hIcon;
372 for(len=1; len<64; len++)
373 if((hIcon=LoadIcon16(wndPtr->hInstance,MAKEINTRESOURCE16(len))))
374 return (LRESULT)hIcon;
375 return (LRESULT)LoadIcon16(0,IDI_APPLICATION16);
377 break;
379 case WM_ISACTIVEICON:
380 return ((wndPtr->flags & WIN_NCACTIVATED) != 0);
382 case WM_QUERYOPEN:
383 case WM_QUERYENDSESSION:
384 return 1;
386 return 0;
391 /***********************************************************************
392 * DefWindowProc16 (USER.107)
394 LRESULT WINAPI DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
395 LPARAM lParam )
397 WND * wndPtr = WIN_FindWndPtr( hwnd );
398 LRESULT result = 0;
400 if (!wndPtr) return 0;
401 SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
403 switch(msg)
405 case WM_NCCREATE:
407 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
408 if (cs->lpszName)
409 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(cs->lpszName) );
410 result = 1;
412 break;
414 case WM_NCCALCSIZE:
416 RECT32 rect32;
417 CONV_RECT16TO32( (RECT16 *)PTR_SEG_TO_LIN(lParam), &rect32 );
418 result = NC_HandleNCCalcSize( wndPtr, &rect32 );
419 CONV_RECT32TO16( &rect32, (RECT16 *)PTR_SEG_TO_LIN(lParam) );
421 break;
423 case WM_WINDOWPOSCHANGING:
424 result = WINPOS_HandleWindowPosChanging16( wndPtr,
425 (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam) );
426 break;
428 case WM_WINDOWPOSCHANGED:
430 WINDOWPOS16 * winPos = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
431 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
433 break;
435 case WM_GETTEXT:
436 if (wParam && wndPtr->text)
438 lstrcpyn32A( (LPSTR)PTR_SEG_TO_LIN(lParam), wndPtr->text, wParam );
439 result = (LRESULT)strlen( (LPSTR)PTR_SEG_TO_LIN(lParam) );
441 break;
443 case WM_SETTEXT:
444 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(lParam) );
445 if( wndPtr->dwStyle & WS_CAPTION ) NC_HandleNCPaint( hwnd , (HRGN32)1 );
446 break;
448 default:
449 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
450 break;
453 SPY_ExitMessage( SPY_RESULT_DEFWND16, hwnd, msg, result );
454 return result;
458 /***********************************************************************
459 * DefWindowProc32A [USER32.126]
462 LRESULT WINAPI DefWindowProc32A( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
463 LPARAM lParam )
465 WND * wndPtr = WIN_FindWndPtr( hwnd );
466 LRESULT result = 0;
468 if (!wndPtr) return 0;
469 SPY_EnterMessage( SPY_DEFWNDPROC32, hwnd, msg, wParam, lParam );
471 switch(msg)
473 case WM_NCCREATE:
475 CREATESTRUCT32A *cs = (CREATESTRUCT32A *)lParam;
476 if (cs->lpszName) DEFWND_SetText( wndPtr, cs->lpszName );
477 result = 1;
479 break;
481 case WM_NCCALCSIZE:
482 result = NC_HandleNCCalcSize( wndPtr, (RECT32 *)lParam );
483 break;
485 case WM_WINDOWPOSCHANGING:
486 result = WINPOS_HandleWindowPosChanging32( wndPtr,
487 (WINDOWPOS32 *)lParam );
488 break;
490 case WM_WINDOWPOSCHANGED:
492 WINDOWPOS32 * winPos = (WINDOWPOS32 *)lParam;
493 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
495 break;
497 case WM_GETTEXT:
498 if (wParam && wndPtr->text)
500 lstrcpyn32A( (LPSTR)lParam, wndPtr->text, wParam );
501 result = (LRESULT)strlen( (LPSTR)lParam );
503 break;
505 case WM_SETTEXT:
506 DEFWND_SetText( wndPtr, (LPSTR)lParam );
507 NC_HandleNCPaint( hwnd , (HRGN32)1 ); /* Repaint caption */
508 break;
510 default:
511 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
512 break;
515 SPY_ExitMessage( SPY_RESULT_DEFWND32, hwnd, msg, result );
516 return result;
520 /***********************************************************************
521 * DefWindowProc32W [USER32.127] Calls default window message handler
523 * Calls default window procedure for messages not processed
524 * by application.
526 * RETURNS
527 * Return value is dependent upon the message.
529 LRESULT WINAPI DefWindowProc32W(
530 HWND32 hwnd, /* [in] window procedure recieving message */
531 UINT32 msg, /* [in] message identifier */
532 WPARAM32 wParam, /* [in] first message parameter */
533 LPARAM lParam ) /* [in] second message parameter */
535 LRESULT result;
537 switch(msg)
539 case WM_NCCREATE:
541 CREATESTRUCT32W *cs = (CREATESTRUCT32W *)lParam;
542 if (cs->lpszName)
544 WND *wndPtr = WIN_FindWndPtr( hwnd );
545 LPSTR str = HEAP_strdupWtoA(GetProcessHeap(), 0, cs->lpszName);
546 DEFWND_SetText( wndPtr, str );
547 HeapFree( GetProcessHeap(), 0, str );
549 result = 1;
551 break;
553 case WM_GETTEXT:
555 LPSTR str = HeapAlloc( GetProcessHeap(), 0, wParam );
556 result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
557 lstrcpynAtoW( (LPWSTR)lParam, str, wParam );
558 HeapFree( GetProcessHeap(), 0, str );
560 break;
562 case WM_SETTEXT:
564 LPSTR str = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPWSTR)lParam );
565 result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
566 HeapFree( GetProcessHeap(), 0, str );
568 break;
570 default:
571 result = DefWindowProc32A( hwnd, msg, wParam, lParam );
572 break;
574 return result;