Check that ci->hWindowMenu is not zero before using it.
[wine.git] / windows / defwnd.c
blob97d535183c9b162cf3e02370bd0bdfbad57fab4f
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 "windef.h"
21 #include "wingdi.h"
22 #include "wine/winuser16.h"
24 DEFAULT_DEBUG_CHANNEL(win)
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 if (TWEAK_WineLook == WIN31_LOOK) {
81 SetTextColor( hDC, RGB(0, 0, 0) );
82 SetBkColor( hDC, RGB(255, 255, 255) );
83 } else {
84 COLORREF bk = GetSysColor(COLOR_3DHILIGHT);
85 SetTextColor( hDC, GetSysColor(COLOR_3DFACE));
86 SetBkColor( hDC, bk);
88 /* if COLOR_WINDOW happens to be the same as COLOR_3DHILIGHT
89 * we better use 0x55aa bitmap brush to make scrollbar's background
90 * look different from the window background.
92 if (bk == GetSysColor(COLOR_WINDOW)) {
93 return CACHE_GetPattern55AABrush();
96 UnrealizeObject( hb );
97 return hb;
100 SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT));
102 if (TWEAK_WineLook > WIN31_LOOK) {
103 if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
104 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
105 else {
106 SetBkColor( hDC, GetSysColor(COLOR_3DFACE) );
107 return GetSysColorBrush(COLOR_3DFACE);
110 else
111 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
112 return GetSysColorBrush(COLOR_WINDOW);
116 /***********************************************************************
117 * DEFWND_SetRedraw
119 static void DEFWND_SetRedraw( WND* wndPtr, WPARAM wParam )
121 BOOL bVisible = wndPtr->dwStyle & WS_VISIBLE;
123 TRACE("%04x %i\n", wndPtr->hwndSelf, (wParam!=0) );
125 if( wParam )
127 if( !bVisible )
129 wndPtr->dwStyle |= WS_VISIBLE;
130 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
133 else if( bVisible )
135 if( wndPtr->dwStyle & WS_MINIMIZE ) wParam = RDW_VALIDATE;
136 else wParam = RDW_ALLCHILDREN | RDW_VALIDATE;
138 PAINT_RedrawWindow( wndPtr->hwndSelf, NULL, 0, wParam, 0 );
139 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
140 wndPtr->dwStyle &= ~WS_VISIBLE;
144 /***********************************************************************
145 * DEFWND_Print
147 * This method handles the default behavior for the WM_PRINT message.
149 static void DEFWND_Print(
150 WND* wndPtr,
151 HDC hdc,
152 ULONG uFlags)
155 * Visibility flag.
157 if ( (uFlags & PRF_CHECKVISIBLE) &&
158 !IsWindowVisible(wndPtr->hwndSelf) )
159 return;
162 * Unimplemented flags.
164 if ( (uFlags & PRF_CHILDREN) ||
165 (uFlags & PRF_OWNED) ||
166 (uFlags & PRF_NONCLIENT) )
168 WARN("WM_PRINT message with unsupported flags\n");
172 * Background
174 if ( uFlags & PRF_ERASEBKGND)
175 SendMessageA(wndPtr->hwndSelf, WM_ERASEBKGND, (WPARAM)hdc, 0);
178 * Client area
180 if ( uFlags & PRF_CLIENT)
181 SendMessageA(wndPtr->hwndSelf, WM_PRINTCLIENT, (WPARAM)hdc, PRF_CLIENT);
184 /***********************************************************************
185 * DEFWND_DefWinProc
187 * Default window procedure for messages that are the same in Win16 and Win32.
189 static LRESULT DEFWND_DefWinProc( WND *wndPtr, UINT msg, WPARAM wParam,
190 LPARAM lParam )
192 switch(msg)
194 case WM_NCPAINT:
195 return NC_HandleNCPaint( wndPtr->hwndSelf, (HRGN)wParam );
197 case WM_NCHITTEST:
198 return NC_HandleNCHitTest( wndPtr->hwndSelf, MAKEPOINT16(lParam) );
200 case WM_NCLBUTTONDOWN:
201 return NC_HandleNCLButtonDown( wndPtr, wParam, lParam );
203 case WM_LBUTTONDBLCLK:
204 case WM_NCLBUTTONDBLCLK:
205 return NC_HandleNCLButtonDblClk( wndPtr, wParam, lParam );
207 case WM_RBUTTONUP:
208 case WM_NCRBUTTONUP:
209 if ((wndPtr->flags & WIN_ISWIN32) || (TWEAK_WineLook > WIN31_LOOK))
211 ClientToScreen16(wndPtr->hwndSelf, (LPPOINT16)&lParam);
212 SendMessageA( wndPtr->hwndSelf, WM_CONTEXTMENU,
213 wndPtr->hwndSelf, lParam);
215 break;
217 case WM_CONTEXTMENU:
218 if( wndPtr->dwStyle & WS_CHILD )
219 SendMessageA( wndPtr->parent->hwndSelf, msg, wParam, lParam );
220 else
221 if (wndPtr->hSysMenu)
222 { /*
223 TrackPopupMenu32(wndPtr->hSysMenu,TPM_LEFTALIGN | TPM_RETURNCMD,LOWORD(lParam),HIWORD(lParam),0,wndPtr->hwndSelf,NULL);
224 DestroyMenu32(wndPtr->hSysMenu);
226 FIXME("Display default popup menu\n");
227 /* Track system popup if click was in the caption area. */
229 break;
231 case WM_NCACTIVATE:
232 return NC_HandleNCActivate( wndPtr, wParam );
234 case WM_NCDESTROY:
235 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
236 wndPtr->text = NULL;
237 if (wndPtr->pVScroll) HeapFree( SystemHeap, 0, wndPtr->pVScroll );
238 if (wndPtr->pHScroll) HeapFree( SystemHeap, 0, wndPtr->pHScroll );
239 wndPtr->pVScroll = wndPtr->pHScroll = NULL;
240 return 0;
242 case WM_PRINT:
243 DEFWND_Print(wndPtr, (HDC)wParam, lParam);
244 return 0;
246 case WM_PAINTICON:
247 case WM_PAINT:
249 PAINTSTRUCT16 ps;
250 HDC16 hdc = BeginPaint16( wndPtr->hwndSelf, &ps );
251 if( hdc )
253 if( (wndPtr->dwStyle & WS_MINIMIZE) && wndPtr->class->hIcon )
255 int x = (wndPtr->rectWindow.right - wndPtr->rectWindow.left -
256 GetSystemMetrics(SM_CXICON))/2;
257 int y = (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top -
258 GetSystemMetrics(SM_CYICON))/2;
259 TRACE("Painting class icon: vis rect=(%i,%i - %i,%i)\n",
260 ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
261 DrawIcon( hdc, x, y, wndPtr->class->hIcon );
263 EndPaint16( wndPtr->hwndSelf, &ps );
265 return 0;
268 case WM_SETREDRAW:
269 DEFWND_SetRedraw( wndPtr, wParam );
270 return 0;
272 case WM_CLOSE:
273 DestroyWindow( wndPtr->hwndSelf );
274 return 0;
276 case WM_MOUSEACTIVATE:
277 if (wndPtr->dwStyle & WS_CHILD)
279 LONG ret = SendMessage16( wndPtr->parent->hwndSelf,
280 WM_MOUSEACTIVATE, wParam, lParam );
281 if (ret) return ret;
284 /* Caption clicks are handled by the NC_HandleNCLButtonDown() */
285 return (LOWORD(lParam) >= HTCLIENT) ? MA_ACTIVATE : MA_NOACTIVATE;
287 case WM_ACTIVATE:
288 /* The default action in Windows is to set the keyboard focus to
289 * the window, if it's being activated and not minimized */
290 if (LOWORD(wParam) != WA_INACTIVE) {
291 /* I don't know who put this SetWindowPos here, it does not
292 * seem very logical to have it here... (FIXME?) */
293 SetWindowPos(wndPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
294 SWP_NOMOVE | SWP_NOSIZE);
295 if (!(wndPtr->dwStyle & WS_MINIMIZE))
296 SetFocus(wndPtr->hwndSelf);
298 break;
300 case WM_ERASEBKGND:
301 case WM_ICONERASEBKGND:
303 RECT rect;
305 if (!wndPtr->class->hbrBackground) return 0;
307 /* Since WM_ERASEBKGND may receive either a window dc or a */
308 /* client dc, the area to be erased has to be retrieved from */
309 /* the device context. */
310 GetClipBox( (HDC)wParam, &rect );
312 /* Always call the Win32 variant of FillRect even on Win16,
313 * since despite the fact that Win16, as well as Win32,
314 * supports special background brushes for a window class,
315 * the Win16 variant of FillRect does not.
317 FillRect( (HDC) wParam, &rect, wndPtr->class->hbrBackground);
318 return 1;
321 case WM_GETDLGCODE:
322 return 0;
324 case WM_CTLCOLORMSGBOX:
325 case WM_CTLCOLOREDIT:
326 case WM_CTLCOLORLISTBOX:
327 case WM_CTLCOLORBTN:
328 case WM_CTLCOLORDLG:
329 case WM_CTLCOLORSTATIC:
330 case WM_CTLCOLORSCROLLBAR:
331 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
333 case WM_CTLCOLOR:
334 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
336 case WM_GETTEXTLENGTH:
337 if (wndPtr->text) return (LRESULT)strlen(wndPtr->text);
338 return 0;
340 case WM_SETCURSOR:
341 if (wndPtr->dwStyle & WS_CHILD)
342 if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
343 wParam, lParam))
344 return TRUE;
345 return NC_HandleSetCursor( wndPtr->hwndSelf, wParam, lParam );
347 case WM_SYSCOMMAND:
348 return NC_HandleSysCommand( wndPtr->hwndSelf, wParam,
349 MAKEPOINT16(lParam) );
351 case WM_KEYDOWN:
352 if(wParam == VK_F10) iF10Key = VK_F10;
353 break;
355 case WM_SYSKEYDOWN:
356 if( HIWORD(lParam) & KEYDATA_ALT )
358 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
359 if( wParam == VK_MENU && !iMenuSysKey )
360 iMenuSysKey = 1;
361 else
362 iMenuSysKey = 0;
364 iF10Key = 0;
366 if( wParam == VK_F4 ) /* try to close the window */
368 HWND hWnd = WIN_GetTopParent( wndPtr->hwndSelf );
369 wndPtr = WIN_FindWndPtr( hWnd );
370 if( wndPtr && !(wndPtr->class->style & CS_NOCLOSE) )
371 PostMessage16( hWnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
372 WIN_ReleaseWndPtr(wndPtr);
375 else if( wParam == VK_F10 )
376 iF10Key = 1;
377 else
378 if( wParam == VK_ESCAPE && (GetKeyState(VK_SHIFT) & 0x8000))
379 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
380 (WPARAM16)SC_KEYMENU, (LPARAM)VK_SPACE);
381 break;
383 case WM_KEYUP:
384 case WM_SYSKEYUP:
385 /* Press and release F10 or ALT */
386 if (((wParam == VK_MENU) && iMenuSysKey) ||
387 ((wParam == VK_F10) && iF10Key))
388 SendMessage16( WIN_GetTopParent(wndPtr->hwndSelf),
389 WM_SYSCOMMAND, SC_KEYMENU, 0L );
390 iMenuSysKey = iF10Key = 0;
391 break;
393 case WM_SYSCHAR:
394 iMenuSysKey = 0;
395 if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
397 PostMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
398 (WPARAM16)SC_RESTORE, 0L );
399 break;
401 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
403 if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
404 if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
405 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
406 else
407 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
408 (WPARAM16)SC_KEYMENU, (LPARAM)(DWORD)wParam );
410 else /* check for Ctrl-Esc */
411 if (wParam != VK_ESCAPE) MessageBeep(0);
412 break;
414 case WM_SHOWWINDOW:
415 if (!lParam) return 0; /* sent from ShowWindow */
416 if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
417 if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
418 else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
419 ShowWindow( wndPtr->hwndSelf, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
420 break;
422 case WM_CANCELMODE:
423 if (wndPtr->parent == WIN_GetDesktop()) EndMenu();
424 if (GetCapture() == wndPtr->hwndSelf) ReleaseCapture();
425 WIN_ReleaseDesktop();
426 break;
428 case WM_VKEYTOITEM:
429 case WM_CHARTOITEM:
430 return -1;
432 case WM_DROPOBJECT:
433 return DRAG_FILE;
435 case WM_QUERYDROPOBJECT:
436 if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
437 break;
439 case WM_QUERYDRAGICON:
441 HICON16 hIcon=0;
442 UINT16 len;
444 if( (hIcon=wndPtr->class->hCursor) ) return (LRESULT)hIcon;
445 for(len=1; len<64; len++)
446 if((hIcon=LoadIcon16(wndPtr->hInstance,MAKEINTRESOURCE16(len))))
447 return (LRESULT)hIcon;
448 return (LRESULT)LoadIcon16(0,IDI_APPLICATION16);
450 break;
452 case WM_ISACTIVEICON:
453 return ((wndPtr->flags & WIN_NCACTIVATED) != 0);
455 case WM_NOTIFYFORMAT:
456 if (IsWindowUnicode(wndPtr->hwndSelf)) return NFR_UNICODE;
457 else return NFR_ANSI;
459 case WM_QUERYOPEN:
460 case WM_QUERYENDSESSION:
461 return 1;
463 case WM_SETICON:
464 case WM_GETICON:
466 LRESULT result = 0;
467 int index = GCL_HICON;
469 if (wParam == ICON_SMALL)
470 index = GCL_HICONSM;
472 result = GetClassLongA(wndPtr->hwndSelf, index);
474 if (msg == WM_SETICON)
475 SetClassLongA(wndPtr->hwndSelf, index, lParam);
477 return result;
481 return 0;
486 /***********************************************************************
487 * DefWindowProc16 (USER.107)
489 LRESULT WINAPI DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
490 LPARAM lParam )
492 WND * wndPtr = WIN_FindWndPtr( hwnd );
493 LRESULT result = 0;
495 if (!wndPtr) return 0;
496 SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
498 switch(msg)
500 case WM_NCCREATE:
502 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
503 if (cs->lpszName)
504 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(cs->lpszName) );
505 result = 1;
507 break;
509 case WM_NCCALCSIZE:
511 RECT rect32;
512 CONV_RECT16TO32( (RECT16 *)PTR_SEG_TO_LIN(lParam), &rect32 );
513 result = NC_HandleNCCalcSize( wndPtr, &rect32 );
514 CONV_RECT32TO16( &rect32, (RECT16 *)PTR_SEG_TO_LIN(lParam) );
516 break;
518 case WM_WINDOWPOSCHANGING:
519 result = WINPOS_HandleWindowPosChanging16( wndPtr,
520 (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam) );
521 break;
523 case WM_WINDOWPOSCHANGED:
525 WINDOWPOS16 * winPos = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
526 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
528 break;
530 case WM_GETTEXT:
531 if (wParam && wndPtr->text)
533 lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(lParam), wndPtr->text, wParam );
534 result = (LRESULT)strlen( (LPSTR)PTR_SEG_TO_LIN(lParam) );
536 break;
538 case WM_SETTEXT:
539 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(lParam) );
540 if( wndPtr->dwStyle & WS_CAPTION ) NC_HandleNCPaint( hwnd , (HRGN)1 );
541 break;
543 default:
544 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
545 break;
548 WIN_ReleaseWndPtr(wndPtr);
549 SPY_ExitMessage( SPY_RESULT_DEFWND16, hwnd, msg, result );
550 return result;
554 /***********************************************************************
555 * DefWindowProc32A [USER32.126]
558 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam,
559 LPARAM lParam )
561 WND * wndPtr = WIN_FindWndPtr( hwnd );
562 LRESULT result = 0;
564 if (!wndPtr) return 0;
565 SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
567 switch(msg)
569 case WM_NCCREATE:
571 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
572 if (cs->lpszName) DEFWND_SetText( wndPtr, cs->lpszName );
573 result = 1;
575 break;
577 case WM_NCCALCSIZE:
578 result = NC_HandleNCCalcSize( wndPtr, (RECT *)lParam );
579 break;
581 case WM_WINDOWPOSCHANGING:
582 result = WINPOS_HandleWindowPosChanging( wndPtr,
583 (WINDOWPOS *)lParam );
584 break;
586 case WM_WINDOWPOSCHANGED:
588 WINDOWPOS * winPos = (WINDOWPOS *)lParam;
589 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
591 break;
593 case WM_GETTEXT:
594 if (wParam && wndPtr->text)
596 lstrcpynA( (LPSTR)lParam, wndPtr->text, wParam );
597 result = (LRESULT)strlen( (LPSTR)lParam );
599 break;
601 case WM_SETTEXT:
602 DEFWND_SetText( wndPtr, (LPSTR)lParam );
603 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
604 break;
606 default:
607 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
608 break;
611 WIN_ReleaseWndPtr(wndPtr);
612 SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result );
613 return result;
617 /***********************************************************************
618 * DefWindowProc32W [USER32.127] Calls default window message handler
620 * Calls default window procedure for messages not processed
621 * by application.
623 * RETURNS
624 * Return value is dependent upon the message.
626 LRESULT WINAPI DefWindowProcW(
627 HWND hwnd, /* [in] window procedure recieving message */
628 UINT msg, /* [in] message identifier */
629 WPARAM wParam, /* [in] first message parameter */
630 LPARAM lParam ) /* [in] second message parameter */
632 LRESULT result;
634 switch(msg)
636 case WM_NCCREATE:
638 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
639 if (cs->lpszName)
641 WND *wndPtr = WIN_FindWndPtr( hwnd );
642 LPSTR str = HEAP_strdupWtoA(GetProcessHeap(), 0, cs->lpszName);
643 DEFWND_SetText( wndPtr, str );
644 HeapFree( GetProcessHeap(), 0, str );
645 WIN_ReleaseWndPtr(wndPtr);
647 result = 1;
649 break;
651 case WM_GETTEXT:
653 LPSTR str = HeapAlloc( GetProcessHeap(), 0, wParam );
654 result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)str );
655 lstrcpynAtoW( (LPWSTR)lParam, str, wParam );
656 HeapFree( GetProcessHeap(), 0, str );
658 break;
660 case WM_SETTEXT:
662 LPSTR str = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPWSTR)lParam );
663 result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)str );
664 HeapFree( GetProcessHeap(), 0, str );
666 break;
668 default:
669 result = DefWindowProcA( hwnd, msg, wParam, lParam );
670 break;
672 return result;