Cast time_t to long for printing.
[wine] / controls / desktop.c
1 /*
2  * Desktop window class.
3  *
4  * Copyright 1994 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "user.h"
34 #include "controls.h"
35 #include "wine/winuser16.h"
36
37 static HBRUSH hbrushPattern;
38 static HBITMAP hbitmapWallPaper;
39 static SIZE bitmapSize;
40 static BOOL fTileWallPaper;
41
42 static LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
43
44
45 /*********************************************************************
46  * desktop class descriptor
47  */
48 const struct builtin_class_descr DESKTOP_builtin_class =
49 {
50     DESKTOP_CLASS_ATOM,   /* name */
51     CS_GLOBALCLASS,       /* style */
52     NULL,                 /* procA (winproc is Unicode only) */
53     DesktopWndProc,       /* procW */
54     0,                    /* extra */
55     IDC_ARROW,            /* cursor */
56     (HBRUSH)(COLOR_BACKGROUND+1)    /* brush */
57 };
58
59
60 /***********************************************************************
61  *           DESKTOP_LoadBitmap
62  *
63  * Load a bitmap from a file. Used by SetDeskWallPaper().
64  */
65 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
66 {
67     BITMAPFILEHEADER *fileHeader;
68     BITMAPINFO *bitmapInfo;
69     HBITMAP hbitmap;
70     HFILE file;
71     LPSTR buffer;
72     LONG size;
73
74     /* Read all the file into memory */
75
76     if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
77     {
78         UINT len = GetWindowsDirectoryA( NULL, 0 );
79         if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
80                                   len + strlen(filename) + 2 )))
81             return 0;
82         GetWindowsDirectoryA( buffer, len + 1 );
83         strcat( buffer, "\\" );
84         strcat( buffer, filename );
85         file = _lopen( buffer, OF_READ );
86         HeapFree( GetProcessHeap(), 0, buffer );
87     }
88     if (file == HFILE_ERROR) return 0;
89     size = _llseek( file, 0, 2 );
90     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
91     {
92         _lclose( file );
93         return 0;
94     }
95     _llseek( file, 0, 0 );
96     size = _lread( file, buffer, size );
97     _lclose( file );
98     fileHeader = (BITMAPFILEHEADER *)buffer;
99     bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
100
101       /* Check header content */
102     if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
103     {
104         HeapFree( GetProcessHeap(), 0, buffer );
105         return 0;
106     }
107     hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
108                                 buffer + fileHeader->bfOffBits,
109                                 bitmapInfo, DIB_RGB_COLORS );
110     HeapFree( GetProcessHeap(), 0, buffer );
111     return hbitmap;
112 }
113
114
115
116 /***********************************************************************
117  *           DesktopWndProc
118  */
119 static LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
120 {
121     /* all messages are ignored */
122     return 0;
123 }
124
125 /***********************************************************************
126  *           PaintDesktop   (USER32.@)
127  *
128  */
129 BOOL WINAPI PaintDesktop(HDC hdc)
130 {
131     HWND hwnd = GetDesktopWindow();
132
133     /* check for an owning thread; otherwise don't paint anything (non-desktop mode) */
134     if (GetWindowThreadProcessId( hwnd, NULL ))
135     {
136         RECT rect;
137
138         GetClientRect( hwnd, &rect );
139
140         /* Paint desktop pattern (only if wall paper does not cover everything) */
141
142         if (!hbitmapWallPaper ||
143             (!fTileWallPaper && ((bitmapSize.cx < rect.right) || (bitmapSize.cy < rect.bottom))))
144         {
145             HBRUSH brush = hbrushPattern;
146             if (!brush) brush = (HBRUSH)GetClassLongA( hwnd, GCL_HBRBACKGROUND );
147             /* Set colors in case pattern is a monochrome bitmap */
148             SetBkColor( hdc, RGB(0,0,0) );
149             SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
150             FillRect( hdc, &rect, brush );
151         }
152
153         /* Paint wall paper */
154
155         if (hbitmapWallPaper)
156         {
157             INT x, y;
158             HDC hMemDC = CreateCompatibleDC( hdc );
159
160             SelectObject( hMemDC, hbitmapWallPaper );
161
162             if (fTileWallPaper)
163             {
164                 for (y = 0; y < rect.bottom; y += bitmapSize.cy)
165                     for (x = 0; x < rect.right; x += bitmapSize.cx)
166                         BitBlt( hdc, x, y, bitmapSize.cx, bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
167             }
168             else
169             {
170                 x = (rect.left + rect.right - bitmapSize.cx) / 2;
171                 y = (rect.top + rect.bottom - bitmapSize.cy) / 2;
172                 if (x < 0) x = 0;
173                 if (y < 0) y = 0;
174                 BitBlt( hdc, x, y, bitmapSize.cx, bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
175             }
176             DeleteDC( hMemDC );
177         }
178     }
179     return TRUE;
180 }
181
182 /***********************************************************************
183  *           OldSetDeskPattern   (USER.279)
184  */
185 BOOL16 WINAPI SetDeskPattern(void)
186 {
187     return SystemParametersInfoA( SPI_SETDESKPATTERN, -1, NULL, FALSE );
188 }
189
190
191 /***********************************************************************
192  *           SetDeskWallPaper   (USER.285)
193  */
194 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
195 {
196     return SetDeskWallPaper( filename );
197 }
198
199
200 /***********************************************************************
201  *           SetDeskWallPaper   (USER32.@)
202  *
203  * FIXME: is there a unicode version?
204  */
205 BOOL WINAPI SetDeskWallPaper( LPCSTR filename )
206 {
207     HBITMAP hbitmap;
208     HDC hdc;
209     char buffer[256];
210
211     if (filename == (LPSTR)-1)
212     {
213         GetProfileStringA( "desktop", "WallPaper", "(None)", buffer, 256 );
214         filename = buffer;
215     }
216     hdc = GetDC( 0 );
217     hbitmap = DESKTOP_LoadBitmap( hdc, filename );
218     ReleaseDC( 0, hdc );
219     if (hbitmapWallPaper) DeleteObject( hbitmapWallPaper );
220     hbitmapWallPaper = hbitmap;
221     fTileWallPaper = GetProfileIntA( "desktop", "TileWallPaper", 0 );
222     if (hbitmap)
223     {
224         BITMAP bmp;
225         GetObjectA( hbitmap, sizeof(bmp), &bmp );
226         bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
227         bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
228     }
229     return TRUE;
230 }
231
232
233 /***********************************************************************
234  *           DESKTOP_SetPattern
235  *
236  * Set the desktop pattern.
237  */
238 BOOL DESKTOP_SetPattern( LPCSTR pattern )
239 {
240     int pat[8];
241
242     if (hbrushPattern) DeleteObject( hbrushPattern );
243     memset( pat, 0, sizeof(pat) );
244     if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
245                            &pat[0], &pat[1], &pat[2], &pat[3],
246                            &pat[4], &pat[5], &pat[6], &pat[7] ))
247     {
248         WORD pattern[8];
249         HBITMAP hbitmap;
250         int i;
251
252         for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
253         hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
254         hbrushPattern = CreatePatternBrush( hbitmap );
255         DeleteObject( hbitmap );
256     }
257     else hbrushPattern = 0;
258     return TRUE;
259 }