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