- New implementation of SendMessage, ReceiveMessage, ReplyMessage functions
[wine] / controls / desktop.c
1 /*
2  * Desktop window class.
3  *
4  * Copyright 1994 Alexandre Julliard
5  */
6
7 #include "x11drv.h"
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 #include "desktop.h"
14 #include "windef.h"
15 #include "heap.h"
16 #include "monitor.h"
17 #include "win.h"
18 #include "wine/winuser16.h"
19
20 /***********************************************************************
21  *              DESKTOP_GetScreenWidth
22  *
23  * Return the width of the screen associated to the current desktop.
24  */
25 int DESKTOP_GetScreenWidth()
26 {
27   DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
28   return MONITOR_GetWidth(pDesktop->pPrimaryMonitor);
29 }
30
31 /***********************************************************************
32  *              DESKTOP_GetScreenHeight
33  *
34  * Return the height of the screen associated to the current desktop.
35  */
36 int DESKTOP_GetScreenHeight()
37 {
38   DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
39   return MONITOR_GetHeight(pDesktop->pPrimaryMonitor);
40 }
41
42 /***********************************************************************
43  *              DESKTOP_GetScreenDepth
44  *
45  * Return the depth of the screen associated to the current desktop.
46  */
47 int DESKTOP_GetScreenDepth()
48 {
49   DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
50   return MONITOR_GetDepth(pDesktop->pPrimaryMonitor);
51 }
52
53 /***********************************************************************
54  *           DESKTOP_LoadBitmap
55  *
56  * Load a bitmap from a file. Used by SetDeskWallPaper().
57  */
58 static HBITMAP32 DESKTOP_LoadBitmap( HDC32 hdc, const char *filename )
59 {
60     BITMAPFILEHEADER *fileHeader;
61     BITMAPINFO *bitmapInfo;
62     HBITMAP32 hbitmap;
63     HFILE32 file;
64     LPSTR buffer;
65     LONG size;
66
67     /* Read all the file into memory */
68
69     if ((file = _lopen32( filename, OF_READ )) == HFILE_ERROR32)
70     {
71         UINT32 len = GetWindowsDirectory32A( NULL, 0 );
72         if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
73                                   len + strlen(filename) + 2 )))
74             return 0;
75         GetWindowsDirectory32A( buffer, len + 1 );
76         strcat( buffer, "\\" );
77         strcat( buffer, filename );
78         file = _lopen32( buffer, OF_READ );
79         HeapFree( GetProcessHeap(), 0, buffer );
80     }
81     if (file == HFILE_ERROR32) return 0;
82     size = _llseek32( file, 0, 2 );
83     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
84     {
85         _lclose32( file );
86         return 0;
87     }
88     _llseek32( file, 0, 0 );
89     size = _lread32( file, buffer, size );
90     _lclose32( file );
91     fileHeader = (BITMAPFILEHEADER *)buffer;
92     bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
93     
94       /* Check header content */
95     if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
96     {
97         HeapFree( GetProcessHeap(), 0, buffer );
98         return 0;
99     }
100     hbitmap = CreateDIBitmap32( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
101                                 buffer + fileHeader->bfOffBits,
102                                 bitmapInfo, DIB_RGB_COLORS );
103     HeapFree( GetProcessHeap(), 0, buffer );
104     return hbitmap;
105 }
106
107
108 /***********************************************************************
109  *           DESKTOP_DoEraseBkgnd
110  *
111  * Handle the WM_ERASEBKGND message.
112  */
113 static LRESULT DESKTOP_DoEraseBkgnd( HWND32 hwnd, HDC32 hdc,
114                                      DESKTOP *desktopPtr )
115 {
116     RECT32 rect;
117     WND*   Wnd = WIN_FindWndPtr( hwnd );
118
119     if (Wnd->hrgnUpdate > 1) DeleteObject32( Wnd->hrgnUpdate );
120     Wnd->hrgnUpdate = 0;
121
122     GetClientRect32( hwnd, &rect );    
123
124     /* Paint desktop pattern (only if wall paper does not cover everything) */
125
126     if (!desktopPtr->hbitmapWallPaper || 
127         (!desktopPtr->fTileWallPaper && ((desktopPtr->bitmapSize.cx < rect.right) ||
128          (desktopPtr->bitmapSize.cy < rect.bottom))))
129     {
130           /* Set colors in case pattern is a monochrome bitmap */
131         SetBkColor32( hdc, RGB(0,0,0) );
132         SetTextColor32( hdc, GetSysColor32(COLOR_BACKGROUND) );
133         FillRect32( hdc, &rect, desktopPtr->hbrushPattern );
134     }
135
136       /* Paint wall paper */
137
138     if (desktopPtr->hbitmapWallPaper)
139     {
140         INT32 x, y;
141         HDC32 hMemDC = CreateCompatibleDC32( hdc );
142         
143         SelectObject32( hMemDC, desktopPtr->hbitmapWallPaper );
144
145         if (desktopPtr->fTileWallPaper)
146         {
147             for (y = 0; y < rect.bottom; y += desktopPtr->bitmapSize.cy)
148                 for (x = 0; x < rect.right; x += desktopPtr->bitmapSize.cx)
149                     BitBlt32( hdc, x, y, desktopPtr->bitmapSize.cx,
150                               desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
151         }
152         else
153         {
154             x = (rect.left + rect.right - desktopPtr->bitmapSize.cx) / 2;
155             y = (rect.top + rect.bottom - desktopPtr->bitmapSize.cy) / 2;
156             if (x < 0) x = 0;
157             if (y < 0) y = 0;
158             BitBlt32( hdc, x, y, desktopPtr->bitmapSize.cx,
159                       desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
160         }
161         DeleteDC32( hMemDC );
162     }
163
164     return 1;
165 }
166
167
168 /***********************************************************************
169  *           DesktopWndProc
170  *
171  * Window procedure for the desktop window.
172  */
173 LRESULT WINAPI DesktopWndProc( HWND32 hwnd, UINT32 message,
174                                WPARAM32 wParam, LPARAM lParam )
175 {
176     WND *wndPtr = WIN_FindWndPtr( hwnd );
177     DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
178
179       /* Most messages are ignored (we DON'T call DefWindowProc) */
180
181     switch(message)
182     {
183         /* Warning: this message is sent directly by                     */
184         /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
185     case WM_NCCREATE:
186         desktopPtr->hbrushPattern = 0;
187         desktopPtr->hbitmapWallPaper = 0;
188         SetDeskPattern();
189         SetDeskWallPaper32( (LPSTR)-1 );
190         return 1;
191         
192     case WM_ERASEBKGND:
193         if (X11DRV_WND_GetXRootWindow(wndPtr) == 
194             DefaultRootWindow(display))
195           return 1;
196         return DESKTOP_DoEraseBkgnd( hwnd, (HDC32)wParam, desktopPtr );
197
198     case WM_SYSCOMMAND:
199         if ((wParam & 0xfff0) != SC_CLOSE) return 0;
200         ExitWindows16( 0, 0 ); 
201
202     case WM_SETCURSOR:
203         return (LRESULT)SetCursor16( LoadCursor16( 0, IDC_ARROW16 ) );
204     }
205     
206     return 0;
207 }
208
209 /***********************************************************************
210  *           PaintDesktop   (USER32.415)
211  *
212  */
213 BOOL32 WINAPI PaintDesktop(HDC32 hdc)
214 {
215     HWND32 hwnd = GetDesktopWindow32();
216     WND *wndPtr = WIN_FindWndPtr( hwnd );
217     DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
218
219     return DESKTOP_DoEraseBkgnd( hwnd, hdc, desktopPtr );
220 }
221
222 /***********************************************************************
223  *           SetDeskPattern   (USER.279)
224  */
225 BOOL16 WINAPI SetDeskPattern(void)
226 {
227     char buffer[100];
228     GetProfileString32A( "desktop", "Pattern", "(None)", buffer, 100 );
229     return DESKTOP_SetPattern( buffer );
230 }
231
232
233 /***********************************************************************
234  *           SetDeskWallPaper16   (USER.285)
235  */
236 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
237 {
238     return SetDeskWallPaper32( filename );
239 }
240
241
242 /***********************************************************************
243  *           SetDeskWallPaper32   (USER32.475)
244  *
245  * FIXME: is there a unicode version?
246  */
247 BOOL32 WINAPI SetDeskWallPaper32( LPCSTR filename )
248 {
249     HBITMAP32 hbitmap;
250     HDC32 hdc;
251     char buffer[256];
252     WND *wndPtr = WIN_GetDesktop();
253     DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
254
255     if (filename == (LPSTR)-1)
256     {
257         GetProfileString32A( "desktop", "WallPaper", "(None)", buffer, 256 );
258         filename = buffer;
259     }
260     hdc = GetDC32( 0 );
261     hbitmap = DESKTOP_LoadBitmap( hdc, filename );
262     ReleaseDC32( 0, hdc );
263     if (desktopPtr->hbitmapWallPaper) DeleteObject32( desktopPtr->hbitmapWallPaper );
264     desktopPtr->hbitmapWallPaper = hbitmap;
265     desktopPtr->fTileWallPaper = GetProfileInt32A( "desktop", "TileWallPaper", 0 );
266     if (hbitmap)
267     {
268         BITMAP32 bmp;
269         GetObject32A( hbitmap, sizeof(bmp), &bmp );
270         desktopPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
271         desktopPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
272     }
273     return TRUE;
274 }
275
276
277 /***********************************************************************
278  *           DESKTOP_SetPattern
279  *
280  * Set the desktop pattern.
281  */
282 BOOL32 DESKTOP_SetPattern( LPCSTR pattern )
283 {
284     WND *wndPtr = WIN_GetDesktop();
285     DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
286     int pat[8];
287
288     if (desktopPtr->hbrushPattern) DeleteObject32( desktopPtr->hbrushPattern );
289     memset( pat, 0, sizeof(pat) );
290     if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
291                            &pat[0], &pat[1], &pat[2], &pat[3],
292                            &pat[4], &pat[5], &pat[6], &pat[7] ))
293     {
294         WORD pattern[8];
295         HBITMAP32 hbitmap;
296         int i;
297
298         for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
299         hbitmap = CreateBitmap32( 8, 8, 1, 1, (LPSTR)pattern );
300         desktopPtr->hbrushPattern = CreatePatternBrush32( hbitmap );
301         DeleteObject32( hbitmap );
302     }
303     else desktopPtr->hbrushPattern = CreateSolidBrush32( GetSysColor32(COLOR_BACKGROUND) );
304     return TRUE;
305 }
306