Moved system metrics definitions to winuser.h.
[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
11 #include "desktop.h"
12 #include "windef.h"
13 #include "wingdi.h"
14 #include "heap.h"
15 #include "user.h"
16 #include "win.h"
17 #include "wine/winuser16.h"
18
19
20 /***********************************************************************
21  *           DESKTOP_LoadBitmap
22  *
23  * Load a bitmap from a file. Used by SetDeskWallPaper().
24  */
25 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
26 {
27     BITMAPFILEHEADER *fileHeader;
28     BITMAPINFO *bitmapInfo;
29     HBITMAP hbitmap;
30     HFILE file;
31     LPSTR buffer;
32     LONG size;
33
34     /* Read all the file into memory */
35
36     if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
37     {
38         UINT len = GetWindowsDirectoryA( NULL, 0 );
39         if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
40                                   len + strlen(filename) + 2 )))
41             return 0;
42         GetWindowsDirectoryA( buffer, len + 1 );
43         strcat( buffer, "\\" );
44         strcat( buffer, filename );
45         file = _lopen( buffer, OF_READ );
46         HeapFree( GetProcessHeap(), 0, buffer );
47     }
48     if (file == HFILE_ERROR) return 0;
49     size = _llseek( file, 0, 2 );
50     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
51     {
52         _lclose( file );
53         return 0;
54     }
55     _llseek( file, 0, 0 );
56     size = _lread( file, buffer, size );
57     _lclose( file );
58     fileHeader = (BITMAPFILEHEADER *)buffer;
59     bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
60     
61       /* Check header content */
62     if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
63     {
64         HeapFree( GetProcessHeap(), 0, buffer );
65         return 0;
66     }
67     hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
68                                 buffer + fileHeader->bfOffBits,
69                                 bitmapInfo, DIB_RGB_COLORS );
70     HeapFree( GetProcessHeap(), 0, buffer );
71     return hbitmap;
72 }
73
74
75 /***********************************************************************
76  *           DESKTOP_DoEraseBkgnd
77  *
78  * Handle the WM_ERASEBKGND message.
79  */
80 static LRESULT DESKTOP_DoEraseBkgnd( HWND hwnd, HDC hdc,
81                                      DESKTOP *desktopPtr )
82 {
83     RECT rect;
84     WND*   Wnd = WIN_FindWndPtr( hwnd );
85
86     if (Wnd->hrgnUpdate > 1) DeleteObject( Wnd->hrgnUpdate );
87     Wnd->hrgnUpdate = 0;
88
89     WIN_ReleaseWndPtr(Wnd);
90     
91     GetClientRect( hwnd, &rect );    
92
93     /* Paint desktop pattern (only if wall paper does not cover everything) */
94
95     if (!desktopPtr->hbitmapWallPaper || 
96         (!desktopPtr->fTileWallPaper && ((desktopPtr->bitmapSize.cx < rect.right) ||
97          (desktopPtr->bitmapSize.cy < rect.bottom))))
98     {
99           /* Set colors in case pattern is a monochrome bitmap */
100         SetBkColor( hdc, RGB(0,0,0) );
101         SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
102         FillRect( hdc, &rect, desktopPtr->hbrushPattern );
103     }
104
105       /* Paint wall paper */
106
107     if (desktopPtr->hbitmapWallPaper)
108     {
109         INT x, y;
110         HDC hMemDC = CreateCompatibleDC( hdc );
111         
112         SelectObject( hMemDC, desktopPtr->hbitmapWallPaper );
113
114         if (desktopPtr->fTileWallPaper)
115         {
116             for (y = 0; y < rect.bottom; y += desktopPtr->bitmapSize.cy)
117                 for (x = 0; x < rect.right; x += desktopPtr->bitmapSize.cx)
118                     BitBlt( hdc, x, y, desktopPtr->bitmapSize.cx,
119                               desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
120         }
121         else
122         {
123             x = (rect.left + rect.right - desktopPtr->bitmapSize.cx) / 2;
124             y = (rect.top + rect.bottom - desktopPtr->bitmapSize.cy) / 2;
125             if (x < 0) x = 0;
126             if (y < 0) y = 0;
127             BitBlt( hdc, x, y, desktopPtr->bitmapSize.cx,
128                       desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
129         }
130         DeleteDC( hMemDC );
131     }
132
133     return 1;
134 }
135
136
137 /***********************************************************************
138  *           DesktopWndProc_locked
139  *
140  * Window procedure for the desktop window.
141  */
142 static inline LRESULT WINAPI DesktopWndProc_locked( WND *wndPtr, UINT message,
143                                WPARAM wParam, LPARAM lParam )
144 {
145     DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
146     HWND hwnd = wndPtr->hwndSelf;
147
148       /* Most messages are ignored (we DON'T call DefWindowProc) */
149
150     switch(message)
151     {
152         /* Warning: this message is sent directly by                     */
153         /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
154     case WM_NCCREATE:
155         desktopPtr->hbrushPattern = 0;
156         desktopPtr->hbitmapWallPaper = 0;
157         SetDeskPattern();
158         SetDeskWallPaper( (LPSTR)-1 );
159         return  1;
160         
161     case WM_ERASEBKGND:
162         if(!USER_Driver->pIsSingleWindow())
163             return  1;
164         return  DESKTOP_DoEraseBkgnd( hwnd, (HDC)wParam, desktopPtr );
165
166     case WM_SYSCOMMAND:
167         if ((wParam & 0xfff0) != SC_CLOSE)
168             return  0;
169         ExitWindows16( 0, 0 ); 
170
171     case WM_SETCURSOR:
172         return  (LRESULT)SetCursor16( LoadCursor16( 0, IDC_ARROW16 ) );
173     }
174     
175     return 0;
176 }
177
178 /***********************************************************************
179  *           DesktopWndProc
180  *
181  * This is just a wrapper for the DesktopWndProc which does windows
182  * locking and unlocking.
183  */
184 LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message,
185                                WPARAM wParam, LPARAM lParam )
186 {
187     WND *wndPtr = WIN_FindWndPtr( hwnd );
188     LRESULT retvalue = DesktopWndProc_locked(wndPtr,message,wParam,lParam);
189
190     WIN_ReleaseWndPtr(wndPtr);
191     return retvalue;
192 }
193
194 /***********************************************************************
195  *           PaintDesktop   (USER32.415)
196  *
197  */
198 BOOL WINAPI PaintDesktop(HDC hdc)
199 {
200     BOOL retvalue;
201     HWND hwnd = GetDesktopWindow();
202     WND *wndPtr = WIN_FindWndPtr( hwnd );
203     DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
204     retvalue = DESKTOP_DoEraseBkgnd( hwnd, hdc, desktopPtr );
205     WIN_ReleaseWndPtr(wndPtr);
206     return retvalue;
207
208 }
209
210 /***********************************************************************
211  *           SetDeskPattern   (USER.279)
212  */
213 BOOL16 WINAPI SetDeskPattern(void)
214 {
215     char buffer[100];
216     GetProfileStringA( "desktop", "Pattern", "(None)", buffer, 100 );
217     return DESKTOP_SetPattern( buffer );
218 }
219
220
221 /***********************************************************************
222  *           SetDeskWallPaper16   (USER.285)
223  */
224 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
225 {
226     return SetDeskWallPaper( filename );
227 }
228
229
230 /***********************************************************************
231  *           SetDeskWallPaper   (USER32.475)
232  *
233  * FIXME: is there a unicode version?
234  */
235 BOOL WINAPI SetDeskWallPaper( LPCSTR filename )
236 {
237     HBITMAP hbitmap;
238     HDC hdc;
239     char buffer[256];
240     WND *wndPtr = WIN_GetDesktop();
241     DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
242
243     if (filename == (LPSTR)-1)
244     {
245         GetProfileStringA( "desktop", "WallPaper", "(None)", buffer, 256 );
246         filename = buffer;
247     }
248     hdc = GetDC( 0 );
249     hbitmap = DESKTOP_LoadBitmap( hdc, filename );
250     ReleaseDC( 0, hdc );
251     if (desktopPtr->hbitmapWallPaper) DeleteObject( desktopPtr->hbitmapWallPaper );
252     desktopPtr->hbitmapWallPaper = hbitmap;
253     desktopPtr->fTileWallPaper = GetProfileIntA( "desktop", "TileWallPaper", 0 );
254     if (hbitmap)
255     {
256         BITMAP bmp;
257         GetObjectA( hbitmap, sizeof(bmp), &bmp );
258         desktopPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
259         desktopPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
260     }
261     WIN_ReleaseDesktop();
262     return TRUE;
263 }
264
265
266 /***********************************************************************
267  *           DESKTOP_SetPattern
268  *
269  * Set the desktop pattern.
270  */
271 BOOL DESKTOP_SetPattern( LPCSTR pattern )
272 {
273     WND *wndPtr = WIN_GetDesktop();
274     DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
275     int pat[8];
276
277     if (desktopPtr->hbrushPattern) DeleteObject( desktopPtr->hbrushPattern );
278     memset( pat, 0, sizeof(pat) );
279     if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
280                            &pat[0], &pat[1], &pat[2], &pat[3],
281                            &pat[4], &pat[5], &pat[6], &pat[7] ))
282     {
283         WORD pattern[8];
284         HBITMAP hbitmap;
285         int i;
286
287         for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
288         hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
289         desktopPtr->hbrushPattern = CreatePatternBrush( hbitmap );
290         DeleteObject( hbitmap );
291     }
292     else desktopPtr->hbrushPattern = CreateSolidBrush( GetSysColor(COLOR_BACKGROUND) );
293     WIN_ReleaseDesktop();
294     return TRUE;
295 }
296