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