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