Release 960717
[wine] / controls / static.c
1 /*
2  * Static control
3  *
4  * Copyright  David W. Metcalfe, 1993
5  *
6  */
7
8 #include <stdio.h>
9 #include <windows.h>
10 #include "win.h"
11 #include "user.h"
12 #include "static.h"
13
14 static void STATIC_PaintTextfn( WND *wndPtr, HDC hdc );
15 static void STATIC_PaintRectfn( WND *wndPtr, HDC hdc );
16 static void STATIC_PaintIconfn( WND *wndPtr, HDC hdc );
17
18
19 static COLORREF color_windowframe, color_background, color_window;
20
21
22 typedef void (*pfPaint)( WND *, HDC);
23
24 #define LAST_STATIC_TYPE  SS_LEFTNOWORDWRAP
25
26 static pfPaint staticPaintFunc[LAST_STATIC_TYPE+1] =
27 {
28     STATIC_PaintTextfn,      /* SS_LEFT */
29     STATIC_PaintTextfn,      /* SS_CENTER */
30     STATIC_PaintTextfn,      /* SS_RIGHT */
31     STATIC_PaintIconfn,      /* SS_ICON */
32     STATIC_PaintRectfn,      /* SS_BLACKRECT */
33     STATIC_PaintRectfn,      /* SS_GRAYRECT */
34     STATIC_PaintRectfn,      /* SS_WHITERECT */
35     STATIC_PaintRectfn,      /* SS_BLACKFRAME */
36     STATIC_PaintRectfn,      /* SS_GRAYFRAME */
37     STATIC_PaintRectfn,      /* SS_WHITEFRAME */
38     NULL,                    /* Not defined */
39     STATIC_PaintTextfn,      /* SS_SIMPLE */
40     STATIC_PaintTextfn       /* SS_LEFTNOWORDWRAP */
41 };
42
43
44 /***********************************************************************
45  *           STATIC_SetIcon
46  *
47  * Set the icon for an SS_ICON control.
48  */
49 static HICON STATIC_SetIcon( WND *wndPtr, HICON hicon )
50 {
51     HICON prevIcon;
52     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
53
54     if ((wndPtr->dwStyle & 0x0f) != SS_ICON) return 0;
55     prevIcon = infoPtr->hIcon;
56     infoPtr->hIcon = hicon;
57     if (hicon)
58     {
59         CURSORICONINFO *info = (CURSORICONINFO *) GlobalLock16( hicon );
60         SetWindowPos( wndPtr->hwndSelf, 0, 0, 0, info->nWidth, info->nHeight,
61                      SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
62         GlobalUnlock16( hicon );
63     }
64     return prevIcon;
65 }
66
67
68 /***********************************************************************
69  *           StaticWndProc
70  */
71 LRESULT StaticWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
72 {
73     LRESULT lResult = 0;
74     WND *wndPtr = WIN_FindWndPtr(hWnd);
75     LONG style = wndPtr->dwStyle & 0x0000000F;
76     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
77
78     switch (uMsg)
79     {
80         case WM_ENABLE:
81             InvalidateRect32( hWnd, NULL, FALSE );
82             break;
83
84         case WM_NCCREATE:
85             if (style == SS_ICON)
86             {
87                 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
88                 if (cs->lpszName)
89                 {
90                     HICON16 hicon = LoadIcon16( cs->hInstance, cs->lpszName );
91                     if (!hicon)  /* Try OEM icon (FIXME: is this right?) */
92                         hicon = LoadIcon16( 0, cs->lpszName );
93                     STATIC_SetIcon( wndPtr, hicon );
94                 }
95                 return 1;
96             }
97             return DefWindowProc16(hWnd, uMsg, wParam, lParam);
98
99         case WM_CREATE:
100             if (style < 0L || style > LAST_STATIC_TYPE)
101             {
102                 fprintf( stderr, "STATIC: Unknown style 0x%02lx\n", style );
103                 lResult = -1L;
104                 break;
105             }
106             /* initialise colours */
107             color_windowframe  = GetSysColor(COLOR_WINDOWFRAME);
108             color_background   = GetSysColor(COLOR_BACKGROUND);
109             color_window       = GetSysColor(COLOR_WINDOW);
110             break;
111
112         case WM_NCDESTROY:
113             if (style == SS_ICON)
114                 DestroyIcon( STATIC_SetIcon( wndPtr, 0 ) );
115             else 
116                 lResult = DefWindowProc16(hWnd, uMsg, wParam, lParam);
117             break;
118
119         case WM_PAINT:
120             {
121                 PAINTSTRUCT16 ps;
122                 BeginPaint16( hWnd, &ps );
123                 if (staticPaintFunc[style])
124                     (staticPaintFunc[style])( wndPtr, ps.hdc );
125                 EndPaint16( hWnd, &ps );
126             }
127             break;
128
129         case WM_SYSCOLORCHANGE:
130             color_windowframe  = GetSysColor(COLOR_WINDOWFRAME);
131             color_background   = GetSysColor(COLOR_BACKGROUND);
132             color_window       = GetSysColor(COLOR_WINDOW);
133             InvalidateRect32( hWnd, NULL, TRUE );
134             break;
135
136         case WM_SETTEXT:
137             if (style == SS_ICON)
138                 /* FIXME : should we also return the previous hIcon here ??? */
139                 STATIC_SetIcon( wndPtr, LoadIcon16( wndPtr->hInstance,
140                                                   (SEGPTR)lParam ));
141             else
142                 DEFWND_SetText( wndPtr, (LPSTR)PTR_SEG_TO_LIN(lParam) );
143             InvalidateRect32( hWnd, NULL, FALSE );
144             UpdateWindow( hWnd );
145             break;
146
147         case WM_SETFONT:
148             if (style == SS_ICON) return 0;
149             infoPtr->hFont = (HFONT)wParam;
150             if (LOWORD(lParam))
151             {
152                 InvalidateRect32( hWnd, NULL, FALSE );
153                 UpdateWindow( hWnd );
154             }
155             break;
156
157         case WM_GETFONT:
158             return infoPtr->hFont;
159
160         case WM_NCHITTEST:
161             return HTTRANSPARENT;
162
163         case WM_GETDLGCODE:
164             return DLGC_STATIC;
165
166         case STM_GETICON:
167             return infoPtr->hIcon;
168
169         case STM_SETICON:
170             lResult = STATIC_SetIcon( wndPtr, (HICON)wParam );
171             InvalidateRect32( hWnd, NULL, FALSE );
172             UpdateWindow( hWnd );
173             break;
174
175         default:
176                 lResult = DefWindowProc16(hWnd, uMsg, wParam, lParam);
177                 break;
178         }
179
180         return lResult;
181 }
182
183
184 static void STATIC_PaintTextfn( WND *wndPtr, HDC hdc )
185 {
186     RECT16 rc;
187     HBRUSH hBrush;
188     WORD wFormat;
189
190     LONG style = wndPtr->dwStyle;
191     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
192
193     GetClientRect16( wndPtr->hwndSelf, &rc);
194
195     switch (style & 0x0000000F)
196     {
197     case SS_LEFT:
198         wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
199         break;
200
201     case SS_CENTER:
202         wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
203         break;
204
205     case SS_RIGHT:
206         wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
207         break;
208
209     case SS_SIMPLE:
210         wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOCLIP;
211         break;
212
213     case SS_LEFTNOWORDWRAP:
214         wFormat = DT_LEFT | DT_SINGLELINE | DT_EXPANDTABS | DT_VCENTER | DT_NOCLIP;
215         break;
216
217     default:
218         return;
219     }
220
221     if (style & SS_NOPREFIX)
222         wFormat |= DT_NOPREFIX;
223
224     if (infoPtr->hFont) SelectObject( hdc, infoPtr->hFont );
225     hBrush = SendMessage32A( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
226                              hdc, wndPtr->hwndSelf );
227     if (!hBrush) hBrush = GetStockObject(WHITE_BRUSH);
228     FillRect16(hdc, &rc, hBrush);
229     if (wndPtr->text) DrawText16( hdc, wndPtr->text, -1, &rc, wFormat );
230 }
231
232 static void STATIC_PaintRectfn( WND *wndPtr, HDC hdc )
233 {
234     RECT16 rc;
235     HBRUSH hBrush;
236
237     GetClientRect16( wndPtr->hwndSelf, &rc);
238     
239     switch (wndPtr->dwStyle & 0x0f)
240     {
241     case SS_BLACKRECT:
242         hBrush = CreateSolidBrush(color_windowframe);
243         FillRect16( hdc, &rc, hBrush );
244         break;
245     case SS_GRAYRECT:
246         hBrush = CreateSolidBrush(color_background);
247         FillRect16( hdc, &rc, hBrush );
248         break;
249     case SS_WHITERECT:
250         hBrush = CreateSolidBrush(color_window);
251         FillRect16( hdc, &rc, hBrush );
252         break;
253     case SS_BLACKFRAME:
254         hBrush = CreateSolidBrush(color_windowframe);
255         FrameRect16( hdc, &rc, hBrush );
256         break;
257     case SS_GRAYFRAME:
258         hBrush = CreateSolidBrush(color_background);
259         FrameRect16( hdc, &rc, hBrush );
260         break;
261     case SS_WHITEFRAME:
262         hBrush = CreateSolidBrush(color_window);
263         FrameRect16( hdc, &rc, hBrush );
264         break;
265     default:
266         return;
267     }
268     DeleteObject( hBrush );
269 }
270
271
272 static void STATIC_PaintIconfn( WND *wndPtr, HDC hdc )
273 {
274     RECT16 rc;
275     HBRUSH      hbrush;
276     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
277
278     GetClientRect16( wndPtr->hwndSelf, &rc);
279     hbrush = SendMessage32A( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
280                              hdc, wndPtr->hwndSelf );
281     FillRect16( hdc, &rc, hbrush );
282     if (infoPtr->hIcon) DrawIcon( hdc, rc.left, rc.top, infoPtr->hIcon );
283 }