Replaced LocalToWideChar() by lstrcpynAtoW(), WideCharToLocal() by
[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 "desktop.h"
12 #include "windef.h"
13 #include "heap.h"
14 #include "monitor.h"
15 #include "win.h"
16 #include "wine/winuser16.h"
17
18 /**********************************************************************/
19
20 DESKTOP_DRIVER *DESKTOP_Driver = NULL;
21
22 /***********************************************************************
23  *              DESKTOP_IsSingleWindow
24  */
25 BOOL DESKTOP_IsSingleWindow(void)
26 {
27   BOOL retvalue;
28   DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
29   retvalue = MONITOR_IsSingleWindow(pDesktop->pPrimaryMonitor);
30   WIN_ReleaseDesktop();
31   return retvalue;
32 }
33
34 /***********************************************************************
35  *              DESKTOP_GetScreenWidth
36  *
37  * Return the width of the screen associated to the current desktop.
38  */
39 int DESKTOP_GetScreenWidth()
40 {
41     int retvalue;
42   DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
43     retvalue = MONITOR_GetWidth(pDesktop->pPrimaryMonitor);
44     WIN_ReleaseDesktop();
45     return retvalue;
46     
47 }
48
49 /***********************************************************************
50  *              DESKTOP_GetScreenHeight
51  *
52  * Return the height of the screen associated to the current desktop.
53  */
54 int DESKTOP_GetScreenHeight()
55 {
56     int retvalue;
57   DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
58     retvalue = MONITOR_GetHeight(pDesktop->pPrimaryMonitor);
59     WIN_ReleaseDesktop();
60     return retvalue;
61     
62 }
63
64 /***********************************************************************
65  *              DESKTOP_GetScreenDepth
66  *
67  * Return the depth of the screen associated to the current desktop.
68  */
69 int DESKTOP_GetScreenDepth()
70 {
71     int retvalue;
72   DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
73     retvalue = MONITOR_GetDepth(pDesktop->pPrimaryMonitor);
74     WIN_ReleaseDesktop();
75     return retvalue;
76
77 }
78
79 /***********************************************************************
80  *           DESKTOP_LoadBitmap
81  *
82  * Load a bitmap from a file. Used by SetDeskWallPaper().
83  */
84 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
85 {
86     BITMAPFILEHEADER *fileHeader;
87     BITMAPINFO *bitmapInfo;
88     HBITMAP hbitmap;
89     HFILE file;
90     LPSTR buffer;
91     LONG size;
92
93     /* Read all the file into memory */
94
95     if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
96     {
97         UINT len = GetWindowsDirectoryA( NULL, 0 );
98         if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
99                                   len + strlen(filename) + 2 )))
100             return 0;
101         GetWindowsDirectoryA( buffer, len + 1 );
102         strcat( buffer, "\\" );
103         strcat( buffer, filename );
104         file = _lopen( buffer, OF_READ );
105         HeapFree( GetProcessHeap(), 0, buffer );
106     }
107     if (file == HFILE_ERROR) return 0;
108     size = _llseek( file, 0, 2 );
109     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
110     {
111         _lclose( file );
112         return 0;
113     }
114     _llseek( file, 0, 0 );
115     size = _lread( file, buffer, size );
116     _lclose( file );
117     fileHeader = (BITMAPFILEHEADER *)buffer;
118     bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
119     
120       /* Check header content */
121     if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
122     {
123         HeapFree( GetProcessHeap(), 0, buffer );
124         return 0;
125     }
126     hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
127                                 buffer + fileHeader->bfOffBits,
128                                 bitmapInfo, DIB_RGB_COLORS );
129     HeapFree( GetProcessHeap(), 0, buffer );
130     return hbitmap;
131 }
132
133
134 /***********************************************************************
135  *           DESKTOP_DoEraseBkgnd
136  *
137  * Handle the WM_ERASEBKGND message.
138  */
139 static LRESULT DESKTOP_DoEraseBkgnd( HWND hwnd, HDC hdc,
140                                      DESKTOP *desktopPtr )
141 {
142     RECT rect;
143     WND*   Wnd = WIN_FindWndPtr( hwnd );
144
145     if (Wnd->hrgnUpdate > 1) DeleteObject( Wnd->hrgnUpdate );
146     Wnd->hrgnUpdate = 0;
147
148     WIN_ReleaseWndPtr(Wnd);
149     
150     GetClientRect( hwnd, &rect );    
151
152     /* Paint desktop pattern (only if wall paper does not cover everything) */
153
154     if (!desktopPtr->hbitmapWallPaper || 
155         (!desktopPtr->fTileWallPaper && ((desktopPtr->bitmapSize.cx < rect.right) ||
156          (desktopPtr->bitmapSize.cy < rect.bottom))))
157     {
158           /* Set colors in case pattern is a monochrome bitmap */
159         SetBkColor( hdc, RGB(0,0,0) );
160         SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
161         FillRect( hdc, &rect, desktopPtr->hbrushPattern );
162     }
163
164       /* Paint wall paper */
165
166     if (desktopPtr->hbitmapWallPaper)
167     {
168         INT x, y;
169         HDC hMemDC = CreateCompatibleDC( hdc );
170         
171         SelectObject( hMemDC, desktopPtr->hbitmapWallPaper );
172
173         if (desktopPtr->fTileWallPaper)
174         {
175             for (y = 0; y < rect.bottom; y += desktopPtr->bitmapSize.cy)
176                 for (x = 0; x < rect.right; x += desktopPtr->bitmapSize.cx)
177                     BitBlt( hdc, x, y, desktopPtr->bitmapSize.cx,
178                               desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
179         }
180         else
181         {
182             x = (rect.left + rect.right - desktopPtr->bitmapSize.cx) / 2;
183             y = (rect.top + rect.bottom - desktopPtr->bitmapSize.cy) / 2;
184             if (x < 0) x = 0;
185             if (y < 0) y = 0;
186             BitBlt( hdc, x, y, desktopPtr->bitmapSize.cx,
187                       desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
188         }
189         DeleteDC( hMemDC );
190     }
191
192     return 1;
193 }
194
195
196 /***********************************************************************
197  *           DesktopWndProc_locked
198  *
199  * Window procedure for the desktop window.
200  */
201 static inline LRESULT WINAPI DesktopWndProc_locked( WND *wndPtr, UINT message,
202                                WPARAM wParam, LPARAM lParam )
203 {
204     DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
205     HWND hwnd = wndPtr->hwndSelf;
206
207       /* Most messages are ignored (we DON'T call DefWindowProc) */
208
209     switch(message)
210     {
211         /* Warning: this message is sent directly by                     */
212         /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
213     case WM_NCCREATE:
214         desktopPtr->hbrushPattern = 0;
215         desktopPtr->hbitmapWallPaper = 0;
216         SetDeskPattern();
217         SetDeskWallPaper( (LPSTR)-1 );
218         return  1;
219         
220     case WM_ERASEBKGND:
221         if(!DESKTOP_IsSingleWindow())
222             return  1;
223         return  DESKTOP_DoEraseBkgnd( hwnd, (HDC)wParam, desktopPtr );
224
225     case WM_SYSCOMMAND:
226         if ((wParam & 0xfff0) != SC_CLOSE)
227             return  0;
228         ExitWindows16( 0, 0 ); 
229
230     case WM_SETCURSOR:
231         return  (LRESULT)SetCursor16( LoadCursor16( 0, IDC_ARROW16 ) );
232     }
233     
234     return 0;
235 }
236
237 /***********************************************************************
238  *           DesktopWndProc
239  *
240  * This is just a wrapper for the DesktopWndProc which does windows
241  * locking and unlocking.
242  */
243 LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message,
244                                WPARAM wParam, LPARAM lParam )
245 {
246     WND *wndPtr = WIN_FindWndPtr( hwnd );
247     LRESULT retvalue = DesktopWndProc_locked(wndPtr,message,wParam,lParam);
248
249     WIN_ReleaseWndPtr(wndPtr);
250     return retvalue;
251 }
252
253 /***********************************************************************
254  *           PaintDesktop   (USER32.415)
255  *
256  */
257 BOOL WINAPI PaintDesktop(HDC hdc)
258 {
259     BOOL retvalue;
260     HWND hwnd = GetDesktopWindow();
261     WND *wndPtr = WIN_FindWndPtr( hwnd );
262     DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
263     retvalue = DESKTOP_DoEraseBkgnd( hwnd, hdc, desktopPtr );
264     WIN_ReleaseWndPtr(wndPtr);
265     return retvalue;
266
267 }
268
269 /***********************************************************************
270  *           SetDeskPattern   (USER.279)
271  */
272 BOOL16 WINAPI SetDeskPattern(void)
273 {
274     char buffer[100];
275     GetProfileStringA( "desktop", "Pattern", "(None)", buffer, 100 );
276     return DESKTOP_SetPattern( buffer );
277 }
278
279
280 /***********************************************************************
281  *           SetDeskWallPaper16   (USER.285)
282  */
283 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
284 {
285     return SetDeskWallPaper( filename );
286 }
287
288
289 /***********************************************************************
290  *           SetDeskWallPaper32   (USER32.475)
291  *
292  * FIXME: is there a unicode version?
293  */
294 BOOL WINAPI SetDeskWallPaper( LPCSTR filename )
295 {
296     HBITMAP hbitmap;
297     HDC hdc;
298     char buffer[256];
299     WND *wndPtr = WIN_GetDesktop();
300     DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
301
302     if (filename == (LPSTR)-1)
303     {
304         GetProfileStringA( "desktop", "WallPaper", "(None)", buffer, 256 );
305         filename = buffer;
306     }
307     hdc = GetDC( 0 );
308     hbitmap = DESKTOP_LoadBitmap( hdc, filename );
309     ReleaseDC( 0, hdc );
310     if (desktopPtr->hbitmapWallPaper) DeleteObject( desktopPtr->hbitmapWallPaper );
311     desktopPtr->hbitmapWallPaper = hbitmap;
312     desktopPtr->fTileWallPaper = GetProfileIntA( "desktop", "TileWallPaper", 0 );
313     if (hbitmap)
314     {
315         BITMAP bmp;
316         GetObjectA( hbitmap, sizeof(bmp), &bmp );
317         desktopPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
318         desktopPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
319     }
320     WIN_ReleaseDesktop();
321     return TRUE;
322 }
323
324
325 /***********************************************************************
326  *           DESKTOP_SetPattern
327  *
328  * Set the desktop pattern.
329  */
330 BOOL DESKTOP_SetPattern( LPCSTR pattern )
331 {
332     WND *wndPtr = WIN_GetDesktop();
333     DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
334     int pat[8];
335
336     if (desktopPtr->hbrushPattern) DeleteObject( desktopPtr->hbrushPattern );
337     memset( pat, 0, sizeof(pat) );
338     if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
339                            &pat[0], &pat[1], &pat[2], &pat[3],
340                            &pat[4], &pat[5], &pat[6], &pat[7] ))
341     {
342         WORD pattern[8];
343         HBITMAP hbitmap;
344         int i;
345
346         for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
347         hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
348         desktopPtr->hbrushPattern = CreatePatternBrush( hbitmap );
349         DeleteObject( hbitmap );
350     }
351     else desktopPtr->hbrushPattern = CreateSolidBrush( GetSysColor(COLOR_BACKGROUND) );
352     WIN_ReleaseDesktop();
353     return TRUE;
354 }
355