Handle suspend/resume_thread requests in phase STARTING correctly.
[wine/hacks.git] / controls / desktop.c
blobf2573f820699d4e1717877d53cdc89e9b763dc02
1 /*
2 * Desktop window class.
4 * Copyright 1994 Alexandre Julliard
5 */
7 #include "x11drv.h"
9 #include <stdio.h>
10 #include <string.h>
11 #include <unistd.h>
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"
20 /***********************************************************************
21 * DESKTOP_GetScreenWidth
23 * Return the width of the screen associated to the current desktop.
25 int DESKTOP_GetScreenWidth()
27 int retvalue;
28 DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
29 retvalue = MONITOR_GetWidth(pDesktop->pPrimaryMonitor);
30 WIN_ReleaseDesktop();
31 return retvalue;
35 /***********************************************************************
36 * DESKTOP_GetScreenHeight
38 * Return the height of the screen associated to the current desktop.
40 int DESKTOP_GetScreenHeight()
42 int retvalue;
43 DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
44 retvalue = MONITOR_GetHeight(pDesktop->pPrimaryMonitor);
45 WIN_ReleaseDesktop();
46 return retvalue;
50 /***********************************************************************
51 * DESKTOP_GetScreenDepth
53 * Return the depth of the screen associated to the current desktop.
55 int DESKTOP_GetScreenDepth()
57 int retvalue;
58 DESKTOP *pDesktop = (DESKTOP *) WIN_GetDesktop()->wExtra;
59 retvalue = MONITOR_GetDepth(pDesktop->pPrimaryMonitor);
60 WIN_ReleaseDesktop();
61 return retvalue;
65 /***********************************************************************
66 * DESKTOP_LoadBitmap
68 * Load a bitmap from a file. Used by SetDeskWallPaper().
70 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
72 BITMAPFILEHEADER *fileHeader;
73 BITMAPINFO *bitmapInfo;
74 HBITMAP hbitmap;
75 HFILE file;
76 LPSTR buffer;
77 LONG size;
79 /* Read all the file into memory */
81 if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
83 UINT len = GetWindowsDirectoryA( NULL, 0 );
84 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
85 len + strlen(filename) + 2 )))
86 return 0;
87 GetWindowsDirectoryA( buffer, len + 1 );
88 strcat( buffer, "\\" );
89 strcat( buffer, filename );
90 file = _lopen( buffer, OF_READ );
91 HeapFree( GetProcessHeap(), 0, buffer );
93 if (file == HFILE_ERROR) return 0;
94 size = _llseek( file, 0, 2 );
95 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
97 _lclose( file );
98 return 0;
100 _llseek( file, 0, 0 );
101 size = _lread( file, buffer, size );
102 _lclose( file );
103 fileHeader = (BITMAPFILEHEADER *)buffer;
104 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
106 /* Check header content */
107 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
109 HeapFree( GetProcessHeap(), 0, buffer );
110 return 0;
112 hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
113 buffer + fileHeader->bfOffBits,
114 bitmapInfo, DIB_RGB_COLORS );
115 HeapFree( GetProcessHeap(), 0, buffer );
116 return hbitmap;
120 /***********************************************************************
121 * DESKTOP_DoEraseBkgnd
123 * Handle the WM_ERASEBKGND message.
125 static LRESULT DESKTOP_DoEraseBkgnd( HWND hwnd, HDC hdc,
126 DESKTOP *desktopPtr )
128 RECT rect;
129 WND* Wnd = WIN_FindWndPtr( hwnd );
131 if (Wnd->hrgnUpdate > 1) DeleteObject( Wnd->hrgnUpdate );
132 Wnd->hrgnUpdate = 0;
134 WIN_ReleaseWndPtr(Wnd);
136 GetClientRect( hwnd, &rect );
138 /* Paint desktop pattern (only if wall paper does not cover everything) */
140 if (!desktopPtr->hbitmapWallPaper ||
141 (!desktopPtr->fTileWallPaper && ((desktopPtr->bitmapSize.cx < rect.right) ||
142 (desktopPtr->bitmapSize.cy < rect.bottom))))
144 /* Set colors in case pattern is a monochrome bitmap */
145 SetBkColor( hdc, RGB(0,0,0) );
146 SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
147 FillRect( hdc, &rect, desktopPtr->hbrushPattern );
150 /* Paint wall paper */
152 if (desktopPtr->hbitmapWallPaper)
154 INT x, y;
155 HDC hMemDC = CreateCompatibleDC( hdc );
157 SelectObject( hMemDC, desktopPtr->hbitmapWallPaper );
159 if (desktopPtr->fTileWallPaper)
161 for (y = 0; y < rect.bottom; y += desktopPtr->bitmapSize.cy)
162 for (x = 0; x < rect.right; x += desktopPtr->bitmapSize.cx)
163 BitBlt( hdc, x, y, desktopPtr->bitmapSize.cx,
164 desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
166 else
168 x = (rect.left + rect.right - desktopPtr->bitmapSize.cx) / 2;
169 y = (rect.top + rect.bottom - desktopPtr->bitmapSize.cy) / 2;
170 if (x < 0) x = 0;
171 if (y < 0) y = 0;
172 BitBlt( hdc, x, y, desktopPtr->bitmapSize.cx,
173 desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
175 DeleteDC( hMemDC );
178 return 1;
182 /***********************************************************************
183 * DesktopWndProc
185 * Window procedure for the desktop window.
187 LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message,
188 WPARAM wParam, LPARAM lParam )
190 LRESULT retvalue;
191 WND *wndPtr = WIN_FindWndPtr( hwnd );
192 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
194 /* Most messages are ignored (we DON'T call DefWindowProc) */
196 switch(message)
198 /* Warning: this message is sent directly by */
199 /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
200 case WM_NCCREATE:
201 desktopPtr->hbrushPattern = 0;
202 desktopPtr->hbitmapWallPaper = 0;
203 SetDeskPattern();
204 SetDeskWallPaper( (LPSTR)-1 );
205 retvalue = 1;
206 goto END;
208 case WM_ERASEBKGND:
209 if (X11DRV_WND_GetXRootWindow(wndPtr) ==
210 DefaultRootWindow(display))
212 retvalue = 1;
213 goto END;
215 retvalue = DESKTOP_DoEraseBkgnd( hwnd, (HDC)wParam, desktopPtr );
216 goto END;
218 case WM_SYSCOMMAND:
219 if ((wParam & 0xfff0) != SC_CLOSE)
221 retvalue = 0;
222 goto END;
224 ExitWindows16( 0, 0 );
226 case WM_SETCURSOR:
227 retvalue = (LRESULT)SetCursor16( LoadCursor16( 0, IDC_ARROW16 ) );
228 goto END;
231 retvalue = 0;
232 END:
233 WIN_ReleaseWndPtr(wndPtr);
234 return retvalue;
237 /***********************************************************************
238 * PaintDesktop (USER32.415)
241 BOOL WINAPI PaintDesktop(HDC hdc)
243 BOOL retvalue;
244 HWND hwnd = GetDesktopWindow();
245 WND *wndPtr = WIN_FindWndPtr( hwnd );
246 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
247 retvalue = DESKTOP_DoEraseBkgnd( hwnd, hdc, desktopPtr );
248 WIN_ReleaseWndPtr(wndPtr);
249 return retvalue;
253 /***********************************************************************
254 * SetDeskPattern (USER.279)
256 BOOL16 WINAPI SetDeskPattern(void)
258 char buffer[100];
259 GetProfileStringA( "desktop", "Pattern", "(None)", buffer, 100 );
260 return DESKTOP_SetPattern( buffer );
264 /***********************************************************************
265 * SetDeskWallPaper16 (USER.285)
267 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
269 return SetDeskWallPaper( filename );
273 /***********************************************************************
274 * SetDeskWallPaper32 (USER32.475)
276 * FIXME: is there a unicode version?
278 BOOL WINAPI SetDeskWallPaper( LPCSTR filename )
280 HBITMAP hbitmap;
281 HDC hdc;
282 char buffer[256];
283 WND *wndPtr = WIN_GetDesktop();
284 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
286 if (filename == (LPSTR)-1)
288 GetProfileStringA( "desktop", "WallPaper", "(None)", buffer, 256 );
289 filename = buffer;
291 hdc = GetDC( 0 );
292 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
293 ReleaseDC( 0, hdc );
294 if (desktopPtr->hbitmapWallPaper) DeleteObject( desktopPtr->hbitmapWallPaper );
295 desktopPtr->hbitmapWallPaper = hbitmap;
296 desktopPtr->fTileWallPaper = GetProfileIntA( "desktop", "TileWallPaper", 0 );
297 if (hbitmap)
299 BITMAP bmp;
300 GetObjectA( hbitmap, sizeof(bmp), &bmp );
301 desktopPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
302 desktopPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
304 WIN_ReleaseDesktop();
305 return TRUE;
309 /***********************************************************************
310 * DESKTOP_SetPattern
312 * Set the desktop pattern.
314 BOOL DESKTOP_SetPattern( LPCSTR pattern )
316 WND *wndPtr = WIN_GetDesktop();
317 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
318 int pat[8];
320 if (desktopPtr->hbrushPattern) DeleteObject( desktopPtr->hbrushPattern );
321 memset( pat, 0, sizeof(pat) );
322 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
323 &pat[0], &pat[1], &pat[2], &pat[3],
324 &pat[4], &pat[5], &pat[6], &pat[7] ))
326 WORD pattern[8];
327 HBITMAP hbitmap;
328 int i;
330 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
331 hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
332 desktopPtr->hbrushPattern = CreatePatternBrush( hbitmap );
333 DeleteObject( hbitmap );
335 else desktopPtr->hbrushPattern = CreateSolidBrush( GetSysColor(COLOR_BACKGROUND) );
336 WIN_ReleaseDesktop();
337 return TRUE;