Release 970215
[wine/hacks.git] / windows / defwnd.c
blob5581bf76da27d8dbdfc3a22cf32ab6e7aeed7684
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_RBUTTONDOWN:
97 case WM_NCRBUTTONDOWN:
98 if( wndPtr->flags & WIN_ISWIN32 )
100 ClientToScreen16(wndPtr->hwndSelf, (LPPOINT16)&lParam);
101 SendMessage32A( wndPtr->hwndSelf, WM_CONTEXTMENU,
102 wndPtr->hwndSelf, lParam);
104 break;
106 case WM_CONTEXTMENU:
107 if( wndPtr->dwStyle & WS_CHILD )
108 SendMessage32A( wndPtr->parent->hwndSelf, msg, wParam, lParam );
110 /* else
111 * FIXME: Track system popup if click was in the caption area. */
113 break;
115 case WM_NCACTIVATE:
116 return NC_HandleNCActivate( wndPtr, wParam );
118 case WM_NCDESTROY:
119 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
120 wndPtr->text = NULL;
121 if (wndPtr->pVScroll) HeapFree( SystemHeap, 0, wndPtr->pVScroll );
122 if (wndPtr->pHScroll) HeapFree( SystemHeap, 0, wndPtr->pHScroll );
123 wndPtr->pVScroll = wndPtr->pHScroll = NULL;
124 return 0;
126 case WM_PAINTICON:
127 case WM_PAINT:
129 PAINTSTRUCT16 ps;
130 HDC16 hdc = BeginPaint16( wndPtr->hwndSelf, &ps );
131 if( hdc )
133 if( (wndPtr->dwStyle & WS_MINIMIZE) && wndPtr->class->hIcon )
135 int x = (wndPtr->rectWindow.right - wndPtr->rectWindow.left -
136 SYSMETRICS_CXICON)/2;
137 int y = (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top -
138 SYSMETRICS_CYICON)/2;
139 dprintf_win(stddeb,"Painting class icon: vis rect=(%i,%i - %i,%i)\n",
140 ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
141 DrawIcon32( hdc, x, y, wndPtr->class->hIcon );
143 EndPaint16( wndPtr->hwndSelf, &ps );
145 return 0;
148 case WM_SETREDRAW:
149 if (!wParam)
151 ValidateRect32( wndPtr->hwndSelf, NULL );
152 wndPtr->flags |= WIN_NO_REDRAW;
154 else
156 wndPtr->flags &= ~WIN_NO_REDRAW;
157 ShowWindow32( wndPtr->hwndSelf, SW_SHOW );
159 return 0;
161 case WM_CLOSE:
162 DestroyWindow32( wndPtr->hwndSelf );
163 return 0;
165 case WM_MOUSEACTIVATE:
166 if (wndPtr->dwStyle & WS_CHILD)
168 LONG ret = SendMessage16( wndPtr->parent->hwndSelf,
169 WM_MOUSEACTIVATE, wParam, lParam );
170 if (ret) return ret;
172 return MA_ACTIVATE;
174 case WM_ACTIVATE:
175 if (LOWORD(wParam) != WA_INACTIVE) SetFocus32( wndPtr->hwndSelf );
176 break;
178 case WM_ERASEBKGND:
179 case WM_ICONERASEBKGND:
181 if (!wndPtr->class->hbrBackground) return 0;
183 /* FIXME: should fill icon text with hbrushActiveCaption
184 instead of this */
186 if (wndPtr->dwStyle & WS_MINIMIZE )
188 if( wndPtr->flags & WIN_NCACTIVATED )
190 FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
191 (HDC16)wParam, sysColorObjects.hbrushActiveCaption );
192 return 1;
196 if (wndPtr->class->hbrBackground <= (HBRUSH16)(COLOR_MAX+1))
198 HBRUSH32 hbrush = CreateSolidBrush32(
199 GetSysColor32(((DWORD)wndPtr->class->hbrBackground)-1));
200 FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
201 (HDC16)wParam, hbrush);
202 DeleteObject32( hbrush );
204 else FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
205 (HDC16)wParam, wndPtr->class->hbrBackground );
206 return 1;
209 case WM_GETDLGCODE:
210 return 0;
212 case WM_CTLCOLORMSGBOX:
213 case WM_CTLCOLOREDIT:
214 case WM_CTLCOLORLISTBOX:
215 case WM_CTLCOLORBTN:
216 case WM_CTLCOLORDLG:
217 case WM_CTLCOLORSTATIC:
218 SetBkColor( (HDC32)wParam, GetSysColor32(COLOR_WINDOW) );
219 SetTextColor( (HDC32)wParam, GetSysColor32(COLOR_WINDOWTEXT) );
220 return (LRESULT)sysColorObjects.hbrushWindow;
222 case WM_CTLCOLORSCROLLBAR:
223 SetBkColor( (HDC32)wParam, RGB(255, 255, 255) );
224 SetTextColor( (HDC32)wParam, RGB(0, 0, 0) );
225 UnrealizeObject32( sysColorObjects.hbrushScrollbar );
226 return (LRESULT)sysColorObjects.hbrushScrollbar;
228 case WM_CTLCOLOR:
230 if (HIWORD(lParam) == CTLCOLOR_SCROLLBAR)
232 SetBkColor( (HDC32)wParam, RGB(255, 255, 255) );
233 SetTextColor( (HDC32)wParam, RGB(0, 0, 0) );
234 UnrealizeObject32( sysColorObjects.hbrushScrollbar );
235 return (LRESULT)sysColorObjects.hbrushScrollbar;
237 else
239 SetBkColor( (HDC32)wParam, GetSysColor32(COLOR_WINDOW) );
240 SetTextColor( (HDC32)wParam, GetSysColor32(COLOR_WINDOWTEXT) );
241 return (LRESULT)sysColorObjects.hbrushWindow;
245 case WM_GETTEXTLENGTH:
246 if (wndPtr->text) return (LRESULT)strlen(wndPtr->text);
247 return 0;
249 case WM_SETCURSOR:
250 if (wndPtr->dwStyle & WS_CHILD)
251 if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
252 wParam, lParam))
253 return TRUE;
254 return NC_HandleSetCursor( wndPtr->hwndSelf, wParam, lParam );
256 case WM_SYSCOMMAND:
257 return NC_HandleSysCommand( wndPtr->hwndSelf, wParam,
258 MAKEPOINT16(lParam) );
260 case WM_KEYDOWN:
261 if(wParam == VK_F10) iF10Key = VK_F10;
262 break;
264 case WM_SYSKEYDOWN:
265 if( HIWORD(lParam) & KEYDATA_ALT )
267 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
268 if( wParam == VK_MENU && !iMenuSysKey )
269 iMenuSysKey = 1;
270 else
271 iMenuSysKey = 0;
273 iF10Key = 0;
276 else if( wParam == VK_F10 )
277 iF10Key = 1;
278 else
279 if( wParam == VK_ESCAPE && (GetKeyState32(VK_SHIFT) & 0x8000))
280 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
281 (WPARAM16)SC_KEYMENU, (LPARAM)VK_SPACE);
282 break;
284 case WM_KEYUP:
285 case WM_SYSKEYUP:
286 /* Press and release F10 or ALT */
287 if (((wParam == VK_MENU) && iMenuSysKey) ||
288 ((wParam == VK_F10) && iF10Key))
289 SendMessage16( WIN_GetTopParent(wndPtr->hwndSelf),
290 WM_SYSCOMMAND, SC_KEYMENU, 0L );
291 iMenuSysKey = iF10Key = 0;
292 break;
294 case WM_SYSCHAR:
295 iMenuSysKey = 0;
296 if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
298 PostMessage( wndPtr->hwndSelf, WM_SYSCOMMAND,
299 (WPARAM16)SC_RESTORE, 0L );
300 break;
302 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
304 if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
305 if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
306 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
307 else
308 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
309 (WPARAM16)SC_KEYMENU, (LPARAM)(DWORD)wParam );
311 else /* check for Ctrl-Esc */
312 if (wParam != VK_ESCAPE) MessageBeep32(0);
313 break;
315 case WM_SHOWWINDOW:
316 if (!lParam) return 0; /* sent from ShowWindow */
317 if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
318 if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
319 else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
320 ShowWindow32( wndPtr->hwndSelf, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
321 break;
323 case WM_CANCELMODE:
324 /* EndMenu() should be called if in menu state but currently it's
325 impossible to detect - menu code should be updated*/
326 if (GetCapture32() == wndPtr->hwndSelf) ReleaseCapture();
327 break;
329 case WM_VKEYTOITEM:
330 case WM_CHARTOITEM:
331 return -1;
333 case WM_DROPOBJECT:
334 return DRAG_FILE;
336 case WM_QUERYDROPOBJECT:
337 if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
338 break;
340 case WM_QUERYDRAGICON:
342 HICON16 hI = 0;
343 UINT16 len = 1;
344 while(len < 64)
345 if( (hI = LoadIcon16(wndPtr->hInstance,MAKEINTRESOURCE(len))) )
346 return (LRESULT)hI;
348 break;
350 case WM_QUERYOPEN:
351 case WM_QUERYENDSESSION:
352 return 1;
354 return 0;
359 /***********************************************************************
360 * DefWindowProc16 (USER.107)
362 LRESULT DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
363 LPARAM lParam )
365 WND * wndPtr = WIN_FindWndPtr( hwnd );
366 LRESULT result = 0;
368 if (!wndPtr) return 0;
369 SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
371 switch(msg)
373 case WM_NCCREATE:
375 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
376 if (cs->lpszName)
377 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(cs->lpszName) );
378 result = 1;
380 break;
382 case WM_NCCALCSIZE:
383 result = NC_HandleNCCalcSize(wndPtr, (RECT16 *)PTR_SEG_TO_LIN(lParam));
384 break;
386 case WM_WINDOWPOSCHANGING:
387 result = WINPOS_HandleWindowPosChanging16( wndPtr,
388 (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam) );
389 break;
391 case WM_WINDOWPOSCHANGED:
393 WINDOWPOS16 * winPos = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
394 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
396 break;
398 case WM_GETTEXT:
399 if (wParam && wndPtr->text)
401 lstrcpyn32A( (LPSTR)PTR_SEG_TO_LIN(lParam), wndPtr->text, wParam );
402 result = (LRESULT)strlen( (LPSTR)PTR_SEG_TO_LIN(lParam) );
404 break;
406 case WM_SETTEXT:
407 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(lParam) );
408 NC_HandleNCPaint( hwnd , (HRGN32)1 ); /* Repaint caption */
409 break;
411 default:
412 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
413 break;
416 SPY_ExitMessage( SPY_RESULT_OK16, hwnd, msg, result );
417 return result;
421 /***********************************************************************
422 * DefWindowProc32A (USER32.125)
424 LRESULT DefWindowProc32A( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
425 LPARAM lParam )
427 WND * wndPtr = WIN_FindWndPtr( hwnd );
428 LRESULT result = 0;
430 if (!wndPtr) return 0;
431 SPY_EnterMessage( SPY_DEFWNDPROC32, hwnd, msg, wParam, lParam );
433 switch(msg)
435 case WM_NCCREATE:
437 CREATESTRUCT32A *cs = (CREATESTRUCT32A *)lParam;
438 if (cs->lpszName) DEFWND_SetText( wndPtr, cs->lpszName );
439 result = 1;
441 break;
443 case WM_NCCALCSIZE:
445 RECT16 rect16;
446 CONV_RECT32TO16( (RECT32 *)lParam, &rect16 );
447 result = NC_HandleNCCalcSize( wndPtr, &rect16 );
448 CONV_RECT16TO32( &rect16, (RECT32 *)lParam );
450 break;
452 case WM_WINDOWPOSCHANGING:
453 result = WINPOS_HandleWindowPosChanging32( wndPtr,
454 (WINDOWPOS32 *)lParam );
455 break;
457 case WM_WINDOWPOSCHANGED:
459 WINDOWPOS32 * winPos = (WINDOWPOS32 *)lParam;
460 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
462 break;
464 case WM_GETTEXT:
465 if (wParam && wndPtr->text)
467 lstrcpyn32A( (LPSTR)lParam, wndPtr->text, wParam );
468 result = (LRESULT)strlen( (LPSTR)lParam );
470 break;
472 case WM_SETTEXT:
473 DEFWND_SetText( wndPtr, (LPSTR)lParam );
474 NC_HandleNCPaint( hwnd , (HRGN32)1 ); /* Repaint caption */
475 break;
477 default:
478 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
479 break;
482 SPY_ExitMessage( SPY_RESULT_OK32, hwnd, msg, result );
483 return result;
487 /***********************************************************************
488 * DefWindowProc32W (USER32.126)
490 LRESULT DefWindowProc32W( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
491 LPARAM lParam )
493 LRESULT result;
495 switch(msg)
497 case WM_NCCREATE:
499 CREATESTRUCT32W *cs = (CREATESTRUCT32W *)lParam;
500 if (cs->lpszName)
502 WND *wndPtr = WIN_FindWndPtr( hwnd );
503 LPSTR str = HEAP_strdupWtoA(GetProcessHeap(), 0, cs->lpszName);
504 DEFWND_SetText( wndPtr, str );
505 HeapFree( GetProcessHeap(), 0, str );
507 result = 1;
509 break;
511 case WM_GETTEXT:
513 LPSTR str = HeapAlloc( GetProcessHeap(), 0, wParam );
514 result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
515 lstrcpynAtoW( (LPWSTR)lParam, str, wParam );
516 HeapFree( GetProcessHeap(), 0, str );
518 break;
520 case WM_SETTEXT:
522 LPSTR str = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPWSTR)lParam );
523 result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
524 HeapFree( GetProcessHeap(), 0, str );
526 break;
528 default:
529 result = DefWindowProc32A( hwnd, msg, wParam, lParam );
530 break;
532 return result;