Release 970120
[wine/multimedia.git] / windows / defwnd.c
blobc393eb7fea8bbec5ffb5e2a668e2395066a40743
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 "syscolor.h"
16 #include "sysmetrics.h"
17 #include "stddebug.h"
18 #include "debug.h"
19 #include "spy.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, UINT32 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 if (wndPtr->window)
67 XStoreName( display, wndPtr->window, wndPtr->text );
68 XSetIconName( display, wndPtr->window, wndPtr->text );
73 /***********************************************************************
74 * DEFWND_DefWinProc
76 * Default window procedure for messages that are the same in Win16 and Win32.
78 static LRESULT DEFWND_DefWinProc( WND *wndPtr, UINT32 msg, WPARAM32 wParam,
79 LPARAM lParam )
81 switch(msg)
83 case WM_NCPAINT:
84 return NC_HandleNCPaint( wndPtr->hwndSelf, (HRGN32)wParam );
86 case WM_NCHITTEST:
87 return NC_HandleNCHitTest( wndPtr->hwndSelf, MAKEPOINT16(lParam) );
89 case WM_NCLBUTTONDOWN:
90 return NC_HandleNCLButtonDown( wndPtr->hwndSelf, wParam, lParam );
92 case WM_LBUTTONDBLCLK:
93 case WM_NCLBUTTONDBLCLK:
94 return NC_HandleNCLButtonDblClk( wndPtr, wParam, lParam );
96 case WM_NCACTIVATE:
97 return NC_HandleNCActivate( wndPtr, wParam );
99 case WM_NCDESTROY:
100 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
101 wndPtr->text = NULL;
102 if (wndPtr->pVScroll) HeapFree( SystemHeap, 0, wndPtr->pVScroll );
103 if (wndPtr->pHScroll) HeapFree( SystemHeap, 0, wndPtr->pHScroll );
104 wndPtr->pVScroll = wndPtr->pHScroll = NULL;
105 return 0;
107 case WM_PAINTICON:
108 case WM_PAINT:
110 PAINTSTRUCT16 ps;
111 HDC16 hdc = BeginPaint16( wndPtr->hwndSelf, &ps );
112 if( hdc )
114 if( (wndPtr->dwStyle & WS_MINIMIZE) && wndPtr->class->hIcon )
116 int x = (wndPtr->rectWindow.right - wndPtr->rectWindow.left -
117 SYSMETRICS_CXICON)/2;
118 int y = (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top -
119 SYSMETRICS_CYICON)/2;
120 dprintf_win(stddeb,"Painting class icon: vis rect=(%i,%i - %i,%i)\n",
121 ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
122 DrawIcon( hdc, x, y, wndPtr->class->hIcon );
124 EndPaint16( wndPtr->hwndSelf, &ps );
126 return 0;
129 case WM_SETREDRAW:
130 if (!wParam)
132 ValidateRect32( wndPtr->hwndSelf, NULL );
133 wndPtr->flags |= WIN_NO_REDRAW;
135 else
137 wndPtr->flags &= ~WIN_NO_REDRAW;
138 ShowWindow32( wndPtr->hwndSelf, SW_SHOW );
140 return 0;
142 case WM_CLOSE:
143 DestroyWindow32( wndPtr->hwndSelf );
144 return 0;
146 case WM_MOUSEACTIVATE:
147 if (wndPtr->dwStyle & WS_CHILD)
149 LONG ret = SendMessage16( wndPtr->parent->hwndSelf,
150 WM_MOUSEACTIVATE, wParam, lParam );
151 if (ret) return ret;
153 return MA_ACTIVATE;
155 case WM_ACTIVATE:
156 if (LOWORD(wParam) != WA_INACTIVE) SetFocus32( wndPtr->hwndSelf );
157 break;
159 case WM_ERASEBKGND:
160 case WM_ICONERASEBKGND:
162 if (!wndPtr->class->hbrBackground) return 0;
164 /* FIXME: should fill icon text with hbrushActiveCaption
165 instead of this */
167 if (wndPtr->dwStyle & WS_MINIMIZE )
169 if( wndPtr->flags & WIN_NCACTIVATED )
171 FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
172 (HDC16)wParam, sysColorObjects.hbrushActiveCaption );
173 return 1;
176 /* FIXME: should draw parent' background somehow
177 (e.g for textured desktop) ? */
180 if (wndPtr->class->hbrBackground <= (HBRUSH16)(COLOR_MAX+1))
182 HBRUSH32 hbrush = CreateSolidBrush32(
183 GetSysColor(((DWORD)wndPtr->class->hbrBackground)-1));
184 FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
185 (HDC16)wParam, hbrush);
186 DeleteObject32( hbrush );
188 else FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
189 (HDC16)wParam, wndPtr->class->hbrBackground );
190 return 1;
193 case WM_GETDLGCODE:
194 return 0;
196 case WM_CTLCOLORMSGBOX:
197 case WM_CTLCOLOREDIT:
198 case WM_CTLCOLORLISTBOX:
199 case WM_CTLCOLORBTN:
200 case WM_CTLCOLORDLG:
201 case WM_CTLCOLORSTATIC:
202 SetBkColor( (HDC32)wParam, GetSysColor(COLOR_WINDOW) );
203 SetTextColor( (HDC32)wParam, GetSysColor(COLOR_WINDOWTEXT) );
204 return (LRESULT)sysColorObjects.hbrushWindow;
206 case WM_CTLCOLORSCROLLBAR:
207 SetBkColor( (HDC32)wParam, RGB(255, 255, 255) );
208 SetTextColor( (HDC32)wParam, RGB(0, 0, 0) );
209 UnrealizeObject32( sysColorObjects.hbrushScrollbar );
210 return (LRESULT)sysColorObjects.hbrushScrollbar;
212 case WM_CTLCOLOR:
214 if (HIWORD(lParam) == CTLCOLOR_SCROLLBAR)
216 SetBkColor( (HDC32)wParam, RGB(255, 255, 255) );
217 SetTextColor( (HDC32)wParam, RGB(0, 0, 0) );
218 UnrealizeObject32( sysColorObjects.hbrushScrollbar );
219 return (LRESULT)sysColorObjects.hbrushScrollbar;
221 else
223 SetBkColor( (HDC32)wParam, GetSysColor(COLOR_WINDOW) );
224 SetTextColor( (HDC32)wParam, GetSysColor(COLOR_WINDOWTEXT) );
225 return (LRESULT)sysColorObjects.hbrushWindow;
229 case WM_GETTEXTLENGTH:
230 if (wndPtr->text) return (LRESULT)strlen(wndPtr->text);
231 return 0;
233 case WM_SETCURSOR:
234 if (wndPtr->dwStyle & WS_CHILD)
235 if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
236 wParam, lParam))
237 return TRUE;
238 return NC_HandleSetCursor( wndPtr->hwndSelf, wParam, lParam );
240 case WM_SYSCOMMAND:
241 return NC_HandleSysCommand( wndPtr->hwndSelf, wParam,
242 MAKEPOINT16(lParam) );
244 case WM_KEYDOWN:
245 if(wParam == VK_F10) iF10Key = VK_F10;
246 break;
248 case WM_SYSKEYDOWN:
249 if( HIWORD(lParam) & KEYDATA_ALT )
251 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
252 if( wParam == VK_MENU && !iMenuSysKey )
253 iMenuSysKey = 1;
254 else
255 iMenuSysKey = 0;
257 iF10Key = 0;
260 else if( wParam == VK_F10 )
261 iF10Key = 1;
262 else
263 if( wParam == VK_ESCAPE && GetKeyState(VK_SHIFT) < 0 )
264 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
265 (WPARAM16)SC_KEYMENU, (LPARAM)VK_SPACE);
266 break;
268 case WM_KEYUP:
269 case WM_SYSKEYUP:
270 /* Press and release F10 or ALT */
271 if (((wParam == VK_MENU) && iMenuSysKey) ||
272 ((wParam == VK_F10) && iF10Key))
273 SendMessage16( WIN_GetTopParent(wndPtr->hwndSelf),
274 WM_SYSCOMMAND, SC_KEYMENU, 0L );
275 iMenuSysKey = iF10Key = 0;
276 break;
278 case WM_SYSCHAR:
279 iMenuSysKey = 0;
280 if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
282 PostMessage( wndPtr->hwndSelf, WM_SYSCOMMAND,
283 (WPARAM16)SC_RESTORE, 0L );
284 break;
286 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
288 if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
289 if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
290 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
291 else
292 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
293 (WPARAM16)SC_KEYMENU, (LPARAM)(DWORD)wParam );
295 else /* check for Ctrl-Esc */
296 if (wParam != VK_ESCAPE) MessageBeep(0);
297 break;
299 case WM_SHOWWINDOW:
300 if (!lParam) return 0; /* sent from ShowWindow */
301 if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
302 if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
303 else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
304 ShowWindow32( wndPtr->hwndSelf, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
305 break;
307 case WM_CANCELMODE:
308 /* EndMenu() should be called if in menu state but currently it's
309 impossible to detect - menu code should be updated*/
310 if (GetCapture32() == wndPtr->hwndSelf) ReleaseCapture();
311 break;
313 case WM_VKEYTOITEM:
314 case WM_CHARTOITEM:
315 return -1;
317 case WM_DROPOBJECT:
318 return DRAG_FILE;
320 case WM_QUERYDROPOBJECT:
321 if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
322 break;
324 case WM_QUERYDRAGICON:
326 HICON16 hI = 0;
327 UINT16 len = 1;
328 while(len < 64)
329 if( (hI = LoadIcon16(wndPtr->hInstance,MAKEINTRESOURCE(len))) )
330 return (LRESULT)hI;
332 break;
334 case WM_QUERYOPEN:
335 case WM_QUERYENDSESSION:
336 return 1;
338 return 0;
343 /***********************************************************************
344 * DefWindowProc16 (USER.107)
346 LRESULT DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
347 LPARAM lParam )
349 WND * wndPtr = WIN_FindWndPtr( hwnd );
350 LRESULT result = 0;
352 if (!wndPtr) return 0;
353 SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
355 switch(msg)
357 case WM_NCCREATE:
359 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
360 if (cs->lpszName)
361 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(cs->lpszName) );
362 result = 1;
364 break;
366 case WM_NCCALCSIZE:
367 result = NC_HandleNCCalcSize(wndPtr, (RECT16 *)PTR_SEG_TO_LIN(lParam));
368 break;
370 case WM_WINDOWPOSCHANGING:
371 result = WINPOS_HandleWindowPosChanging16( wndPtr,
372 (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam) );
373 break;
375 case WM_WINDOWPOSCHANGED:
377 WINDOWPOS16 * winPos = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
378 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
380 break;
382 case WM_GETTEXT:
383 if (wParam && wndPtr->text)
385 lstrcpyn32A( (LPSTR)PTR_SEG_TO_LIN(lParam), wndPtr->text, wParam );
386 result = (LRESULT)strlen( (LPSTR)PTR_SEG_TO_LIN(lParam) );
388 break;
390 case WM_SETTEXT:
391 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(lParam) );
392 NC_HandleNCPaint( hwnd , (HRGN32)1 ); /* Repaint caption */
393 break;
395 default:
396 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
397 break;
400 SPY_ExitMessage( SPY_RESULT_OK16, hwnd, msg, result );
401 return result;
405 /***********************************************************************
406 * DefWindowProc32A (USER32.125)
408 LRESULT DefWindowProc32A( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
409 LPARAM lParam )
411 WND * wndPtr = WIN_FindWndPtr( hwnd );
412 LRESULT result = 0;
414 if (!wndPtr) return 0;
415 SPY_EnterMessage( SPY_DEFWNDPROC32, hwnd, msg, wParam, lParam );
417 switch(msg)
419 case WM_NCCREATE:
421 CREATESTRUCT32A *cs = (CREATESTRUCT32A *)lParam;
422 if (cs->lpszName) DEFWND_SetText( wndPtr, cs->lpszName );
423 result = 1;
425 break;
427 case WM_NCCALCSIZE:
429 RECT16 rect16;
430 CONV_RECT32TO16( (RECT32 *)lParam, &rect16 );
431 result = NC_HandleNCCalcSize( wndPtr, &rect16 );
432 CONV_RECT16TO32( &rect16, (RECT32 *)lParam );
434 break;
436 case WM_WINDOWPOSCHANGING:
437 result = WINPOS_HandleWindowPosChanging32( wndPtr,
438 (WINDOWPOS32 *)lParam );
439 break;
441 case WM_WINDOWPOSCHANGED:
443 WINDOWPOS32 * winPos = (WINDOWPOS32 *)lParam;
444 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
446 break;
448 case WM_GETTEXT:
449 if (wParam && wndPtr->text)
451 lstrcpyn32A( (LPSTR)lParam, wndPtr->text, wParam );
452 result = (LRESULT)strlen( (LPSTR)lParam );
454 break;
456 case WM_SETTEXT:
457 DEFWND_SetText( wndPtr, (LPSTR)lParam );
458 NC_HandleNCPaint( hwnd , (HRGN32)1 ); /* Repaint caption */
459 break;
461 default:
462 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
463 break;
466 SPY_ExitMessage( SPY_RESULT_OK32, hwnd, msg, result );
467 return result;
471 /***********************************************************************
472 * DefWindowProc32W (USER32.126)
474 LRESULT DefWindowProc32W( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
475 LPARAM lParam )
477 LRESULT result;
479 switch(msg)
481 case WM_NCCREATE:
483 CREATESTRUCT32W *cs = (CREATESTRUCT32W *)lParam;
484 if (cs->lpszName)
486 WND *wndPtr = WIN_FindWndPtr( hwnd );
487 LPSTR str = HEAP_strdupWtoA(GetProcessHeap(), 0, cs->lpszName);
488 DEFWND_SetText( wndPtr, str );
489 HeapFree( GetProcessHeap(), 0, str );
491 result = 1;
493 break;
495 case WM_GETTEXT:
497 LPSTR str = HeapAlloc( GetProcessHeap(), 0, wParam );
498 result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
499 lstrcpynAtoW( (LPWSTR)lParam, str, wParam );
500 HeapFree( GetProcessHeap(), 0, str );
502 break;
504 case WM_SETTEXT:
506 LPSTR str = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPWSTR)lParam );
507 result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
508 HeapFree( GetProcessHeap(), 0, str );
510 break;
512 default:
513 result = DefWindowProc32A( hwnd, msg, wParam, lParam );
514 break;
516 return result;