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