Added Win32 synchro to FILEs (useful only for terminal handles).
[wine/multimedia.git] / controls / desktop.c
blob73baa5a6e80d5e389d631afa88150aa63e40db16
1 /*
2 * Desktop window class.
4 * Copyright 1994 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include "win.h"
11 #include "desktop.h"
12 #include "heap.h"
15 /***********************************************************************
16 * DESKTOP_LoadBitmap
18 * Load a bitmap from a file. Used by SetDeskWallPaper().
20 static HBITMAP32 DESKTOP_LoadBitmap( HDC32 hdc, const char *filename )
22 BITMAPFILEHEADER *fileHeader;
23 BITMAPINFO *bitmapInfo;
24 HBITMAP32 hbitmap;
25 HFILE32 file;
26 LPSTR buffer;
27 LONG size;
29 /* Read all the file into memory */
31 if ((file = _lopen32( filename, OF_READ )) == HFILE_ERROR32)
33 UINT32 len = GetWindowsDirectory32A( NULL, 0 );
34 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
35 len + strlen(filename) + 2 )))
36 return 0;
37 GetWindowsDirectory32A( buffer, len + 1 );
38 strcat( buffer, "\\" );
39 strcat( buffer, filename );
40 file = _lopen32( buffer, OF_READ );
41 HeapFree( GetProcessHeap(), 0, buffer );
43 if (file == HFILE_ERROR32) return 0;
44 size = _llseek32( file, 0, 2 );
45 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
47 _lclose32( file );
48 return 0;
50 _llseek32( file, 0, 0 );
51 size = _lread32( file, buffer, size );
52 _lclose32( file );
53 fileHeader = (BITMAPFILEHEADER *)buffer;
54 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
56 /* Check header content */
57 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
59 HeapFree( GetProcessHeap(), 0, buffer );
60 return 0;
62 hbitmap = CreateDIBitmap32( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
63 buffer + fileHeader->bfOffBits,
64 bitmapInfo, DIB_RGB_COLORS );
65 HeapFree( GetProcessHeap(), 0, buffer );
66 return hbitmap;
70 /***********************************************************************
71 * DESKTOP_DoEraseBkgnd
73 * Handle the WM_ERASEBKGND message.
75 static LRESULT DESKTOP_DoEraseBkgnd( HWND32 hwnd, HDC32 hdc,
76 DESKTOPINFO *infoPtr )
78 RECT32 rect;
79 WND* Wnd = WIN_FindWndPtr( hwnd );
81 if (Wnd->hrgnUpdate > 1) DeleteObject32( Wnd->hrgnUpdate );
82 Wnd->hrgnUpdate = 0;
84 GetClientRect32( hwnd, &rect );
86 /* Paint desktop pattern (only if wall paper does not cover everything) */
88 if (!infoPtr->hbitmapWallPaper ||
89 (!infoPtr->fTileWallPaper && ((infoPtr->bitmapSize.cx < rect.right) ||
90 (infoPtr->bitmapSize.cy < rect.bottom))))
92 /* Set colors in case pattern is a monochrome bitmap */
93 SetBkColor32( hdc, RGB(0,0,0) );
94 SetTextColor32( hdc, GetSysColor32(COLOR_BACKGROUND) );
95 FillRect32( hdc, &rect, infoPtr->hbrushPattern );
98 /* Paint wall paper */
100 if (infoPtr->hbitmapWallPaper)
102 INT32 x, y;
103 HDC32 hMemDC = CreateCompatibleDC32( hdc );
105 SelectObject32( hMemDC, infoPtr->hbitmapWallPaper );
107 if (infoPtr->fTileWallPaper)
109 for (y = 0; y < rect.bottom; y += infoPtr->bitmapSize.cy)
110 for (x = 0; x < rect.right; x += infoPtr->bitmapSize.cx)
111 BitBlt32( hdc, x, y, infoPtr->bitmapSize.cx,
112 infoPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
114 else
116 x = (rect.left + rect.right - infoPtr->bitmapSize.cx) / 2;
117 y = (rect.top + rect.bottom - infoPtr->bitmapSize.cy) / 2;
118 if (x < 0) x = 0;
119 if (y < 0) y = 0;
120 BitBlt32( hdc, x, y, infoPtr->bitmapSize.cx,
121 infoPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
123 DeleteDC32( hMemDC );
126 return 1;
130 /***********************************************************************
131 * DesktopWndProc
133 * Window procedure for the desktop window.
135 LRESULT WINAPI DesktopWndProc( HWND32 hwnd, UINT32 message,
136 WPARAM32 wParam, LPARAM lParam )
138 WND *wndPtr = WIN_FindWndPtr( hwnd );
139 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
141 /* Most messages are ignored (we DON'T call DefWindowProc) */
143 switch(message)
145 /* Warning: this message is sent directly by */
146 /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
147 case WM_NCCREATE:
148 infoPtr->hbrushPattern = 0;
149 infoPtr->hbitmapWallPaper = 0;
150 SetDeskPattern();
151 SetDeskWallPaper32( (LPSTR)-1 );
152 return 1;
154 case WM_ERASEBKGND:
155 if (rootWindow == DefaultRootWindow(display)) return 1;
156 return DESKTOP_DoEraseBkgnd( hwnd, (HDC32)wParam, infoPtr );
158 case WM_SYSCOMMAND:
159 if ((wParam & 0xfff0) != SC_CLOSE) return 0;
160 ExitWindows16( 0, 0 );
162 case WM_SETCURSOR:
163 return (LRESULT)SetCursor16( LoadCursor16( 0, IDC_ARROW16 ) );
166 return 0;
169 /***********************************************************************
170 * PaintDesktop (USER32.415)
173 BOOL32 WINAPI PaintDesktop(HDC32 hdc)
175 HWND32 hwnd = GetDesktopWindow32();
176 WND *wndPtr = WIN_FindWndPtr( hwnd );
177 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
179 return DESKTOP_DoEraseBkgnd( hwnd, hdc, infoPtr );
182 /***********************************************************************
183 * SetDeskPattern (USER.279)
185 BOOL16 WINAPI SetDeskPattern(void)
187 char buffer[100];
188 GetProfileString32A( "desktop", "Pattern", "(None)", buffer, 100 );
189 return DESKTOP_SetPattern( buffer );
193 /***********************************************************************
194 * SetDeskWallPaper16 (USER.285)
196 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
198 return SetDeskWallPaper32( filename );
202 /***********************************************************************
203 * SetDeskWallPaper32 (USER32.475)
205 * FIXME: is there a unicode version?
207 BOOL32 WINAPI SetDeskWallPaper32( LPCSTR filename )
209 HBITMAP32 hbitmap;
210 HDC32 hdc;
211 char buffer[256];
212 WND *wndPtr = WIN_GetDesktop();
213 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
215 if (filename == (LPSTR)-1)
217 GetProfileString32A( "desktop", "WallPaper", "(None)", buffer, 256 );
218 filename = buffer;
220 hdc = GetDC32( 0 );
221 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
222 ReleaseDC32( 0, hdc );
223 if (infoPtr->hbitmapWallPaper) DeleteObject32( infoPtr->hbitmapWallPaper );
224 infoPtr->hbitmapWallPaper = hbitmap;
225 infoPtr->fTileWallPaper = GetProfileInt32A( "desktop", "TileWallPaper", 0 );
226 if (hbitmap)
228 BITMAP32 bmp;
229 GetObject32A( hbitmap, sizeof(bmp), &bmp );
230 infoPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
231 infoPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
233 return TRUE;
237 /***********************************************************************
238 * DESKTOP_SetPattern
240 * Set the desktop pattern.
242 BOOL32 DESKTOP_SetPattern( LPCSTR pattern )
244 WND *wndPtr = WIN_GetDesktop();
245 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
246 int pat[8];
248 if (infoPtr->hbrushPattern) DeleteObject32( infoPtr->hbrushPattern );
249 memset( pat, 0, sizeof(pat) );
250 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
251 &pat[0], &pat[1], &pat[2], &pat[3],
252 &pat[4], &pat[5], &pat[6], &pat[7] ))
254 WORD pattern[8];
255 HBITMAP32 hbitmap;
256 int i;
258 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
259 hbitmap = CreateBitmap32( 8, 8, 1, 1, (LPSTR)pattern );
260 infoPtr->hbrushPattern = CreatePatternBrush32( hbitmap );
261 DeleteObject32( hbitmap );
263 else infoPtr->hbrushPattern = CreateSolidBrush32( GetSysColor32(COLOR_BACKGROUND) );
264 return TRUE;