Added LGPL standard comment, and copyright notices where necessary.
[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 <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "windef.h"
26 #include "wingdi.h"
27 #include "user.h"
28 #include "controls.h"
29 #include "wine/winuser16.h"
30
31 static HBRUSH hbrushPattern;
32 static HBITMAP hbitmapWallPaper;
33 static SIZE bitmapSize;
34 static BOOL fTileWallPaper;
35
36 static LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
37
38
39 /*********************************************************************
40  * desktop class descriptor
41  */
42 const struct builtin_class_descr DESKTOP_builtin_class =
43 {
44     DESKTOP_CLASS_ATOM,   /* name */
45     CS_GLOBALCLASS,       /* style */
46     NULL,                 /* procA (winproc is Unicode only) */
47     DesktopWndProc,       /* procW */
48     0,                    /* extra */
49     IDC_ARROWA,           /* cursor */
50     COLOR_BACKGROUND+1    /* brush */
51 };
52
53
54 /***********************************************************************
55  *           DESKTOP_LoadBitmap
56  *
57  * Load a bitmap from a file. Used by SetDeskWallPaper().
58  */
59 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
60 {
61     BITMAPFILEHEADER *fileHeader;
62     BITMAPINFO *bitmapInfo;
63     HBITMAP hbitmap;
64     HFILE file;
65     LPSTR buffer;
66     LONG size;
67
68     /* Read all the file into memory */
69
70     if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
71     {
72         UINT len = GetWindowsDirectoryA( NULL, 0 );
73         if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
74                                   len + strlen(filename) + 2 )))
75             return 0;
76         GetWindowsDirectoryA( buffer, len + 1 );
77         strcat( buffer, "\\" );
78         strcat( buffer, filename );
79         file = _lopen( buffer, OF_READ );
80         HeapFree( GetProcessHeap(), 0, buffer );
81     }
82     if (file == HFILE_ERROR) return 0;
83     size = _llseek( file, 0, 2 );
84     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
85     {
86         _lclose( file );
87         return 0;
88     }
89     _llseek( file, 0, 0 );
90     size = _lread( file, buffer, size );
91     _lclose( file );
92     fileHeader = (BITMAPFILEHEADER *)buffer;
93     bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
94     
95       /* Check header content */
96     if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
97     {
98         HeapFree( GetProcessHeap(), 0, buffer );
99         return 0;
100     }
101     hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
102                                 buffer + fileHeader->bfOffBits,
103                                 bitmapInfo, DIB_RGB_COLORS );
104     HeapFree( GetProcessHeap(), 0, buffer );
105     return hbitmap;
106 }
107
108
109
110 /***********************************************************************
111  *           DesktopWndProc
112  */
113 static LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
114 {
115     LRESULT retvalue = 0;
116
117     if (message == WM_NCCREATE)
118     {
119         hbrushPattern = 0;
120         hbitmapWallPaper = 0;
121         SetDeskPattern();
122         SetDeskWallPaper( (LPSTR)-1 );
123         retvalue = 1;
124     }
125     /* all other messages are ignored */
126     return retvalue;
127 }
128
129 /***********************************************************************
130  *           PaintDesktop   (USER32.@)
131  *
132  */
133 BOOL WINAPI PaintDesktop(HDC hdc)
134 {
135     HWND hwnd = GetDesktopWindow();
136
137     /* check for an owning thread; otherwise don't paint anything (non-desktop mode) */
138     if (GetWindowThreadProcessId( hwnd, NULL ))
139     {
140         RECT rect;
141
142         GetClientRect( hwnd, &rect );
143
144         /* Paint desktop pattern (only if wall paper does not cover everything) */
145
146         if (!hbitmapWallPaper ||
147             (!fTileWallPaper && ((bitmapSize.cx < rect.right) || (bitmapSize.cy < rect.bottom))))
148         {
149             HBRUSH brush = hbrushPattern;
150             if (!brush) brush = GetClassLongA( hwnd, GCL_HBRBACKGROUND );
151             /* Set colors in case pattern is a monochrome bitmap */
152             SetBkColor( hdc, RGB(0,0,0) );
153             SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
154             FillRect( hdc, &rect, brush );
155         }
156
157         /* Paint wall paper */
158
159         if (hbitmapWallPaper)
160         {
161             INT x, y;
162             HDC hMemDC = CreateCompatibleDC( hdc );
163
164             SelectObject( hMemDC, hbitmapWallPaper );
165
166             if (fTileWallPaper)
167             {
168                 for (y = 0; y < rect.bottom; y += bitmapSize.cy)
169                     for (x = 0; x < rect.right; x += bitmapSize.cx)
170                         BitBlt( hdc, x, y, bitmapSize.cx, bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
171             }
172             else
173             {
174                 x = (rect.left + rect.right - bitmapSize.cx) / 2;
175                 y = (rect.top + rect.bottom - bitmapSize.cy) / 2;
176                 if (x < 0) x = 0;
177                 if (y < 0) y = 0;
178                 BitBlt( hdc, x, y, bitmapSize.cx, bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
179             }
180             DeleteDC( hMemDC );
181         }
182     }
183     return TRUE;
184 }
185
186 /***********************************************************************
187  *           OldSetDeskPattern   (USER.279)
188  */
189 BOOL16 WINAPI SetDeskPattern(void)
190 {
191     char buffer[100];
192     GetProfileStringA( "desktop", "Pattern", "(None)", buffer, 100 );
193     return DESKTOP_SetPattern( buffer );
194 }
195
196
197 /***********************************************************************
198  *           SetDeskWallPaper   (USER.285)
199  */
200 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
201 {
202     return SetDeskWallPaper( filename );
203 }
204
205
206 /***********************************************************************
207  *           SetDeskWallPaper   (USER32.@)
208  *
209  * FIXME: is there a unicode version?
210  */
211 BOOL WINAPI SetDeskWallPaper( LPCSTR filename )
212 {
213     HBITMAP hbitmap;
214     HDC hdc;
215     char buffer[256];
216
217     if (filename == (LPSTR)-1)
218     {
219         GetProfileStringA( "desktop", "WallPaper", "(None)", buffer, 256 );
220         filename = buffer;
221     }
222     hdc = GetDC( 0 );
223     hbitmap = DESKTOP_LoadBitmap( hdc, filename );
224     ReleaseDC( 0, hdc );
225     if (hbitmapWallPaper) DeleteObject( hbitmapWallPaper );
226     hbitmapWallPaper = hbitmap;
227     fTileWallPaper = GetProfileIntA( "desktop", "TileWallPaper", 0 );
228     if (hbitmap)
229     {
230         BITMAP bmp;
231         GetObjectA( hbitmap, sizeof(bmp), &bmp );
232         bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
233         bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
234     }
235     return TRUE;
236 }
237
238
239 /***********************************************************************
240  *           DESKTOP_SetPattern
241  *
242  * Set the desktop pattern.
243  */
244 BOOL DESKTOP_SetPattern( LPCSTR pattern )
245 {
246     int pat[8];
247
248     if (hbrushPattern) DeleteObject( 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         HBITMAP hbitmap;
256         int i;
257
258         for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
259         hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
260         hbrushPattern = CreatePatternBrush( hbitmap );
261         DeleteObject( hbitmap );
262     }
263     else hbrushPattern = 0;
264     return TRUE;
265 }
266