Release 960928
[wine/multimedia.git] / windows / defwnd.c
blob7284cd1a50594b4fb33d17bc7f998e5e04a7fae5
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 "string32.h"
16 #include "syscolor.h"
17 #include "sysmetrics.h"
18 #include "stddebug.h"
19 #include "debug.h"
20 #include "spy.h"
22 /* Last COLOR id */
23 #define COLOR_MAX COLOR_BTNHIGHLIGHT
25 /* bits in the dwKeyData */
26 #define KEYDATA_ALT 0x2000
27 #define KEYDATA_PREVSTATE 0x4000
29 static short iF10Key = 0;
30 static short iMenuSysKey = 0;
32 /***********************************************************************
33 * DEFWND_HandleWindowPosChanged
35 * Handle the WM_WINDOWPOSCHANGED message.
37 static void DEFWND_HandleWindowPosChanged( WND *wndPtr, UINT32 flags )
39 WPARAM16 wp = SIZE_RESTORED;
41 if (!(flags & SWP_NOCLIENTMOVE))
42 SendMessage16( wndPtr->hwndSelf, WM_MOVE, 0,
43 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top));
44 if (!(flags & SWP_NOCLIENTSIZE))
46 if (wndPtr->dwStyle & WS_MAXIMIZE) wp = SIZE_MAXIMIZED;
47 else if (wndPtr->dwStyle & WS_MINIMIZE) wp = SIZE_MINIMIZED;
49 SendMessage16( wndPtr->hwndSelf, WM_SIZE, wp,
50 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
51 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
56 /***********************************************************************
57 * DEFWND_SetText
59 * Set the window text.
61 void DEFWND_SetText( WND *wndPtr, LPCSTR text )
63 if (!text) text = "";
64 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
65 wndPtr->text = HEAP_strdupA( SystemHeap, 0, text );
66 if (wndPtr->window) XStoreName( display, wndPtr->window, wndPtr->text );
70 /***********************************************************************
71 * DEFWND_DefWinProc
73 * Default window procedure for messages that are the same in Win16 and Win32.
75 static LRESULT DEFWND_DefWinProc( WND *wndPtr, UINT32 msg, WPARAM32 wParam,
76 LPARAM lParam )
78 switch(msg)
80 case WM_NCPAINT:
81 return NC_HandleNCPaint( wndPtr->hwndSelf, (HRGN)wParam );
83 case WM_NCHITTEST:
84 return NC_HandleNCHitTest( wndPtr->hwndSelf, MAKEPOINT16(lParam) );
86 case WM_NCLBUTTONDOWN:
87 return NC_HandleNCLButtonDown( wndPtr->hwndSelf, wParam, lParam );
89 case WM_LBUTTONDBLCLK:
90 case WM_NCLBUTTONDBLCLK:
91 return NC_HandleNCLButtonDblClk( wndPtr, wParam, lParam );
93 case WM_NCACTIVATE:
94 return NC_HandleNCActivate( wndPtr, wParam );
96 case WM_NCDESTROY:
97 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
98 wndPtr->text = NULL;
99 if (wndPtr->pVScroll) HeapFree( SystemHeap, 0, wndPtr->pVScroll );
100 if (wndPtr->pHScroll) HeapFree( SystemHeap, 0, wndPtr->pHScroll );
101 wndPtr->pVScroll = wndPtr->pHScroll = NULL;
102 return 0;
104 case WM_PAINTICON:
105 case WM_PAINT:
107 PAINTSTRUCT16 ps;
108 HDC hdc = BeginPaint16( wndPtr->hwndSelf, &ps );
109 if( hdc )
111 if( (wndPtr->dwStyle & WS_MINIMIZE) && wndPtr->class->hIcon )
113 int x = (wndPtr->rectWindow.right - wndPtr->rectWindow.left -
114 SYSMETRICS_CXICON)/2;
115 int y = (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top -
116 SYSMETRICS_CYICON)/2;
117 dprintf_win(stddeb,"Painting class icon: vis rect=(%i,%i - %i,%i)\n",
118 ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
119 DrawIcon( hdc, x, y, wndPtr->class->hIcon );
121 EndPaint16( wndPtr->hwndSelf, &ps );
123 return 0;
126 case WM_SETREDRAW:
127 if (!wParam)
129 ValidateRect32( wndPtr->hwndSelf, NULL );
130 wndPtr->flags |= WIN_NO_REDRAW;
132 else wndPtr->flags &= ~WIN_NO_REDRAW;
133 return 0;
135 case WM_CLOSE:
136 DestroyWindow( wndPtr->hwndSelf );
137 return 0;
139 case WM_MOUSEACTIVATE:
140 if (wndPtr->dwStyle & WS_CHILD)
142 LONG ret = SendMessage16( wndPtr->parent->hwndSelf,
143 WM_MOUSEACTIVATE, wParam, lParam );
144 if (ret) return ret;
146 return MA_ACTIVATE;
148 case WM_ACTIVATE:
149 if (LOWORD(wParam) != WA_INACTIVE) SetFocus32( wndPtr->hwndSelf );
150 break;
152 case WM_ERASEBKGND:
153 case WM_ICONERASEBKGND:
155 if (!wndPtr->class->hbrBackground) return 0;
157 /* FIXME: should fill icon text with hbrushActiveCaption
158 instead of this */
160 if (wndPtr->dwStyle & WS_MINIMIZE )
162 if( wndPtr->flags & WIN_NCACTIVATED )
164 FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
165 (HDC)wParam, sysColorObjects.hbrushActiveCaption );
166 return 1;
169 /* FIXME: should draw parent' background somehow
170 (e.g for textured desktop) ? */
173 if (wndPtr->class->hbrBackground <= (HBRUSH)(COLOR_MAX+1))
175 HBRUSH hbrush = CreateSolidBrush(
176 GetSysColor(((DWORD)wndPtr->class->hbrBackground)-1));
177 FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
178 (HDC)wParam, hbrush);
179 DeleteObject (hbrush);
181 else FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
182 (HDC)wParam, wndPtr->class->hbrBackground );
183 return 1;
186 case WM_GETDLGCODE:
187 return 0;
189 case WM_CTLCOLORMSGBOX:
190 case WM_CTLCOLOREDIT:
191 case WM_CTLCOLORLISTBOX:
192 case WM_CTLCOLORBTN:
193 case WM_CTLCOLORDLG:
194 case WM_CTLCOLORSTATIC:
195 SetBkColor( (HDC)wParam, GetSysColor(COLOR_WINDOW) );
196 SetTextColor( (HDC)wParam, GetSysColor(COLOR_WINDOWTEXT) );
197 return (LRESULT)sysColorObjects.hbrushWindow;
199 case WM_CTLCOLORSCROLLBAR:
200 SetBkColor( (HDC)wParam, RGB(255, 255, 255) );
201 SetTextColor( (HDC)wParam, RGB(0, 0, 0) );
202 UnrealizeObject( sysColorObjects.hbrushScrollbar );
203 return (LRESULT)sysColorObjects.hbrushScrollbar;
205 case WM_CTLCOLOR:
207 if (HIWORD(lParam) == CTLCOLOR_SCROLLBAR)
209 SetBkColor( (HDC)wParam, RGB(255, 255, 255) );
210 SetTextColor( (HDC)wParam, RGB(0, 0, 0) );
211 UnrealizeObject( sysColorObjects.hbrushScrollbar );
212 return (LRESULT)sysColorObjects.hbrushScrollbar;
214 else
216 SetBkColor( (HDC)wParam, GetSysColor(COLOR_WINDOW) );
217 SetTextColor( (HDC)wParam, GetSysColor(COLOR_WINDOWTEXT) );
218 return (LRESULT)sysColorObjects.hbrushWindow;
222 case WM_GETTEXTLENGTH:
223 if (wndPtr->text) return (LRESULT)strlen(wndPtr->text);
224 return 0;
226 case WM_SETCURSOR:
227 if (wndPtr->dwStyle & WS_CHILD)
228 if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
229 wParam, lParam))
230 return TRUE;
231 return NC_HandleSetCursor( wndPtr->hwndSelf, wParam, lParam );
233 case WM_SYSCOMMAND:
234 return NC_HandleSysCommand( wndPtr->hwndSelf, wParam,
235 MAKEPOINT16(lParam) );
237 case WM_KEYDOWN:
238 if(wParam == VK_F10) iF10Key = VK_F10;
239 break;
241 case WM_SYSKEYDOWN:
242 if( HIWORD(lParam) & KEYDATA_ALT )
244 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
245 if( wParam == VK_MENU && !iMenuSysKey )
246 iMenuSysKey = 1;
247 else
248 iMenuSysKey = 0;
250 iF10Key = 0;
253 else if( wParam == VK_F10 )
254 iF10Key = 1;
255 else
256 if( wParam == VK_ESCAPE && GetKeyState(VK_SHIFT) < 0 )
257 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
258 (WPARAM)SC_KEYMENU, (LPARAM)VK_SPACE);
259 break;
261 case WM_KEYUP:
262 case WM_SYSKEYUP:
263 /* Press and release F10 or ALT */
264 if (((wParam == VK_MENU) && iMenuSysKey) ||
265 ((wParam == VK_F10) && iF10Key))
266 SendMessage16( WIN_GetTopParent(wndPtr->hwndSelf),
267 WM_SYSCOMMAND, SC_KEYMENU, 0L );
268 iMenuSysKey = iF10Key = 0;
269 break;
271 case WM_SYSCHAR:
272 iMenuSysKey = 0;
273 if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
275 PostMessage( wndPtr->hwndSelf, WM_SYSCOMMAND,
276 (WPARAM)SC_RESTORE, 0L );
277 break;
279 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
281 if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
282 if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
283 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
284 else
285 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
286 (WPARAM)SC_KEYMENU, (LPARAM)(DWORD)wParam );
288 else /* check for Ctrl-Esc */
289 if (wParam != VK_ESCAPE) MessageBeep(0);
290 break;
292 case WM_SHOWWINDOW:
293 if (!lParam) return 0; /* sent from ShowWindow */
294 if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
295 if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
296 else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
297 ShowWindow( wndPtr->hwndSelf, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
298 break;
300 case WM_CANCELMODE:
301 /* EndMenu() should be called if in menu state but currently it's
302 impossible to detect - menu code should be updated*/
303 if (GetCapture32() == wndPtr->hwndSelf) ReleaseCapture();
304 break;
306 case WM_VKEYTOITEM:
307 case WM_CHARTOITEM:
308 return -1;
310 case WM_DROPOBJECT:
311 return DRAG_FILE;
313 case WM_QUERYDROPOBJECT:
314 if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
315 break;
317 case WM_QUERYDRAGICON:
319 HICON16 hI = 0;
320 UINT16 len = 1;
321 while(len < 64)
322 if( (hI = LoadIcon16(wndPtr->hInstance,MAKEINTRESOURCE(len))) )
323 return (LRESULT)hI;
325 break;
327 case WM_QUERYOPEN:
328 case WM_QUERYENDSESSION:
329 return 1;
331 return 0;
336 /***********************************************************************
337 * DefWindowProc16 (USER.107)
339 LRESULT DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
340 LPARAM lParam )
342 WND * wndPtr = WIN_FindWndPtr( hwnd );
343 LRESULT result = 0;
345 if (!wndPtr) return 0;
346 SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
348 switch(msg)
350 case WM_NCCREATE:
352 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
353 if (cs->lpszName)
354 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(cs->lpszName) );
355 result = 1;
357 break;
359 case WM_NCCALCSIZE:
360 result = NC_HandleNCCalcSize(wndPtr, (RECT16 *)PTR_SEG_TO_LIN(lParam));
361 break;
363 case WM_WINDOWPOSCHANGING:
364 result = WINPOS_HandleWindowPosChanging16( wndPtr,
365 (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam) );
366 break;
368 case WM_WINDOWPOSCHANGED:
370 WINDOWPOS16 * winPos = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
371 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
373 break;
375 case WM_GETTEXT:
376 if (wParam && wndPtr->text)
378 lstrcpyn32A( (LPSTR)PTR_SEG_TO_LIN(lParam), wndPtr->text, wParam );
379 result = (LRESULT)strlen( (LPSTR)PTR_SEG_TO_LIN(lParam) ) + 1;
381 break;
383 case WM_SETTEXT:
384 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(lParam) );
385 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
386 break;
388 default:
389 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
390 break;
393 SPY_ExitMessage( SPY_RESULT_OK16, hwnd, msg, result );
394 return result;
398 /***********************************************************************
399 * DefWindowProc32A (USER32.125)
401 LRESULT DefWindowProc32A( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
402 LPARAM lParam )
404 WND * wndPtr = WIN_FindWndPtr( hwnd );
405 LRESULT result = 0;
407 if (!wndPtr) return 0;
408 SPY_EnterMessage( SPY_DEFWNDPROC32, hwnd, msg, wParam, lParam );
410 switch(msg)
412 case WM_NCCREATE:
414 CREATESTRUCT32A *cs = (CREATESTRUCT32A *)lParam;
415 if (cs->lpszName) DEFWND_SetText( wndPtr, cs->lpszName );
416 result = 1;
418 break;
420 case WM_NCCALCSIZE:
422 RECT16 rect16;
423 CONV_RECT32TO16( (RECT32 *)lParam, &rect16 );
424 result = NC_HandleNCCalcSize( wndPtr, &rect16 );
425 CONV_RECT16TO32( &rect16, (RECT32 *)lParam );
427 break;
429 case WM_WINDOWPOSCHANGING:
430 result = WINPOS_HandleWindowPosChanging32( wndPtr,
431 (WINDOWPOS32 *)lParam );
432 break;
434 case WM_WINDOWPOSCHANGED:
436 WINDOWPOS32 * winPos = (WINDOWPOS32 *)lParam;
437 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
439 break;
441 case WM_GETTEXT:
442 if (wParam && wndPtr->text)
444 lstrcpyn32A( (LPSTR)lParam, wndPtr->text, wParam );
445 result = (LRESULT)strlen( (LPSTR)lParam ) + 1;
447 break;
449 case WM_SETTEXT:
450 DEFWND_SetText( wndPtr, (LPSTR)lParam );
451 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
452 break;
454 default:
455 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
456 break;
459 SPY_ExitMessage( SPY_RESULT_OK32, hwnd, msg, result );
460 return result;
464 /***********************************************************************
465 * DefWindowProc32W (USER32.126)
467 LRESULT DefWindowProc32W( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
468 LPARAM lParam )
470 LRESULT result;
472 switch(msg)
474 case WM_NCCREATE:
476 CREATESTRUCT32W *cs = (CREATESTRUCT32W *)lParam;
477 if (cs->lpszName)
479 WND *wndPtr = WIN_FindWndPtr( hwnd );
480 LPSTR str = STRING32_DupUniToAnsi( cs->lpszName );
481 DEFWND_SetText( wndPtr, str );
482 free( str );
484 result = 1;
486 break;
488 case WM_GETTEXT:
490 LPSTR str = malloc( wParam );
491 result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
492 STRING32_AnsiToUni( (LPWSTR)lParam, str );
493 free( str );
495 break;
497 case WM_SETTEXT:
499 LPSTR str = STRING32_DupUniToAnsi( (LPWSTR)lParam );
500 result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
501 free( str );
503 break;
505 default:
506 result = DefWindowProc32A( hwnd, msg, wParam, lParam );
507 break;
509 return result;