Release 0.4.10
[wine] / controls / static.c
1 /*
2  * Static control
3  *
4  * Copyright  David W. Metcalfe, 1993
5  *
6  */
7
8 static char Copyright[] = "Copyright  David W. Metcalfe, 1993";
9
10 #include <windows.h>
11 #include "win.h"
12 #include "user.h"
13
14 LONG StaticWndProc(HWND hWnd, WORD uMsg, WORD wParam, LONG lParam);
15
16 static LONG PaintTextfn(HWND hwnd);
17 static LONG PaintRectfn(HWND hwnd);
18 static LONG PaintFramefn(HWND hwnd);
19 static LONG PaintIconfn(HWND hwnd);
20
21
22 static COLORREF color_windowframe, color_background, color_window,
23                                                      color_windowtext;
24
25 #define NOTIFY_PARENT(hWndCntrl, wNotifyCode) \
26         SendMessage(GetParent(hWndCntrl), WM_COMMAND, \
27                     GetDlgCtrlID(hWndCntrl), MAKELPARAM(hWndCntrl, wNotifyCode));
28 #define DIM(array)      ((sizeof array)/(sizeof array[0]))
29
30 typedef struct
31 {
32     LONG (*paintfn)();
33 } STATICFN;
34
35 #define MAX_STATIC_TYPE  12
36
37 static STATICFN staticfn[MAX_STATIC_TYPE] =
38 {
39     { (LONG(*)())PaintTextfn },                    /* SS_LEFT */
40     { (LONG(*)())PaintTextfn },                    /* SS_CENTER */
41     { (LONG(*)())PaintTextfn },                    /* SS_RIGHT */
42     { (LONG(*)())PaintIconfn },                    /* SS_ICON */
43     { (LONG(*)())PaintRectfn },                    /* SS_BLACKRECT */
44     { (LONG(*)())PaintRectfn },                    /* SS_GRAYRECT */
45     { (LONG(*)())PaintRectfn },                    /* SS_WHITERECT */
46     { (LONG(*)())PaintFramefn },                   /* SS_BLACKFRAME */
47     { (LONG(*)())PaintFramefn },                   /* SS_GRAYFRAME */
48     { (LONG(*)())PaintFramefn },                   /* SS_WHITEFRAME */
49     { (LONG(*)())PaintTextfn },                    /* SS_SIMPLE */
50     { (LONG(*)())PaintTextfn }                     /* SS_LEFTNOWORDWRAP */
51 };
52
53
54 LONG StaticWndProc(HWND hWnd, WORD uMsg, WORD wParam, LONG lParam)
55 {
56         LONG lResult = 0;
57         HDC hDC;
58         RECT rc;
59         LPSTR textPtr;
60
61         WND *wndPtr = WIN_FindWndPtr(hWnd);
62         LONG style = wndPtr->dwStyle & 0x0000000F;
63
64         switch (uMsg) {
65         case WM_ENABLE:
66             InvalidateRect(hWnd, NULL, FALSE);
67             break;
68
69         case WM_CREATE:
70             if (style < 0L || style >= (LONG)DIM(staticfn)) {
71                 lResult = -1L;
72                 break;
73                 }
74             /* initialise colours */
75             color_windowframe  = GetSysColor(COLOR_WINDOWFRAME);
76             color_background   = GetSysColor(COLOR_BACKGROUND);
77             color_window       = GetSysColor(COLOR_WINDOW);
78             color_windowtext   = GetSysColor(COLOR_WINDOWTEXT);
79             lResult = 0L;
80             if (style == SS_ICON) {
81 /*
82                 SetWindowPos(hWnd, (HWND)NULL, 0, 0, 32, 32,
83                                 SWP_NOZORDER | SWP_NOMOVE);
84 */
85                 }
86             break;
87
88         case WM_PAINT:
89             if (staticfn[style].paintfn)
90                 (staticfn[style].paintfn)(hWnd);
91             break;
92
93         case WM_SYSCOLORCHANGE:
94             color_windowframe  = GetSysColor(COLOR_WINDOWFRAME);
95             color_background   = GetSysColor(COLOR_BACKGROUND);
96             color_window       = GetSysColor(COLOR_WINDOW);
97             color_windowtext   = GetSysColor(COLOR_WINDOWTEXT);
98             InvalidateRect(hWnd, NULL, TRUE);
99             break;
100
101         case WM_SETTEXT:
102             if (wndPtr->hText)
103                 USER_HEAP_FREE(wndPtr->hText);
104
105             wndPtr->hText = USER_HEAP_ALLOC(GMEM_MOVEABLE, 
106                                             strlen((LPSTR)lParam) + 1);
107             textPtr = (LPSTR)USER_HEAP_ADDR(wndPtr->hText);
108             strcpy(textPtr, (LPSTR)lParam);
109             InvalidateRect(hWnd, NULL, TRUE);
110             break;
111
112         default:
113                 lResult = DefWindowProc(hWnd, uMsg, wParam, lParam);
114                 break;
115         }
116
117         GlobalUnlock(hWnd);
118         return lResult;
119 }
120
121
122 static LONG PaintTextfn(HWND hwnd)
123 {
124     PAINTSTRUCT ps;
125     RECT rc;
126     HDC hdc;
127     HBRUSH hBrush;
128     HANDLE hText;
129     char *text;
130     int textlen;
131     WORD wFormat;
132
133     WND *wndPtr = WIN_FindWndPtr(hwnd);
134     LONG style = wndPtr->dwStyle;
135
136     hdc = BeginPaint(hwnd, &ps);
137     GetClientRect(hwnd, &rc);
138
139     textlen = GetWindowTextLength(hwnd);
140     hText = LocalAlloc(LMEM_MOVEABLE, textlen+1);
141     text = LocalLock(hText);
142     GetWindowText(hwnd, text, textlen+1);
143
144     switch (style & 0x0000000F)
145     {
146     case SS_LEFT:
147         wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK;
148         break;
149
150     case SS_CENTER:
151         wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK;
152         break;
153
154     case SS_RIGHT:
155         wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK;
156         break;
157
158     case SS_SIMPLE:
159         wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
160         break;
161
162     case SS_LEFTNOWORDWRAP:
163         wFormat = DT_LEFT | DT_SINGLELINE | DT_EXPANDTABS | DT_VCENTER;
164         break;
165     }
166
167     if (style & SS_NOPREFIX)
168         wFormat |= DT_NOPREFIX;
169
170     hBrush = SendMessage(GetParent(hwnd), WM_CTLCOLOR, (WORD)hdc,
171                     MAKELONG(hwnd, CTLCOLOR_STATIC));
172     if (hBrush == (HBRUSH)NULL) hBrush = GetStockObject(WHITE_BRUSH);
173     FillRect(hdc, &rc, hBrush);
174     DrawText(hdc, text, textlen, &rc, wFormat);
175
176     LocalUnlock(hText);
177     LocalFree(hText);
178     GlobalUnlock(hwnd);
179     EndPaint(hwnd, &ps);
180 }
181
182 static LONG PaintRectfn(HWND hwnd)
183 {
184     PAINTSTRUCT ps;
185     RECT rc;
186     HDC hdc;
187     HPEN hOldPen, hPen;
188     HBRUSH hOldBrush, hBrush;
189
190     WND *wndPtr = WIN_FindWndPtr(hwnd);
191
192     hdc = BeginPaint(hwnd, &ps);
193     GetClientRect(hwnd, &rc);
194     
195     switch (wndPtr->dwStyle & 0x0000000F)
196     {
197     case SS_BLACKRECT:
198         hPen = CreatePen(PS_SOLID, 1, color_windowframe);
199         hBrush = CreateSolidBrush(color_windowframe);
200         break;
201
202     case SS_GRAYRECT:
203         hPen = CreatePen(PS_SOLID, 1, color_background);
204         hBrush = CreateSolidBrush(color_background);
205         break;
206
207     case SS_WHITERECT:
208         hPen = CreatePen(PS_SOLID, 1, color_window);
209         hBrush = CreateSolidBrush(color_window);
210         break;
211     }
212
213     hOldPen = (HPEN)SelectObject(hdc, (HANDLE)hPen);
214     hOldBrush = (HBRUSH)SelectObject(hdc, (HANDLE)hBrush);
215     Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);
216
217     SelectObject(hdc, (HANDLE)hOldPen);
218     SelectObject(hdc, (HANDLE)hOldBrush);
219     DeleteObject((HANDLE)hPen);
220     DeleteObject((HANDLE)hBrush);
221
222     GlobalUnlock(hwnd);
223     EndPaint(hwnd, &ps);
224 }
225
226 static LONG PaintFramefn(HWND hwnd)
227 {
228     PAINTSTRUCT ps;
229     RECT rc;
230     HDC hdc;
231     HPEN hOldPen, hPen;
232     HBRUSH hOldBrush, hBrush;
233
234     WND *wndPtr = WIN_FindWndPtr(hwnd);
235
236     hdc = BeginPaint(hwnd, &ps);
237     GetClientRect(hwnd, &rc);
238     
239     switch (wndPtr->dwStyle & 0x0000000F)
240     {
241     case SS_BLACKFRAME:
242         hPen = CreatePen(PS_SOLID, 1, color_windowframe);
243         break;
244
245     case SS_GRAYFRAME:
246         hPen = CreatePen(PS_SOLID, 1, color_background);
247         break;
248
249     case SS_WHITEFRAME:
250         hPen = CreatePen(PS_SOLID, 1, color_window);
251         break;
252     }
253
254     hBrush = CreateSolidBrush(color_window);
255     hOldPen = (HPEN)SelectObject(hdc, (HANDLE)hPen);
256     hOldBrush = (HBRUSH)SelectObject(hdc, (HANDLE)hBrush);
257     Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);
258
259     SelectObject(hdc, (HANDLE)hOldPen);
260     SelectObject(hdc, (HANDLE)hOldBrush);
261     DeleteObject((HANDLE)hPen);
262     DeleteObject((HANDLE)hBrush);
263
264     GlobalUnlock(hwnd);
265     EndPaint(hwnd, &ps);
266 }
267
268
269 static LONG PaintIconfn(HWND hwnd)
270 {
271     WND         *wndPtr;
272     PAINTSTRUCT ps;
273     RECT        rc;
274     HDC         hdc;
275     LPSTR       textPtr;
276     HICON       hIcon;
277
278     wndPtr = WIN_FindWndPtr(hwnd);
279     hdc = BeginPaint(hwnd, &ps);
280     GetClientRect(hwnd, &rc);
281     FillRect(hdc, &rc, GetStockObject(WHITE_BRUSH));
282     textPtr = (LPSTR)USER_HEAP_ADDR(wndPtr->hText);
283     printf("SS_ICON : textPtr='%s' / left=%d top=%d right=%d bottom=%d \n", 
284                 textPtr, rc.left, rc.top, rc.right, rc.bottom);
285 /*
286     SetWindowPos(hwnd, (HWND)NULL, 0, 0, 32, 32,
287                 SWP_NOZORDER | SWP_NOMOVE);
288     GetClientRect(hwnd, &rc);
289     printf("SS_ICON : textPtr='%s' / left=%d top=%d right=%d bottom=%d \n", 
290                 textPtr, rc.left, rc.top, rc.right, rc.bottom);
291 */
292     hIcon = LoadIcon(wndPtr->hInstance, textPtr);
293     DrawIcon(hdc, rc.left, rc.top, hIcon);
294     EndPaint(hwnd, &ps);
295     GlobalUnlock(hwnd);
296 }
297
298
299
300
301
302