Fix the Z-order of maximized/minimized child windows.
[wine] / windows / defwnd.c
1 /*
2  * Default window procedure
3  *
4  * Copyright 1993, 1996 Alexandre Julliard
5  *           1995 Alex Korobka
6  */
7
8 #include <stdlib.h>
9 #include "win.h"
10 #include "user.h"
11 #include "heap.h"
12 #include "nonclient.h"
13 #include "winpos.h"
14 #include "dce.h"
15 #include "sysmetrics.h"
16 #include "debug.h"
17 #include "spy.h"
18 #include "tweak.h"
19
20   /* Last COLOR id */
21 #define COLOR_MAX   COLOR_BTNHIGHLIGHT
22
23   /* bits in the dwKeyData */
24 #define KEYDATA_ALT             0x2000
25 #define KEYDATA_PREVSTATE       0x4000
26
27 static short iF10Key = 0;
28 static short iMenuSysKey = 0;
29
30 /***********************************************************************
31  *           DEFWND_HandleWindowPosChanged
32  *
33  * Handle the WM_WINDOWPOSCHANGED message.
34  */
35 static void DEFWND_HandleWindowPosChanged( WND *wndPtr, UINT32 flags )
36 {
37     WPARAM16 wp = SIZE_RESTORED;
38
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))
43     {
44         if (wndPtr->dwStyle & WS_MAXIMIZE) wp = SIZE_MAXIMIZED;
45         else if (wndPtr->dwStyle & WS_MINIMIZE) wp = SIZE_MINIMIZED;
46
47         SendMessage16( wndPtr->hwndSelf, WM_SIZE, wp, 
48                      MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
49                             wndPtr->rectClient.bottom-wndPtr->rectClient.top));
50     }
51 }
52
53
54 /***********************************************************************
55  *           DEFWND_SetText
56  *
57  * Set the window text.
58  */
59 void DEFWND_SetText( WND *wndPtr, LPCSTR text )
60 {
61     if (!text) text = "";
62     if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
63     wndPtr->text = HEAP_strdupA( SystemHeap, 0, text );    
64     wndPtr->pDriver->pSetText(wndPtr, wndPtr->text);
65 }
66
67 /***********************************************************************
68  *           DEFWND_ControlColor
69  *
70  * Default colors for control painting.
71  */
72 HBRUSH32 DEFWND_ControlColor( HDC32 hDC, UINT16 ctlType )
73 {
74     if( ctlType == CTLCOLOR_SCROLLBAR)
75     {
76         HBRUSH32 hb = GetSysColorBrush32(COLOR_SCROLLBAR);
77         SetBkColor32( hDC, RGB(255, 255, 255) );
78         SetTextColor32( hDC, RGB(0, 0, 0) );
79         UnrealizeObject32( hb );
80         return hb;
81     }
82
83     SetTextColor32( hDC, GetSysColor32(COLOR_WINDOWTEXT));
84
85     if (TWEAK_WineLook > WIN31_LOOK) {
86         if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
87             SetBkColor32( hDC, GetSysColor32(COLOR_WINDOW) );
88         else {
89             SetBkColor32( hDC, GetSysColor32(COLOR_3DFACE) );
90             return GetSysColorBrush32(COLOR_3DFACE);
91         }
92     }
93     else
94         SetBkColor32( hDC, GetSysColor32(COLOR_WINDOW) );
95     return GetSysColorBrush32(COLOR_WINDOW);
96 }
97
98
99 /***********************************************************************
100  *           DEFWND_SetRedraw
101  */
102 static void DEFWND_SetRedraw( WND* wndPtr, WPARAM32 wParam )
103 {
104     BOOL32 bVisible = wndPtr->dwStyle & WS_VISIBLE;
105
106     TRACE(win,"%04x %i\n", wndPtr->hwndSelf, (wParam!=0) );
107
108     if( wParam )
109     {
110         if( !bVisible )
111         {
112             wndPtr->dwStyle |= WS_VISIBLE;
113             DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
114         }
115     }
116     else if( bVisible )
117     {
118         if( wndPtr->dwStyle & WS_MINIMIZE ) wParam = RDW_VALIDATE;
119         else wParam = RDW_ALLCHILDREN | RDW_VALIDATE;
120
121         PAINT_RedrawWindow( wndPtr->hwndSelf, NULL, 0, wParam, 0 );
122         DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
123         wndPtr->dwStyle &= ~WS_VISIBLE;
124     }
125 }
126
127 /***********************************************************************
128  *           DEFWND_DefWinProc
129  *
130  * Default window procedure for messages that are the same in Win16 and Win32.
131  */
132 static LRESULT DEFWND_DefWinProc( WND *wndPtr, UINT32 msg, WPARAM32 wParam,
133                                   LPARAM lParam )
134 {
135     switch(msg)
136     {
137     case WM_NCPAINT:
138         return NC_HandleNCPaint( wndPtr->hwndSelf, (HRGN32)wParam );
139
140     case WM_NCHITTEST:
141         return NC_HandleNCHitTest( wndPtr->hwndSelf, MAKEPOINT16(lParam) );
142
143     case WM_NCLBUTTONDOWN:
144         return NC_HandleNCLButtonDown( wndPtr, wParam, lParam );
145
146     case WM_LBUTTONDBLCLK:
147     case WM_NCLBUTTONDBLCLK:
148         return NC_HandleNCLButtonDblClk( wndPtr, wParam, lParam );
149
150     case WM_RBUTTONDOWN:
151     case WM_NCRBUTTONDOWN:
152         if ((wndPtr->flags & WIN_ISWIN32) || (TWEAK_WineLook > WIN31_LOOK))
153         {
154             ClientToScreen16(wndPtr->hwndSelf, (LPPOINT16)&lParam);
155             SendMessage32A( wndPtr->hwndSelf, WM_CONTEXTMENU,
156                             wndPtr->hwndSelf, lParam);
157         }
158         break;
159
160     case WM_CONTEXTMENU:
161         if( wndPtr->dwStyle & WS_CHILD )
162             SendMessage32A( wndPtr->parent->hwndSelf, msg, wParam, lParam );
163         else
164           if (wndPtr->hSysMenu)
165           { /*
166             TrackPopupMenu32(wndPtr->hSysMenu,TPM_LEFTALIGN | TPM_RETURNCMD,LOWORD(lParam),HIWORD(lParam),0,wndPtr->hwndSelf,NULL);
167             DestroyMenu32(wndPtr->hSysMenu);
168             */
169             FIXME(win,"Display default popup menu\n");
170           /* Track system popup if click was in the caption area. */
171           }
172         break;
173
174     case WM_NCACTIVATE:
175         return NC_HandleNCActivate( wndPtr, wParam );
176
177     case WM_NCDESTROY:
178         if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
179         wndPtr->text = NULL;
180         if (wndPtr->pVScroll) HeapFree( SystemHeap, 0, wndPtr->pVScroll );
181         if (wndPtr->pHScroll) HeapFree( SystemHeap, 0, wndPtr->pHScroll );
182         wndPtr->pVScroll = wndPtr->pHScroll = NULL;
183         return 0;
184
185     case WM_PAINTICON:
186     case WM_PAINT:
187         {
188             PAINTSTRUCT16 ps;
189             HDC16 hdc = BeginPaint16( wndPtr->hwndSelf, &ps );
190             if( hdc ) 
191             {
192               if( (wndPtr->dwStyle & WS_MINIMIZE) && wndPtr->class->hIcon )
193               {
194                 int x = (wndPtr->rectWindow.right - wndPtr->rectWindow.left -
195                         SYSMETRICS_CXICON)/2;
196                 int y = (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top -
197                         SYSMETRICS_CYICON)/2;
198                 TRACE(win,"Painting class icon: vis rect=(%i,%i - %i,%i)\n",
199                 ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
200                 DrawIcon32( hdc, x, y, wndPtr->class->hIcon );
201               }
202               EndPaint16( wndPtr->hwndSelf, &ps );
203             }
204             return 0;
205         }
206
207     case WM_SETREDRAW:
208         DEFWND_SetRedraw( wndPtr, wParam );
209         return 0;
210
211     case WM_CLOSE:
212         DestroyWindow32( wndPtr->hwndSelf );
213         return 0;
214
215     case WM_MOUSEACTIVATE:
216         if (wndPtr->dwStyle & WS_CHILD)
217         {
218             LONG ret = SendMessage16( wndPtr->parent->hwndSelf,
219                                       WM_MOUSEACTIVATE, wParam, lParam );
220             if (ret) return ret;
221         }
222
223         /* Caption clicks are handled by the NC_HandleNCLButtonDown() */ 
224         return (LOWORD(lParam) == HTCAPTION) ? MA_NOACTIVATE : MA_ACTIVATE;
225
226     case WM_ACTIVATE:
227         if (LOWORD(wParam) != WA_INACTIVE) 
228                 SetWindowPos32(wndPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
229                          SWP_NOMOVE | SWP_NOSIZE);
230         break;
231
232     case WM_ERASEBKGND:
233     case WM_ICONERASEBKGND:
234         {
235             if (!wndPtr->class->hbrBackground) return 0;
236
237             if (wndPtr->class->hbrBackground <= (HBRUSH16)(COLOR_MAX+1))
238             {
239                 HBRUSH32 hbrush = CreateSolidBrush32( 
240                        GetSysColor32(((DWORD)wndPtr->class->hbrBackground)-1));
241                  FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
242                              (HDC16)wParam, hbrush);
243                  DeleteObject32( hbrush );
244             }
245             else FillWindow( GetParent16(wndPtr->hwndSelf), wndPtr->hwndSelf,
246                              (HDC16)wParam, wndPtr->class->hbrBackground );
247             return 1;
248         }
249
250     case WM_GETDLGCODE:
251         return 0;
252
253     case WM_CTLCOLORMSGBOX:
254     case WM_CTLCOLOREDIT:
255     case WM_CTLCOLORLISTBOX:
256     case WM_CTLCOLORBTN:
257     case WM_CTLCOLORDLG:
258     case WM_CTLCOLORSTATIC:
259     case WM_CTLCOLORSCROLLBAR:
260         return (LRESULT)DEFWND_ControlColor( (HDC32)wParam, msg - WM_CTLCOLORMSGBOX );
261
262     case WM_CTLCOLOR:
263         return (LRESULT)DEFWND_ControlColor( (HDC32)wParam, HIWORD(lParam) );
264         
265     case WM_GETTEXTLENGTH:
266         if (wndPtr->text) return (LRESULT)strlen(wndPtr->text);
267         return 0;
268
269     case WM_SETCURSOR:
270         if (wndPtr->dwStyle & WS_CHILD)
271             if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
272                             wParam, lParam))
273                 return TRUE;
274         return NC_HandleSetCursor( wndPtr->hwndSelf, wParam, lParam );
275
276     case WM_SYSCOMMAND:
277         return NC_HandleSysCommand( wndPtr->hwndSelf, wParam,
278                                     MAKEPOINT16(lParam) );
279
280     case WM_KEYDOWN:
281         if(wParam == VK_F10) iF10Key = VK_F10;
282         break;
283
284     case WM_SYSKEYDOWN:
285         if( HIWORD(lParam) & KEYDATA_ALT )
286         {
287             /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
288               if( wParam == VK_MENU && !iMenuSysKey )
289                 iMenuSysKey = 1;
290               else
291                 iMenuSysKey = 0;
292             
293             iF10Key = 0;
294
295             if( wParam == VK_F4 )       /* try to close the window */
296             {
297                 HWND32 hWnd = WIN_GetTopParent( wndPtr->hwndSelf );
298                 wndPtr = WIN_FindWndPtr( hWnd );
299                 if( wndPtr && !(wndPtr->class->style & CS_NOCLOSE) )
300                     PostMessage16( hWnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
301             }
302         } 
303         else if( wParam == VK_F10 )
304                 iF10Key = 1;
305              else
306                 if( wParam == VK_ESCAPE && (GetKeyState32(VK_SHIFT) & 0x8000))
307                     SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
308                                   (WPARAM16)SC_KEYMENU, (LPARAM)VK_SPACE);
309         break;
310
311     case WM_KEYUP:
312     case WM_SYSKEYUP:
313         /* Press and release F10 or ALT */
314         if (((wParam == VK_MENU) && iMenuSysKey) ||
315             ((wParam == VK_F10) && iF10Key))
316               SendMessage16( WIN_GetTopParent(wndPtr->hwndSelf),
317                              WM_SYSCOMMAND, SC_KEYMENU, 0L );
318         iMenuSysKey = iF10Key = 0;
319         break;
320
321     case WM_SYSCHAR:
322         iMenuSysKey = 0;
323         if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
324         {
325             PostMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
326                            (WPARAM16)SC_RESTORE, 0L ); 
327             break;
328         } 
329         if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
330         {
331             if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
332             if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
333                 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
334             else
335                 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
336                                (WPARAM16)SC_KEYMENU, (LPARAM)(DWORD)wParam );
337         } 
338         else /* check for Ctrl-Esc */
339             if (wParam != VK_ESCAPE) MessageBeep32(0);
340         break;
341
342     case WM_SHOWWINDOW:
343         if (!lParam) return 0; /* sent from ShowWindow */
344         if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
345         if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
346         else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
347         ShowWindow32( wndPtr->hwndSelf, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
348         break; 
349
350     case WM_CANCELMODE:
351         if (wndPtr->parent == WIN_GetDesktop()) EndMenu();
352         if (GetCapture32() == wndPtr->hwndSelf) ReleaseCapture();
353         break;
354
355     case WM_VKEYTOITEM:
356     case WM_CHARTOITEM:
357         return -1;
358
359     case WM_DROPOBJECT:
360         return DRAG_FILE;  
361
362     case WM_QUERYDROPOBJECT:
363         if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
364         break;
365
366     case WM_QUERYDRAGICON:
367         {
368             HICON16 hIcon=0;
369             UINT16 len;
370
371             if( (hIcon=wndPtr->class->hCursor) ) return (LRESULT)hIcon;
372             for(len=1; len<64; len++)
373                 if((hIcon=LoadIcon16(wndPtr->hInstance,MAKEINTRESOURCE16(len))))
374                     return (LRESULT)hIcon;
375             return (LRESULT)LoadIcon16(0,IDI_APPLICATION16);
376         }
377         break;
378
379     case WM_ISACTIVEICON:
380         return ((wndPtr->flags & WIN_NCACTIVATED) != 0);
381
382     case WM_QUERYOPEN:
383     case WM_QUERYENDSESSION:
384         return 1;
385     }
386     return 0;
387 }
388
389
390
391 /***********************************************************************
392  *           DefWindowProc16   (USER.107)
393  */
394 LRESULT WINAPI DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
395                                 LPARAM lParam )
396 {
397     WND * wndPtr = WIN_FindWndPtr( hwnd );
398     LRESULT result = 0;
399
400     if (!wndPtr) return 0;
401     SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
402
403     switch(msg)
404     {
405     case WM_NCCREATE:
406         {
407             CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
408             if (cs->lpszName)
409                 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(cs->lpszName) );
410             result = 1;
411         }
412         break;
413
414     case WM_NCCALCSIZE:
415         {
416             RECT32 rect32;
417             CONV_RECT16TO32( (RECT16 *)PTR_SEG_TO_LIN(lParam), &rect32 );
418             result = NC_HandleNCCalcSize( wndPtr, &rect32 );
419             CONV_RECT32TO16( &rect32, (RECT16 *)PTR_SEG_TO_LIN(lParam) );
420         }
421         break;
422
423     case WM_WINDOWPOSCHANGING:
424         result = WINPOS_HandleWindowPosChanging16( wndPtr,
425                                        (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam) );
426         break;
427
428     case WM_WINDOWPOSCHANGED:
429         {
430             WINDOWPOS16 * winPos = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
431             DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
432         }
433         break;
434
435     case WM_GETTEXT:
436         if (wParam && wndPtr->text)
437         {
438             lstrcpyn32A( (LPSTR)PTR_SEG_TO_LIN(lParam), wndPtr->text, wParam );
439             result = (LRESULT)strlen( (LPSTR)PTR_SEG_TO_LIN(lParam) );
440         }
441         break;
442
443     case WM_SETTEXT:
444         DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(lParam) );
445         if( wndPtr->dwStyle & WS_CAPTION ) NC_HandleNCPaint( hwnd , (HRGN32)1 );
446         break;
447
448     default:
449         result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
450         break;
451     }
452
453     SPY_ExitMessage( SPY_RESULT_DEFWND16, hwnd, msg, result );
454     return result;
455 }
456
457
458 /***********************************************************************
459  *  DefWindowProc32A [USER32.126] 
460  *
461  */
462 LRESULT WINAPI DefWindowProc32A( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
463                                  LPARAM lParam )
464 {
465     WND * wndPtr = WIN_FindWndPtr( hwnd );
466     LRESULT result = 0;
467
468     if (!wndPtr) return 0;
469     SPY_EnterMessage( SPY_DEFWNDPROC32, hwnd, msg, wParam, lParam );
470
471     switch(msg)
472     {
473     case WM_NCCREATE:
474         {
475             CREATESTRUCT32A *cs = (CREATESTRUCT32A *)lParam;
476             if (cs->lpszName) DEFWND_SetText( wndPtr, cs->lpszName );
477             result = 1;
478         }
479         break;
480
481     case WM_NCCALCSIZE:
482         result = NC_HandleNCCalcSize( wndPtr, (RECT32 *)lParam );
483         break;
484
485     case WM_WINDOWPOSCHANGING:
486         result = WINPOS_HandleWindowPosChanging32( wndPtr,
487                                                    (WINDOWPOS32 *)lParam );
488         break;
489
490     case WM_WINDOWPOSCHANGED:
491         {
492             WINDOWPOS32 * winPos = (WINDOWPOS32 *)lParam;
493             DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
494         }
495         break;
496
497     case WM_GETTEXT:
498         if (wParam && wndPtr->text)
499         {
500             lstrcpyn32A( (LPSTR)lParam, wndPtr->text, wParam );
501             result = (LRESULT)strlen( (LPSTR)lParam );
502         }
503         break;
504
505     case WM_SETTEXT:
506         DEFWND_SetText( wndPtr, (LPSTR)lParam );
507         NC_HandleNCPaint( hwnd , (HRGN32)1 );  /* Repaint caption */
508         break;
509
510     default:
511         result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
512         break;
513     }
514
515     SPY_ExitMessage( SPY_RESULT_DEFWND32, hwnd, msg, result );
516     return result;
517 }
518
519
520 /***********************************************************************
521  * DefWindowProc32W [USER32.127] Calls default window message handler
522  * 
523  * Calls default window procedure for messages not processed 
524  *  by application.
525  *
526  *  RETURNS
527  *     Return value is dependent upon the message.
528 */
529 LRESULT WINAPI DefWindowProc32W( 
530     HWND32 hwnd,      /* [in] window procedure recieving message */
531     UINT32 msg,       /* [in] message identifier */
532     WPARAM32 wParam,  /* [in] first message parameter */
533     LPARAM lParam )   /* [in] second message parameter */
534 {
535     LRESULT result;
536
537     switch(msg)
538     {
539     case WM_NCCREATE:
540         {
541             CREATESTRUCT32W *cs = (CREATESTRUCT32W *)lParam;
542             if (cs->lpszName)
543             {
544                 WND *wndPtr = WIN_FindWndPtr( hwnd );
545                 LPSTR str = HEAP_strdupWtoA(GetProcessHeap(), 0, cs->lpszName);
546                 DEFWND_SetText( wndPtr, str );
547                 HeapFree( GetProcessHeap(), 0, str );
548             }
549             result = 1;
550         }
551         break;
552
553     case WM_GETTEXT:
554         {
555             LPSTR str = HeapAlloc( GetProcessHeap(), 0, wParam );
556             result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
557             lstrcpynAtoW( (LPWSTR)lParam, str, wParam );
558             HeapFree( GetProcessHeap(), 0, str );
559         }
560         break;
561
562     case WM_SETTEXT:
563         {
564             LPSTR str = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPWSTR)lParam );
565             result = DefWindowProc32A( hwnd, msg, wParam, (LPARAM)str );
566             HeapFree( GetProcessHeap(), 0, str );
567         }
568         break;
569
570     default:
571         result = DefWindowProc32A( hwnd, msg, wParam, lParam );
572         break;
573     }
574     return result;
575 }