Release 990226.
[wine/gsoc-2012-control.git] / windows / defwnd.c
blob265a559771239ab2ae02c125b1a93df06bbb15a9
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"
19 #include "wine/winuser16.h"
21 /* Last COLOR id */
22 #define COLOR_MAX COLOR_BTNHIGHLIGHT
24 /* bits in the dwKeyData */
25 #define KEYDATA_ALT 0x2000
26 #define KEYDATA_PREVSTATE 0x4000
28 static short iF10Key = 0;
29 static short iMenuSysKey = 0;
31 /***********************************************************************
32 * DEFWND_HandleWindowPosChanged
34 * Handle the WM_WINDOWPOSCHANGED message.
36 static void DEFWND_HandleWindowPosChanged( WND *wndPtr, UINT flags )
38 WPARAM16 wp = SIZE_RESTORED;
40 if (!(flags & SWP_NOCLIENTMOVE))
41 SendMessage16( wndPtr->hwndSelf, WM_MOVE, 0,
42 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top));
43 if (!(flags & SWP_NOCLIENTSIZE))
45 if (wndPtr->dwStyle & WS_MAXIMIZE) wp = SIZE_MAXIMIZED;
46 else if (wndPtr->dwStyle & WS_MINIMIZE) wp = SIZE_MINIMIZED;
48 SendMessage16( wndPtr->hwndSelf, WM_SIZE, wp,
49 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
50 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
55 /***********************************************************************
56 * DEFWND_SetText
58 * Set the window text.
60 void DEFWND_SetText( WND *wndPtr, LPCSTR text )
62 if (!text) text = "";
63 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
64 wndPtr->text = HEAP_strdupA( SystemHeap, 0, text );
65 wndPtr->pDriver->pSetText(wndPtr, wndPtr->text);
68 /***********************************************************************
69 * DEFWND_ControlColor
71 * Default colors for control painting.
73 HBRUSH DEFWND_ControlColor( HDC hDC, UINT16 ctlType )
75 if( ctlType == CTLCOLOR_SCROLLBAR)
77 HBRUSH hb = GetSysColorBrush(COLOR_SCROLLBAR);
78 SetBkColor( hDC, RGB(255, 255, 255) );
79 SetTextColor( hDC, RGB(0, 0, 0) );
80 UnrealizeObject( hb );
81 return hb;
84 SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT));
86 if (TWEAK_WineLook > WIN31_LOOK) {
87 if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
88 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
89 else {
90 SetBkColor( hDC, GetSysColor(COLOR_3DFACE) );
91 return GetSysColorBrush(COLOR_3DFACE);
94 else
95 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
96 return GetSysColorBrush(COLOR_WINDOW);
100 /***********************************************************************
101 * DEFWND_SetRedraw
103 static void DEFWND_SetRedraw( WND* wndPtr, WPARAM wParam )
105 BOOL bVisible = wndPtr->dwStyle & WS_VISIBLE;
107 TRACE(win,"%04x %i\n", wndPtr->hwndSelf, (wParam!=0) );
109 if( wParam )
111 if( !bVisible )
113 wndPtr->dwStyle |= WS_VISIBLE;
114 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
117 else if( bVisible )
119 if( wndPtr->dwStyle & WS_MINIMIZE ) wParam = RDW_VALIDATE;
120 else wParam = RDW_ALLCHILDREN | RDW_VALIDATE;
122 PAINT_RedrawWindow( wndPtr->hwndSelf, NULL, 0, wParam, 0 );
123 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
124 wndPtr->dwStyle &= ~WS_VISIBLE;
128 /***********************************************************************
129 * DEFWND_DefWinProc
131 * Default window procedure for messages that are the same in Win16 and Win32.
133 static LRESULT DEFWND_DefWinProc( WND *wndPtr, UINT msg, WPARAM wParam,
134 LPARAM lParam )
136 switch(msg)
138 case WM_NCPAINT:
139 return NC_HandleNCPaint( wndPtr->hwndSelf, (HRGN)wParam );
141 case WM_NCHITTEST:
142 return NC_HandleNCHitTest( wndPtr->hwndSelf, MAKEPOINT16(lParam) );
144 case WM_NCLBUTTONDOWN:
145 return NC_HandleNCLButtonDown( wndPtr, wParam, lParam );
147 case WM_LBUTTONDBLCLK:
148 case WM_NCLBUTTONDBLCLK:
149 return NC_HandleNCLButtonDblClk( wndPtr, wParam, lParam );
151 case WM_RBUTTONDOWN:
152 case WM_NCRBUTTONDOWN:
153 if ((wndPtr->flags & WIN_ISWIN32) || (TWEAK_WineLook > WIN31_LOOK))
155 ClientToScreen16(wndPtr->hwndSelf, (LPPOINT16)&lParam);
156 SendMessageA( wndPtr->hwndSelf, WM_CONTEXTMENU,
157 wndPtr->hwndSelf, lParam);
159 break;
161 case WM_CONTEXTMENU:
162 if( wndPtr->dwStyle & WS_CHILD )
163 SendMessageA( wndPtr->parent->hwndSelf, msg, wParam, lParam );
164 else
165 if (wndPtr->hSysMenu)
166 { /*
167 TrackPopupMenu32(wndPtr->hSysMenu,TPM_LEFTALIGN | TPM_RETURNCMD,LOWORD(lParam),HIWORD(lParam),0,wndPtr->hwndSelf,NULL);
168 DestroyMenu32(wndPtr->hSysMenu);
170 FIXME(win,"Display default popup menu\n");
171 /* Track system popup if click was in the caption area. */
173 break;
175 case WM_NCACTIVATE:
176 return NC_HandleNCActivate( wndPtr, wParam );
178 case WM_NCDESTROY:
179 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
180 wndPtr->text = NULL;
181 if (wndPtr->pVScroll) HeapFree( SystemHeap, 0, wndPtr->pVScroll );
182 if (wndPtr->pHScroll) HeapFree( SystemHeap, 0, wndPtr->pHScroll );
183 wndPtr->pVScroll = wndPtr->pHScroll = NULL;
184 return 0;
186 case WM_PAINTICON:
187 case WM_PAINT:
189 PAINTSTRUCT16 ps;
190 HDC16 hdc = BeginPaint16( wndPtr->hwndSelf, &ps );
191 if( hdc )
193 if( (wndPtr->dwStyle & WS_MINIMIZE) && wndPtr->class->hIcon )
195 int x = (wndPtr->rectWindow.right - wndPtr->rectWindow.left -
196 SYSMETRICS_CXICON)/2;
197 int y = (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top -
198 SYSMETRICS_CYICON)/2;
199 TRACE(win,"Painting class icon: vis rect=(%i,%i - %i,%i)\n",
200 ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
201 DrawIcon( hdc, x, y, wndPtr->class->hIcon );
203 EndPaint16( wndPtr->hwndSelf, &ps );
205 return 0;
208 case WM_SETREDRAW:
209 DEFWND_SetRedraw( wndPtr, wParam );
210 return 0;
212 case WM_CLOSE:
213 DestroyWindow( wndPtr->hwndSelf );
214 return 0;
216 case WM_MOUSEACTIVATE:
217 if (wndPtr->dwStyle & WS_CHILD)
219 LONG ret = SendMessage16( wndPtr->parent->hwndSelf,
220 WM_MOUSEACTIVATE, wParam, lParam );
221 if (ret) return ret;
224 /* Caption clicks are handled by the NC_HandleNCLButtonDown() */
225 return (LOWORD(lParam) == HTCAPTION) ? MA_NOACTIVATE : MA_ACTIVATE;
227 case WM_ACTIVATE:
228 /* The default action in Windows is to set the keyboard focus to
229 * the window, if it's being activated and not minimized */
230 if (LOWORD(wParam) != WA_INACTIVE) {
231 /* I don't know who put this SetWindowPos here, it does not
232 * seem very logical to have it here... (FIXME?) */
233 SetWindowPos(wndPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
234 SWP_NOMOVE | SWP_NOSIZE);
235 if (!(wndPtr->dwStyle & WS_MINIMIZE))
236 SetFocus(wndPtr->hwndSelf);
238 break;
240 case WM_ERASEBKGND:
241 case WM_ICONERASEBKGND:
243 if (!wndPtr->class->hbrBackground) return 0;
245 if (wndPtr->class->hbrBackground <= (HBRUSH16)(COLOR_MAX+1))
247 HBRUSH hbrush = CreateSolidBrush(
248 GetSysColor(((DWORD)wndPtr->class->hbrBackground)-1));
249 FillWindow16( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
250 (HDC16)wParam, hbrush);
251 DeleteObject( hbrush );
253 else FillWindow16( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
254 (HDC16)wParam, wndPtr->class->hbrBackground );
255 return 1;
258 case WM_GETDLGCODE:
259 return 0;
261 case WM_CTLCOLORMSGBOX:
262 case WM_CTLCOLOREDIT:
263 case WM_CTLCOLORLISTBOX:
264 case WM_CTLCOLORBTN:
265 case WM_CTLCOLORDLG:
266 case WM_CTLCOLORSTATIC:
267 case WM_CTLCOLORSCROLLBAR:
268 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
270 case WM_CTLCOLOR:
271 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
273 case WM_GETTEXTLENGTH:
274 if (wndPtr->text) return (LRESULT)strlen(wndPtr->text);
275 return 0;
277 case WM_SETCURSOR:
278 if (wndPtr->dwStyle & WS_CHILD)
279 if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
280 wParam, lParam))
281 return TRUE;
282 return NC_HandleSetCursor( wndPtr->hwndSelf, wParam, lParam );
284 case WM_SYSCOMMAND:
285 return NC_HandleSysCommand( wndPtr->hwndSelf, wParam,
286 MAKEPOINT16(lParam) );
288 case WM_KEYDOWN:
289 if(wParam == VK_F10) iF10Key = VK_F10;
290 break;
292 case WM_SYSKEYDOWN:
293 if( HIWORD(lParam) & KEYDATA_ALT )
295 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
296 if( wParam == VK_MENU && !iMenuSysKey )
297 iMenuSysKey = 1;
298 else
299 iMenuSysKey = 0;
301 iF10Key = 0;
303 if( wParam == VK_F4 ) /* try to close the window */
305 HWND hWnd = WIN_GetTopParent( wndPtr->hwndSelf );
306 wndPtr = WIN_FindWndPtr( hWnd );
307 if( wndPtr && !(wndPtr->class->style & CS_NOCLOSE) )
308 PostMessage16( hWnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
311 else if( wParam == VK_F10 )
312 iF10Key = 1;
313 else
314 if( wParam == VK_ESCAPE && (GetKeyState(VK_SHIFT) & 0x8000))
315 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
316 (WPARAM16)SC_KEYMENU, (LPARAM)VK_SPACE);
317 break;
319 case WM_KEYUP:
320 case WM_SYSKEYUP:
321 /* Press and release F10 or ALT */
322 if (((wParam == VK_MENU) && iMenuSysKey) ||
323 ((wParam == VK_F10) && iF10Key))
324 SendMessage16( WIN_GetTopParent(wndPtr->hwndSelf),
325 WM_SYSCOMMAND, SC_KEYMENU, 0L );
326 iMenuSysKey = iF10Key = 0;
327 break;
329 case WM_SYSCHAR:
330 iMenuSysKey = 0;
331 if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
333 PostMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
334 (WPARAM16)SC_RESTORE, 0L );
335 break;
337 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
339 if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
340 if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
341 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
342 else
343 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
344 (WPARAM16)SC_KEYMENU, (LPARAM)(DWORD)wParam );
346 else /* check for Ctrl-Esc */
347 if (wParam != VK_ESCAPE) MessageBeep(0);
348 break;
350 case WM_SHOWWINDOW:
351 if (!lParam) return 0; /* sent from ShowWindow */
352 if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
353 if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
354 else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
355 ShowWindow( wndPtr->hwndSelf, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
356 break;
358 case WM_CANCELMODE:
359 if (wndPtr->parent == WIN_GetDesktop()) EndMenu();
360 if (GetCapture() == wndPtr->hwndSelf) ReleaseCapture();
361 break;
363 case WM_VKEYTOITEM:
364 case WM_CHARTOITEM:
365 return -1;
367 case WM_DROPOBJECT:
368 return DRAG_FILE;
370 case WM_QUERYDROPOBJECT:
371 if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
372 break;
374 case WM_QUERYDRAGICON:
376 HICON16 hIcon=0;
377 UINT16 len;
379 if( (hIcon=wndPtr->class->hCursor) ) return (LRESULT)hIcon;
380 for(len=1; len<64; len++)
381 if((hIcon=LoadIcon16(wndPtr->hInstance,MAKEINTRESOURCE16(len))))
382 return (LRESULT)hIcon;
383 return (LRESULT)LoadIcon16(0,IDI_APPLICATION16);
385 break;
387 case WM_ISACTIVEICON:
388 return ((wndPtr->flags & WIN_NCACTIVATED) != 0);
390 case WM_QUERYOPEN:
391 case WM_QUERYENDSESSION:
392 return 1;
394 return 0;
399 /***********************************************************************
400 * DefWindowProc16 (USER.107)
402 LRESULT WINAPI DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
403 LPARAM lParam )
405 WND * wndPtr = WIN_FindWndPtr( hwnd );
406 LRESULT result = 0;
408 if (!wndPtr) return 0;
409 SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
411 switch(msg)
413 case WM_NCCREATE:
415 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
416 if (cs->lpszName)
417 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(cs->lpszName) );
418 result = 1;
420 break;
422 case WM_NCCALCSIZE:
424 RECT rect32;
425 CONV_RECT16TO32( (RECT16 *)PTR_SEG_TO_LIN(lParam), &rect32 );
426 result = NC_HandleNCCalcSize( wndPtr, &rect32 );
427 CONV_RECT32TO16( &rect32, (RECT16 *)PTR_SEG_TO_LIN(lParam) );
429 break;
431 case WM_WINDOWPOSCHANGING:
432 result = WINPOS_HandleWindowPosChanging16( wndPtr,
433 (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam) );
434 break;
436 case WM_WINDOWPOSCHANGED:
438 WINDOWPOS16 * winPos = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
439 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
441 break;
443 case WM_GETTEXT:
444 if (wParam && wndPtr->text)
446 lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(lParam), wndPtr->text, wParam );
447 result = (LRESULT)strlen( (LPSTR)PTR_SEG_TO_LIN(lParam) );
449 break;
451 case WM_SETTEXT:
452 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(lParam) );
453 if( wndPtr->dwStyle & WS_CAPTION ) NC_HandleNCPaint( hwnd , (HRGN)1 );
454 break;
456 default:
457 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
458 break;
461 SPY_ExitMessage( SPY_RESULT_DEFWND16, hwnd, msg, result );
462 return result;
466 /***********************************************************************
467 * DefWindowProc32A [USER32.126]
470 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam,
471 LPARAM lParam )
473 WND * wndPtr = WIN_FindWndPtr( hwnd );
474 LRESULT result = 0;
476 if (!wndPtr) return 0;
477 SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
479 switch(msg)
481 case WM_NCCREATE:
483 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
484 if (cs->lpszName) DEFWND_SetText( wndPtr, cs->lpszName );
485 result = 1;
487 break;
489 case WM_NCCALCSIZE:
490 result = NC_HandleNCCalcSize( wndPtr, (RECT *)lParam );
491 break;
493 case WM_WINDOWPOSCHANGING:
494 result = WINPOS_HandleWindowPosChanging( wndPtr,
495 (WINDOWPOS *)lParam );
496 break;
498 case WM_WINDOWPOSCHANGED:
500 WINDOWPOS * winPos = (WINDOWPOS *)lParam;
501 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
503 break;
505 case WM_GETTEXT:
506 if (wParam && wndPtr->text)
508 lstrcpynA( (LPSTR)lParam, wndPtr->text, wParam );
509 result = (LRESULT)strlen( (LPSTR)lParam );
511 break;
513 case WM_SETTEXT:
514 DEFWND_SetText( wndPtr, (LPSTR)lParam );
515 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
516 break;
518 default:
519 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
520 break;
523 SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result );
524 return result;
528 /***********************************************************************
529 * DefWindowProc32W [USER32.127] Calls default window message handler
531 * Calls default window procedure for messages not processed
532 * by application.
534 * RETURNS
535 * Return value is dependent upon the message.
537 LRESULT WINAPI DefWindowProcW(
538 HWND hwnd, /* [in] window procedure recieving message */
539 UINT msg, /* [in] message identifier */
540 WPARAM wParam, /* [in] first message parameter */
541 LPARAM lParam ) /* [in] second message parameter */
543 LRESULT result;
545 switch(msg)
547 case WM_NCCREATE:
549 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
550 if (cs->lpszName)
552 WND *wndPtr = WIN_FindWndPtr( hwnd );
553 LPSTR str = HEAP_strdupWtoA(GetProcessHeap(), 0, cs->lpszName);
554 DEFWND_SetText( wndPtr, str );
555 HeapFree( GetProcessHeap(), 0, str );
557 result = 1;
559 break;
561 case WM_GETTEXT:
563 LPSTR str = HeapAlloc( GetProcessHeap(), 0, wParam );
564 result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)str );
565 lstrcpynAtoW( (LPWSTR)lParam, str, wParam );
566 HeapFree( GetProcessHeap(), 0, str );
568 break;
570 case WM_SETTEXT:
572 LPSTR str = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPWSTR)lParam );
573 result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)str );
574 HeapFree( GetProcessHeap(), 0, str );
576 break;
578 default:
579 result = DefWindowProcA( hwnd, msg, wParam, lParam );
580 break;
582 return result;