Release 980503
[wine/hacks.git] / windows / defwnd.c
blob0411b64cbf41029d9b6af92a89c8a724c96b08f2
1 /*
2 * Default window procedure
4 * Copyright 1993, 1996 Alexandre Julliard
5 * 1995 Alex Korobka
6 */
8 #include <stdlib.h>
9 #include <stdio.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 "sysmetrics.h"
17 #include "debug.h"
18 #include "spy.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 if (wndPtr->window)
66 TSXStoreName( display, wndPtr->window, wndPtr->text );
67 TSXSetIconName( display, wndPtr->window, wndPtr->text );
71 /***********************************************************************
72 * DEFWND_ControlColor
74 * Default colors for control painting.
76 HBRUSH32 DEFWND_ControlColor( HDC32 hDC, UINT16 ctlType )
78 if( ctlType == CTLCOLOR_SCROLLBAR)
80 HBRUSH32 hb = GetSysColorBrush32(COLOR_SCROLLBAR);
81 SetBkColor32( hDC, RGB(255, 255, 255) );
82 SetTextColor32( hDC, RGB(0, 0, 0) );
83 UnrealizeObject32( hb );
84 return hb;
87 SetBkColor32( hDC, GetSysColor32(COLOR_WINDOW) );
88 SetTextColor32( hDC, GetSysColor32(COLOR_WINDOWTEXT));
89 return GetSysColorBrush32(COLOR_WINDOW);
93 /***********************************************************************
94 * DEFWND_SetRedraw
96 static void DEFWND_SetRedraw( WND* wndPtr, WPARAM32 wParam )
98 BOOL32 bVisible = wndPtr->dwStyle & WS_VISIBLE;
100 TRACE(win,"%04x %i\n", wndPtr->hwndSelf, (wParam!=0) );
102 if( wParam )
104 if( !bVisible )
106 wndPtr->dwStyle |= WS_VISIBLE;
107 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
110 else if( bVisible )
112 if( wndPtr->dwStyle & WS_MINIMIZE ) wParam = RDW_VALIDATE;
113 else wParam = RDW_ALLCHILDREN | RDW_VALIDATE;
115 PAINT_RedrawWindow( wndPtr->hwndSelf, NULL, 0, wParam, 0 );
116 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
117 wndPtr->dwStyle &= ~WS_VISIBLE;
121 /***********************************************************************
122 * DEFWND_DefWinProc
124 * Default window procedure for messages that are the same in Win16 and Win32.
126 static LRESULT DEFWND_DefWinProc( WND *wndPtr, UINT32 msg, WPARAM32 wParam,
127 LPARAM lParam )
129 switch(msg)
131 case WM_NCPAINT:
132 return NC_HandleNCPaint( wndPtr->hwndSelf, (HRGN32)wParam );
134 case WM_NCHITTEST:
135 return NC_HandleNCHitTest( wndPtr->hwndSelf, MAKEPOINT16(lParam) );
137 case WM_NCLBUTTONDOWN:
138 return NC_HandleNCLButtonDown( wndPtr, wParam, lParam );
140 case WM_LBUTTONDBLCLK:
141 case WM_NCLBUTTONDBLCLK:
142 return NC_HandleNCLButtonDblClk( wndPtr, wParam, lParam );
144 case WM_RBUTTONDOWN:
145 case WM_NCRBUTTONDOWN:
146 if( wndPtr->flags & WIN_ISWIN32 )
148 ClientToScreen16(wndPtr->hwndSelf, (LPPOINT16)&lParam);
149 SendMessage32A( wndPtr->hwndSelf, WM_CONTEXTMENU,
150 wndPtr->hwndSelf, lParam);
152 break;
154 case WM_CONTEXTMENU:
155 if( wndPtr->dwStyle & WS_CHILD )
156 SendMessage32A( wndPtr->parent->hwndSelf, msg, wParam, lParam );
158 /* else
159 * FIXME: Track system popup if click was in the caption area. */
161 break;
163 case WM_NCACTIVATE:
164 return NC_HandleNCActivate( wndPtr, wParam );
166 case WM_NCDESTROY:
167 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
168 wndPtr->text = NULL;
169 if (wndPtr->pVScroll) HeapFree( SystemHeap, 0, wndPtr->pVScroll );
170 if (wndPtr->pHScroll) HeapFree( SystemHeap, 0, wndPtr->pHScroll );
171 wndPtr->pVScroll = wndPtr->pHScroll = NULL;
172 return 0;
174 case WM_PAINTICON:
175 case WM_PAINT:
177 PAINTSTRUCT16 ps;
178 HDC16 hdc = BeginPaint16( wndPtr->hwndSelf, &ps );
179 if( hdc )
181 if( (wndPtr->dwStyle & WS_MINIMIZE) && wndPtr->class->hIcon )
183 int x = (wndPtr->rectWindow.right - wndPtr->rectWindow.left -
184 SYSMETRICS_CXICON)/2;
185 int y = (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top -
186 SYSMETRICS_CYICON)/2;
187 TRACE(win,"Painting class icon: vis rect=(%i,%i - %i,%i)\n",
188 ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
189 DrawIcon32( hdc, x, y, wndPtr->class->hIcon );
191 EndPaint16( wndPtr->hwndSelf, &ps );
193 return 0;
196 case WM_SETREDRAW:
197 DEFWND_SetRedraw( wndPtr, wParam );
198 return 0;
200 case WM_CLOSE:
201 DestroyWindow32( wndPtr->hwndSelf );
202 return 0;
204 case WM_MOUSEACTIVATE:
205 if (wndPtr->dwStyle & WS_CHILD)
207 LONG ret = SendMessage16( wndPtr->parent->hwndSelf,
208 WM_MOUSEACTIVATE, wParam, lParam );
209 if (ret) return ret;
212 /* Caption clicks are handled by the NC_HandleNCLButtonDown() */
213 return (LOWORD(lParam) == HTCAPTION) ? MA_NOACTIVATE : MA_ACTIVATE;
215 case WM_ACTIVATE:
216 if (LOWORD(wParam) != WA_INACTIVE) SetFocus32( wndPtr->hwndSelf );
217 break;
219 case WM_ERASEBKGND:
220 case WM_ICONERASEBKGND:
222 if (!wndPtr->class->hbrBackground) return 0;
224 if (wndPtr->class->hbrBackground <= (HBRUSH16)(COLOR_MAX+1))
226 HBRUSH32 hbrush = CreateSolidBrush32(
227 GetSysColor32(((DWORD)wndPtr->class->hbrBackground)-1));
228 FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
229 (HDC16)wParam, hbrush);
230 DeleteObject32( hbrush );
232 else FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
233 (HDC16)wParam, wndPtr->class->hbrBackground );
234 return 1;
237 case WM_GETDLGCODE:
238 return 0;
240 case WM_CTLCOLORMSGBOX:
241 case WM_CTLCOLOREDIT:
242 case WM_CTLCOLORLISTBOX:
243 case WM_CTLCOLORBTN:
244 case WM_CTLCOLORDLG:
245 case WM_CTLCOLORSTATIC:
246 case WM_CTLCOLORSCROLLBAR:
247 return (LRESULT)DEFWND_ControlColor( (HDC32)wParam, msg - WM_CTLCOLORMSGBOX );
249 case WM_CTLCOLOR:
250 return (LRESULT)DEFWND_ControlColor( (HDC32)wParam, HIWORD(lParam) );
252 case WM_GETTEXTLENGTH:
253 if (wndPtr->text) return (LRESULT)strlen(wndPtr->text);
254 return 0;
256 case WM_SETCURSOR:
257 if (wndPtr->dwStyle & WS_CHILD)
258 if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
259 wParam, lParam))
260 return TRUE;
261 return NC_HandleSetCursor( wndPtr->hwndSelf, wParam, lParam );
263 case WM_SYSCOMMAND:
264 return NC_HandleSysCommand( wndPtr->hwndSelf, wParam,
265 MAKEPOINT16(lParam) );
267 case WM_KEYDOWN:
268 if(wParam == VK_F10) iF10Key = VK_F10;
269 break;
271 case WM_SYSKEYDOWN:
272 if( HIWORD(lParam) & KEYDATA_ALT )
274 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
275 if( wParam == VK_MENU && !iMenuSysKey )
276 iMenuSysKey = 1;
277 else
278 iMenuSysKey = 0;
280 iF10Key = 0;
282 if( wParam == VK_F4 ) /* try to close the window */
284 HWND32 hWnd = WIN_GetTopParent( wndPtr->hwndSelf );
285 wndPtr = WIN_FindWndPtr( hWnd );
286 if( wndPtr && !(wndPtr->class->style & CS_NOCLOSE) )
287 PostMessage16( hWnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
290 else if( wParam == VK_F10 )
291 iF10Key = 1;
292 else
293 if( wParam == VK_ESCAPE && (GetKeyState32(VK_SHIFT) & 0x8000))
294 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
295 (WPARAM16)SC_KEYMENU, (LPARAM)VK_SPACE);
296 break;
298 case WM_KEYUP:
299 case WM_SYSKEYUP:
300 /* Press and release F10 or ALT */
301 if (((wParam == VK_MENU) && iMenuSysKey) ||
302 ((wParam == VK_F10) && iF10Key))
303 SendMessage16( WIN_GetTopParent(wndPtr->hwndSelf),
304 WM_SYSCOMMAND, SC_KEYMENU, 0L );
305 iMenuSysKey = iF10Key = 0;
306 break;
308 case WM_SYSCHAR:
309 iMenuSysKey = 0;
310 if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
312 PostMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
313 (WPARAM16)SC_RESTORE, 0L );
314 break;
316 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
318 if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
319 if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
320 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
321 else
322 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
323 (WPARAM16)SC_KEYMENU, (LPARAM)(DWORD)wParam );
325 else /* check for Ctrl-Esc */
326 if (wParam != VK_ESCAPE) MessageBeep32(0);
327 break;
329 case WM_SHOWWINDOW:
330 if (!lParam) return 0; /* sent from ShowWindow */
331 if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
332 if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
333 else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
334 ShowWindow32( wndPtr->hwndSelf, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
335 break;
337 case WM_CANCELMODE:
338 if (wndPtr->parent == WIN_GetDesktop()) EndMenu();
339 if (GetCapture32() == wndPtr->hwndSelf) ReleaseCapture();
340 break;
342 case WM_VKEYTOITEM:
343 case WM_CHARTOITEM:
344 return -1;
346 case WM_DROPOBJECT:
347 return DRAG_FILE;
349 case WM_QUERYDROPOBJECT:
350 if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
351 break;
353 case WM_QUERYDRAGICON:
355 HICON16 hIcon=0;
356 UINT16 len;
358 if( (hIcon=wndPtr->class->hCursor) ) return (LRESULT)hIcon;
359 for(len=1; len<64; len++)
360 if((hIcon=LoadIcon16(wndPtr->hInstance,MAKEINTRESOURCE16(len))))
361 return (LRESULT)hIcon;
362 return (LRESULT)LoadIcon16(NULL,IDI_APPLICATION16);
364 break;
366 case WM_ISACTIVEICON:
367 return ((wndPtr->flags & WIN_NCACTIVATED) != 0);
369 case WM_QUERYOPEN:
370 case WM_QUERYENDSESSION:
371 return 1;
373 return 0;
378 /***********************************************************************
379 * DefWindowProc16 (USER.107)
381 LRESULT WINAPI DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
382 LPARAM lParam )
384 WND * wndPtr = WIN_FindWndPtr( hwnd );
385 LRESULT result = 0;
387 if (!wndPtr) return 0;
388 SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
390 switch(msg)
392 case WM_NCCREATE:
394 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
395 if (cs->lpszName)
396 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(cs->lpszName) );
397 result = 1;
399 break;
401 case WM_NCCALCSIZE:
403 RECT32 rect32;
404 CONV_RECT16TO32( (RECT16 *)PTR_SEG_TO_LIN(lParam), &rect32 );
405 result = NC_HandleNCCalcSize( wndPtr, &rect32 );
406 CONV_RECT32TO16( &rect32, (RECT16 *)PTR_SEG_TO_LIN(lParam) );
408 break;
410 case WM_WINDOWPOSCHANGING:
411 result = WINPOS_HandleWindowPosChanging16( wndPtr,
412 (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam) );
413 break;
415 case WM_WINDOWPOSCHANGED:
417 WINDOWPOS16 * winPos = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
418 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
420 break;
422 case WM_GETTEXT:
423 if (wParam && wndPtr->text)
425 lstrcpyn32A( (LPSTR)PTR_SEG_TO_LIN(lParam), wndPtr->text, wParam );
426 result = (LRESULT)strlen( (LPSTR)PTR_SEG_TO_LIN(lParam) );
428 break;
430 case WM_SETTEXT:
431 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(lParam) );
432 if( wndPtr->dwStyle & WS_CAPTION ) NC_HandleNCPaint( hwnd , (HRGN32)1 );
433 break;
435 default:
436 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
437 break;
440 SPY_ExitMessage( SPY_RESULT_DEFWND16, hwnd, msg, result );
441 return result;
445 /***********************************************************************
446 * DefWindowProc32A [USER32.126]
449 LRESULT WINAPI DefWindowProc32A( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
450 LPARAM lParam )
452 WND * wndPtr = WIN_FindWndPtr( hwnd );
453 LRESULT result = 0;
455 if (!wndPtr) return 0;
456 SPY_EnterMessage( SPY_DEFWNDPROC32, hwnd, msg, wParam, lParam );
458 switch(msg)
460 case WM_NCCREATE:
462 CREATESTRUCT32A *cs = (CREATESTRUCT32A *)lParam;
463 if (cs->lpszName) DEFWND_SetText( wndPtr, cs->lpszName );
464 result = 1;
466 break;
468 case WM_NCCALCSIZE:
469 result = NC_HandleNCCalcSize( wndPtr, (RECT32 *)lParam );
470 break;
472 case WM_WINDOWPOSCHANGING:
473 result = WINPOS_HandleWindowPosChanging32( wndPtr,
474 (WINDOWPOS32 *)lParam );
475 break;
477 case WM_WINDOWPOSCHANGED:
479 WINDOWPOS32 * winPos = (WINDOWPOS32 *)lParam;
480 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
482 break;
484 case WM_GETTEXT:
485 if (wParam && wndPtr->text)
487 lstrcpyn32A( (LPSTR)lParam, wndPtr->text, wParam );
488 result = (LRESULT)strlen( (LPSTR)lParam );
490 break;
492 case WM_SETTEXT:
493 DEFWND_SetText( wndPtr, (LPSTR)lParam );
494 NC_HandleNCPaint( hwnd , (HRGN32)1 ); /* Repaint caption */
495 break;
497 default:
498 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
499 break;
502 SPY_ExitMessage( SPY_RESULT_DEFWND32, hwnd, msg, result );
503 return result;
507 /***********************************************************************
508 * DefWindowProc32W [USER32.127] Calls default window message handler
510 * Calls default window procedure for messages not processed
511 * by application.
513 * RETURNS
514 * Return value is dependent upon the message.
516 LRESULT WINAPI DefWindowProc32W(
517 HWND32 hwnd, /* [in] window procedure recieving message */
518 UINT32 msg, /* [in] message identifier */
519 WPARAM32 wParam, /* [in] first message parameter */
520 LPARAM lParam ) /* [in] second message parameter */
522 LRESULT result;
524 switch(msg)
526 case WM_NCCREATE:
528 CREATESTRUCT32W *cs = (CREATESTRUCT32W *)lParam;
529 if (cs->lpszName)
531 WND *wndPtr = WIN_FindWndPtr( hwnd );
532 LPSTR str = HEAP_strdupWtoA(GetProcessHeap(), 0, cs->lpszName);
533 DEFWND_SetText( wndPtr, str );
534 HeapFree( GetProcessHeap(), 0, str );
536 result = 1;
538 break;
540 case WM_GETTEXT:
542 LPSTR str = HeapAlloc( GetProcessHeap(), 0, wParam );
543 result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
544 lstrcpynAtoW( (LPWSTR)lParam, str, wParam );
545 HeapFree( GetProcessHeap(), 0, str );
547 break;
549 case WM_SETTEXT:
551 LPSTR str = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPWSTR)lParam );
552 result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
553 HeapFree( GetProcessHeap(), 0, str );
555 break;
557 default:
558 result = DefWindowProc32A( hwnd, msg, wParam, lParam );
559 break;
561 return result;