Added exception handling wrapper to a number of server requests.
[wine.git] / controls / desktop.c
blob859cfbaf842e63b7797efa13901110383ae5d6ac
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 "wingdi.h"
14 #include "heap.h"
15 #include "user.h"
16 #include "win.h"
17 #include "wine/winuser16.h"
20 /***********************************************************************
21 * DESKTOP_LoadBitmap
23 * Load a bitmap from a file. Used by SetDeskWallPaper().
25 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
27 BITMAPFILEHEADER *fileHeader;
28 BITMAPINFO *bitmapInfo;
29 HBITMAP hbitmap;
30 HFILE file;
31 LPSTR buffer;
32 LONG size;
34 /* Read all the file into memory */
36 if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
38 UINT len = GetWindowsDirectoryA( NULL, 0 );
39 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
40 len + strlen(filename) + 2 )))
41 return 0;
42 GetWindowsDirectoryA( buffer, len + 1 );
43 strcat( buffer, "\\" );
44 strcat( buffer, filename );
45 file = _lopen( buffer, OF_READ );
46 HeapFree( GetProcessHeap(), 0, buffer );
48 if (file == HFILE_ERROR) return 0;
49 size = _llseek( file, 0, 2 );
50 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
52 _lclose( file );
53 return 0;
55 _llseek( file, 0, 0 );
56 size = _lread( file, buffer, size );
57 _lclose( file );
58 fileHeader = (BITMAPFILEHEADER *)buffer;
59 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
61 /* Check header content */
62 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
64 HeapFree( GetProcessHeap(), 0, buffer );
65 return 0;
67 hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
68 buffer + fileHeader->bfOffBits,
69 bitmapInfo, DIB_RGB_COLORS );
70 HeapFree( GetProcessHeap(), 0, buffer );
71 return hbitmap;
75 /***********************************************************************
76 * DESKTOP_DoEraseBkgnd
78 * Handle the WM_ERASEBKGND message.
80 static LRESULT DESKTOP_DoEraseBkgnd( HWND hwnd, HDC hdc,
81 DESKTOP *desktopPtr )
83 RECT rect;
84 WND* Wnd = WIN_FindWndPtr( hwnd );
86 if (Wnd->hrgnUpdate > 1) DeleteObject( Wnd->hrgnUpdate );
87 Wnd->hrgnUpdate = 0;
89 WIN_ReleaseWndPtr(Wnd);
91 GetClientRect( hwnd, &rect );
93 /* Paint desktop pattern (only if wall paper does not cover everything) */
95 if (!desktopPtr->hbitmapWallPaper ||
96 (!desktopPtr->fTileWallPaper && ((desktopPtr->bitmapSize.cx < rect.right) ||
97 (desktopPtr->bitmapSize.cy < rect.bottom))))
99 /* Set colors in case pattern is a monochrome bitmap */
100 SetBkColor( hdc, RGB(0,0,0) );
101 SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
102 FillRect( hdc, &rect, desktopPtr->hbrushPattern );
105 /* Paint wall paper */
107 if (desktopPtr->hbitmapWallPaper)
109 INT x, y;
110 HDC hMemDC = CreateCompatibleDC( hdc );
112 SelectObject( hMemDC, desktopPtr->hbitmapWallPaper );
114 if (desktopPtr->fTileWallPaper)
116 for (y = 0; y < rect.bottom; y += desktopPtr->bitmapSize.cy)
117 for (x = 0; x < rect.right; x += desktopPtr->bitmapSize.cx)
118 BitBlt( hdc, x, y, desktopPtr->bitmapSize.cx,
119 desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
121 else
123 x = (rect.left + rect.right - desktopPtr->bitmapSize.cx) / 2;
124 y = (rect.top + rect.bottom - desktopPtr->bitmapSize.cy) / 2;
125 if (x < 0) x = 0;
126 if (y < 0) y = 0;
127 BitBlt( hdc, x, y, desktopPtr->bitmapSize.cx,
128 desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
130 DeleteDC( hMemDC );
133 return 1;
137 /***********************************************************************
138 * DesktopWndProc_locked
140 * Window procedure for the desktop window.
142 static inline LRESULT WINAPI DesktopWndProc_locked( WND *wndPtr, UINT message,
143 WPARAM wParam, LPARAM lParam )
145 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
146 HWND hwnd = wndPtr->hwndSelf;
148 /* Most messages are ignored (we DON'T call DefWindowProc) */
150 switch(message)
152 /* Warning: this message is sent directly by */
153 /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
154 case WM_NCCREATE:
155 desktopPtr->hbrushPattern = 0;
156 desktopPtr->hbitmapWallPaper = 0;
157 SetDeskPattern();
158 SetDeskWallPaper( (LPSTR)-1 );
159 return 1;
161 case WM_ERASEBKGND:
162 if(!USER_Driver.pIsSingleWindow())
163 return 1;
164 return DESKTOP_DoEraseBkgnd( hwnd, (HDC)wParam, desktopPtr );
166 case WM_SYSCOMMAND:
167 if ((wParam & 0xfff0) != SC_CLOSE)
168 return 0;
169 ExitWindows16( 0, 0 );
171 case WM_SETCURSOR:
172 return (LRESULT)SetCursor16( LoadCursor16( 0, IDC_ARROW16 ) );
175 return 0;
178 /***********************************************************************
179 * DesktopWndProc
181 * This is just a wrapper for the DesktopWndProc which does windows
182 * locking and unlocking.
184 LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message,
185 WPARAM wParam, LPARAM lParam )
187 WND *wndPtr = WIN_FindWndPtr( hwnd );
188 LRESULT retvalue = DesktopWndProc_locked(wndPtr,message,wParam,lParam);
190 WIN_ReleaseWndPtr(wndPtr);
191 return retvalue;
194 /***********************************************************************
195 * PaintDesktop (USER32.415)
198 BOOL WINAPI PaintDesktop(HDC hdc)
200 BOOL retvalue;
201 HWND hwnd = GetDesktopWindow();
202 WND *wndPtr = WIN_FindWndPtr( hwnd );
203 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
204 retvalue = DESKTOP_DoEraseBkgnd( hwnd, hdc, desktopPtr );
205 WIN_ReleaseWndPtr(wndPtr);
206 return retvalue;
210 /***********************************************************************
211 * SetDeskPattern (USER.279)
213 BOOL16 WINAPI SetDeskPattern(void)
215 char buffer[100];
216 GetProfileStringA( "desktop", "Pattern", "(None)", buffer, 100 );
217 return DESKTOP_SetPattern( buffer );
221 /***********************************************************************
222 * SetDeskWallPaper16 (USER.285)
224 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
226 return SetDeskWallPaper( filename );
230 /***********************************************************************
231 * SetDeskWallPaper (USER32.475)
233 * FIXME: is there a unicode version?
235 BOOL WINAPI SetDeskWallPaper( LPCSTR filename )
237 HBITMAP hbitmap;
238 HDC hdc;
239 char buffer[256];
240 WND *wndPtr = WIN_GetDesktop();
241 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
243 if (filename == (LPSTR)-1)
245 GetProfileStringA( "desktop", "WallPaper", "(None)", buffer, 256 );
246 filename = buffer;
248 hdc = GetDC( 0 );
249 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
250 ReleaseDC( 0, hdc );
251 if (desktopPtr->hbitmapWallPaper) DeleteObject( desktopPtr->hbitmapWallPaper );
252 desktopPtr->hbitmapWallPaper = hbitmap;
253 desktopPtr->fTileWallPaper = GetProfileIntA( "desktop", "TileWallPaper", 0 );
254 if (hbitmap)
256 BITMAP bmp;
257 GetObjectA( hbitmap, sizeof(bmp), &bmp );
258 desktopPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
259 desktopPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
261 WIN_ReleaseDesktop();
262 return TRUE;
266 /***********************************************************************
267 * DESKTOP_SetPattern
269 * Set the desktop pattern.
271 BOOL DESKTOP_SetPattern( LPCSTR pattern )
273 WND *wndPtr = WIN_GetDesktop();
274 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
275 int pat[8];
277 if (desktopPtr->hbrushPattern) DeleteObject( desktopPtr->hbrushPattern );
278 memset( pat, 0, sizeof(pat) );
279 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
280 &pat[0], &pat[1], &pat[2], &pat[3],
281 &pat[4], &pat[5], &pat[6], &pat[7] ))
283 WORD pattern[8];
284 HBITMAP hbitmap;
285 int i;
287 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
288 hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
289 desktopPtr->hbrushPattern = CreatePatternBrush( hbitmap );
290 DeleteObject( hbitmap );
292 else desktopPtr->hbrushPattern = CreateSolidBrush( GetSysColor(COLOR_BACKGROUND) );
293 WIN_ReleaseDesktop();
294 return TRUE;