Release 980628
[wine] / controls / desktop.c
1 /*
2  * Desktop window class.
3  *
4  * Copyright 1994 Alexandre Julliard
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include "win.h"
11 #include "desktop.h"
12 #include "graphics.h"
13 #include "heap.h"
14
15
16 /***********************************************************************
17  *           DESKTOP_LoadBitmap
18  *
19  * Load a bitmap from a file. Used by SetDeskWallPaper().
20  */
21 static HBITMAP32 DESKTOP_LoadBitmap( HDC32 hdc, const char *filename )
22 {
23     BITMAPFILEHEADER *fileHeader;
24     BITMAPINFO *bitmapInfo;
25     HBITMAP32 hbitmap;
26     HFILE32 file;
27     LPSTR buffer;
28     LONG size;
29
30     /* Read all the file into memory */
31
32     if ((file = _lopen32( filename, OF_READ )) == HFILE_ERROR32)
33     {
34         UINT32 len = GetWindowsDirectory32A( NULL, 0 );
35         if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
36                                   len + strlen(filename) + 2 )))
37             return 0;
38         GetWindowsDirectory32A( buffer, len + 1 );
39         strcat( buffer, "\\" );
40         strcat( buffer, filename );
41         file = _lopen32( buffer, OF_READ );
42         HeapFree( GetProcessHeap(), 0, buffer );
43     }
44     if (file == HFILE_ERROR32) return 0;
45     size = _llseek32( file, 0, 2 );
46     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
47     {
48         _lclose32( file );
49         return 0;
50     }
51     _llseek32( file, 0, 0 );
52     size = _lread32( file, buffer, size );
53     _lclose32( file );
54     fileHeader = (BITMAPFILEHEADER *)buffer;
55     bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
56     
57       /* Check header content */
58     if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
59     {
60         HeapFree( GetProcessHeap(), 0, buffer );
61         return 0;
62     }
63     hbitmap = CreateDIBitmap32( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
64                                 buffer + fileHeader->bfOffBits,
65                                 bitmapInfo, DIB_RGB_COLORS );
66     HeapFree( GetProcessHeap(), 0, buffer );
67     return hbitmap;
68 }
69
70
71 /***********************************************************************
72  *           DESKTOP_DoEraseBkgnd
73  *
74  * Handle the WM_ERASEBKGND message.
75  */
76 static LRESULT DESKTOP_DoEraseBkgnd( HWND32 hwnd, HDC32 hdc,
77                                      DESKTOPINFO *infoPtr )
78 {
79     RECT32 rect;
80     WND*   Wnd = WIN_FindWndPtr( hwnd );
81
82     if (Wnd->hrgnUpdate > 1) DeleteObject32( Wnd->hrgnUpdate );
83     Wnd->hrgnUpdate = 0;
84
85     GetClientRect32( hwnd, &rect );    
86
87     /* Paint desktop pattern (only if wall paper does not cover everything) */
88
89     if (!infoPtr->hbitmapWallPaper || 
90         (!infoPtr->fTileWallPaper && ((infoPtr->bitmapSize.cx < rect.right) ||
91          (infoPtr->bitmapSize.cy < rect.bottom))))
92     {
93           /* Set colors in case pattern is a monochrome bitmap */
94         SetBkColor32( hdc, RGB(0,0,0) );
95         SetTextColor32( hdc, GetSysColor32(COLOR_BACKGROUND) );
96         FillRect32( hdc, &rect, infoPtr->hbrushPattern );
97     }
98
99       /* Paint wall paper */
100
101     if (infoPtr->hbitmapWallPaper)
102     {
103         INT32 x, y;
104
105         if (infoPtr->fTileWallPaper)
106         {
107             for (y = 0; y < rect.bottom; y += infoPtr->bitmapSize.cy)
108                 for (x = 0; x < rect.right; x += infoPtr->bitmapSize.cx)
109                     GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper,
110                                       x, y, 0, 0, 
111                                       infoPtr->bitmapSize.cx,
112                                       infoPtr->bitmapSize.cy, FALSE );
113         }
114         else
115         {
116             x = (rect.left + rect.right - infoPtr->bitmapSize.cx) / 2;
117             y = (rect.top + rect.bottom - infoPtr->bitmapSize.cy) / 2;
118             if (x < 0) x = 0;
119             if (y < 0) y = 0;
120             GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper, 
121                               x, y, 0, 0, infoPtr->bitmapSize.cx, 
122                               infoPtr->bitmapSize.cy, FALSE );
123         }
124     }
125
126     return 1;
127 }
128
129
130 /***********************************************************************
131  *           DesktopWndProc
132  *
133  * Window procedure for the desktop window.
134  */
135 LRESULT WINAPI DesktopWndProc( HWND32 hwnd, UINT32 message,
136                                WPARAM32 wParam, LPARAM lParam )
137 {
138     WND *wndPtr = WIN_FindWndPtr( hwnd );
139     DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
140
141       /* Most messages are ignored (we DON'T call DefWindowProc) */
142
143     switch(message)
144     {
145         /* Warning: this message is sent directly by                     */
146         /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
147     case WM_NCCREATE:
148         infoPtr->hbrushPattern = 0;
149         infoPtr->hbitmapWallPaper = 0;
150         SetDeskPattern();
151         SetDeskWallPaper32( (LPSTR)-1 );
152         return 1;
153         
154     case WM_ERASEBKGND:
155         if (rootWindow == DefaultRootWindow(display)) return 1;
156         return DESKTOP_DoEraseBkgnd( hwnd, (HDC32)wParam, infoPtr );
157
158     case WM_SYSCOMMAND:
159         if ((wParam & 0xfff0) != SC_CLOSE) return 0;
160         ExitWindows16( 0, 0 ); 
161
162     case WM_SETCURSOR:
163         return (LRESULT)SetCursor16( LoadCursor16( 0, IDC_ARROW16 ) );
164     }
165     
166     return 0;
167 }
168
169 /***********************************************************************
170  *           PaintDesktop   (USER32.415)
171  *
172  */
173 BOOL32 WINAPI PaintDesktop(HDC32 hdc)
174 {
175     HWND32 hwnd = GetDesktopWindow32();
176     WND *wndPtr = WIN_FindWndPtr( hwnd );
177     DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
178
179     return DESKTOP_DoEraseBkgnd( hwnd, hdc, infoPtr );
180 }
181
182 /***********************************************************************
183  *           SetDeskPattern   (USER.279)
184  */
185 BOOL16 WINAPI SetDeskPattern(void)
186 {
187     char buffer[100];
188     GetProfileString32A( "desktop", "Pattern", "(None)", buffer, 100 );
189     return DESKTOP_SetPattern( buffer );
190 }
191
192
193 /***********************************************************************
194  *           SetDeskWallPaper16   (USER.285)
195  */
196 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
197 {
198     return SetDeskWallPaper32( filename );
199 }
200
201
202 /***********************************************************************
203  *           SetDeskWallPaper32   (USER32.475)
204  *
205  * FIXME: is there a unicode version?
206  */
207 BOOL32 WINAPI SetDeskWallPaper32( LPCSTR filename )
208 {
209     HBITMAP32 hbitmap;
210     HDC32 hdc;
211     char buffer[256];
212     WND *wndPtr = WIN_GetDesktop();
213     DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
214
215     if (filename == (LPSTR)-1)
216     {
217         GetProfileString32A( "desktop", "WallPaper", "(None)", buffer, 256 );
218         filename = buffer;
219     }
220     hdc = GetDC32( 0 );
221     hbitmap = DESKTOP_LoadBitmap( hdc, filename );
222     ReleaseDC32( 0, hdc );
223     if (infoPtr->hbitmapWallPaper) DeleteObject32( infoPtr->hbitmapWallPaper );
224     infoPtr->hbitmapWallPaper = hbitmap;
225     infoPtr->fTileWallPaper = GetProfileInt32A( "desktop", "TileWallPaper", 0 );
226     if (hbitmap)
227     {
228         BITMAP32 bmp;
229         GetObject32A( hbitmap, sizeof(bmp), &bmp );
230         infoPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
231         infoPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
232     }
233     return TRUE;
234 }
235
236
237 /***********************************************************************
238  *           DESKTOP_SetPattern
239  *
240  * Set the desktop pattern.
241  */
242 BOOL32 DESKTOP_SetPattern( LPCSTR pattern )
243 {
244     WND *wndPtr = WIN_GetDesktop();
245     DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
246     int pat[8];
247
248     if (infoPtr->hbrushPattern) DeleteObject32( infoPtr->hbrushPattern );
249     memset( pat, 0, sizeof(pat) );
250     if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
251                            &pat[0], &pat[1], &pat[2], &pat[3],
252                            &pat[4], &pat[5], &pat[6], &pat[7] ))
253     {
254         WORD pattern[8];
255         HBITMAP32 hbitmap;
256         int i;
257
258         for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
259         hbitmap = CreateBitmap32( 8, 8, 1, 1, (LPSTR)pattern );
260         infoPtr->hbrushPattern = CreatePatternBrush32( hbitmap );
261         DeleteObject32( hbitmap );
262     }
263     else infoPtr->hbrushPattern = CreateSolidBrush32( GetSysColor32(COLOR_BACKGROUND) );
264     return TRUE;
265 }
266