Implemented SHCreateShellFolderViewEx.
[wine/hacks.git] / windows / defwnd.c
blob662856cce08851986fe43ee9b5bc23d1a6d0c797
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 "debugtools.h"
17 #include "spy.h"
18 #include "tweak.h"
19 #include "cache.h"
20 #include "wine/winuser16.h"
22 DEFAULT_DEBUG_CHANNEL(win)
24 /* Last COLOR id */
25 #define COLOR_MAX COLOR_BTNHIGHLIGHT
27 /* bits in the dwKeyData */
28 #define KEYDATA_ALT 0x2000
29 #define KEYDATA_PREVSTATE 0x4000
31 static short iF10Key = 0;
32 static short iMenuSysKey = 0;
34 /***********************************************************************
35 * DEFWND_HandleWindowPosChanged
37 * Handle the WM_WINDOWPOSCHANGED message.
39 static void DEFWND_HandleWindowPosChanged( WND *wndPtr, UINT flags )
41 WPARAM16 wp = SIZE_RESTORED;
43 if (!(flags & SWP_NOCLIENTMOVE))
44 SendMessage16( wndPtr->hwndSelf, WM_MOVE, 0,
45 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top));
46 if (!(flags & SWP_NOCLIENTSIZE))
48 if (wndPtr->dwStyle & WS_MAXIMIZE) wp = SIZE_MAXIMIZED;
49 else if (wndPtr->dwStyle & WS_MINIMIZE) wp = SIZE_MINIMIZED;
51 SendMessage16( wndPtr->hwndSelf, WM_SIZE, wp,
52 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
53 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
58 /***********************************************************************
59 * DEFWND_SetText
61 * Set the window text.
63 void DEFWND_SetText( WND *wndPtr, LPCSTR text )
65 if (!text) text = "";
66 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
67 wndPtr->text = HEAP_strdupA( SystemHeap, 0, text );
68 wndPtr->pDriver->pSetText(wndPtr, wndPtr->text);
71 /***********************************************************************
72 * DEFWND_ControlColor
74 * Default colors for control painting.
76 HBRUSH DEFWND_ControlColor( HDC hDC, UINT16 ctlType )
78 if( ctlType == CTLCOLOR_SCROLLBAR)
80 HBRUSH hb = GetSysColorBrush(COLOR_SCROLLBAR);
81 if (TWEAK_WineLook == WIN31_LOOK) {
82 SetTextColor( hDC, RGB(0, 0, 0) );
83 SetBkColor( hDC, RGB(255, 255, 255) );
84 } else {
85 COLORREF bk = GetSysColor(COLOR_3DHILIGHT);
86 SetTextColor( hDC, GetSysColor(COLOR_3DFACE));
87 SetBkColor( hDC, bk);
89 /* if COLOR_WINDOW happens to be the same as COLOR_3DHILIGHT
90 * we better use 0x55aa bitmap brush to make scrollbar's background
91 * look different from the window background.
93 if (bk == GetSysColor(COLOR_WINDOW)) {
94 return CACHE_GetPattern55AABrush();
97 UnrealizeObject( hb );
98 return hb;
101 SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT));
103 if (TWEAK_WineLook > WIN31_LOOK) {
104 if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
105 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
106 else {
107 SetBkColor( hDC, GetSysColor(COLOR_3DFACE) );
108 return GetSysColorBrush(COLOR_3DFACE);
111 else
112 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
113 return GetSysColorBrush(COLOR_WINDOW);
117 /***********************************************************************
118 * DEFWND_SetRedraw
120 static void DEFWND_SetRedraw( WND* wndPtr, WPARAM wParam )
122 BOOL bVisible = wndPtr->dwStyle & WS_VISIBLE;
124 TRACE("%04x %i\n", wndPtr->hwndSelf, (wParam!=0) );
126 if( wParam )
128 if( !bVisible )
130 wndPtr->dwStyle |= WS_VISIBLE;
131 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
134 else if( bVisible )
136 if( wndPtr->dwStyle & WS_MINIMIZE ) wParam = RDW_VALIDATE;
137 else wParam = RDW_ALLCHILDREN | RDW_VALIDATE;
139 PAINT_RedrawWindow( wndPtr->hwndSelf, NULL, 0, wParam, 0 );
140 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
141 wndPtr->dwStyle &= ~WS_VISIBLE;
145 /***********************************************************************
146 * DEFWND_Print
148 * This method handles the default behavior for the WM_PRINT message.
150 static void DEFWND_Print(
151 WND* wndPtr,
152 HDC hdc,
153 ULONG uFlags)
156 * Visibility flag.
158 if ( (uFlags & PRF_CHECKVISIBLE) &&
159 !IsWindowVisible(wndPtr->hwndSelf) )
160 return;
163 * Unimplemented flags.
165 if ( (uFlags & PRF_CHILDREN) ||
166 (uFlags & PRF_OWNED) ||
167 (uFlags & PRF_NONCLIENT) )
169 WARN("WM_PRINT message with unsupported flags\n");
173 * Background
175 if ( uFlags & PRF_ERASEBKGND)
176 SendMessageA(wndPtr->hwndSelf, WM_ERASEBKGND, (WPARAM)hdc, 0);
179 * Client area
181 if ( uFlags & PRF_CLIENT)
182 SendMessageA(wndPtr->hwndSelf, WM_PRINTCLIENT, (WPARAM)hdc, PRF_CLIENT);
185 /***********************************************************************
186 * DEFWND_DefWinProc
188 * Default window procedure for messages that are the same in Win16 and Win32.
190 static LRESULT DEFWND_DefWinProc( WND *wndPtr, UINT msg, WPARAM wParam,
191 LPARAM lParam )
193 switch(msg)
195 case WM_NCPAINT:
196 return NC_HandleNCPaint( wndPtr->hwndSelf, (HRGN)wParam );
198 case WM_NCHITTEST:
199 return NC_HandleNCHitTest( wndPtr->hwndSelf, MAKEPOINT16(lParam) );
201 case WM_NCLBUTTONDOWN:
202 return NC_HandleNCLButtonDown( wndPtr, wParam, lParam );
204 case WM_LBUTTONDBLCLK:
205 case WM_NCLBUTTONDBLCLK:
206 return NC_HandleNCLButtonDblClk( wndPtr, wParam, lParam );
208 case WM_RBUTTONUP:
209 case WM_NCRBUTTONUP:
210 if ((wndPtr->flags & WIN_ISWIN32) || (TWEAK_WineLook > WIN31_LOOK))
212 ClientToScreen16(wndPtr->hwndSelf, (LPPOINT16)&lParam);
213 SendMessageA( wndPtr->hwndSelf, WM_CONTEXTMENU,
214 wndPtr->hwndSelf, lParam);
216 break;
218 case WM_CONTEXTMENU:
219 if( wndPtr->dwStyle & WS_CHILD )
220 SendMessageA( wndPtr->parent->hwndSelf, msg, wParam, lParam );
221 else
222 if (wndPtr->hSysMenu)
223 { /*
224 TrackPopupMenu32(wndPtr->hSysMenu,TPM_LEFTALIGN | TPM_RETURNCMD,LOWORD(lParam),HIWORD(lParam),0,wndPtr->hwndSelf,NULL);
225 DestroyMenu32(wndPtr->hSysMenu);
227 FIXME("Display default popup menu\n");
228 /* Track system popup if click was in the caption area. */
230 break;
232 case WM_NCACTIVATE:
233 return NC_HandleNCActivate( wndPtr, wParam );
235 case WM_NCDESTROY:
236 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
237 wndPtr->text = NULL;
238 if (wndPtr->pVScroll) HeapFree( SystemHeap, 0, wndPtr->pVScroll );
239 if (wndPtr->pHScroll) HeapFree( SystemHeap, 0, wndPtr->pHScroll );
240 wndPtr->pVScroll = wndPtr->pHScroll = NULL;
241 return 0;
243 case WM_PRINT:
244 DEFWND_Print(wndPtr, (HDC)wParam, lParam);
245 return 0;
247 case WM_PAINTICON:
248 case WM_PAINT:
250 PAINTSTRUCT16 ps;
251 HDC16 hdc = BeginPaint16( wndPtr->hwndSelf, &ps );
252 if( hdc )
254 if( (wndPtr->dwStyle & WS_MINIMIZE) && wndPtr->class->hIcon )
256 int x = (wndPtr->rectWindow.right - wndPtr->rectWindow.left -
257 GetSystemMetrics(SM_CXICON))/2;
258 int y = (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top -
259 GetSystemMetrics(SM_CYICON))/2;
260 TRACE("Painting class icon: vis rect=(%i,%i - %i,%i)\n",
261 ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
262 DrawIcon( hdc, x, y, wndPtr->class->hIcon );
264 EndPaint16( wndPtr->hwndSelf, &ps );
266 return 0;
269 case WM_SETREDRAW:
270 DEFWND_SetRedraw( wndPtr, wParam );
271 return 0;
273 case WM_CLOSE:
274 DestroyWindow( wndPtr->hwndSelf );
275 return 0;
277 case WM_MOUSEACTIVATE:
278 if (wndPtr->dwStyle & WS_CHILD)
280 LONG ret = SendMessage16( wndPtr->parent->hwndSelf,
281 WM_MOUSEACTIVATE, wParam, lParam );
282 if (ret) return ret;
285 /* Caption clicks are handled by the NC_HandleNCLButtonDown() */
286 return (LOWORD(lParam) == HTCAPTION) ? MA_NOACTIVATE : MA_ACTIVATE;
288 case WM_ACTIVATE:
289 /* The default action in Windows is to set the keyboard focus to
290 * the window, if it's being activated and not minimized */
291 if (LOWORD(wParam) != WA_INACTIVE) {
292 /* I don't know who put this SetWindowPos here, it does not
293 * seem very logical to have it here... (FIXME?) */
294 SetWindowPos(wndPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
295 SWP_NOMOVE | SWP_NOSIZE);
296 if (!(wndPtr->dwStyle & WS_MINIMIZE))
297 SetFocus(wndPtr->hwndSelf);
299 break;
301 case WM_ERASEBKGND:
302 case WM_ICONERASEBKGND:
304 RECT16 rect;
306 if (!wndPtr->class->hbrBackground) return 0;
308 /* Since WM_ERASEBKGND may receive either a window dc or a */
309 /* client dc, the area to be erased has to be retrieved from */
310 /* the device context. */
311 GetClipBox16( (HDC16)wParam, &rect );
313 if (wndPtr->class->hbrBackground <= (HBRUSH16)(COLOR_MAX+1))
315 HBRUSH hbrush = CreateSolidBrush(
316 GetSysColor(((DWORD)wndPtr->class->hbrBackground)-1));
317 PaintRect16( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
318 (HDC16)wParam, hbrush, &rect);
319 DeleteObject( hbrush );
321 else
323 PaintRect16( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
324 (HDC16)wParam, wndPtr->class->hbrBackground, &rect );
326 return 1;
329 case WM_GETDLGCODE:
330 return 0;
332 case WM_CTLCOLORMSGBOX:
333 case WM_CTLCOLOREDIT:
334 case WM_CTLCOLORLISTBOX:
335 case WM_CTLCOLORBTN:
336 case WM_CTLCOLORDLG:
337 case WM_CTLCOLORSTATIC:
338 case WM_CTLCOLORSCROLLBAR:
339 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
341 case WM_CTLCOLOR:
342 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
344 case WM_GETTEXTLENGTH:
345 if (wndPtr->text) return (LRESULT)strlen(wndPtr->text);
346 return 0;
348 case WM_SETCURSOR:
349 if (wndPtr->dwStyle & WS_CHILD)
350 if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
351 wParam, lParam))
352 return TRUE;
353 return NC_HandleSetCursor( wndPtr->hwndSelf, wParam, lParam );
355 case WM_SYSCOMMAND:
356 return NC_HandleSysCommand( wndPtr->hwndSelf, wParam,
357 MAKEPOINT16(lParam) );
359 case WM_KEYDOWN:
360 if(wParam == VK_F10) iF10Key = VK_F10;
361 break;
363 case WM_SYSKEYDOWN:
364 if( HIWORD(lParam) & KEYDATA_ALT )
366 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
367 if( wParam == VK_MENU && !iMenuSysKey )
368 iMenuSysKey = 1;
369 else
370 iMenuSysKey = 0;
372 iF10Key = 0;
374 if( wParam == VK_F4 ) /* try to close the window */
376 HWND hWnd = WIN_GetTopParent( wndPtr->hwndSelf );
377 wndPtr = WIN_FindWndPtr( hWnd );
378 if( wndPtr && !(wndPtr->class->style & CS_NOCLOSE) )
379 PostMessage16( hWnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
380 WIN_ReleaseWndPtr(wndPtr);
383 else if( wParam == VK_F10 )
384 iF10Key = 1;
385 else
386 if( wParam == VK_ESCAPE && (GetKeyState(VK_SHIFT) & 0x8000))
387 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
388 (WPARAM16)SC_KEYMENU, (LPARAM)VK_SPACE);
389 break;
391 case WM_KEYUP:
392 case WM_SYSKEYUP:
393 /* Press and release F10 or ALT */
394 if (((wParam == VK_MENU) && iMenuSysKey) ||
395 ((wParam == VK_F10) && iF10Key))
396 SendMessage16( WIN_GetTopParent(wndPtr->hwndSelf),
397 WM_SYSCOMMAND, SC_KEYMENU, 0L );
398 iMenuSysKey = iF10Key = 0;
399 break;
401 case WM_SYSCHAR:
402 iMenuSysKey = 0;
403 if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
405 PostMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
406 (WPARAM16)SC_RESTORE, 0L );
407 break;
409 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
411 if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
412 if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
413 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
414 else
415 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
416 (WPARAM16)SC_KEYMENU, (LPARAM)(DWORD)wParam );
418 else /* check for Ctrl-Esc */
419 if (wParam != VK_ESCAPE) MessageBeep(0);
420 break;
422 case WM_SHOWWINDOW:
423 if (!lParam) return 0; /* sent from ShowWindow */
424 if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
425 if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
426 else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
427 ShowWindow( wndPtr->hwndSelf, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
428 break;
430 case WM_CANCELMODE:
431 if (wndPtr->parent == WIN_GetDesktop()) EndMenu();
432 if (GetCapture() == wndPtr->hwndSelf) ReleaseCapture();
433 WIN_ReleaseDesktop();
434 break;
436 case WM_VKEYTOITEM:
437 case WM_CHARTOITEM:
438 return -1;
440 case WM_DROPOBJECT:
441 return DRAG_FILE;
443 case WM_QUERYDROPOBJECT:
444 if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
445 break;
447 case WM_QUERYDRAGICON:
449 HICON16 hIcon=0;
450 UINT16 len;
452 if( (hIcon=wndPtr->class->hCursor) ) return (LRESULT)hIcon;
453 for(len=1; len<64; len++)
454 if((hIcon=LoadIcon16(wndPtr->hInstance,MAKEINTRESOURCE16(len))))
455 return (LRESULT)hIcon;
456 return (LRESULT)LoadIcon16(0,IDI_APPLICATION16);
458 break;
460 case WM_ISACTIVEICON:
461 return ((wndPtr->flags & WIN_NCACTIVATED) != 0);
463 case WM_NOTIFYFORMAT:
464 if (IsWindowUnicode(wndPtr->hwndSelf)) return NFR_UNICODE;
465 else return NFR_ANSI;
467 case WM_QUERYOPEN:
468 case WM_QUERYENDSESSION:
469 return 1;
471 return 0;
476 /***********************************************************************
477 * DefWindowProc16 (USER.107)
479 LRESULT WINAPI DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
480 LPARAM lParam )
482 WND * wndPtr = WIN_FindWndPtr( hwnd );
483 LRESULT result = 0;
485 if (!wndPtr) return 0;
486 SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
488 switch(msg)
490 case WM_NCCREATE:
492 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
493 if (cs->lpszName)
494 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(cs->lpszName) );
495 result = 1;
497 break;
499 case WM_NCCALCSIZE:
501 RECT rect32;
502 CONV_RECT16TO32( (RECT16 *)PTR_SEG_TO_LIN(lParam), &rect32 );
503 result = NC_HandleNCCalcSize( wndPtr, &rect32 );
504 CONV_RECT32TO16( &rect32, (RECT16 *)PTR_SEG_TO_LIN(lParam) );
506 break;
508 case WM_WINDOWPOSCHANGING:
509 result = WINPOS_HandleWindowPosChanging16( wndPtr,
510 (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam) );
511 break;
513 case WM_WINDOWPOSCHANGED:
515 WINDOWPOS16 * winPos = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
516 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
518 break;
520 case WM_GETTEXT:
521 if (wParam && wndPtr->text)
523 lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(lParam), wndPtr->text, wParam );
524 result = (LRESULT)strlen( (LPSTR)PTR_SEG_TO_LIN(lParam) );
526 break;
528 case WM_SETTEXT:
529 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(lParam) );
530 if( wndPtr->dwStyle & WS_CAPTION ) NC_HandleNCPaint( hwnd , (HRGN)1 );
531 break;
533 default:
534 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
535 break;
538 WIN_ReleaseWndPtr(wndPtr);
539 SPY_ExitMessage( SPY_RESULT_DEFWND16, hwnd, msg, result );
540 return result;
544 /***********************************************************************
545 * DefWindowProc32A [USER32.126]
548 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam,
549 LPARAM lParam )
551 WND * wndPtr = WIN_FindWndPtr( hwnd );
552 LRESULT result = 0;
554 if (!wndPtr) return 0;
555 SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
557 switch(msg)
559 case WM_NCCREATE:
561 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
562 if (cs->lpszName) DEFWND_SetText( wndPtr, cs->lpszName );
563 result = 1;
565 break;
567 case WM_NCCALCSIZE:
568 result = NC_HandleNCCalcSize( wndPtr, (RECT *)lParam );
569 break;
571 case WM_WINDOWPOSCHANGING:
572 result = WINPOS_HandleWindowPosChanging( wndPtr,
573 (WINDOWPOS *)lParam );
574 break;
576 case WM_WINDOWPOSCHANGED:
578 WINDOWPOS * winPos = (WINDOWPOS *)lParam;
579 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
581 break;
583 case WM_GETTEXT:
584 if (wParam && wndPtr->text)
586 lstrcpynA( (LPSTR)lParam, wndPtr->text, wParam );
587 result = (LRESULT)strlen( (LPSTR)lParam );
589 break;
591 case WM_SETTEXT:
592 DEFWND_SetText( wndPtr, (LPSTR)lParam );
593 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
594 break;
596 default:
597 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
598 break;
601 WIN_ReleaseWndPtr(wndPtr);
602 SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result );
603 return result;
607 /***********************************************************************
608 * DefWindowProc32W [USER32.127] Calls default window message handler
610 * Calls default window procedure for messages not processed
611 * by application.
613 * RETURNS
614 * Return value is dependent upon the message.
616 LRESULT WINAPI DefWindowProcW(
617 HWND hwnd, /* [in] window procedure recieving message */
618 UINT msg, /* [in] message identifier */
619 WPARAM wParam, /* [in] first message parameter */
620 LPARAM lParam ) /* [in] second message parameter */
622 LRESULT result;
624 switch(msg)
626 case WM_NCCREATE:
628 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
629 if (cs->lpszName)
631 WND *wndPtr = WIN_FindWndPtr( hwnd );
632 LPSTR str = HEAP_strdupWtoA(GetProcessHeap(), 0, cs->lpszName);
633 DEFWND_SetText( wndPtr, str );
634 HeapFree( GetProcessHeap(), 0, str );
635 WIN_ReleaseWndPtr(wndPtr);
637 result = 1;
639 break;
641 case WM_GETTEXT:
643 LPSTR str = HeapAlloc( GetProcessHeap(), 0, wParam );
644 result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)str );
645 lstrcpynAtoW( (LPWSTR)lParam, str, wParam );
646 HeapFree( GetProcessHeap(), 0, str );
648 break;
650 case WM_SETTEXT:
652 LPSTR str = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPWSTR)lParam );
653 result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)str );
654 HeapFree( GetProcessHeap(), 0, str );
656 break;
658 default:
659 result = DefWindowProcA( hwnd, msg, wParam, lParam );
660 break;
662 return result;