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