Fixed potential buffer overflows (spotted by Francois Gouget).
[wine/multimedia.git] / controls / desktop.c
blobc170f2cd0e11d814aaecb323ac669a22198a5c30
1 /*
2 * Desktop window class.
4 * Copyright 1994 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
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"
18 /**********************************************************************/
20 DESKTOP_DRIVER *DESKTOP_Driver = NULL;
22 /***********************************************************************
23 * DESKTOP_IsSingleWindow
25 BOOL DESKTOP_IsSingleWindow(void)
27 BOOL retvalue;
28 DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
29 retvalue = MONITOR_IsSingleWindow(pDesktop->pPrimaryMonitor);
30 WIN_ReleaseDesktop();
31 return retvalue;
34 /***********************************************************************
35 * DESKTOP_GetScreenWidth
37 * Return the width of the screen associated to the current desktop.
39 int DESKTOP_GetScreenWidth()
41 int retvalue;
42 DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
43 retvalue = MONITOR_GetWidth(pDesktop->pPrimaryMonitor);
44 WIN_ReleaseDesktop();
45 return retvalue;
49 /***********************************************************************
50 * DESKTOP_GetScreenHeight
52 * Return the height of the screen associated to the current desktop.
54 int DESKTOP_GetScreenHeight()
56 int retvalue;
57 DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
58 retvalue = MONITOR_GetHeight(pDesktop->pPrimaryMonitor);
59 WIN_ReleaseDesktop();
60 return retvalue;
64 /***********************************************************************
65 * DESKTOP_GetScreenDepth
67 * Return the depth of the screen associated to the current desktop.
69 int DESKTOP_GetScreenDepth()
71 int retvalue;
72 DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
73 retvalue = MONITOR_GetDepth(pDesktop->pPrimaryMonitor);
74 WIN_ReleaseDesktop();
75 return retvalue;
79 /***********************************************************************
80 * DESKTOP_LoadBitmap
82 * Load a bitmap from a file. Used by SetDeskWallPaper().
84 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
86 BITMAPFILEHEADER *fileHeader;
87 BITMAPINFO *bitmapInfo;
88 HBITMAP hbitmap;
89 HFILE file;
90 LPSTR buffer;
91 LONG size;
93 /* Read all the file into memory */
95 if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
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 );
107 if (file == HFILE_ERROR) return 0;
108 size = _llseek( file, 0, 2 );
109 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
111 _lclose( file );
112 return 0;
114 _llseek( file, 0, 0 );
115 size = _lread( file, buffer, size );
116 _lclose( file );
117 fileHeader = (BITMAPFILEHEADER *)buffer;
118 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
120 /* Check header content */
121 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
123 HeapFree( GetProcessHeap(), 0, buffer );
124 return 0;
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;
134 /***********************************************************************
135 * DESKTOP_DoEraseBkgnd
137 * Handle the WM_ERASEBKGND message.
139 static LRESULT DESKTOP_DoEraseBkgnd( HWND hwnd, HDC hdc,
140 DESKTOP *desktopPtr )
142 RECT rect;
143 WND* Wnd = WIN_FindWndPtr( hwnd );
145 if (Wnd->hrgnUpdate > 1) DeleteObject( Wnd->hrgnUpdate );
146 Wnd->hrgnUpdate = 0;
148 WIN_ReleaseWndPtr(Wnd);
150 GetClientRect( hwnd, &rect );
152 /* Paint desktop pattern (only if wall paper does not cover everything) */
154 if (!desktopPtr->hbitmapWallPaper ||
155 (!desktopPtr->fTileWallPaper && ((desktopPtr->bitmapSize.cx < rect.right) ||
156 (desktopPtr->bitmapSize.cy < rect.bottom))))
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 );
164 /* Paint wall paper */
166 if (desktopPtr->hbitmapWallPaper)
168 INT x, y;
169 HDC hMemDC = CreateCompatibleDC( hdc );
171 SelectObject( hMemDC, desktopPtr->hbitmapWallPaper );
173 if (desktopPtr->fTileWallPaper)
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 );
180 else
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 );
189 DeleteDC( hMemDC );
192 return 1;
196 /***********************************************************************
197 * DesktopWndProc_locked
199 * Window procedure for the desktop window.
201 static inline LRESULT WINAPI DesktopWndProc_locked( WND *wndPtr, UINT message,
202 WPARAM wParam, LPARAM lParam )
204 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
205 HWND hwnd = wndPtr->hwndSelf;
207 /* Most messages are ignored (we DON'T call DefWindowProc) */
209 switch(message)
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;
220 case WM_ERASEBKGND:
221 if(!DESKTOP_IsSingleWindow())
222 return 1;
223 return DESKTOP_DoEraseBkgnd( hwnd, (HDC)wParam, desktopPtr );
225 case WM_SYSCOMMAND:
226 if ((wParam & 0xfff0) != SC_CLOSE)
227 return 0;
228 ExitWindows16( 0, 0 );
230 case WM_SETCURSOR:
231 return (LRESULT)SetCursor16( LoadCursor16( 0, IDC_ARROW16 ) );
234 return 0;
237 /***********************************************************************
238 * DesktopWndProc
240 * This is just a wrapper for the DesktopWndProc which does windows
241 * locking and unlocking.
243 LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message,
244 WPARAM wParam, LPARAM lParam )
246 WND *wndPtr = WIN_FindWndPtr( hwnd );
247 LRESULT retvalue = DesktopWndProc_locked(wndPtr,message,wParam,lParam);
249 WIN_ReleaseWndPtr(wndPtr);
250 return retvalue;
253 /***********************************************************************
254 * PaintDesktop (USER32.415)
257 BOOL WINAPI PaintDesktop(HDC hdc)
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;
269 /***********************************************************************
270 * SetDeskPattern (USER.279)
272 BOOL16 WINAPI SetDeskPattern(void)
274 char buffer[100];
275 GetProfileStringA( "desktop", "Pattern", "(None)", buffer, 100 );
276 return DESKTOP_SetPattern( buffer );
280 /***********************************************************************
281 * SetDeskWallPaper16 (USER.285)
283 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
285 return SetDeskWallPaper( filename );
289 /***********************************************************************
290 * SetDeskWallPaper32 (USER32.475)
292 * FIXME: is there a unicode version?
294 BOOL WINAPI SetDeskWallPaper( LPCSTR filename )
296 HBITMAP hbitmap;
297 HDC hdc;
298 char buffer[256];
299 WND *wndPtr = WIN_GetDesktop();
300 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
302 if (filename == (LPSTR)-1)
304 GetProfileStringA( "desktop", "WallPaper", "(None)", buffer, 256 );
305 filename = buffer;
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)
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;
320 WIN_ReleaseDesktop();
321 return TRUE;
325 /***********************************************************************
326 * DESKTOP_SetPattern
328 * Set the desktop pattern.
330 BOOL DESKTOP_SetPattern( LPCSTR pattern )
332 WND *wndPtr = WIN_GetDesktop();
333 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
334 int pat[8];
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] ))
342 WORD pattern[8];
343 HBITMAP hbitmap;
344 int i;
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 );
351 else desktopPtr->hbrushPattern = CreateSolidBrush( GetSysColor(COLOR_BACKGROUND) );
352 WIN_ReleaseDesktop();
353 return TRUE;