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