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