Release 940804
[wine/gsoc-2012-control.git] / controls / static.c
blob0f8419532b63881deeb95077399bac049c86deaa
1 /*
2 * Static control
4 * Copyright David W. Metcalfe, 1993
6 */
8 static char Copyright[] = "Copyright David W. Metcalfe, 1993";
10 #include <stdio.h>
11 #include <windows.h>
12 #include "win.h"
13 #include "user.h"
15 LONG StaticWndProc(HWND hWnd, WORD uMsg, WORD wParam, LONG lParam);
17 static LONG PaintTextfn(HWND hwnd);
18 static LONG PaintRectfn(HWND hwnd);
19 static LONG PaintFramefn(HWND hwnd);
20 static LONG PaintIconfn(HWND hwnd);
23 static COLORREF color_windowframe, color_background, color_window,
24 color_windowtext;
26 #define NOTIFY_PARENT(hWndCntrl, wNotifyCode) \
27 SendMessage(GetParent(hWndCntrl), WM_COMMAND, \
28 GetDlgCtrlID(hWndCntrl), MAKELPARAM(hWndCntrl, wNotifyCode));
29 #define DIM(array) ((sizeof array)/(sizeof array[0]))
31 typedef struct
33 LONG (*paintfn)();
34 } STATICFN;
36 #define MAX_STATIC_TYPE 12
38 static STATICFN staticfn[MAX_STATIC_TYPE] =
40 { (LONG(*)())PaintTextfn }, /* SS_LEFT */
41 { (LONG(*)())PaintTextfn }, /* SS_CENTER */
42 { (LONG(*)())PaintTextfn }, /* SS_RIGHT */
43 { (LONG(*)())PaintIconfn }, /* SS_ICON */
44 { (LONG(*)())PaintRectfn }, /* SS_BLACKRECT */
45 { (LONG(*)())PaintRectfn }, /* SS_GRAYRECT */
46 { (LONG(*)())PaintRectfn }, /* SS_WHITERECT */
47 { (LONG(*)())PaintFramefn }, /* SS_BLACKFRAME */
48 { (LONG(*)())PaintFramefn }, /* SS_GRAYFRAME */
49 { (LONG(*)())PaintFramefn }, /* SS_WHITEFRAME */
50 { (LONG(*)())PaintTextfn }, /* SS_SIMPLE */
51 { (LONG(*)())PaintTextfn } /* SS_LEFTNOWORDWRAP */
55 LONG StaticWndProc(HWND hWnd, WORD uMsg, WORD wParam, LONG lParam)
57 LONG lResult = 0;
58 HDC hDC;
59 RECT rc;
60 LPSTR textPtr;
62 WND *wndPtr = WIN_FindWndPtr(hWnd);
63 LONG style = wndPtr->dwStyle & 0x0000000F;
65 switch (uMsg) {
66 case WM_ENABLE:
67 InvalidateRect(hWnd, NULL, FALSE);
68 break;
70 case WM_CREATE:
71 if (style < 0L || style >= (LONG)DIM(staticfn)) {
72 lResult = -1L;
73 break;
75 /* initialise colours */
76 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
77 color_background = GetSysColor(COLOR_BACKGROUND);
78 color_window = GetSysColor(COLOR_WINDOW);
79 color_windowtext = GetSysColor(COLOR_WINDOWTEXT);
80 lResult = 0L;
81 if (style == SS_ICON) {
83 SetWindowPos(hWnd, (HWND)NULL, 0, 0, 32, 32,
84 SWP_NOZORDER | SWP_NOMOVE);
87 break;
89 case WM_PAINT:
90 if (staticfn[style].paintfn)
91 (staticfn[style].paintfn)(hWnd);
92 break;
94 case WM_SYSCOLORCHANGE:
95 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
96 color_background = GetSysColor(COLOR_BACKGROUND);
97 color_window = GetSysColor(COLOR_WINDOW);
98 color_windowtext = GetSysColor(COLOR_WINDOWTEXT);
99 InvalidateRect(hWnd, NULL, TRUE);
100 break;
102 case WM_SETTEXT:
103 if (wndPtr->hText)
104 USER_HEAP_FREE(wndPtr->hText);
106 wndPtr->hText = USER_HEAP_ALLOC(GMEM_MOVEABLE,
107 strlen((LPSTR)lParam) + 1);
108 textPtr = (LPSTR)USER_HEAP_ADDR(wndPtr->hText);
109 strcpy(textPtr, (LPSTR)lParam);
110 InvalidateRect(hWnd, NULL, TRUE);
111 break;
113 case WM_KEYDOWN:
114 case WM_KEYUP:
115 case WM_CHAR:
116 case WM_LBUTTONDOWN:
117 case WM_LBUTTONUP:
118 case WM_MBUTTONDOWN:
119 case WM_MBUTTONUP:
120 case WM_RBUTTONDOWN:
121 case WM_RBUTTONUP:
122 case WM_MOUSEMOVE:
123 return(SendMessage(wndPtr->hwndParent, uMsg, wParam, lParam));
125 default:
126 lResult = DefWindowProc(hWnd, uMsg, wParam, lParam);
127 break;
130 GlobalUnlock(hWnd);
131 return lResult;
135 static LONG PaintTextfn(HWND hwnd)
137 PAINTSTRUCT ps;
138 RECT rc;
139 HDC hdc;
140 HBRUSH hBrush;
141 HANDLE hText;
142 char *text;
143 int textlen;
144 WORD wFormat;
146 WND *wndPtr = WIN_FindWndPtr(hwnd);
147 LONG style = wndPtr->dwStyle;
149 hdc = BeginPaint(hwnd, &ps);
150 GetClientRect(hwnd, &rc);
152 textlen = GetWindowTextLength(hwnd);
153 hText = USER_HEAP_ALLOC(0, textlen+1);
154 text = USER_HEAP_ADDR(hText);
155 GetWindowText(hwnd, text, textlen+1);
157 switch (style & 0x0000000F)
159 case SS_LEFT:
160 wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK;
161 break;
163 case SS_CENTER:
164 wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK;
165 break;
167 case SS_RIGHT:
168 wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK;
169 break;
171 case SS_SIMPLE:
172 wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
173 break;
175 case SS_LEFTNOWORDWRAP:
176 wFormat = DT_LEFT | DT_SINGLELINE | DT_EXPANDTABS | DT_VCENTER;
177 break;
180 if (style & SS_NOPREFIX)
181 wFormat |= DT_NOPREFIX;
183 hBrush = SendMessage(GetParent(hwnd), WM_CTLCOLOR, (WORD)hdc,
184 MAKELONG(hwnd, CTLCOLOR_STATIC));
185 if (hBrush == (HBRUSH)NULL) hBrush = GetStockObject(WHITE_BRUSH);
186 FillRect(hdc, &rc, hBrush);
187 DrawText(hdc, text, textlen, &rc, wFormat);
189 USER_HEAP_FREE(hText);
190 GlobalUnlock(hwnd);
191 EndPaint(hwnd, &ps);
194 static LONG PaintRectfn(HWND hwnd)
196 PAINTSTRUCT ps;
197 RECT rc;
198 HDC hdc;
199 HPEN hOldPen, hPen;
200 HBRUSH hOldBrush, hBrush;
202 WND *wndPtr = WIN_FindWndPtr(hwnd);
204 hdc = BeginPaint(hwnd, &ps);
205 GetClientRect(hwnd, &rc);
207 switch (wndPtr->dwStyle & 0x0000000F)
209 case SS_BLACKRECT:
210 hPen = CreatePen(PS_SOLID, 1, color_windowframe);
211 hBrush = CreateSolidBrush(color_windowframe);
212 break;
214 case SS_GRAYRECT:
215 hPen = CreatePen(PS_SOLID, 1, color_background);
216 hBrush = CreateSolidBrush(color_background);
217 break;
219 case SS_WHITERECT:
220 hPen = CreatePen(PS_SOLID, 1, color_window);
221 hBrush = CreateSolidBrush(color_window);
222 break;
225 hOldPen = (HPEN)SelectObject(hdc, (HANDLE)hPen);
226 hOldBrush = (HBRUSH)SelectObject(hdc, (HANDLE)hBrush);
227 Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);
229 SelectObject(hdc, (HANDLE)hOldPen);
230 SelectObject(hdc, (HANDLE)hOldBrush);
231 DeleteObject((HANDLE)hPen);
232 DeleteObject((HANDLE)hBrush);
234 GlobalUnlock(hwnd);
235 EndPaint(hwnd, &ps);
238 static LONG PaintFramefn(HWND hwnd)
240 PAINTSTRUCT ps;
241 RECT rc;
242 HDC hdc;
243 HPEN hOldPen, hPen;
244 HBRUSH hOldBrush, hBrush;
246 WND *wndPtr = WIN_FindWndPtr(hwnd);
248 hdc = BeginPaint(hwnd, &ps);
249 GetClientRect(hwnd, &rc);
251 switch (wndPtr->dwStyle & 0x0000000F)
253 case SS_BLACKFRAME:
254 hPen = CreatePen(PS_SOLID, 1, color_windowframe);
255 break;
257 case SS_GRAYFRAME:
258 hPen = CreatePen(PS_SOLID, 1, color_background);
259 break;
261 case SS_WHITEFRAME:
262 hPen = CreatePen(PS_SOLID, 1, color_window);
263 break;
266 hBrush = CreateSolidBrush(color_window);
267 hOldPen = (HPEN)SelectObject(hdc, (HANDLE)hPen);
268 hOldBrush = (HBRUSH)SelectObject(hdc, (HANDLE)hBrush);
269 Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);
271 SelectObject(hdc, (HANDLE)hOldPen);
272 SelectObject(hdc, (HANDLE)hOldBrush);
273 DeleteObject((HANDLE)hPen);
274 DeleteObject((HANDLE)hBrush);
276 GlobalUnlock(hwnd);
277 EndPaint(hwnd, &ps);
281 static LONG PaintIconfn(HWND hwnd)
283 WND *wndPtr;
284 PAINTSTRUCT ps;
285 RECT rc;
286 HDC hdc;
287 LPSTR textPtr;
288 HICON hIcon;
290 wndPtr = WIN_FindWndPtr(hwnd);
291 hdc = BeginPaint(hwnd, &ps);
292 GetClientRect(hwnd, &rc);
293 FillRect(hdc, &rc, GetStockObject(WHITE_BRUSH));
294 textPtr = (LPSTR)USER_HEAP_ADDR(wndPtr->hText);
295 printf("SS_ICON : textPtr='%8x' / left=%d top=%d right=%d bottom=%d \n",
296 textPtr, rc.left, rc.top, rc.right, rc.bottom);
298 SetWindowPos(hwnd, (HWND)NULL, 0, 0, 32, 32,
299 SWP_NOZORDER | SWP_NOMOVE);
300 GetClientRect(hwnd, &rc);
301 printf("SS_ICON : textPtr='%s' / left=%d top=%d right=%d bottom=%d \n",
302 textPtr, rc.left, rc.top, rc.right, rc.bottom);
304 hIcon = LoadIcon(wndPtr->hInstance, textPtr);
305 DrawIcon(hdc, rc.left, rc.top, hIcon);
306 EndPaint(hwnd, &ps);
307 GlobalUnlock(hwnd);