Release 960606
[wine.git] / windows / defwnd.c
blobbed900934cc701c9e10aa6d1682a665a9e53499d
1 /*
2 * Default window procedure
4 * Copyright 1993 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 "stddebug.h"
18 /* #define DEBUG_MESSAGE */
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;
33 /***********************************************************************
34 * DEFWND_InitSysMenuPopup
36 * Handle the WM_INITMENUPOPUP message on the system menu.
38 static void DEFWND_InitSysMenuPopup( HMENU hmenu, DWORD style, DWORD clsStyle )
40 BOOL gray;
42 gray = !(style & WS_THICKFRAME) || (style & (WS_MAXIMIZE | WS_MINIMIZE));
43 EnableMenuItem( hmenu, SC_SIZE, (gray ? MF_GRAYED : MF_ENABLED) );
44 gray = ((style & WS_MAXIMIZE) != 0);
45 EnableMenuItem( hmenu, SC_MOVE, (gray ? MF_GRAYED : MF_ENABLED) );
46 gray = !(style & WS_MINIMIZEBOX) || (style & WS_MINIMIZE);
47 EnableMenuItem( hmenu, SC_MINIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
48 gray = !(style & WS_MAXIMIZEBOX) || (style & WS_MAXIMIZE);
49 EnableMenuItem( hmenu, SC_MAXIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
50 gray = !(style & (WS_MAXIMIZE | WS_MINIMIZE));
51 EnableMenuItem( hmenu, SC_RESTORE, (gray ? MF_GRAYED : MF_ENABLED) );
52 gray = (clsStyle & CS_NOCLOSE) != 0;
53 EnableMenuItem( hmenu, SC_CLOSE, (gray ? MF_GRAYED : MF_ENABLED) );
57 /***********************************************************************
58 * DEFWND_HandleWindowPosChanged
60 * Handle the WM_WINDOWPOSCHANGED message.
62 static void DEFWND_HandleWindowPosChanged( WND *wndPtr, UINT32 flags )
64 WPARAM16 wp = SIZE_RESTORED;
66 if (!(flags & SWP_NOCLIENTMOVE))
67 SendMessage16( wndPtr->hwndSelf, WM_MOVE, 0,
68 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top));
69 if (!(flags & SWP_NOCLIENTSIZE))
71 if (wndPtr->dwStyle & WS_MAXIMIZE) wp = SIZE_MAXIMIZED;
72 else if (wndPtr->dwStyle & WS_MINIMIZE) wp = SIZE_MINIMIZED;
74 SendMessage16( wndPtr->hwndSelf, WM_SIZE, wp,
75 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
76 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
81 /***********************************************************************
82 * DEFWND_SetText
84 * Set the window text.
86 void DEFWND_SetText( WND *wndPtr, LPCSTR text )
88 if (!text) text = "";
89 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
90 wndPtr->text = HEAP_strdupA( SystemHeap, 0, text );
91 if (wndPtr->window) XStoreName( display, wndPtr->window, wndPtr->text );
95 /***********************************************************************
96 * DEFWND_DefWinProc
98 * Default window procedure for messages that are the same in Win16 and Win32.
100 static LRESULT DEFWND_DefWinProc( WND *wndPtr, UINT32 msg, WPARAM32 wParam,
101 LPARAM lParam )
103 HWND16 hwnd = wndPtr->hwndSelf;
105 switch(msg)
107 case WM_PAINTICON:
108 case WM_NCPAINT:
109 return NC_HandleNCPaint( hwnd, (HRGN)wParam );
111 case WM_NCHITTEST:
112 return NC_HandleNCHitTest( hwnd, MAKEPOINT16(lParam) );
114 case WM_NCLBUTTONDOWN:
115 return NC_HandleNCLButtonDown( hwnd, wParam, lParam );
117 case WM_LBUTTONDBLCLK:
118 case WM_NCLBUTTONDBLCLK:
119 return NC_HandleNCLButtonDblClk( wndPtr, wParam, lParam );
121 case WM_NCACTIVATE:
122 return NC_HandleNCActivate( hwnd, wParam );
124 case WM_NCDESTROY:
125 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
126 wndPtr->text = NULL;
127 if (wndPtr->hVScroll) USER_HEAP_FREE(wndPtr->hVScroll);
128 if (wndPtr->hHScroll) USER_HEAP_FREE(wndPtr->hHScroll);
129 wndPtr->hVScroll = wndPtr->hHScroll = 0;
130 return 0;
132 case WM_PAINT:
134 PAINTSTRUCT16 paintstruct;
135 BeginPaint16( hwnd, &paintstruct );
136 EndPaint16( hwnd, &paintstruct );
137 return 0;
140 case WM_SETREDRAW:
141 if (!wParam)
143 ValidateRect32( hwnd, NULL );
144 wndPtr->flags |= WIN_NO_REDRAW;
146 else wndPtr->flags &= ~WIN_NO_REDRAW;
147 return 0;
149 case WM_CLOSE:
150 DestroyWindow( hwnd );
151 return 0;
153 case WM_MOUSEACTIVATE:
154 if (wndPtr->dwStyle & WS_CHILD)
156 LONG ret = SendMessage16( wndPtr->parent->hwndSelf, WM_MOUSEACTIVATE,
157 wParam, lParam );
158 if (ret) return ret;
160 return MA_ACTIVATE;
162 case WM_ACTIVATE:
163 if (LOWORD(wParam) != WA_INACTIVE) SetFocus( hwnd );
164 break;
166 case WM_ERASEBKGND:
167 case WM_ICONERASEBKGND:
169 if (!wndPtr->class->hbrBackground) return 0;
170 if (wndPtr->class->hbrBackground <= (HBRUSH)(COLOR_MAX+1))
172 HBRUSH hbrush;
173 hbrush = CreateSolidBrush(
174 GetSysColor(((DWORD)wndPtr->class->hbrBackground)-1));
175 FillWindow( GetParent(hwnd), hwnd, (HDC)wParam, hbrush);
176 DeleteObject (hbrush);
178 else
179 FillWindow( GetParent(hwnd), hwnd, (HDC)wParam,
180 wndPtr->class->hbrBackground );
181 return 1;
184 case WM_GETDLGCODE:
185 return 0;
187 case WM_CTLCOLORMSGBOX:
188 case WM_CTLCOLOREDIT:
189 case WM_CTLCOLORLISTBOX:
190 case WM_CTLCOLORBTN:
191 case WM_CTLCOLORDLG:
192 case WM_CTLCOLORSTATIC:
193 SetBkColor( (HDC)wParam, GetSysColor(COLOR_WINDOW) );
194 SetTextColor( (HDC)wParam, GetSysColor(COLOR_WINDOWTEXT) );
195 return (LRESULT)sysColorObjects.hbrushWindow;
197 case WM_CTLCOLORSCROLLBAR:
198 SetBkColor( (HDC)wParam, RGB(255, 255, 255) );
199 SetTextColor( (HDC)wParam, RGB(0, 0, 0) );
200 UnrealizeObject( sysColorObjects.hbrushScrollbar );
201 return (LRESULT)sysColorObjects.hbrushScrollbar;
203 case WM_CTLCOLOR:
205 if (HIWORD(lParam) == CTLCOLOR_SCROLLBAR)
207 SetBkColor( (HDC)wParam, RGB(255, 255, 255) );
208 SetTextColor( (HDC)wParam, RGB(0, 0, 0) );
209 UnrealizeObject( sysColorObjects.hbrushScrollbar );
210 return (LRESULT)sysColorObjects.hbrushScrollbar;
212 else
214 SetBkColor( (HDC)wParam, GetSysColor(COLOR_WINDOW) );
215 SetTextColor( (HDC)wParam, GetSysColor(COLOR_WINDOWTEXT) );
216 return (LRESULT)sysColorObjects.hbrushWindow;
220 case WM_GETTEXTLENGTH:
221 if (wndPtr->text) return (LRESULT)strlen(wndPtr->text);
222 return 0;
224 case WM_SETCURSOR:
225 if (wndPtr->dwStyle & WS_CHILD)
226 if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
227 wParam, lParam))
228 return TRUE;
229 return NC_HandleSetCursor( hwnd, wParam, lParam );
231 case WM_SYSCOMMAND:
232 return NC_HandleSysCommand( hwnd, wParam, MAKEPOINT16(lParam) );
234 case WM_KEYDOWN:
235 if(wParam == VK_F10) iF10Key = VK_F10;
236 break;
238 case WM_SYSKEYDOWN:
239 if( HIWORD(lParam) & KEYDATA_ALT )
241 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
242 if( wParam == VK_MENU && !iMenuSysKey )
243 iMenuSysKey = 1;
244 else
245 iMenuSysKey = 0;
247 iF10Key = 0;
250 else if( wParam == VK_F10 )
251 iF10Key = 1;
252 else
253 if( wParam == VK_ESCAPE && GetKeyState(VK_SHIFT) < 0 )
254 SendMessage16( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU,
255 (LPARAM)VK_SPACE);
256 break;
258 case WM_KEYUP:
259 case WM_SYSKEYUP:
260 /* Press and release F10 or ALT */
261 if (((wParam == VK_MENU) && iMenuSysKey) ||
262 ((wParam == VK_F10) && iF10Key))
263 SendMessage16( WIN_GetTopParent(hwnd), WM_SYSCOMMAND,
264 SC_KEYMENU, 0L );
265 iMenuSysKey = iF10Key = 0;
266 break;
268 case WM_SYSCHAR:
269 iMenuSysKey = 0;
270 if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
272 PostMessage(hwnd, WM_SYSCOMMAND, (WPARAM)SC_RESTORE, 0L );
273 break;
275 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
277 if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
278 if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
279 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
280 else
281 SendMessage16( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)(DWORD)wParam );
283 else /* check for Ctrl-Esc */
284 if (wParam != VK_ESCAPE) MessageBeep(0);
285 break;
287 case WM_SHOWWINDOW:
288 if (!lParam) return 0; /* sent from ShowWindow */
289 if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
290 if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
291 else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
292 ShowWindow( hwnd, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
293 break;
295 case WM_INITMENUPOPUP:
296 /* Not absolutely sure this belongs here -- AJ */
297 if (HIWORD(lParam)) /* system menu */
298 DEFWND_InitSysMenuPopup( (HMENU)wParam, wndPtr->dwStyle,
299 wndPtr->class->style );
300 break;
302 case WM_CANCELMODE:
303 /* EndMenu() should be called if in menu state but currently it's
304 impossible to detect - menu code should be updated*/
305 if (GetCapture() == hwnd) ReleaseCapture();
306 break;
308 case WM_VKEYTOITEM:
309 case WM_CHARTOITEM:
310 return -1;
312 case WM_DROPOBJECT:
313 return DRAG_FILE;
315 case WM_QUERYDROPOBJECT:
316 if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
317 break;
319 case WM_QUERYDRAGICON:
321 HICON hI = 0;
322 UINT16 len = 1;
323 while(len < 64)
324 if( (hI = LoadIcon(wndPtr->hInstance,MAKEINTRESOURCE(len))) )
325 return (LRESULT)hI;
327 break;
329 case WM_QUERYOPEN:
330 case WM_QUERYENDSESSION:
331 return 1;
333 return 0;
338 /***********************************************************************
339 * DefWindowProc16 (USER.107)
341 LRESULT DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
342 LPARAM lParam )
344 WND * wndPtr = WIN_FindWndPtr( hwnd );
345 LRESULT result = 0;
347 SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
349 switch(msg)
351 case WM_NCCREATE:
353 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
354 if (cs->lpszName)
355 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(cs->lpszName) );
356 result = 1;
358 break;
360 case WM_NCCALCSIZE:
361 result = NC_HandleNCCalcSize(wndPtr, (RECT16 *)PTR_SEG_TO_LIN(lParam));
362 break;
364 case WM_WINDOWPOSCHANGING:
365 result = WINPOS_HandleWindowPosChanging16( wndPtr,
366 (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam) );
367 break;
369 case WM_WINDOWPOSCHANGED:
371 WINDOWPOS16 * winPos = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
372 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
374 break;
376 case WM_GETTEXT:
377 if (wParam && wndPtr->text)
379 lstrcpyn( (LPSTR)PTR_SEG_TO_LIN(lParam), wndPtr->text, wParam );
380 result = (LRESULT)strlen( (LPSTR)PTR_SEG_TO_LIN(lParam) ) + 1;
382 break;
384 case WM_SETTEXT:
385 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(lParam) );
386 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
387 break;
389 default:
390 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
391 break;
394 SPY_ExitMessage( SPY_RESULT_OK16, hwnd, msg, result );
395 return result;
399 /***********************************************************************
400 * DefWindowProc32A (USER32.125)
402 LRESULT DefWindowProc32A( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
403 LPARAM lParam )
405 WND * wndPtr = WIN_FindWndPtr( hwnd );
406 LRESULT result = 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 lstrcpyn( (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;
487 case WM_GETTEXT:
489 LPSTR str = malloc( wParam );
490 result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
491 STRING32_AnsiToUni( (LPWSTR)lParam, str );
492 free( str );
494 break;
496 case WM_SETTEXT:
498 LPSTR str = STRING32_DupUniToAnsi( (LPWSTR)lParam );
499 result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
500 free( str );
502 break;
504 default:
505 result = DefWindowProc32A( hwnd, msg, wParam, lParam );
506 break;
508 return result;