Release 951105
[wine] / windows / defwnd.c
1 /*
2  * Default window procedure
3  *
4  * Copyright 1993 Alexandre Julliard
5  */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include "win.h"
10 #include "class.h"
11 #include "user.h"
12 #include "nonclient.h"
13 #include "winpos.h"
14 #include "syscolor.h"
15 #include "stddebug.h"
16 /* #define DEBUG_MESSAGE */
17 #include "debug.h"
18 #include "spy.h"
19
20   /* Last COLOR id */
21 #define COLOR_MAX   COLOR_BTNHIGHLIGHT
22
23
24 /***********************************************************************
25  *           DEFWND_SetText
26  *
27  * Set the window text.
28  */
29 void DEFWND_SetText( HWND hwnd, LPSTR text )
30 {
31     LPSTR textPtr;
32     WND *wndPtr = WIN_FindWndPtr( hwnd );
33
34     if (!text) text = "";
35     if (wndPtr->hText) USER_HEAP_FREE( wndPtr->hText );
36     wndPtr->hText = USER_HEAP_ALLOC( strlen(text) + 1 );
37     textPtr = (LPSTR) USER_HEAP_LIN_ADDR( wndPtr->hText );
38     strcpy( textPtr, text );
39 }
40
41
42 /***********************************************************************
43  *           DefWindowProc   (USER.107)
44  */
45 LRESULT DefWindowProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
46 {
47     CLASS * classPtr;
48     LPSTR textPtr;
49     int len;
50     WND * wndPtr = WIN_FindWndPtr( hwnd );
51
52     EnterSpyMessage(SPY_DEFWNDPROC,hwnd,msg,wParam,lParam);
53
54     switch(msg)
55     {
56     case WM_NCCREATE:
57         {
58             CREATESTRUCT *createStruct = (CREATESTRUCT*)PTR_SEG_TO_LIN(lParam);
59             if (createStruct->lpszName)
60                 DEFWND_SetText( hwnd,
61                                (LPSTR)PTR_SEG_TO_LIN(createStruct->lpszName) );
62             return 1;
63         }
64
65     case WM_NCCALCSIZE:
66         return NC_HandleNCCalcSize( hwnd,
67                                  (NCCALCSIZE_PARAMS *)PTR_SEG_TO_LIN(lParam) );
68
69     case WM_PAINTICON: 
70     case WM_NCPAINT:
71         return NC_HandleNCPaint( hwnd );
72
73     case WM_NCHITTEST:
74         {
75             POINT pt = { LOWORD(lParam), HIWORD(lParam) };
76             return NC_HandleNCHitTest( hwnd, pt );
77         }
78
79     case WM_NCLBUTTONDOWN:
80         return NC_HandleNCLButtonDown( hwnd, wParam, lParam );
81
82     case WM_LBUTTONDBLCLK:
83     case WM_NCLBUTTONDBLCLK:
84         return NC_HandleNCLButtonDblClk( hwnd, wParam, lParam );
85
86     case WM_NCACTIVATE:
87         return NC_HandleNCActivate( hwnd, wParam );
88
89     case WM_NCDESTROY:
90         if (wndPtr->hText) USER_HEAP_FREE(wndPtr->hText);
91         if (wndPtr->hVScroll) USER_HEAP_FREE(wndPtr->hVScroll);
92         if (wndPtr->hHScroll) USER_HEAP_FREE(wndPtr->hHScroll);
93         wndPtr->hText = wndPtr->hVScroll = wndPtr->hHScroll = 0;
94         return 0;
95         
96     case WM_PAINT:
97         {
98             PAINTSTRUCT paintstruct;
99             BeginPaint( hwnd, &paintstruct );
100             EndPaint( hwnd, &paintstruct );
101             return 0;
102         }
103
104     case WM_SETREDRAW:
105         if (!wParam)
106         {
107             ValidateRect( hwnd, NULL );
108             wndPtr->flags |= WIN_NO_REDRAW;
109         }
110         else wndPtr->flags &= ~WIN_NO_REDRAW;
111         return 0;
112
113     case WM_CLOSE:
114         DestroyWindow( hwnd );
115         return 0;
116
117     case WM_MOUSEACTIVATE:
118         if (wndPtr->dwStyle & WS_CHILD)
119         {
120             LONG ret = SendMessage( wndPtr->hwndParent, WM_MOUSEACTIVATE,
121                                     wParam, lParam );
122             if (ret) return ret;
123         }
124         return MA_ACTIVATE;
125
126     case WM_ACTIVATE:
127       /* LOWORD() needed for WINELIB32 implementation.  Should be fine. */
128         if (LOWORD(wParam)!=WA_INACTIVE) SetFocus( hwnd );
129         break;
130
131     case WM_WINDOWPOSCHANGING:
132         return WINPOS_HandleWindowPosChanging( (WINDOWPOS *)PTR_SEG_TO_LIN(lParam) );
133
134     case WM_WINDOWPOSCHANGED:
135         {
136             WINDOWPOS * winPos = (WINDOWPOS *)PTR_SEG_TO_LIN(lParam);
137             if (!(winPos->flags & SWP_NOMOVE))
138                 SendMessage( hwnd, WM_MOVE, 0,
139                              MAKELONG( wndPtr->rectClient.left,
140                                        wndPtr->rectClient.top ));
141             if (!(winPos->flags & SWP_NOSIZE))
142                 SendMessage( hwnd, WM_SIZE, SIZE_RESTORED,
143                    MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
144                             wndPtr->rectClient.bottom-wndPtr->rectClient.top));
145             return 0;
146         }
147
148     case WM_ERASEBKGND:
149     case WM_ICONERASEBKGND:
150         {
151             if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return 0;
152             if (!classPtr->wc.hbrBackground) return 0;
153             if (classPtr->wc.hbrBackground <= (HBRUSH)(COLOR_MAX+1))
154             {
155                  HBRUSH hbrush;
156                  hbrush = CreateSolidBrush(
157                      GetSysColor(((DWORD)classPtr->wc.hbrBackground)-1));
158                  FillWindow( GetParent(hwnd), hwnd, (HDC)wParam, hbrush);
159                  DeleteObject (hbrush);
160             }
161             else
162                  FillWindow( GetParent(hwnd), hwnd, (HDC)wParam,
163                         classPtr->wc.hbrBackground );
164             return 1;
165         }
166
167     case WM_GETDLGCODE:
168         return 0;
169
170     case WM_CTLCOLORMSGBOX:
171     case WM_CTLCOLOREDIT:
172     case WM_CTLCOLORLISTBOX:
173     case WM_CTLCOLORBTN:
174     case WM_CTLCOLORDLG:
175     case WM_CTLCOLORSTATIC:
176         SetBkColor( (HDC)wParam, GetSysColor(COLOR_WINDOW) );
177         SetTextColor( (HDC)wParam, GetSysColor(COLOR_WINDOWTEXT) );
178         return (LONG)sysColorObjects.hbrushWindow;
179
180     case WM_CTLCOLORSCROLLBAR:
181         SetBkColor( (HDC)wParam, RGB(255, 255, 255) );
182         SetTextColor( (HDC)wParam, RGB(0, 0, 0) );
183         UnrealizeObject( sysColorObjects.hbrushScrollbar );
184         return (LONG)sysColorObjects.hbrushScrollbar;
185
186     case WM_CTLCOLOR:
187         {
188             if (HIWORD(lParam) == CTLCOLOR_SCROLLBAR)
189             {
190                 SetBkColor( (HDC)wParam, RGB(255, 255, 255) );
191                 SetTextColor( (HDC)wParam, RGB(0, 0, 0) );
192                 UnrealizeObject( sysColorObjects.hbrushScrollbar );
193                 return (LONG)sysColorObjects.hbrushScrollbar;
194             }
195             else
196             {
197                 SetBkColor( (HDC)wParam, GetSysColor(COLOR_WINDOW) );
198                 SetTextColor( (HDC)wParam, GetSysColor(COLOR_WINDOWTEXT) );
199                 return (LONG)sysColorObjects.hbrushWindow;
200             }
201         }
202         
203     case WM_GETTEXT:
204         {
205             if (wParam)
206             {
207                 if (wndPtr->hText)
208                 {
209                     textPtr = (LPSTR)USER_HEAP_LIN_ADDR(wndPtr->hText);
210                     if ((int)wParam > (len = strlen(textPtr)))
211                     {
212                         strcpy((char *)PTR_SEG_TO_LIN(lParam), textPtr);
213                         return (DWORD)len;
214                     }
215                 }
216             }
217             return 0;
218         }
219
220     case WM_GETTEXTLENGTH:
221         {
222             if (wndPtr->hText)
223             {
224                 textPtr = (LPSTR)USER_HEAP_LIN_ADDR(wndPtr->hText);
225                 return (DWORD)strlen(textPtr);
226             }
227             return 0;
228         }
229
230     case WM_SETTEXT:
231         DEFWND_SetText( hwnd, (LPSTR)PTR_SEG_TO_LIN(lParam) );
232         NC_HandleNCPaint( hwnd );  /* Repaint caption */
233         return 0;
234
235     case WM_SETCURSOR:
236         if (wndPtr->dwStyle & WS_CHILD)
237             if (SendMessage(wndPtr->hwndParent, WM_SETCURSOR, wParam, lParam))
238                 return TRUE;
239         return NC_HandleSetCursor( hwnd, wParam, lParam );
240
241     case WM_SYSCOMMAND:
242         {
243             POINT pt = { LOWORD(lParam), HIWORD(lParam) };
244             return NC_HandleSysCommand( hwnd, wParam, pt );
245         }
246
247     case WM_SYSKEYDOWN:
248         if (wParam == VK_MENU)
249         {   /* Send to WS_OVERLAPPED parent. TODO: Handle MDI */
250             SendMessage( WIN_GetTopParent(hwnd), WM_SYSCOMMAND,
251                          SC_KEYMENU, 0L );
252         }
253         break;
254
255     case WM_SYSKEYUP:
256         break;
257     }
258     return 0;
259 }